Home | History | Annotate | Download | only in sem_wait
      1 /*
      2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
      3  * Created by:  majid.awad REMOVE-THIS AT intel DOT com
      4  * This file is licensed under the GPL license.  For the full content
      5  * of this license, see the COPYING file at the top level of this
      6  * source tree.
      7  */
      8 
      9 /* sem_wait hangs when it tries to lock a locked semaphore, and then killed
     10  * by sending a kill signal.
     11  */
     12 
     13 #include <stdio.h>
     14 #include <errno.h>
     15 #include <unistd.h>
     16 #include <semaphore.h>
     17 #include <sys/stat.h>
     18 #include <fcntl.h>
     19 #include <signal.h>
     20 #include <sys/wait.h>
     21 #include "posixtest.h"
     22 
     23 #define TEST "7-1"
     24 #define FUNCTION "sem_wait"
     25 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     26 
     27 #define CHILDPASS 1
     28 #define CHILDFAIL 0
     29 
     30 void handler(int signo)
     31 {
     32 	printf("In handler\n");
     33 }
     34 
     35 int main(void)
     36 {
     37 	sem_t *mysemp;
     38 	char semname[28];
     39 	int pid, status;
     40 
     41 	sprintf(semname, "/" FUNCTION "_" TEST "_%d", getpid());
     42 
     43 	mysemp = sem_open(semname, O_CREAT, 0, 1);
     44 	if (mysemp == SEM_FAILED || mysemp == NULL) {
     45 		perror(ERROR_PREFIX "sem_open");
     46 		return PTS_UNRESOLVED;
     47 	}
     48 
     49 	if (sem_wait(mysemp) == -1) {
     50 		perror(ERROR_PREFIX "sem_wait");
     51 		return PTS_UNRESOLVED;
     52 	}
     53 
     54 	pid = fork();
     55 	if (pid == 0) {		// child create the semaphore.
     56 		struct sigaction act;
     57 
     58 		act.sa_handler = handler;
     59 		act.sa_flags = 0;
     60 		if (sigemptyset(&act.sa_mask) == -1) {
     61 			perror("Error calling sigemptyset\n");
     62 			return CHILDFAIL;
     63 		}
     64 		if (sigaction(SIGABRT, &act, 0) == -1) {
     65 			perror("Error calling sigaction\n");
     66 			return CHILDFAIL;
     67 		}
     68 
     69 		sem_wait(mysemp);
     70 
     71 		if (errno == EINTR) {
     72 			printf("Test PASSED\n");
     73 			return (CHILDPASS);
     74 		}
     75 		puts("TEST FAILED: errno != EINTR");
     76 		return (CHILDFAIL);
     77 
     78 	} else {		// parent to send a signal to child
     79 		int i;
     80 		sleep(1);
     81 		status = kill(pid, SIGABRT);	// send signal to child
     82 		if (wait(&i) == -1) {
     83 			perror("Error waiting for child to exit\n");
     84 			return PTS_UNRESOLVED;
     85 		}
     86 
     87 		if (!WEXITSTATUS(i)) {
     88 			return PTS_FAIL;
     89 		}
     90 		puts("TEST PASSED");
     91 		sem_unlink(semname);
     92 		return PTS_PASS;
     93 	}
     94 
     95 	return PTS_UNRESOLVED;
     96 }
     97