int main() { char mqname[NAMESIZE], msgrv[BUFFER]; const char *msgptr = "test message"; mqd_t mqdes; int rvprio, sdprio = 1; struct timespec ts; struct mq_attr attr; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); attr.mq_msgsize = BUFFER; attr.mq_maxmsg = BUFFER; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); if (mqdes == (mqd_t)-1) { perror(ERROR_PREFIX "mq_open"); unresolved = 1; } if (mq_send(mqdes, msgptr, strlen(msgptr), sdprio) != 0) { perror(ERROR_PREFIX "mq_send"); unresolved = 1; } sleep(1); /* wait for a while */ ts.tv_sec = time(NULL) -1; /* No wait */ ts.tv_nsec = -1; /* Invalid */ if (mq_timedreceive(mqdes, msgrv, BUFFER, &rvprio, &ts) == -1) { if (errno == EINVAL) printf("FAIL: the validity of abs_timeout " "is checked\n"); else perror("Unexpected error at mq_timedreceive"); failure = 1; } if (mq_close(mqdes) != 0) { perror(ERROR_PREFIX "mq_close"); unresolved = 1; } if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink"); unresolved = 1; } if (failure==1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved==1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; }
static void benchmark_mq_timedreceive(void) { benchmark_timer_t end_time; int status; unsigned int priority; struct timespec timeout; int message[MQ_MAXMSG]; priority = 1; /*priority low*/ timeout.tv_sec = 0; timeout.tv_nsec = 0; benchmark_timer_initialize(); status = mq_timedreceive( queue2, ( char *)message, MQ_MSGSIZE, &priority, &timeout); end_time = benchmark_timer_read(); rtems_test_assert( status != (-1) ); put_time( "mq_timedreceive: available", end_time, 1, /* Only executed once */ 0, 0 ); }
int orange_eq_recv(struct orange_eq *self, struct blob *out){ if(!self->buf || self->mq == -1) return -EINVAL; struct timespec ts; timespec_from_now_us(&ts, 5000000UL); int rsize = mq_timedreceive(self->mq, self->buf, self->attr.mq_msgsize, NULL, &ts); if(rsize <= 0) return -EAGAIN; blob_init(out, self->buf, rsize); return 1; }
int main() { char mqname[NAMESIZE], msgrv[BUFFER]; const char *msgptr = "test message"; mqd_t mqdes; int prio = 1; struct mq_attr mqstat; struct timespec ts; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); memset(&mqstat, 0, sizeof(mqstat)); mqstat.mq_msgsize = BUFFER + 1; mqstat.mq_maxmsg = BUFFER + 1; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &mqstat); if (mqdes == (mqd_t)-1) { perror(ERROR_PREFIX "mq_open"); unresolved = 1; } if (mq_send(mqdes, msgptr, strlen(msgptr), prio) != 0) { perror(ERROR_PREFIX "mq_send"); unresolved = 1; } ts.tv_sec = time(NULL) + 1; ts.tv_nsec = 0; if (mq_timedreceive(mqdes, msgrv, BUFFER, NULL, &ts) > 0) { printf("FAIL: mq_timedreceive succeed unexpectly\n"); failure = 1; } else { if (EMSGSIZE != errno) { printf("errno != EMSGSIZE\n"); failure = 1; } } if (mq_close(mqdes) != 0) { perror(ERROR_PREFIX "mq_close"); unresolved = 1; } if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink"); unresolved = 1; } if (failure == 1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved == 1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; }
int main() { char mqname[NAMESIZE], msgrv[BUFFER]; const char *msgptr = "test message"; mqd_t mqdes; unsigned rvprio, sdprio = 1; struct timespec ts; struct mq_attr attr; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); attr.mq_msgsize = BUFFER; attr.mq_maxmsg = BUFFER; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); if (mqdes == (mqd_t) - 1) { perror(ERROR_PREFIX "mq_open"); unresolved = 1; } if (mq_send(mqdes, msgptr, strlen(msgptr), sdprio) != 0) { perror(ERROR_PREFIX "mq_send"); unresolved = 1; } ts.tv_sec = time(NULL) - 1; ts.tv_nsec = 0; if (mq_timedreceive(mqdes, msgrv, BUFFER, &rvprio, &ts) == -1) { perror("FAIL: mq_receive fails unexpectly\n"); failure = 1; } if (mq_close(mqdes) != 0) { perror(ERROR_PREFIX "mq_close"); unresolved = 1; } if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink"); unresolved = 1; } if (failure == 1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved == 1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; }
/* runs without GVL */ static void * xrecv(void *ptr) { struct rw_args *x = ptr; x->received = x->timeout ? mq_timedreceive(x->des, x->msg_ptr, x->msg_len, &x->msg_prio, x->timeout) : mq_receive(x->des, x->msg_ptr, x->msg_len, &x->msg_prio); return NULL; }
ssize_t ui_ipc_msgrecv(enum UI_IPC_MSQ e_mq, char *msg_ptr, time_t tmout) { if (tmout > 0) { struct timespec tm; clock_gettime(CLOCK_REALTIME, &tm); tm.tv_sec += tmout; return mq_timedreceive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL, &tm); } else return mq_receive(msqs[e_mq], msg_ptr, IPC_MQSIZ, NULL); }
SWORD32 Vos_PopQueue(WORD32 dwQueueId,WORD32 dwTimeout, BYTE *pucDataBuf,WORD32 dwLen) { T_QueueCtl *ptQueueCtl; SWORD32 sdwRevLen; struct timespec timeout; if (MAX_QUEUE_COUNT <= dwQueueId) { /* 队列控制块ID不合法 */ printf("[%s:%d]Value of QueueID is invalid!\n" , __FUNCTION__, __LINE__); return VOS_QUEUE_ERROR; } ptQueueCtl = &s_atQueueCtl[dwQueueId]; if (VOS_QUEUE_CTL_USED != ptQueueCtl->ucUsed) { printf("[%s:%d]Value of QueueID is invalid!\n" , __FUNCTION__, __LINE__); return VOS_QUEUE_ERROR; } if (dwTimeout == (WORD32)WAIT_FOREVER) { do { sdwRevLen = VOS_MsgQReceive(ptQueueCtl->dwQueueId, (BYTE*)pucDataBuf, dwLen, 0); }while((-1 == sdwRevLen) && ((errno == EINTR) || (errno == 512) || (errno == EAGAIN))); if (-1 == sdwRevLen) { printf("[%s:%d]Fail to receive message queue of Linux, error %d %s \n", __FUNCTION__, __LINE__,errno,strerror(errno)); return VOS_QUEUE_ERROR; } } else { /* 获取绝对时间 */ clock_gettime(0, &timeout); timeout.tv_sec += dwTimeout/1000; timeout.tv_sec += ((dwTimeout%1000)*1000*1000 + timeout.tv_nsec)/(1000*1000*1000); timeout.tv_nsec = ((dwTimeout%1000)*1000*1000 + timeout.tv_nsec)%(1000*1000*1000); sdwRevLen = mq_timedreceive(ptQueueCtl->dwQueueId, (CHAR*)pucDataBuf, dwLen, NULL, &timeout); if (-1 == sdwRevLen) { if (110 != errno) { printf("[%s:%d]Fail to receive message queue of Linux, error %d\n", __FUNCTION__, __LINE__,errno); } return VOS_QUEUE_ERROR; } } return sdwRevLen; }
void mqueue_check_functions(mqd_t m) { (void)mq_close(m); (void)mq_getattr(m, (struct mq_attr *)0); (void)mq_notify(m, (const struct sigevent *)0); (void)mq_open((const char *)0, 0, 0); (void)mq_receive(m, (char*)0, (size_t)0, (unsigned*)0); (void)mq_send(m, (const char *)0, (size_t)0, (unsigned)0); (void)mq_setattr(m, (const struct mq_attr *)0, (struct mq_attr *)0); (void)mq_timedreceive(m, (char*)0, (size_t)0, (unsigned*)0, (const struct timespec*)0); (void)mq_timedsend(m, (const char *)0, (size_t)0, (unsigned)0, (const struct timespec*)0); (void)mq_unlink((const char *)0); }
int main(int argc, char *argv[]) { pthread_t guiThread; pthread_t controllerThread; int ret; mqd_t messageQueue; struct mq_attr attr; attr.mq_flags = 0; attr.mq_maxmsg = 10; attr.mq_msgsize = sizeof(actionEnum); attr.mq_curmsgs = 0; messageBuf message; struct timespec wait = {0, 100000000}; messageQueue = mq_open(messageQueuePath, (int)(O_CREAT | O_RDWR), 0666, &attr); while (mq_timedreceive(messageQueue, (char*)&message, sizeof(message), NULL, &wait) != -1); GUI_Initialization(argc, argv, messageQueuePath, SIZE); ModelSetup(messageQueuePath, SIZE); if (messageQueue == -1) { perror("main: "); } else { ret |= pthread_create(&guiThread, NULL, (void *)GUI_Start, NULL); ret |= pthread_create(&controllerThread, NULL, (void *)ReceiveMessages, NULL); if (ret == 0) { pthread_join(guiThread, NULL); pthread_join(controllerThread, NULL); } else { printf("Something went wrong. Start guessing...\n"); } } ret = mq_close(messageQueue); if (ret == -1) perror("Closing mq"); ModelTakeDown(); return 0; }
int main() { char mqname[NAMESIZE]; mqd_t mqdes; char msgrv[BUFFER]; struct timespec ts; struct mq_attr attr; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); attr.mq_msgsize = BUFFER; attr.mq_maxmsg = BUFFER; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); if (mqdes == (mqd_t)-1) { perror(ERROR_PREFIX "mq_open()"); unresolved = 1; } mqdes = mqdes + 1; ts.tv_sec = time(NULL) + 1; ts.tv_nsec = 0; if (mq_timedreceive(mqdes, msgrv, BUFFER, NULL, &ts) == -1) { if (EBADF != errno) { printf("errno != EBADF \n"); failure = 1; } } else { printf("mq_timedreceive() succeed unexpectly \n"); failure = 1; } if (mq_close(mqdes - 1) != 0) { perror(ERROR_PREFIX "mq_close()"); unresolved=1; } if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink()"); unresolved=1; } if (failure==1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved==1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; }
void *Thread_DebugTX(void* pvParameters){ uint8_t data[32], len; struct timespec timeout; sem_t *sem_startup = (sem_t*)pvParameters; sem_wait(sem_startup); LREP("Thread DebugTx is running.\r\n"); timeout.tv_sec = 1; timeout.tv_nsec = 0; while(1){ len = mq_timedreceive(g_debug_tx_buffer, data, 32, 0, &timeout); if(len > 0) write(g_fd_debug, data, len); } }
static ssize_t qb_ipc_pmq_recv(struct qb_ipc_one_way *one_way, void *msg_ptr, size_t msg_len, int32_t ms_timeout) { uint32_t msg_prio; struct timespec ts_timeout; ssize_t res; if (ms_timeout >= 0) { qb_util_timespec_from_epoch_get(&ts_timeout); qb_timespec_add_ms(&ts_timeout, ms_timeout); } mq_receive_again: if (ms_timeout >= 0) { res = mq_timedreceive(one_way->u.pmq.q, (char *)msg_ptr, one_way->max_msg_size, &msg_prio, &ts_timeout); } else { res = mq_receive(one_way->u.pmq.q, (char *)msg_ptr, one_way->max_msg_size, &msg_prio); } if (res == -1) { switch (errno) { case EINTR: goto mq_receive_again; break; case EAGAIN: res = -ETIMEDOUT; break; case ETIMEDOUT: res = -errno; break; default: res = -errno; qb_util_perror(LOG_ERR, "error waiting for mq_timedreceive"); break; } } return res; }
void Consumer::work() { if(m_mqd == (mqd_t)-1) return; WorkItem item; ssize_t receivedBytes = 0; timespec ts = get_expire_time(); // The reason why we must +1 to the sizeof(WorkItem) is that the mq_receive requires the // buffer size must be greater than the msgsize_max receivedBytes = mq_timedreceive(m_mqd, (char*)&item, sizeof(WorkItem) + 1, NULL, &ts); if(receivedBytes > 0) consume_item(item); else if(receivedBytes == 0) std::cout << "0 length message received" << std::endl; else if(errno != ETIMEDOUT) { std::cout << "Error is " << strerror(errno) << std::endl; std::cout << "Oops, fail to receive message from Consumer " << m_id << ". Bytes = " << receivedBytes << std::endl; } }
void worker_init(upstreams *upstr_ptr, mqd_t msgq_id) { char msgcontent[MAX_MSG_LEN_CH]; int msgsz; unsigned int sender; struct mq_attr msgq_attr; struct timespec tm; const config_setting_t *upstr_setting; upstr_setting = config_lookup(&cfg, "upstreams"); upstream_count = config_setting_length(upstr_setting); while (running) { sleep(1); mq_getattr(msgq_id, &msgq_attr); error_log(DEBUG, "Queue \"%s\":\n\t- stores at most %ld messages\n\t- large at most %ld bytes each\n\t- currently holds %ld messages\n", QUEUE, msgq_attr.mq_maxmsg, msgq_attr.mq_msgsize, msgq_attr.mq_curmsgs); /* getting a message */ clock_gettime(CLOCK_REALTIME, &tm); tm.tv_sec += 1; msgsz = mq_timedreceive(msgq_id, msgcontent, MAX_MSG_LEN_CH, &sender, &tm); if (msgsz == -1) { if (errno == ETIMEDOUT) { continue; } error_log(ERROR, "mq_receive error: %s", strerror(errno)); exit(EXIT_FAILURE); } /* Ok, we got file to download */ if (initpath(msgcontent) != 0) { error_log(ERROR, "Initpath failed"); } else if (download(msgcontent) != 0) { error_log(ERROR, "Failed to download file"); } } error_log(ERROR, "Exiting"); }
int main(void) { mqd_t qdes; char qname[] = "/mailbox_achng1"; mode_t mode = S_IRUSR | S_IWUSR; struct mq_attr attr; attr.mq_maxmsg = QUEUE_SIZE; attr.mq_msgsize = sizeof(int); attr.mq_flags = 0; // blocking queue qdes = mq_open(qname, O_RDONLY, mode, &attr); if (qdes == -1 ) { perror("mq_open()"); exit(1); } signal(SIGINT, sig_handler); // install Ctl-C signal handler srand(time(0)); while (g_continue) { int integer_received; struct timespec ts = {time(0) + 5, 0}; // only block for a limited time if queue is empty if (mq_timedreceive(qdes, (char *) &integer_received, \ sizeof(int), 0, &ts) == -1) { perror("mq_timedreceive() failed"); printf("Type Ctrl-C and wait for 5 seconds to terminate.\n"); } else { printf("Received a random integer: %d \n", integer_received); } }; if (mq_close(qdes) == -1) { perror("mq_close() failed"); exit(2); } return 0; }
int mqueue_lat_test(void) { mqd_t q; struct mq_attr attr; struct timespec start, end, now, target; int i, count, ret; q = mq_open("/foo", O_CREAT | O_RDONLY, 0666, NULL); if (q < 0) { perror("mq_open"); return -1; } mq_getattr(q, &attr); count = 100; clock_gettime(CLOCK_MONOTONIC, &start); for (i = 0; i < count; i++) { char buf[attr.mq_msgsize]; clock_gettime(CLOCK_REALTIME, &now); target = now; target = timespec_add(now, TARGET_TIMEOUT); /* 100ms */ ret = mq_timedreceive(q, buf, sizeof(buf), NULL, &target); if (ret < 0 && errno != ETIMEDOUT) { perror("mq_timedreceive"); return -1; } } clock_gettime(CLOCK_MONOTONIC, &end); mq_close(q); if ((timespec_sub(start, end)/count) > TARGET_TIMEOUT + UNRESONABLE_LATENCY) return -1; return 0; }
int nilfs_cleaner_wait(struct nilfs_cleaner *cleaner, uint32_t jobid, const struct timespec *abs_timeout) { struct nilfs_cleaner_request_with_jobid req; struct nilfs_cleaner_response res; int bytes, ret = -1; if (cleaner->sendq < 0 || cleaner->recvq < 0) { errno = EBADF; goto out; } if (nilfs_cleaner_clear_queueu(cleaner) < 0) goto out; req.hdr.cmd = NILFS_CLEANER_CMD_WAIT; req.hdr.argsize = 0; uuid_copy(req.hdr.client_uuid, cleaner->client_uuid); req.jobid = jobid; ret = mq_send(cleaner->sendq, (char *)&req, sizeof(req), NILFS_CLEANER_PRIO_NORMAL); if (ret < 0) goto out; /* ETIMEDOUT will be returned in case of timeout */ bytes = mq_timedreceive(cleaner->recvq, (char *)&res, sizeof(res), NULL, abs_timeout); if (bytes < sizeof(res)) { if (bytes >= 0) errno = EIO; ret = -1; goto out; } if (res.result == NILFS_CLEANER_RSP_NACK) { ret = -1; errno = res.err; } out: return ret; }
static void * tf (void *arg) { int r = pthread_barrier_wait (&b); if (r != 0 && r != PTHREAD_BARRIER_SERIAL_THREAD) { puts ("tf: barrier_wait failed"); exit (1); } pthread_cleanup_push (cl, NULL); char c = ' '; switch ((long) arg) { case TF_MQ_SEND: TEMP_FAILURE_RETRY (mq_send (q, &c, 1, 1)); break; case TF_MQ_TIMEDSEND: TEMP_FAILURE_RETRY (mq_timedsend (q, &c, 1, 1, &never)); break; case TF_MQ_RECEIVE: TEMP_FAILURE_RETRY (mq_receive (q, &c, 1, NULL)); break; case TF_MQ_TIMEDRECEIVE: TEMP_FAILURE_RETRY (mq_timedreceive (q, &c, 1, NULL, &never)); break; } pthread_cleanup_pop (0); printf ("tf: %s returned\n", names[(long) arg]); exit (1); }
int main(int argc, char *argv[]) { int flags; int opt; mqd_t mqd; unsigned int prio; void *buffer; struct mq_attr attr; ssize_t numRead; struct timespec ts; struct timespec *tsp; flags = O_RDONLY; tsp = NULL; while ((opt = getopt(argc, argv, "t:n")) != -1) { switch (opt) { case 'n': flags |= O_NONBLOCK; break; case 't': if (clock_gettime(CLOCK_REALTIME, &ts) == -1) { errExit("clock_gettime"); } ts.tv_sec += getInt(optarg, GN_NONNEG, "timeout"); tsp = &ts; break; default: usageError(argv[0]); } } if (optind >= argc) { usageError(argv[0]); } mqd = mq_open(argv[optind], flags); if (mqd == (mqd_t)-1) { errExit("mq_open"); } if (mq_getattr(mqd, &attr) == -1) { errExit("mq_getattr"); } buffer = malloc(attr.mq_msgsize); if (buffer == NULL) { errExit("malloc"); } numRead = mq_timedreceive(mqd, buffer, attr.mq_msgsize, &prio, tsp); if (numRead == -1) { errExit("mq_receive"); } printf("Read %ld butes; priority = %u\n", (long)numRead, prio); if (write(STDOUT_FILENO, buffer, numRead) == -1) { errExit("write"); } write(STDOUT_FILENO, "\n", 1); exit(EXIT_SUCCESS); }
/*========================================================== dv task entry function ===========================================================*/ int DC_task_entry( int argc, char **argv ) { UINT16 i; gp_size_t resolution; UINT8* addr; UINT16* addr1; STRING_ASCII_INFO ascii_str; STRING_INFO str; UINT16 charge_flag = 1; int ret; UINT16 *fb; UINT8 msg_id,msg_prio; struct timespec timeout; //run dv and init pipe free_size_flag = 1; DC_Set_Color(); while(dv_set.change_disp == 1); dv_set.zoom_flag = 0; dv_set.zoom_num = 0; DC_pipe_init(); dispGetResolution(hDisp, &resolution); printf("resolution.width=%d,resolution.height=%d\n",resolution.width,resolution.height); //init UI for DV DC_UI_foreground(); do { clock_gettime(CLOCK_REALTIME, &timeout); timeout.tv_nsec += 10000; ret = mq_timedreceive(menumodeMsgQ, (char *)&msg_id, 1, (unsigned int *)&msg_prio,&timeout); if(ret != -1) DC_UI_foreground(); }while((dv_set.dv_UI_flag == DV_EXIT_MODE)||(ret != -1)); dv_set.dv_UI_flag = DV_FOREGROUND_MODE; if(dv_set.display_mode == SP_DISP_OUTPUT_LCD) { LCD_Backlight_Set(1); } if(dv_set.default_setting_flag == 1) { dv_set.default_setting_flag = 0; CVR_Set_PIPE_With_CMD(CMD_SET_DEFAULT_SETTING,0); } if(dv_set.date_type_flag == 1) { CVR_Set_PIPE(CMD_SET_DATE_TYPE,setting_config_get(SET_DATA_FORMAT)); dv_set.date_type_flag = 0; } while(1) { ret = mq_receive(menumodeMsgQ, (char *)&msg_id, 1, (UINT32 *)&msg_prio); switch(msg_id){ case DV_FOREGROUND_MODE: dv_set.draw_start = 1; DC_UI_foreground(); dv_set.draw_start = 0; break; case DV_MENU_MODE: dv_set.draw_start = 1; disp_OSD0_Set_Alpha(hDisp,65); UI_Menu_draw(Playmode_flag); dv_set.draw_start = 0; break; case DV_SETTING_MODE: dv_set.draw_start = 1; DV_UI_Setting(); dv_set.draw_start = 0; break; default: break; } if(msg_id == DV_EXIT_MODE) { CVR_Set_PIPE_With_CMD(CMD_SET_EXIT,1); break; } //pend message from TimeKeyThread and update UI/send message to ipcam } pipeDelete(); return 0; }
/** * Manage shared memory segment (respond to commands, etc) * * Does not return until shmmem destruction (unless you execute in poll mode) * Typically run in a dedicated thread. * * @param B BAKA World * @param shmmap Shared memory map structure * @param flags BK_SHMMEM_MANAGE_POLL */ void bk_shmmap_manage(bk_s B, struct bk_shmmap *shmmap, bk_flags flags) { BK_ENTRY(B, __FUNCTION__, __FILE__, "libbk"); struct bk_shmmap_cmds bop; struct timespec deltatime; if (!shmmap) { bk_error_printf(B, BK_ERR_ERR, "Illegal arguments\n"); BK_VRETURN(B); } deltatime.tv_sec = shmmap->sm_addr->sh_fresh / 2; if (shmmap->sm_addr->sh_fresh & 1) deltatime.tv_nsec = 500000000; while (1) { struct timeval tvtime; struct timespec tstime; gettimeofday(&tvtime, NULL); shmmap->sm_addr->sh_creatortime = tvtime.tv_sec; if (BK_FLAG_ISSET(flags, BK_SHMMAP_MANAGE_POLL)) { tstime.tv_sec = 0; tstime.tv_nsec = 0; } else { tstime.tv_sec = tvtime.tv_sec + deltatime.tv_sec; tstime.tv_nsec = tvtime.tv_usec * 1000 + deltatime.tv_nsec; BK_TS_RECTIFY(&tstime); } if (mq_timedreceive(shmmap->sm_creatorcmds, (char *)&bop, sizeof(bop), NULL, &tstime) < 0) { if (errno == ETIMEDOUT) { if (BK_FLAG_ISSET(flags, BK_SHMMAP_MANAGE_POLL)) break; continue; } bk_error_printf(B, BK_ERR_ERR, "shmmap mq receive failed: %s\n", strerror(errno)); BK_VRETURN(B); } if (bop.bsc_op == bk_shmmap_op_destroy) { shm_unlink(shmmap->sm_name); mq_unlink(shmmap->sm_name); free(shmmap->sm_name); free(shmmap); BK_VRETURN(B); } if (bop.bsc_op == bk_shmmap_op_detach) { int x; for(x=0;x<shmmap->sm_addr->sh_numclients;x++) { if (shmmap->sm_addr->sh_client[x].su_pid == bop.bsc_pid && !strncmp(shmmap->sm_addr->sh_client[x].su_name, bop.bsc_name, BK_SHMMAP_MAXCLIENTNAME)) { shmmap->sm_addr->sh_client[x].su_state = BK_SHMMAP_USER_STATEEMPTY; if (shmmap->sm_addr->sh_client[x].su_state == BK_SHMMAP_USER_STATEPREP || shmmap->sm_addr->sh_client[x].su_state == BK_SHMMAP_USER_STATEINIT || shmmap->sm_addr->sh_client[x].su_state == BK_SHMMAP_USER_STATEREADY || shmmap->sm_addr->sh_client[x].su_state == BK_SHMMAP_USER_STATECLOSE) shmmap->sm_addr->sh_numattach--; } } } if (bop.bsc_op == bk_shmmap_op_attach) { int x; for(x=0;x<shmmap->sm_addr->sh_numclients;x++) { if (shmmap->sm_addr->sh_client[x].su_state == BK_SHMMAP_USER_STATEEMPTY) { shmmap->sm_addr->sh_client[x].su_pid = bop.bsc_pid; strncpy(shmmap->sm_addr->sh_client[x].su_name, bop.bsc_name, BK_SHMMAP_MAXCLIENTNAME); shmmap->sm_addr->sh_client[x].su_state = BK_SHMMAP_USER_STATEPREP; shmmap->sm_addr->sh_numattach++; break; } } } } BK_VRETURN(B); }
/* Receive the oldest from highest priority messages in message queue MQDES. */ ssize_t mq_receive (mqd_t mqdes, char *msg_ptr, size_t msg_len, unsigned int *msg_prio) { return mq_timedreceive (mqdes, msg_ptr, msg_len, msg_prio, NULL); }
static int do_test (void) { int result = 0; char name[sizeof "/tst-mqueue2-" + sizeof (pid_t) * 3]; snprintf (name, sizeof (name), "/tst-mqueue2-%u", getpid ()); struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 }; mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q == (mqd_t) -1) { printf ("mq_open failed with: %m\n"); return result; } else add_temp_mq (name); mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q2 != (mqd_t) -1) { puts ("mq_open with O_EXCL unexpectedly succeeded"); result = 1; } else if (errno != EEXIST) { printf ("mq_open did not fail with EEXIST: %m\n"); result = 1; } char name2[sizeof "/tst-mqueue2-2-" + sizeof (pid_t) * 3]; snprintf (name2, sizeof (name2), "/tst-mqueue2-2-%u", getpid ()); attr.mq_maxmsg = -2; q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q2 != (mqd_t) -1) { puts ("mq_open with invalid mq_maxmsg unexpectedly succeeded"); add_temp_mq (name2); result = 1; } else if (errno != EINVAL) { printf ("mq_open with invalid mq_maxmsg did not fail with " "EINVAL: %m\n"); result = 1; } attr.mq_maxmsg = 2; attr.mq_msgsize = -56; q2 = mq_open (name2, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q2 != (mqd_t) -1) { puts ("mq_open with invalid mq_msgsize unexpectedly succeeded"); add_temp_mq (name2); result = 1; } else if (errno != EINVAL) { printf ("mq_open with invalid mq_msgsize did not fail with " "EINVAL: %m\n"); result = 1; } char buf[3]; struct timespec ts; if (clock_gettime (CLOCK_REALTIME, &ts) == 0) ts.tv_sec += 10; else { ts.tv_sec = time (NULL) + 10; ts.tv_nsec = 0; } if (mq_timedreceive (q, buf, 1, NULL, &ts) == 0) { puts ("mq_timedreceive with too small msg_len did not fail"); result = 1; } else if (errno != EMSGSIZE) { printf ("mq_timedreceive with too small msg_len did not fail with " "EMSGSIZE: %m\n"); result = 1; } ts.tv_nsec = -1; if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0) { puts ("mq_timedreceive with negative tv_nsec did not fail"); result = 1; } else if (errno != EINVAL) { printf ("mq_timedreceive with negative tv_nsec did not fail with " "EINVAL: %m\n"); result = 1; } ts.tv_nsec = 1000000000; if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0) { puts ("mq_timedreceive with tv_nsec >= 1000000000 did not fail"); result = 1; } else if (errno != EINVAL) { printf ("mq_timedreceive with tv_nsec >= 1000000000 did not fail with " "EINVAL: %m\n"); result = 1; } struct sigaction sa = { .sa_handler = alrm_handler, .sa_flags = 0 }; sigemptyset (&sa.sa_mask); sigaction (SIGALRM, &sa, NULL); struct itimerval it = { .it_value = { .tv_sec = 1 } }; setitimer (ITIMER_REAL, &it, NULL); if (mq_receive (q, buf, 2, NULL) == 0) { puts ("mq_receive on empty queue did not block"); result = 1; } else if (errno != EINTR) { printf ("mq_receive on empty queue did not fail with EINTR: %m\n"); result = 1; } setitimer (ITIMER_REAL, &it, NULL); ts.tv_nsec = 0; if (mq_timedreceive (q, buf, 2, NULL, &ts) == 0) { puts ("mq_timedreceive on empty queue did not block"); result = 1; } else if (errno != EINTR) { printf ("mq_timedreceive on empty queue did not fail with EINTR: %m\n"); result = 1; } buf[0] = '6'; buf[1] = '7'; if (mq_send (q, buf, 2, 3) != 0 || (buf[0] = '8', mq_send (q, buf, 1, 4) != 0)) { printf ("mq_send failed: %m\n"); result = 1; } memset (buf, ' ', sizeof (buf)); unsigned int prio; ssize_t rets = mq_receive (q, buf, 3, &prio); if (rets != 1) { if (rets == -1) printf ("mq_receive failed: %m\n"); else printf ("mq_receive returned %zd != 1\n", rets); result = 1; } else if (prio != 4 || memcmp (buf, "8 ", 3) != 0) { printf ("mq_receive prio %u (4) buf \"%c%c%c\" (\"8 \")\n", prio, buf[0], buf[1], buf[2]); result = 1; } rets = mq_receive (q, buf, 2, NULL); if (rets != 2) { if (rets == -1) printf ("mq_receive failed: %m\n"); else printf ("mq_receive returned %zd != 2\n", rets); result = 1; } else if (memcmp (buf, "67 ", 3) != 0) { printf ("mq_receive buf \"%c%c%c\" != \"67 \"\n", buf[0], buf[1], buf[2]); result = 1; } buf[0] = '2'; buf[1] = '1'; if (clock_gettime (CLOCK_REALTIME, &ts) != 0) ts.tv_sec = time (NULL); ts.tv_nsec = -1000000001; if ((mq_timedsend (q, buf, 2, 5, &ts) != 0 && (errno != EINVAL || mq_send (q, buf, 2, 5) != 0)) || (buf[0] = '3', ts.tv_nsec = -ts.tv_nsec, (mq_timedsend (q, buf, 1, 4, &ts) != 0 && (errno != EINVAL || mq_send (q, buf, 1, 4) != 0)))) { printf ("mq_timedsend failed: %m\n"); result = 1; } buf[0] = '-'; ts.tv_nsec = 1000000001; if (mq_timedsend (q, buf, 1, 6, &ts) == 0) { puts ("mq_timedsend with tv_nsec >= 1000000000 did not fail"); result = 1; } else if (errno != EINVAL) { printf ("mq_timedsend with tv_nsec >= 1000000000 did not fail with " "EINVAL: %m\n"); result = 1; } ts.tv_nsec = -2; if (mq_timedsend (q, buf, 1, 6, &ts) == 0) { puts ("mq_timedsend with negative tv_nsec did not fail"); result = 1; } else if (errno != EINVAL) { printf ("mq_timedsend with megatove tv_nsec did not fail with " "EINVAL: %m\n"); result = 1; } setitimer (ITIMER_REAL, &it, NULL); if (mq_send (q, buf, 2, 8) == 0) { puts ("mq_send on full queue did not block"); result = 1; } else if (errno != EINTR) { printf ("mq_send on full queue did not fail with EINTR: %m\n"); result = 1; } setitimer (ITIMER_REAL, &it, NULL); ts.tv_sec += 10; ts.tv_nsec = 0; if (mq_timedsend (q, buf, 2, 7, &ts) == 0) { puts ("mq_timedsend on full queue did not block"); result = 1; } else if (errno != EINTR) { printf ("mq_timedsend on full queue did not fail with EINTR: %m\n"); result = 1; } memset (buf, ' ', sizeof (buf)); if (clock_gettime (CLOCK_REALTIME, &ts) != 0) ts.tv_sec = time (NULL); ts.tv_nsec = -1000000001; rets = mq_timedreceive (q, buf, 2, &prio, &ts); if (rets == -1 && errno == EINVAL) rets = mq_receive (q, buf, 2, &prio); if (rets != 2) { if (rets == -1) printf ("mq_timedreceive failed: %m\n"); else printf ("mq_timedreceive returned %zd != 2\n", rets); result = 1; } else if (prio != 5 || memcmp (buf, "21 ", 3) != 0) { printf ("mq_timedreceive prio %u (5) buf \"%c%c%c\" (\"21 \")\n", prio, buf[0], buf[1], buf[2]); result = 1; } if (mq_receive (q, buf, 1, NULL) == 0) { puts ("mq_receive with too small msg_len did not fail"); result = 1; } else if (errno != EMSGSIZE) { printf ("mq_receive with too small msg_len did not fail with " "EMSGSIZE: %m\n"); result = 1; } ts.tv_nsec = -ts.tv_nsec; rets = mq_timedreceive (q, buf, 2, NULL, &ts); if (rets == -1 && errno == EINVAL) rets = mq_receive (q, buf, 2, NULL); if (rets != 1) { if (rets == -1) printf ("mq_timedreceive failed: %m\n"); else printf ("mq_timedreceive returned %zd != 1\n", rets); result = 1; } else if (memcmp (buf, "31 ", 3) != 0) { printf ("mq_timedreceive buf \"%c%c%c\" != \"31 \"\n", buf[0], buf[1], buf[2]); result = 1; } if (mq_send (q, "", 0, 2) != 0) { printf ("mq_send with msg_len 0 failed: %m\n"); result = 1; } rets = mq_receive (q, buf, 2, &prio); if (rets) { if (rets == -1) printf ("mq_receive failed: %m\n"); else printf ("mq_receive returned %zd != 0\n", rets); result = 1; } long mq_prio_max = sysconf (_SC_MQ_PRIO_MAX); if (mq_prio_max > 0 && (unsigned int) mq_prio_max == mq_prio_max) { if (mq_send (q, buf, 1, mq_prio_max) == 0) { puts ("mq_send with MQ_PRIO_MAX priority unpexpectedly succeeded"); result = 1; } else if (errno != EINVAL) { printf ("mq_send with MQ_PRIO_MAX priority did not fail with " "EINVAL: %m\n"); result = 1; } if (mq_send (q, buf, 1, mq_prio_max - 1) != 0) { printf ("mq_send with MQ_PRIO_MAX-1 priority failed: %m\n"); result = 1; } } if (mq_unlink (name) != 0) { printf ("mq_unlink failed: %m\n"); result = 1; } q2 = mq_open (name, O_RDWR); if (q2 != (mqd_t) -1) { printf ("mq_open of unlinked %s without O_CREAT unexpectedly" "succeeded\n", name); result = 1; } else if (errno != ENOENT) { printf ("mq_open of unlinked %s without O_CREAT did not fail with " "ENOENT: %m\n", name); result = 1; } if (mq_close (q) != 0) { printf ("mq_close in parent failed: %m\n"); result = 1; } if (mq_receive (q, buf, 2, NULL) == 0) { puts ("mq_receive on invalid mqd_t did not fail"); result = 1; } else if (errno != EBADF) { printf ("mq_receive on invalid mqd_t did not fail with EBADF: %m\n"); result = 1; } if (mq_send (q, buf, 1, 2) == 0) { puts ("mq_send on invalid mqd_t did not fail"); result = 1; } else if (errno != EBADF) { printf ("mq_send on invalid mqd_t did not fail with EBADF: %m\n"); result = 1; } if (mq_getattr (q, &attr) == 0) { puts ("mq_getattr on invalid mqd_t did not fail"); result = 1; } else if (errno != EBADF) { printf ("mq_getattr on invalid mqd_t did not fail with EBADF: %m\n"); result = 1; } memset (&attr, 0, sizeof (attr)); if (mq_setattr (q, &attr, NULL) == 0) { puts ("mq_setattr on invalid mqd_t did not fail"); result = 1; } else if (errno != EBADF) { printf ("mq_setattr on invalid mqd_t did not fail with EBADF: %m\n"); result = 1; } if (mq_unlink ("/tst-mqueue2-which-should-never-exist") != -1) { puts ("mq_unlink of non-existant message queue unexpectedly succeeded"); result = 1; } else if (errno != ENOENT) { printf ("mq_unlink of non-existant message queue did not fail with " "ENOENT: %m\n"); result = 1; } return result; }
int main(void) { char mqname[NAMESIZE], msgrv1[BUFFER], msgrv2[BUFFER]; const char *msgptr1 = "test message 1"; const char *msgptr2 = "test message 2"; mqd_t mqdes; unsigned rvprio, sdprio1 = 1, sdprio2 = 2; struct timespec ts; struct mq_attr attr; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); attr.mq_msgsize = BUFFER; attr.mq_maxmsg = BUFFER; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); if (mqdes == (mqd_t) - 1) { perror(ERROR_PREFIX "mq_open"); unresolved = 1; } if (mq_send(mqdes, msgptr1, strlen(msgptr1), sdprio1) != 0) { perror(ERROR_PREFIX "mq_send"); unresolved = 1; } if (mq_send(mqdes, msgptr2, strlen(msgptr2), sdprio2) != 0) { perror(ERROR_PREFIX "mq_send"); unresolved = 1; } ts.tv_sec = time(NULL) + 1; ts.tv_nsec = 0; if (mq_timedreceive(mqdes, msgrv1, BUFFER, &rvprio, &ts) == -1) { perror(ERROR_PREFIX "mq_timedreceive"); failure = 1; } if (strncmp(msgptr2, msgrv1, strlen(msgptr2)) != 0) { printf("FAIL: mq_timedreceive didn't receive the highest " "priority message\n"); failure = 1; } if (rvprio != sdprio2) { printf("FAIL: receive priority %d != send priority %d\n", rvprio, sdprio2); failure = 1; } ts.tv_sec = time(NULL) + 1; ts.tv_nsec = 0; if (mq_timedreceive(mqdes, msgrv2, BUFFER, &rvprio, &ts) == -1) { perror(ERROR_PREFIX "mq_timedreceive"); failure = 1; } if (strncmp(msgptr1, msgrv2, strlen(msgptr1)) != 0) { printf("FAIL: mq_timedreceive didn't receive the correct " "message\n"); failure = 1; } if (rvprio != sdprio1) { printf("FAIL: receive priority %d != send priority %d\n", rvprio, sdprio1); failure = 1; } if (mq_close(mqdes) != 0) { perror(ERROR_PREFIX "mq_close"); unresolved = 1; } if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink"); unresolved = 1; } if (failure == 1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved == 1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; }
int main() { char mqname[NAMESIZE], msgrv[BUFFER]; mqd_t mqdes; struct timespec ts; time_t oldtime, newtime; struct mq_attr attr; pid_t pid; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); attr.mq_msgsize = BUFFER; attr.mq_maxmsg = BUFFER; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); if (mqdes == (mqd_t)-1) { perror(ERROR_PREFIX "mq_open"); unresolved = 1; } pid = fork(); if (pid != 0) { /* Parent process */ struct sigaction act; act.sa_handler = exit_handler; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGABRT, &act, 0); ts.tv_sec = time(NULL) + TIMEOUT; ts.tv_nsec = 0; oldtime = time(NULL); mq_timedreceive(mqdes, msgrv, BUFFER, NULL, &ts); newtime = time(NULL); if ((newtime - oldtime) < THRESHOLD) { printf("FAIL: mq_timedreceive didn't block until " "timout expires\n"); failure = 1; } /* Parent is not blocking, let child abort */ kill(pid, SIGABRT); if (mq_close(mqdes) != 0) { perror(ERROR_PREFIX "mq_close"); unresolved = 1; } if (mq_unlink(mqname) != 0) { perror(ERROR_PREFIX "mq_unlink"); unresolved = 1; } if (failure == 1 || blocking == 1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved == 1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; } else { sleep(TIMEOUT + 3); /* Parent is probably blocking send a signal to let it abort */ kill(getppid(), SIGABRT); return 0; } printf("Test PASSED\n"); return PTS_PASS; }
void *pmqthread(void *param) { int mustgetcpu = 0; struct params *par = param; cpu_set_t mask; int policy = SCHED_FIFO; struct sched_param schedp; struct timespec ts; memset(&schedp, 0, sizeof(schedp)); schedp.sched_priority = par->priority; sched_setscheduler(0, policy, &schedp); if (par->cpu != -1) { CPU_ZERO(&mask); CPU_SET(par->cpu, &mask); if(sched_setaffinity(0, sizeof(mask), &mask) == -1) fprintf(stderr, "WARNING: Could not set CPU affinity " "to CPU #%d\n", par->cpu); } else mustgetcpu = 1; par->tid = gettid(); while (!par->shutdown) { if (par->sender) { /* Optionally force receiver timeout */ if (par->forcetimeout) { struct timespec senddelay; senddelay.tv_sec = par->forcetimeout; senddelay.tv_nsec = 0; clock_nanosleep(CLOCK_MONOTONIC, 0, &senddelay, NULL); } /* Send message: Start of latency measurement ... */ gettimeofday(&par->sent, NULL); if (mq_send(par->testmq, testmsg, strlen(testmsg), 1) != 0) { fprintf(stderr, "could not send test message\n"); par->shutdown = 1; } par->samples++; if(par->max_cycles && par->samples >= par->max_cycles) par->shutdown = 1; if (mustgetcpu) par->cpu = get_cpu(); /* Wait until receiver ready */ if (par->timeout) { clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += par->timeout; if (mq_timedreceive(par->syncmq, par->recvsyncmsg, MSG_SIZE, NULL, &ts) != strlen(syncmsg)) { fprintf(stderr, "could not receive sync message\n"); par->shutdown = 1; } } if (mq_receive(par->syncmq, par->recvsyncmsg, MSG_SIZE, NULL) != strlen(syncmsg)) { perror("could not receive sync message"); par->shutdown = 1; } if (!par->shutdown && strcmp(syncmsg, par->recvsyncmsg)) { fprintf(stderr, "ERROR: Sync message mismatch detected\n"); fprintf(stderr, " %s != %s\n", syncmsg, par->recvsyncmsg); par->shutdown = 1; } } else { /* Receiver */ if (par->timeout) { clock_gettime(CLOCK_REALTIME, &ts); par->timeoutcount = 0; ts.tv_sec += par->timeout; do { if (mq_timedreceive(par->testmq, par->recvtestmsg, MSG_SIZE, NULL, &ts) != strlen(testmsg)) { if (!par->forcetimeout || errno != ETIMEDOUT) { perror("could not receive test message"); par->shutdown = 1; break; } if (errno == ETIMEDOUT) { par->timeoutcount++; clock_gettime(CLOCK_REALTIME, &ts); ts.tv_sec += par->timeout; } } else break; } while (1); } else { if (mq_receive(par->testmq, par->recvtestmsg, MSG_SIZE, NULL) != strlen(testmsg)) { perror("could not receive test message"); par->shutdown = 1; } } /* ... Received the message: End of latency measurement */ gettimeofday(&par->received, NULL); if (!par->shutdown && strcmp(testmsg, par->recvtestmsg)) { fprintf(stderr, "ERROR: Test message mismatch detected\n"); fprintf(stderr, " %s != %s\n", testmsg, par->recvtestmsg); par->shutdown = 1; } par->samples++; timersub(&par->received, &par->neighbor->sent, &par->diff); if (par->diff.tv_usec < par->mindiff) par->mindiff = par->diff.tv_usec; if (par->diff.tv_usec > par->maxdiff) par->maxdiff = par->diff.tv_usec; par->sumdiff += (double) par->diff.tv_usec; if (par->tracelimit && par->maxdiff > par->tracelimit) { char tracing_enabled_file[MAX_PATH]; strcpy(tracing_enabled_file, get_debugfileprefix()); strcat(tracing_enabled_file, "tracing_enabled"); int tracing_enabled = open(tracing_enabled_file, O_WRONLY); if (tracing_enabled >= 0) { write(tracing_enabled, "0", 1); close(tracing_enabled); } else snprintf(par->error, sizeof(par->error), "Could not access %s\n", tracing_enabled_file); par->shutdown = 1; par->neighbor->shutdown = 1; } if (par->max_cycles && par->samples >= par->max_cycles) par->shutdown = 1; if (mustgetcpu) par->cpu = get_cpu(); clock_nanosleep(CLOCK_MONOTONIC, 0, &par->delay, NULL); /* Tell receiver that we are ready for the next measurement */ if (mq_send(par->syncmq, syncmsg, strlen(syncmsg), 1) != 0) { fprintf(stderr, "could not send sync message\n"); par->shutdown = 1; } } } par->stopped = 1; return NULL; }
static int do_test (void) { int result = 0; char name[sizeof "/tst-mqueue4-" + sizeof (pid_t) * 3 + NAME_MAX]; char *p; p = name + snprintf (name, sizeof (name), "/tst-mqueue4-%u", getpid ()); struct mq_attr attr = { .mq_maxmsg = 2, .mq_msgsize = 2 }; mqd_t q = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q == (mqd_t) -1) { printf ("mq_open failed with: %m\n"); return result; } else add_temp_mq (name); *p = '.'; memset (p + 1, 'x', NAME_MAX + 1 - (p - name)); name[NAME_MAX + 1] = '\0'; mqd_t q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q2 == (mqd_t) -1) { printf ("mq_open with NAME_MAX long name compoment failed with: %m\n"); result = 1; } if (mq_unlink (name) != 0) { printf ("mq_unlink failed: %m\n"); result = 1; } if (mq_close (q2) != 0) { printf ("mq_close failed: %m\n"); result = 1; } name[NAME_MAX + 1] = 'x'; name[NAME_MAX + 2] = '\0'; q2 = mq_open (name, O_CREAT | O_EXCL | O_RDWR, 0600, &attr); if (q2 != (mqd_t) -1) { puts ("mq_open with too long name component unexpectedly succeeded"); mq_unlink (name); mq_close (q2); result = 1; } else if (errno != ENAMETOOLONG) { printf ("mq_open with too long name component did not fail with " "ENAMETOOLONG: %m\n"); result = 1; } if (mq_unlink (name) == 0) { puts ("mq_unlink with too long name component unexpectedly succeeded"); result = 1; } else if (errno != ENAMETOOLONG) { printf ("mq_unlink with too long name component did not fail with " "ENAMETOOLONG: %m\n"); result = 1; } *p = '\0'; attr.mq_maxmsg = 1; attr.mq_msgsize = 3; q2 = mq_open (name, O_CREAT | O_RDWR, 0600, &attr); if (q2 == (mqd_t) -1) { printf ("mq_open without O_EXCL failed with %m\n"); result = 1; } char buf[3]; strcpy (buf, "jk"); if (mq_send (q, buf, 2, 4) != 0) { printf ("mq_send failed: %m\n"); result = 1; } if (mq_send (q, buf + 1, 1, 5) != 0) { printf ("mq_send failed: %m\n"); result = 1; } if (mq_getattr (q2, &attr) != 0) { printf ("mq_getattr failed: %m\n"); result = 1; } if ((attr.mq_flags & O_NONBLOCK) || attr.mq_maxmsg != 2 || attr.mq_msgsize != 2 || attr.mq_curmsgs != 2) { printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n" ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n", (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg, (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs); result = 1; } struct timespec ts; if (clock_gettime (CLOCK_REALTIME, &ts) == 0) ++ts.tv_sec; else { ts.tv_sec = time (NULL) + 1; ts.tv_nsec = 0; } if (mq_timedsend (q2, buf, 1, 1, &ts) == 0) { puts ("mq_timedsend unexpectedly succeeded"); result = 1; } else if (errno != ETIMEDOUT) { printf ("mq_timedsend did not fail with ETIMEDOUT: %m\n"); result = 1; } if (mq_close (q2) != 0) { printf ("mq_close failed: %m\n"); result = 1; } q2 = mq_open (name, O_RDONLY, 0600); if (q2 == (mqd_t) -1) { printf ("mq_open without O_CREAT failed with %m\n"); result = 1; } mqd_t q3 = mq_open (name, O_RDONLY, 0600); if (q3 == (mqd_t) -1) { printf ("mq_open without O_CREAT failed with %m\n"); result = 1; } memset (buf, ' ', sizeof (buf)); unsigned int prio; ssize_t rets = mq_receive (q2, buf, 2, &prio); if (rets != 1) { if (rets == -1) printf ("mq_receive failed with: %m\n"); else printf ("mq_receive returned %zd != 1\n", rets); result = 1; } else if (prio != 5 || memcmp (buf, "k ", 3) != 0) { printf ("mq_receive returned prio %u (2) buf \"%c%c%c\" (\"k \")\n", prio, buf[0], buf[1], buf[2]); result = 1; } if (mq_getattr (q3, &attr) != 0) { printf ("mq_getattr failed: %m\n"); result = 1; } if ((attr.mq_flags & O_NONBLOCK) || attr.mq_maxmsg != 2 || attr.mq_msgsize != 2 || attr.mq_curmsgs != 1) { printf ("mq_getattr returned unexpected { .mq_flags = %jd,\n" ".mq_maxmsg = %jd, .mq_msgsize = %jd, .mq_curmsgs = %jd }\n", (intmax_t) attr.mq_flags, (intmax_t) attr.mq_maxmsg, (intmax_t) attr.mq_msgsize, (intmax_t) attr.mq_curmsgs); result = 1; } rets = mq_receive (q3, buf, 2, NULL); if (rets != 2) { if (rets == -1) printf ("mq_receive failed with: %m\n"); else printf ("mq_receive returned %zd != 2\n", rets); result = 1; } else if (memcmp (buf, "jk ", 3) != 0) { printf ("mq_receive returned buf \"%c%c%c\" != \"jk \"\n", buf[0], buf[1], buf[2]); result = 1; } if (clock_gettime (CLOCK_REALTIME, &ts) == 0) ++ts.tv_sec; else { ts.tv_sec = time (NULL) + 1; ts.tv_nsec = 0; } if (mq_timedreceive (q2, buf, 2, NULL, &ts) != -1) { puts ("mq_timedreceive on empty queue unexpectedly succeeded"); result = 1; } else if (errno != ETIMEDOUT) { printf ("mq_timedreceive on empty queue did not fail with " "ETIMEDOUT: %m\n"); result = 1; } if (mq_unlink (name) != 0) { printf ("mq_unlink failed: %m\n"); result = 1; } if (mq_close (q) != 0) { printf ("mq_close failed: %m\n"); result = 1; } if (mq_close (q2) != 0) { printf ("mq_close failed: %m\n"); result = 1; } if (mq_close (q3) != 0) { printf ("mq_close failed: %m\n"); result = 1; } return result; }
int main(void) { char mqname[NAMESIZE], msgrv[BUFFER]; mqd_t mqdes; int pid; struct mq_attr attr; struct timespec ts; int unresolved = 0, failure = 0; sprintf(mqname, "/" FUNCTION "_" TEST "_%d", getpid()); attr.mq_msgsize = BUFFER; attr.mq_maxmsg = BUFFER; mqdes = mq_open(mqname, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR, &attr); if (mqdes == (mqd_t) - 1) { perror(ERROR_PREFIX "mq_open"); unresolved = 1; } pid = fork(); if (pid != 0) { /* Parent process */ struct sigaction act; act.sa_handler = stopreceive; act.sa_flags = 0; sigemptyset(&act.sa_mask); sigaction(SIGABRT, &act, 0); ts.tv_sec = INT32_MAX; ts.tv_nsec = 0; if (mq_timedreceive(mqdes, msgrv, BUFFER, NULL, &ts) == -1) { wait(NULL); if (EINTR != errno) { printf("errno != EINTR\n"); failure = 1; } } else { wait(NULL); printf("mq_timedreceive() succeed unexpectly\n"); failure = 1; } if (mq_close(mqdes) != 0) { perror("mq_close() did not return success"); unresolved = 1; } if (mq_unlink(mqname) != 0) { perror("mq_unlink() did not return success"); unresolved = 1; } if (failure == 1) { printf("Test FAILED\n"); return PTS_FAIL; } if (unresolved == 1) { printf("Test UNRESOLVED\n"); return PTS_UNRESOLVED; } printf("Test PASSED\n"); return PTS_PASS; } else { /* Child Process */ sleep(1); /* give time to parent to set up handler */ /* send signal to parent */ kill(getppid(), SIGABRT); } }
static void *receiver_thread(void *arg) { mqd_t mqfd; char msg_buffer[TEST_MSGLEN]; struct mq_attr attr; int nbytes; int nerrors = 0; int i; printf("receiver_thread: Starting\n"); /* Fill in attributes for message queue */ attr.mq_maxmsg = TEST_SEND_NMSGS-1; attr.mq_msgsize = TEST_MSGLEN; attr.mq_flags = 0; /* Set the flags for the open of the queue. * Make it a blocking open on the queue, meaning it will block if * this process tries to* send to the queue and the queue is full. * * O_CREAT - the queue will get created if it does not already exist. * O_RDONLY - we are only planning to write to the queue. * * Open the queue, and create it if the sending process hasn't * already created it. */ mqfd = mq_open("testmq", O_RDONLY|O_CREAT, 0666, &attr); if (mqfd < 0) { printf("receiver_thread: ERROR mq_open failed\n"); pthread_exit((pthread_addr_t)1); } /* Perform the receive TEST_RECEIVE_NMSGS times */ for (i = 0; i < TEST_RECEIVE_NMSGS; i++) { struct timespec ts; int status = clock_gettime(CLOCK_REALTIME, &ts); if (status != 0) { printf("sender_thread: ERROR clock_gettime failed\n"); } ts.tv_sec += 5; /* The first TEST_SEND_NMSGS-1 send should succeed. The last * one should fail with errno == ETIMEDOUT */ memset(msg_buffer, 0xaa, TEST_MSGLEN); nbytes = mq_timedreceive(mqfd, msg_buffer, TEST_MSGLEN, 0, &ts); if (nbytes < 0) { if (i == TEST_SEND_NMSGS-1 && errno == ETIMEDOUT) { printf("receiver_thread: Receive %d timed out as expected\n", i); } else { printf("receiver_thread: ERROR mq_timedreceive failure=%d on msg %d\n", errno, i); nerrors++; } } else if (nbytes != TEST_MSGLEN) { printf("receiver_thread: mq_timedreceive return bad size %d on msg %d\n", nbytes, i); nerrors++; } else if (memcmp(TEST_MESSAGE, msg_buffer, nbytes) != 0) { int j; printf("receiver_thread: mq_timedreceive returned corrupt message on msg %d\n", i); printf("receiver_thread: i Expected Received\n"); for (j = 0; j < TEST_MSGLEN-1; j++) { if (isprint(msg_buffer[j])) { printf("receiver_thread: %2d %02x (%c) %02x (%c)\n", j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j], msg_buffer[j]); } else { printf("receiver_thread: %2d %02x (%c) %02x\n", j, TEST_MESSAGE[j], TEST_MESSAGE[j], msg_buffer[j]); } } printf("receiver_thread: %2d 00 %02x\n", j, msg_buffer[j]); } else if (i == TEST_SEND_NMSGS-1) { printf("receiver_thread: ERROR mq_timedreceive of msg %d succeeded\n", i); nerrors++; } else { printf("receiver_thread: mq_timedreceive succeeded on msg %d\n", i); } } /* Close the queue and return success */ if (mq_close(mqfd) < 0) { printf("receiver_thread: ERROR mq_close failed\n"); nerrors++; } /* Destroy the queue */ if (mq_unlink("testmq") < 0) { printf("receiver_thread: ERROR mq_close failed\n"); nerrors++; } printf("receiver_thread: returning nerrors=%d\n", nerrors); FFLUSH(); pthread_exit((pthread_addr_t)nerrors); return (pthread_addr_t)nerrors; }