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 36int enter_handler = 0; 37 38void msg_handler() 39{ 40 enter_handler = 1; 41} 42 43void mqclean(mqd_t queue, const char *qname) 44{ 45 mq_close(queue); 46 mq_unlink(qname); 47} 48int main() 49{ 50 char mqname[50]; 51 mqd_t mqdes; 52 const char s_msg_ptr[MSG_SIZE] = "test message \n"; 53 struct sigevent notification; 54 struct sigaction sa; 55 unsigned int prio = 1; 56 57 sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); 58 59 mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, 0); 60 if (mqdes == (mqd_t)-1) { 61 perror(ERROR_PREFIX "mq_open"); 62 return PTS_UNRESOLVED; 63 } 64 65 notification.sigev_notify = SIGEV_SIGNAL; 66 notification.sigev_signo = SIGUSR1; 67 sa.sa_handler = msg_handler; 68 sa.sa_flags = 0; 69 sigaction(SIGUSR1, &sa, NULL); 70 if (mq_notify(mqdes, ¬ification) != 0) { 71 perror(ERROR_PREFIX "mq_notify"); 72 mqclean(mqdes, mqname); 73 return PTS_UNRESOLVED; 74 } 75 if (mq_send(mqdes, s_msg_ptr, MSG_SIZE, prio) == -1) { 76 perror(ERROR_PREFIX "mq_send"); 77 mqclean(mqdes, mqname); 78 return PTS_UNRESOLVED; 79 } 80 if (!enter_handler) { 81 perror(ERROR_PREFIX "mq_notify"); 82 mqclean(mqdes, mqname); 83 return PTS_UNRESOLVED; 84 } 85 if (mq_notify(mqdes, ¬ification) != 0) { 86 printf("Test FAILED \n"); 87 mqclean(mqdes, mqname); 88 return PTS_FAIL; 89 } 90 else { 91 printf("Test PASSED \n"); 92 mqclean(mqdes, mqname); 93 return PTS_PASS; 94 } 95} 96