1 /*
2 * Copyright (C) Bull S.A. 1996
3 * Copyright (c) International Business Machines Corp., 2001
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
13 * the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19 /*---------------------------------------------------------------------+
20 | semaphore_test_01 |
21 | ==================================================================== |
22 | |
23 | Description: Simplistic test to verify the semget () and semctl () |
24 | system function calls. |
25 | |
26 | o Create semaphore with semget () |
27 | |
28 | o Set the semaphore user id, group id and permission |
29 | bits with semctl (IPC_SET) |
30 | |
31 | o Retrieve the semaphore status with semctl (IPC_STAT)|
32 | and compare with expected values |
33 | |
34 | o Print out the semaphore id for comparison with the |
35 | output of the 'ipcs -s' command |
36 | |
37 | System calls: The following system calls are made |
38 | |
39 | semget () - Gets a set of semaphores |
40 | semctl () - Controls semaphore operations |
41 | |
42 | Usage: semaphore_test_01 |
43 | NOTE: this program creates a system semaphore, but never deletes it. |
44 | In order to delete it, use `ipcrm sem <id_num>`. An alternate |
45 | to this is to run the program as follows: |
46 | ipcrm sem `./semaphore_test_01` |
47 | This will delete the semaphore after the program exits, but |
48 | does not print out the semaphore id to see. |
49 | |
50 | To compile: cc -o semaphore_test_01 semaphore_test_01.c |
51 | |
52 | Last update: Ver. 1.2, 2/14/94 00:18:16 |
53 | |
54 | Change Activity |
55 | |
56 | Version Date Name Reason |
57 | 0.1 050689 CTU Initial version |
58 | 0.2 111993 DJK Modify for AIX version 4.1 |
59 | 1.2 021494 DJK Move to "prod" directory |
60 | |
61 +---------------------------------------------------------------------*/
62
63 #include <errno.h>
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <sys/sem.h>
67 #include <sys/types.h>
68 #include <unistd.h>
69 #include <string.h>
70
71 #ifdef _LINUX_
72 #include <sys/stat.h>
73 #endif
74
75 #include "lapi/semun.h"
76
77 /* Defines
78 *
79 * NUM_SEMAPHORES: number of semaphores to create
80 */
81 #define NUM_SEMAPHORES 1
82
83 /*
84 * Function prototypes
85 *
86 * sys_error (): System error message function
87 * error (): Error message function
88 */
89 static void sys_error(const char *, int);
90 static void error(const char *, int);
91
92 /*---------------------------------------------------------------------+
93 | main |
94 | ==================================================================== |
95 | |
96 | Function: Main program (see prolog for more details) |
97 | |
98 | Returns: (0) Successful completion |
99 | (-1) Error occurred |
100 | |
101 +---------------------------------------------------------------------*/
main(int argc,char ** argv)102 int main(int argc, char **argv)
103 {
104 uid_t uid = getuid(); /* User's user id */
105 gid_t gid = getgid(); /* User's group id */
106 int semid; /* Unique semaphore id */
107 int nsems = NUM_SEMAPHORES; /* Number of semaphores to create */
108 struct semid_ds exp_semdata; /* Expected semaphore values */
109 union semun exp_semdatap;
110 struct semid_ds act_semdata; /* Actual semaphore values */
111 union semun act_semdatap;
112
113 exp_semdatap.buf = &exp_semdata;
114 act_semdatap.buf = &act_semdata;
115
116 umask(0000);
117
118 /* SET semid_ds STRUCTURE TO DESIRED VALUES........ */
119
120 /*
121 * Initialize the "expected" sempahore value structure
122 */
123 exp_semdata.sem_perm.cuid = exp_semdata.sem_perm.uid = uid;
124 exp_semdata.sem_perm.cgid = exp_semdata.sem_perm.gid = gid;
125 exp_semdata.sem_perm.mode = 0660;
126 exp_semdata.sem_nsems = nsems;
127
128 /*
129 * Create a semaphore, set the semaphore fields and then
130 * retrieve the fields.
131 */
132 if ((semid = semget(IPC_PRIVATE, nsems, IPC_CREAT | 0666)) < 0)
133 sys_error("semget (IPC_PRIVATE) failed", __LINE__);
134
135 if (semctl(semid, nsems, IPC_SET, exp_semdatap) < 0)
136 sys_error("semctl (IPC_SET) failed", __LINE__);
137
138 if (semctl(semid, nsems, IPC_STAT, act_semdatap) < 0)
139 sys_error("semctl (IPC_STAT) failed", __LINE__);
140
141 /*
142 * Verify that the semaphore fields were set correctly
143 */
144 if (act_semdata.sem_perm.cuid != exp_semdata.sem_perm.cuid)
145 error("sem_perm.cuid field was not set!", __LINE__);
146 if (act_semdata.sem_perm.uid != exp_semdata.sem_perm.uid)
147 error("sem_perm.uid field was not set!", __LINE__);
148 if (act_semdata.sem_perm.cgid != exp_semdata.sem_perm.cgid)
149 error("sem_perm.cgid field was not set!", __LINE__);
150 if (act_semdata.sem_perm.gid != exp_semdata.sem_perm.gid)
151 error("sem_perm.gid field was not set!", __LINE__);
152 if (act_semdata.sem_perm.mode != exp_semdata.sem_perm.mode)
153 error("sem_perm.mode field was not set!", __LINE__);
154 if (act_semdata.sem_nsems != exp_semdata.sem_nsems)
155 error("sem_nsems field was not set!", __LINE__);
156
157 /*
158 * Print out the id of the newly created semaphore for comparison
159 * with the 'ipcs -s' command and then exit.
160 */
161 printf("%d\n", semid);
162
163 return (0);
164 }
165
166 /*---------------------------------------------------------------------+
167 | sys_error () |
168 | ==================================================================== |
169 | |
170 | Function: Creates system error message and calls error () |
171 | |
172 +---------------------------------------------------------------------*/
sys_error(const char * msg,int line)173 static void sys_error(const char *msg, int line)
174 {
175 char syserr_msg[256];
176
177 sprintf(syserr_msg, "%s: %s\n", msg, strerror(errno));
178 error(syserr_msg, line);
179 }
180
181 /*---------------------------------------------------------------------+
182 | error () |
183 | ==================================================================== |
184 | |
185 | Function: Prints out message and exits... |
186 | |
187 +---------------------------------------------------------------------*/
error(const char * msg,int line)188 static void error(const char *msg, int line)
189 {
190 fprintf(stderr, "ERROR [line: %d] %s\n", line, msg);
191 exit(-1);
192 }
193