]> git.saurik.com Git - apple/xnu.git/blame - tools/tests/xnu_quick_test/shared_memory_tests.c
xnu-1699.26.8.tar.gz
[apple/xnu.git] / tools / tests / xnu_quick_test / shared_memory_tests.c
CommitLineData
2d21ac55
A
1/*
2 * shared_memory_tests.c
3 * xnu_quick_test
4 *
5 * Created by Jerry Cottingham on 6/2/2005.
6 * Copyright 2005 Apple Computer Inc. All rights reserved.
7 *
8 */
9
10#include "tests.h"
11#include <sys/ipc.h>
12#include <sys/mman.h>
13#include <sys/shm.h>
14
15extern char g_target_path[ PATH_MAX ];
16
17
18/* **************************************************************************************************************
19 * Test shmat, shmctl, shmdt, shmget system calls.
20 * **************************************************************************************************************
21 */
22int shm_tests( void * the_argp )
cf7d32b8
A
23{
24#if !TARGET_OS_EMBEDDED
2d21ac55
A
25 int my_err;
26 int my_shm_id;
27 void * my_shm_addr = NULL;
28 struct shmid_ds my_shmid_ds;
29
30 my_shm_id = shmget( IPC_PRIVATE, 4096, (IPC_CREAT | IPC_R | IPC_W) );
31 if ( my_shm_id == -1 ) {
32 printf( "shmget failed with error %d - \"%s\" \n", errno, strerror( errno) );
33 goto test_failed_exit;
34 }
35
36 my_shm_addr = shmat( my_shm_id, NULL, SHM_RND );
37 if ( my_shm_addr == (void *) -1 ) {
38 my_shm_addr = NULL;
39 printf( "shmat failed with error %d - \"%s\" \n", errno, strerror( errno) );
40 goto test_failed_exit;
41 }
42
43 /* try writing to the shared segment */
44 *((char *) my_shm_addr) = 'A';
45
46 my_err = shmctl( my_shm_id, IPC_STAT, &my_shmid_ds );
47 if ( my_err == -1 ) {
48 printf( "shmctl failed with error %d - \"%s\" \n", errno, strerror( errno) );
49 goto test_failed_exit;
50 }
51 if ( my_shmid_ds.shm_segsz != 4096 ) {
52 printf( "shmctl failed get correct shared segment size \n" );
53 goto test_failed_exit;
54 }
55 if ( getpid( ) != my_shmid_ds.shm_cpid ) {
56 printf( "shmctl failed get correct creator pid \n" );
57 goto test_failed_exit;
58 }
59
60 my_err = shmdt( my_shm_addr );
61 if ( my_err == -1 ) {
62 printf( "shmdt failed with error %d - \"%s\" \n", errno, strerror( errno) );
63 goto test_failed_exit;
64 }
b0d623f7
A
65
66 my_err = shmctl( my_shm_id, IPC_RMID, NULL );
67 if ( my_err == -1 ) {
68 printf("shmctl failed to delete memory segment.\n");
69 goto test_failed_exit;
70 }
71
2d21ac55
A
72 my_shm_addr = NULL;
73
74 my_err = 0;
75 goto test_passed_exit;
76
77test_failed_exit:
78 my_err = -1;
79
80test_passed_exit:
81 if ( my_shm_addr != NULL ) {
82 shmdt( my_shm_addr );
b0d623f7 83 shmctl( my_shm_id, IPC_RMID, NULL);
2d21ac55
A
84 }
85 return( my_err );
cf7d32b8
A
86#else
87 printf( "\t--> Not supported on EMBEDDED TARGET\n" );
88 return 0;
89#endif
2d21ac55
A
90}
91
92
93/* **************************************************************************************************************
94 * Test BSD shared memory system calls.
95 * **************************************************************************************************************
96 */
97int bsd_shm_tests( void * the_argp )
98{
99 int my_err, i;
100 int my_fd = -1;
101 char * my_addr = NULL;
102 char my_name[ 64 ];
103
104 for ( i = 0; i < 100; i++ ) {
105 sprintf( &my_name[0], "bsd_shm_tests_%d", i );
106 my_fd = shm_open( &my_name[0], (O_RDWR | O_CREAT | O_EXCL), S_IRWXU );
107 if ( my_fd != -1 )
108 break;
109 my_err = errno;
110 if ( my_err != EEXIST ) {
111 printf( "shm_open failed with error %d - \"%s\" \n", my_err, strerror( my_err) );
112 goto test_failed_exit;
113 }
114 }
115 if ( my_fd == -1 ) {
116 printf( "shm_open failed to open a shared memory object with name \"%s\" \n", &my_name[0] );
117 goto test_failed_exit;
118 }
119
120 /* grow shared memory object */
121 my_err = ftruncate( my_fd, 4096 );
122 if ( my_err == -1 ) {
123 printf( "ftruncate call failed with error %d - \"%s\" \n", errno, strerror( errno) );
124 goto test_failed_exit;
125 }
126
127 my_err = shm_unlink( &my_name[0] );
128 if ( my_err == -1 ) {
129 printf( "shm_unlink failed with error %d - \"%s\" \n", errno, strerror( errno) );
130 goto test_failed_exit;
131 }
132
133 my_addr = (char *) mmap( NULL, 4096, (PROT_READ | PROT_WRITE), (MAP_FILE | MAP_SHARED), my_fd, 0 );
134 if ( my_addr == (char *) -1 ) {
135 printf( "mmap call failed with error %d - \"%s\" \n", errno, strerror( errno) );
136 goto test_failed_exit;
137 }
138
139 my_err = 0;
140 goto test_passed_exit;
141
142test_failed_exit:
143 my_err = -1;
144
145test_passed_exit:
146 if ( my_fd != -1 )
147 close( my_fd );
148 return( my_err );
149}
150