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_notify() test plan: 11 * If the notification is sent to the registered process, its registration 12 * will be removed, then, the message queue will be available for 13 * next registration. 14 * 15 * 4/11/2003 change sa_flags from SA_RESTART to 0 to avoid compile 16 * error. 17 * 18 * 2/17/2004 call mq_close and mq_unlink before exit to release mq 19 * resources 20 */ 21 22 #include <stdio.h> 23 #include <mqueue.h> 24 #include <fcntl.h> 25 #include <signal.h> 26 #include <sys/stat.h> 27 #include <sys/types.h> 28 #include <unistd.h> 29 #include "posixtest.h" 30 31 #define TEST "4-1" 32 #define FUNCTION "mq_notify" 33 #define MSG_SIZE 50 34 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " 35 36 int enter_handler = 0; 37 38 void msg_handler() 39 { 40 enter_handler = 1; 41 } 42 43 void mqclean(mqd_t queue, const char *qname) 44 { 45 mq_close(queue); 46 mq_unlink(qname); 47 } 48 49 int main(void) 50 { 51 char mqname[50]; 52 mqd_t mqdes; 53 const char s_msg_ptr[MSG_SIZE] = "test message \n"; 54 struct sigevent notification; 55 struct sigaction sa; 56 unsigned int prio = 1; 57 58 sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); 59 60 mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0); 61 if (mqdes == (mqd_t) - 1) { 62 perror(ERROR_PREFIX "mq_open"); 63 return PTS_UNRESOLVED; 64 } 65 66 notification.sigev_notify = SIGEV_SIGNAL; 67 notification.sigev_signo = SIGUSR1; 68 sa.sa_handler = msg_handler; 69 sa.sa_flags = 0; 70 sigaction(SIGUSR1, &sa, NULL); 71 if (mq_notify(mqdes, ¬ification) != 0) { 72 perror(ERROR_PREFIX "mq_notify"); 73 mqclean(mqdes, mqname); 74 return PTS_UNRESOLVED; 75 } 76 if (mq_send(mqdes, s_msg_ptr, MSG_SIZE, prio) == -1) { 77 perror(ERROR_PREFIX "mq_send"); 78 mqclean(mqdes, mqname); 79 return PTS_UNRESOLVED; 80 } 81 if (!enter_handler) { 82 perror(ERROR_PREFIX "mq_notify"); 83 mqclean(mqdes, mqname); 84 return PTS_UNRESOLVED; 85 } 86 if (mq_notify(mqdes, ¬ification) != 0) { 87 printf("Test FAILED \n"); 88 mqclean(mqdes, mqname); 89 return PTS_FAIL; 90 } else { 91 printf("Test PASSED \n"); 92 mqclean(mqdes, mqname); 93 return PTS_PASS; 94 } 95 } 96