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