t-sem.c (302408) | t-sem.c (147078) |
---|---|
1/* | 1/* |
2 * Copyright (c) 2000-2001, 2005-2008 Proofpoint, Inc. and its suppliers. | 2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers. |
3 * All rights reserved. 4 * 5 * By using this file, you agree to the terms and conditions set 6 * forth in the LICENSE file which can be found at the top level of 7 * the sendmail distribution. 8 */ 9 10#include <sm/gen.h> | 3 * All rights reserved. 4 * 5 * By using this file, you agree to the terms and conditions set 6 * forth in the LICENSE file which can be found at the top level of 7 * the sendmail distribution. 8 */ 9 10#include <sm/gen.h> |
11SM_RCSID("@(#)$Id: t-sem.c,v 1.18 2013-11-22 20:51:43 ca Exp $") | 11SM_RCSID("@(#)$Id: t-sem.c,v 1.14 2005/03/25 21:27:41 ca Exp $") |
12 13#include <stdio.h> 14 15#if SM_CONF_SEM 16# include <stdlib.h> 17# include <unistd.h> 18# include <sysexits.h> 19# include <sm/heap.h> 20# include <sm/string.h> 21# include <sm/signal.h> 22# include <sm/test.h> 23# include <sm/sem.h> 24 | 12 13#include <stdio.h> 14 15#if SM_CONF_SEM 16# include <stdlib.h> 17# include <unistd.h> 18# include <sysexits.h> 19# include <sm/heap.h> 20# include <sm/string.h> 21# include <sm/signal.h> 22# include <sm/test.h> 23# include <sm/sem.h> 24 |
25# define T_SM_SEM_KEY (4321L) 26 | |
27static void 28delay(t, s) 29 int t; 30 char *s; 31{ 32 if (t > 0) 33 { 34#if DEBUG --- 20 unchanged lines hidden (view full) --- 55 56static int 57seminter(owner) 58 bool owner; 59{ 60 int semid; 61 int t; 62 | 25static void 26delay(t, s) 27 int t; 28 char *s; 29{ 30 if (t > 0) 31 { 32#if DEBUG --- 20 unchanged lines hidden (view full) --- 53 54static int 55seminter(owner) 56 bool owner; 57{ 58 int semid; 59 int t; 60 |
63 semid = sm_sem_start(T_SM_SEM_KEY, SM_NSEM, 0, owner); | 61 semid = sm_sem_start(SM_SEM_KEY, SM_NSEM, 0, owner); |
64 if (semid < 0) 65 { 66 perror("sm_sem_start failed"); 67 return 1; 68 } 69 70 while ((t = getchar()) != EOF) 71 { --- 50 unchanged lines hidden (view full) --- 122sem_cleanup(sig) 123 int sig; 124{ 125 if (semid_c >= 0) 126 (void) sm_sem_stop(semid_c); 127 exit(EX_UNAVAILABLE); 128} 129 | 62 if (semid < 0) 63 { 64 perror("sm_sem_start failed"); 65 return 1; 66 } 67 68 while ((t = getchar()) != EOF) 69 { --- 50 unchanged lines hidden (view full) --- 120sem_cleanup(sig) 121 int sig; 122{ 123 if (semid_c >= 0) 124 (void) sm_sem_stop(semid_c); 125 exit(EX_UNAVAILABLE); 126} 127 |
130static int 131drop_priv(uid, gid) 132 uid_t uid; 133 gid_t gid; 134{ 135 int r; 136 137 r = setgid(gid); 138 if (r != 0) 139 return r; 140 r = setuid(uid); 141 return r; 142} 143 | |
144/* 145** SEMTEST -- test of semaphores 146** 147** Parameters: 148** owner -- create semaphores. 149** 150** Returns: 151** 0 on success 152** < 0 on failure. 153*/ 154 155# define MAX_CNT 10 156 157static int | 128/* 129** SEMTEST -- test of semaphores 130** 131** Parameters: 132** owner -- create semaphores. 133** 134** Returns: 135** 0 on success 136** < 0 on failure. 137*/ 138 139# define MAX_CNT 10 140 141static int |
158semtest(owner, uid, gid) | 142semtest(owner) |
159 int owner; | 143 int owner; |
160 uid_t uid; 161 gid_t gid; | |
162{ 163 int semid, r; 164 int cnt = 0; 165 | 144{ 145 int semid, r; 146 int cnt = 0; 147 |
166 if (!owner && uid != 0) 167 { 168 r = drop_priv(uid, gid); 169 if (r < 0) 170 { 171 perror("drop_priv child failed"); 172 return -1; 173 } 174 } 175 semid = sm_sem_start(T_SM_SEM_KEY, 1, 0, owner); | 148 semid = sm_sem_start(SM_SEM_KEY, 1, 0, owner); |
176 if (semid < 0) 177 { 178 perror("sm_sem_start failed"); 179 return -1; 180 } 181 182 if (owner) 183 { | 149 if (semid < 0) 150 { 151 perror("sm_sem_start failed"); 152 return -1; 153 } 154 155 if (owner) 156 { |
184 if (uid != 0) 185 { 186 r = sm_semsetowner(semid, uid, gid, 0660); 187 if (r < 0) 188 { 189 perror("sm_semsetowner failed"); 190 return -1; 191 } 192 r = drop_priv(uid, gid); 193 if (r < 0) 194 { 195 perror("drop_priv owner failed"); 196 return -1; 197 } 198 } 199 | |
200 /* just in case someone kills the program... */ 201 semid_c = semid; 202 (void) sm_signal(SIGHUP, sem_cleanup); 203 (void) sm_signal(SIGINT, sem_cleanup); 204 (void) sm_signal(SIGTERM, sem_cleanup); 205 206 delay(1, "parent: acquire 1"); 207 cnt = 0; --- 109 unchanged lines hidden (view full) --- 317 318int 319main(argc, argv) 320 int argc; 321 char *argv[]; 322{ 323 bool interactive = false; 324 bool owner = false; | 157 /* just in case someone kills the program... */ 158 semid_c = semid; 159 (void) sm_signal(SIGHUP, sem_cleanup); 160 (void) sm_signal(SIGINT, sem_cleanup); 161 (void) sm_signal(SIGTERM, sem_cleanup); 162 163 delay(1, "parent: acquire 1"); 164 cnt = 0; --- 109 unchanged lines hidden (view full) --- 274 275int 276main(argc, argv) 277 int argc; 278 char *argv[]; 279{ 280 bool interactive = false; 281 bool owner = false; |
325 int ch, r; 326 uid_t uid; 327 gid_t gid; | 282 int ch; 283 int r = 0; |
328 | 284 |
329 uid = 0; 330 gid = 0; 331 r = 0; 332 333# define OPTIONS "iog:u:" | 285# define OPTIONS "io" |
334 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 335 { 336 switch ((char) ch) 337 { | 286 while ((ch = getopt(argc, argv, OPTIONS)) != -1) 287 { 288 switch ((char) ch) 289 { |
338 case 'g': 339 gid = (gid_t)strtoul(optarg, 0, 0); 340 break; 341 | |
342 case 'i': 343 interactive = true; 344 break; 345 | 290 case 'i': 291 interactive = true; 292 break; 293 |
346 case 'u': 347 uid = (uid_t)strtoul(optarg, 0, 0); 348 break; 349 | |
350 case 'o': 351 owner = true; 352 break; 353 354 default: 355 break; 356 } 357 } 358 359 if (interactive) 360 r = seminter(owner); 361 else 362 { 363 pid_t pid; 364 365 printf("This test takes about 8 seconds.\n"); | 294 case 'o': 295 owner = true; 296 break; 297 298 default: 299 break; 300 } 301 } 302 303 if (interactive) 304 r = seminter(owner); 305 else 306 { 307 pid_t pid; 308 309 printf("This test takes about 8 seconds.\n"); |
366 printf("If it takes longer than 30 seconds, please interrupt it\n"); | 310 printf("If it takes longer than 30 second, please interrupt it\n"); |
367 printf("and compile again without semaphore support, i.e.,"); 368 printf("-DSM_CONF_SEM=0\n"); 369 if ((pid = fork()) < 0) 370 { 371 perror("fork failed\n"); 372 return -1; 373 } 374 375 sm_test_begin(argc, argv, "test semaphores"); 376 if (pid == 0) 377 { 378 /* give the parent the chance to setup data */ 379 sleep(1); | 311 printf("and compile again without semaphore support, i.e.,"); 312 printf("-DSM_CONF_SEM=0\n"); 313 if ((pid = fork()) < 0) 314 { 315 perror("fork failed\n"); 316 return -1; 317 } 318 319 sm_test_begin(argc, argv, "test semaphores"); 320 if (pid == 0) 321 { 322 /* give the parent the chance to setup data */ 323 sleep(1); |
380 r = semtest(false, uid, gid); | 324 r = semtest(false); |
381 } 382 else 383 { | 325 } 326 else 327 { |
384 r = semtest(true, uid, gid); | 328 r = semtest(true); |
385 } 386 SM_TEST(r == 0); 387 return sm_test_end(); 388 } 389 return r; 390} 391#else /* SM_CONF_SEM */ 392int 393main(argc, argv) 394 int argc; 395 char *argv[]; 396{ 397 printf("No support for semaphores configured on this machine\n"); 398 return 0; 399} 400#endif /* SM_CONF_SEM */ | 329 } 330 SM_TEST(r == 0); 331 return sm_test_end(); 332 } 333 return r; 334} 335#else /* SM_CONF_SEM */ 336int 337main(argc, argv) 338 int argc; 339 char *argv[]; 340{ 341 printf("No support for semaphores configured on this machine\n"); 342 return 0; 343} 344#endif /* SM_CONF_SEM */ |