Home | History | Annotate | Download | only in mq_timedreceive
      1 /*
      2  * Copyright (c) 2003, Intel Corporation. All rights reserved.
      3  * Created by:  crystal.xiong 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 /*
     10  * mq_timedreceive() test plan:
     11  * Test that if abs_timeout has already passed the time
     12  * at the time mq_timedreceive() called, the timeout will expire.
     13  * mq_timedreceive() will return with ETIMEDOUT.
     14  *
     15  */
     16 
     17 #include <sys/stat.h>
     18 #include <sys/types.h>
     19 #include <sys/wait.h>
     20 #include <errno.h>
     21 #include <fcntl.h>
     22 #include <stdio.h>
     23 #include <mqueue.h>
     24 #include <signal.h>
     25 #include <stdlib.h>
     26 #include <string.h>
     27 #include <time.h>
     28 #include <unistd.h>
     29 #include "posixtest.h"
     30 
     31 #define TEST "18-2"
     32 #define FUNCTION "mq_timedreceive"
     33 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     34 
     35 #define NAMESIZE 50
     36 #define BUFFER 40
     37 #define TIMEOUT	3
     38 
     39 int blocking;
     40 void exit_handler(int signo)
     41 {
     42 	printf("FAIL: the case is blocking, exit anyway\n");
     43 	blocking = 1;
     44 	return;
     45 }
     46 
     47 int main(void)
     48 {
     49 	char mqname[NAMESIZE], msgrv[BUFFER];
     50 	mqd_t mqdes;
     51 	struct timespec ts;
     52 	struct mq_attr attr;
     53 	pid_t pid;
     54 	int unresolved = 0, failure = 0;
     55 
     56 	sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid());
     57 
     58 	attr.mq_msgsize = BUFFER;
     59 	attr.mq_maxmsg = BUFFER;
     60 	mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr);
     61 	if (mqdes == (mqd_t) - 1) {
     62 		perror(ERROR_PREFIX "mq_open");
     63 		unresolved = 1;
     64 	}
     65 
     66 	pid = fork();
     67 	if (pid != 0) {
     68 		/* Parent process */
     69 		struct sigaction act;
     70 		act.sa_handler = exit_handler;
     71 		act.sa_flags = 0;
     72 		sigemptyset(&act.sa_mask);
     73 		sigaction(SIGABRT, &act, 0);
     74 
     75 		ts.tv_sec = time(NULL) - TIMEOUT;
     76 		ts.tv_nsec = 0;
     77 		if (mq_timedreceive(mqdes, msgrv, BUFFER, NULL, &ts) != -1) {
     78 			printf("FAIL: mq_timedreceive succeed unexpectely\n");
     79 			failure = 1;
     80 		} else {
     81 			if (errno != ETIMEDOUT) {
     82 				printf("errno != ETIMEDOUT\n");
     83 				failure = 1;
     84 			}
     85 		}
     86 		/* Parent is not blocking, let child abort */
     87 		kill(pid, SIGABRT);
     88 		if (mq_close(mqdes) != 0) {
     89 			perror(ERROR_PREFIX "mq_close");
     90 			unresolved = 1;
     91 		}
     92 		if (mq_unlink(mqname) != 0) {
     93 			perror(ERROR_PREFIX "mq_unlink");
     94 			unresolved = 1;
     95 		}
     96 		if (failure == 1 || blocking == 1) {
     97 			printf("Test FAILED\n");
     98 			return PTS_FAIL;
     99 		}
    100 		if (unresolved == 1) {
    101 			printf("Test UNRESOLVED\n");
    102 			return PTS_UNRESOLVED;
    103 		}
    104 		printf("Test PASSED\n");
    105 		return PTS_PASS;
    106 	} else {
    107 		sleep(TIMEOUT + 3);	/* Parent is probably blocking
    108 					   send a signal to let it abort */
    109 		kill(getppid(), SIGABRT);
    110 		return 0;
    111 	}
    112 	printf("Test PASSED\n");
    113 	return PTS_PASS;
    114 }
    115