void mesoperate() { char* msg = mqrecv(MESSAGE_CENTER, 0); if(msg[0] == 'i') { install_android_apk(msg+2); } }
static inline int handler_recv_parse(Handler *handler, HandlerParser *parser) { log_info("handler_recv_parse"); zmq_msg_t *inmsg = NULL; check(handler->running, "Called while handler wasn't running, that's not good."); inmsg = calloc(sizeof(zmq_msg_t), 1); int rc = 0; check_mem(inmsg); rc = zmq_msg_init(inmsg); check(rc == 0, "Failed to initialize message."); taskstate("recv"); rc = mqrecv(handler->recv_socket, inmsg, 0); check(rc == 0, "Receive on handler socket failed."); check(handler->running, "Handler marked as not running."); rc = HandlerParser_execute(parser, zmq_msg_data(inmsg), zmq_msg_size(inmsg)); log_info("handler_recv_parse - HandlerParser_execute %d", rc); check(rc == 1, "Failed to parse message from handler."); check(parser->target_count > 0, "Message sent had 0 targets: %.*s", (int)zmq_msg_size(inmsg), (char *)zmq_msg_data(inmsg)); zmq_msg_close(inmsg); free(inmsg); return 0; error: if(inmsg) { zmq_msg_close(inmsg); free(inmsg); } return -1; }
static inline int handler_recv_parse(Handler *handler, HandlerParser *parser) { check(handler->running, "Called while handler wasn't running, that's not good."); zmq_msg_t *inmsg = calloc(sizeof(zmq_msg_t), 1); int rc = 0; check_mem(inmsg); rc = zmq_msg_init(inmsg); check(rc == 0, "Failed to initialize message."); taskstate("recv"); rc = mqrecv(handler->recv_socket, inmsg, ZMQ_NOBLOCK); check(rc == 0, "Receive on handler socket failed."); check(handler->running, "Received shutdown notification, goodbye."); rc = HandlerParser_execute(parser, zmq_msg_data(inmsg), zmq_msg_size(inmsg)); check(rc == 1, "Failed to parse message from handler."); check(parser->target_count > 0, "Message sent had 0 targets: %.*s", (int)zmq_msg_size(inmsg), (char *)zmq_msg_data(inmsg)); debug("Parsed message with %d targets, first: %d, uuid: %s, and body: %d", (int)parser->target_count, parser->targets[0], bdata(parser->uuid), blength(parser->body)); zmq_msg_close(inmsg); free(inmsg); return 0; error: if(inmsg) { zmq_msg_close(inmsg); free(inmsg); } return -1; }
static void * thr (void *arg) { pthread_barrier_t *b3 = ((struct thr_data *)arg)->b3; mqd_t q = ((struct thr_data *)arg)->q; const char *name = ((struct thr_data *)arg)->name; int result = 0; result |= mqrecv (q); (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been sent. */ (void) pthread_barrier_wait (b3); /* Parent calls mqsend (q), which should trigger notification. */ (void) pthread_barrier_wait (b3); if (rtmin_cnt != 2) { puts ("SIGRTMIN signal in child did not arrive"); result = 1; } else if (rtmin_pid != getppid () || rtmin_uid != getuid () || rtmin_code != SI_MESGQ || rtmin_sigval.sival_int != 0xdeadbeef) { printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (%d)\n", rtmin_pid, getppid (), rtmin_uid, getuid (), rtmin_code, SI_MESGQ, rtmin_sigval.sival_int, 0xdeadbeef); result = 1; } struct sigevent ev; memset (&ev, 0x82, sizeof (ev)); ev.sigev_notify = SIGEV_NONE; if (mq_notify (q, &ev) != 0) { printf ("mq_notify in thread (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } if (mq_notify (q, NULL) != 0) { printf ("mq_notify in thread (q, NULL) failed with: %m\n"); result = 1; } result |= mqrecv (q); (void) pthread_barrier_wait (b3); /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */ (void) pthread_barrier_wait (b3); if (mq_notify (q, NULL) != 0) { printf ("second mq_notify in thread (q, NULL) failed with: %m\n"); result = 1; } (void) pthread_barrier_wait (b3); /* Parent calls mqsend (q), which should not trigger notification. */ (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been received. */ /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */ (void) pthread_barrier_wait (b3); mqd_t q4 = mq_open (name, O_RDONLY); if (q4 == (mqd_t) -1) { printf ("mq_open in thread failed with: %m\n"); result = 1; } if (mq_notify (q4, NULL) != 0) { printf ("mq_notify in thread (q4, NULL) failed with: %m\n"); result = 1; } if (mq_close (q4) != 0) { printf ("mq_close in thread failed with: %m\n"); result = 1; } (void) pthread_barrier_wait (b3); /* Parent calls mqsend (q), which should not trigger notification. */ (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been received. */ /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */ (void) pthread_barrier_wait (b3); mqd_t q5 = mq_open (name, O_WRONLY); if (q5 == (mqd_t) -1) { printf ("mq_open O_WRONLY in thread failed with: %m\n"); result = 1; } if (mq_notify (q5, NULL) != 0) { printf ("mq_notify in thread (q5, NULL) failed with: %m\n"); result = 1; } if (mq_close (q5) != 0) { printf ("mq_close in thread failed with: %m\n"); result = 1; } (void) pthread_barrier_wait (b3); /* Parent calls mqsend (q), which should not trigger notification. */ (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been received. */ return (void *) (long) result; }
static int do_test (void) { int result = 0; char tmpfname[] = "/tmp/tst-mqueue5-barrier.XXXXXX"; int fd = mkstemp (tmpfname); if (fd == -1) { printf ("cannot open temporary file: %m\n"); return 1; } /* Make sure it is always removed. */ unlink (tmpfname); /* Create one page of data. */ size_t ps = sysconf (_SC_PAGESIZE); char data[ps]; memset (data, '\0', ps); /* Write the data to the file. */ if (write (fd, data, ps) != (ssize_t) ps) { puts ("short write"); return 1; } void *mem = mmap (NULL, ps, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); if (mem == MAP_FAILED) { printf ("mmap failed: %m\n"); return 1; } pthread_barrier_t *b2; b2 = (pthread_barrier_t *) (((uintptr_t) mem + __alignof (pthread_barrier_t)) & ~(__alignof (pthread_barrier_t) - 1)); pthread_barrier_t *b3; b3 = b2 + 1; pthread_barrierattr_t a; if (pthread_barrierattr_init (&a) != 0) { puts ("barrierattr_init failed"); return 1; } if (pthread_barrierattr_setpshared (&a, PTHREAD_PROCESS_SHARED) != 0) { puts ("barrierattr_setpshared failed, could not test"); return 0; } if (pthread_barrier_init (b2, &a, 2) != 0) { puts ("barrier_init failed"); return 1; } if (pthread_barrier_init (b3, &a, 3) != 0) { puts ("barrier_init failed"); return 1; } if (pthread_barrierattr_destroy (&a) != 0) { puts ("barrierattr_destroy failed"); return 1; } char name[sizeof "/tst-mqueue5-" + sizeof (pid_t) * 3]; snprintf (name, sizeof (name), "/tst-mqueue5-%u", getpid ()); struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 }; 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); struct sigevent ev; memset (&ev, 0xaa, sizeof (ev)); ev.sigev_notify = SIGEV_NONE; if (mq_notify (q, &ev) != 0) { printf ("mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } if (mq_notify (q, &ev) == 0) { puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded"); result = 1; } else if (errno != EBUSY) { printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } result |= mqsend (q); if (mq_notify (q, &ev) != 0) { printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } result |= mqrecv (q); if (mq_notify (q, NULL) != 0) { printf ("mq_notify (q, NULL) failed with: %m\n"); result = 1; } if (mq_notify (q, NULL) != 0) { /* Implementation-defined behaviour, so don't fail, just inform. */ printf ("second mq_notify (q, NULL) failed with: %m\n"); } struct sigaction sa = { .sa_sigaction = rtmin_handler, .sa_flags = SA_SIGINFO }; sigemptyset (&sa.sa_mask); sigaction (SIGRTMIN, &sa, NULL); memset (&ev, 0x55, sizeof (ev)); ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGRTMIN; ev.sigev_value.sival_int = 26; if (mq_notify (q, &ev) != 0) { printf ("mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n"); result = 1; } ev.sigev_value.sival_ptr = &ev; if (mq_notify (q, &ev) == 0) { puts ("second mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded"); result = 1; } else if (errno != EBUSY) { printf ("second mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n"); result = 1; } if (rtmin_cnt != 0) { puts ("SIGRTMIN signal caught too early"); result = 1; } result |= mqsend (q); if (rtmin_cnt != 1) { puts ("SIGRTMIN signal did not arrive"); result = 1; } else if (rtmin_pid != getpid () || rtmin_uid != getuid () || rtmin_code != SI_MESGQ || rtmin_sigval.sival_int != 26) { printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (26)\n", rtmin_pid, getpid (), rtmin_uid, getuid (), rtmin_code, SI_MESGQ, rtmin_sigval.sival_int); result = 1; } ev.sigev_value.sival_int = 75; if (mq_notify (q, &ev) != 0) { printf ("third mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n"); result = 1; } result |= mqrecv (q); if (mq_notify (q, NULL) != 0) { printf ("mq_notify (q, NULL) failed with: %m\n"); result = 1; } memset (&ev, 0x33, sizeof (ev)); ev.sigev_notify = SIGEV_NONE; if (mq_notify (q, &ev) != 0) { printf ("fourth mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } pid_t pid = fork (); if (pid == -1) { printf ("fork () failed: %m\n"); mq_unlink (name); return 1; } if (pid == 0) do_child (name, b2, b3, q); /* Child unsuccessfully attempts to mq_notify. */ (void) pthread_barrier_wait (b2); result |= mqsend (q); (void) pthread_barrier_wait (b2); /* Child successfully calls mq_notify SIGEV_SIGNAL now. */ result |= mqrecv (q); (void) pthread_barrier_wait (b2); memset (&ev, 0xbb, sizeof (ev)); ev.sigev_notify = SIGEV_SIGNAL; ev.sigev_signo = SIGRTMIN; ev.sigev_value.sival_int = 15; if (mq_notify (q, &ev) == 0) { puts ("fourth mq_notify (q, { SIGEV_SIGNAL }) unexpectedly succeeded"); result = 1; } else if (errno != EBUSY) { printf ("fourth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n"); result = 1; } result |= mqsend (q); if (mq_notify (q, &ev) != 0) { printf ("fifth mq_notify (q, { SIGEV_SIGNAL }) failed with: %m\n"); result = 1; } if (rtmin_cnt != 1) { puts ("SIGRTMIN signal caught too early"); result = 1; } result |= mqrecv (q); (void) pthread_barrier_wait (b2); /* Child verifies caught SIGRTMIN signal. */ /* Child calls mq_send (q) which triggers SIGRTMIN signal here. */ (void) pthread_barrier_wait (b2); /* Child mq_open's another mqd_t for the same queue (q2). */ if (rtmin_cnt != 2) { puts ("SIGRTMIN signal did not arrive"); result = 1; } else if (rtmin_pid != pid || rtmin_uid != getuid () || rtmin_code != SI_MESGQ || rtmin_sigval.sival_int != 15) { printf ("unexpected siginfo_t fields: pid %u (%u), uid %u (%u), code %d (%d), si_int %d (15)\n", rtmin_pid, pid, rtmin_uid, getuid (), rtmin_code, SI_MESGQ, rtmin_sigval.sival_int); result = 1; } result |= mqrecv (q); (void) pthread_barrier_wait (b2); /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q2. */ (void) pthread_barrier_wait (b2); memset (&ev, 0xbb, sizeof (ev)); ev.sigev_notify = SIGEV_NONE; if (mq_notify (q, &ev) == 0) { puts ("fifth mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded"); result = 1; } else if (errno != EBUSY) { printf ("fifth mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } (void) pthread_barrier_wait (b2); /* Child calls mq_close on q2, which makes the queue available again for notification. */ mqd_t q3 = mq_open (name, O_RDWR); if (q3 == (mqd_t) -1) { printf ("mq_open q3 in parent failed with: %m\n"); result = 1; } (void) pthread_barrier_wait (b2); memset (&ev, 0x12, sizeof (ev)); ev.sigev_notify = SIGEV_NONE; if (mq_notify (q3, &ev) != 0) { printf ("mq_notify (q3, { SIGEV_NONE }) failed with: %m\n"); result = 1; } (void) pthread_barrier_wait (b2); /* Child unsuccessfully attempts to mq_notify { SIGEV_SIGNAL } on q. */ (void) pthread_barrier_wait (b2); if (mq_close (q3) != 0) { printf ("mq_close failed: %m\n"); result = 1; } (void) pthread_barrier_wait (b2); /* Child successfully calls mq_notify { SIGEV_NONE } on q. */ /* Child successfully calls mq_notify NULL on q. */ (void) pthread_barrier_wait (b2); /* Child creates new thread. */ /* Thread blocks on mqrecv (q). */ /* Child sleeps for 1sec so that thread has time to reach that point. */ /* Child successfully calls mq_notify { SIGEV_SIGNAL } on q. */ (void) pthread_barrier_wait (b2); result |= mqsend (q); (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been sent. */ (void) pthread_barrier_wait (b3); result |= mqsend (q); (void) pthread_barrier_wait (b3); /* Thread verifies SIGRTMIN has been caught. */ /* Thread calls mq_notify (q, { SIGEV_NONE }) to verify notification is now available for registration. */ /* Thread calls mq_notify (q, NULL). */ (void) pthread_barrier_wait (b3); /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */ (void) pthread_barrier_wait (b3); /* Thread calls mq_notify (q, NULL). */ (void) pthread_barrier_wait (b3); result |= mqsend (q); result |= mqrecv (q); (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been sent. */ /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */ (void) pthread_barrier_wait (b3); /* Thread opens a new O_RDONLY mqd_t (q4). */ /* Thread calls mq_notify (q4, NULL). */ /* Thread calls mq_close (q4). */ (void) pthread_barrier_wait (b3); result |= mqsend (q); result |= mqrecv (q); (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been sent. */ /* Child calls mq_notify (q, { SIGEV_SIGNAL }). */ (void) pthread_barrier_wait (b3); /* Thread opens a new O_WRONLY mqd_t (q5). */ /* Thread calls mq_notify (q5, NULL). */ /* Thread calls mq_close (q5). */ (void) pthread_barrier_wait (b3); result |= mqsend (q); result |= mqrecv (q); (void) pthread_barrier_wait (b3); /* Child verifies SIGRTMIN has not been sent. */ int status; if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) { puts ("waitpid failed"); kill (pid, SIGKILL); result = 1; } else if (!WIFEXITED (status) || WEXITSTATUS (status)) { printf ("child failed with status %d\n", status); 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_notify (q, NULL) == 0) { puts ("mq_notify on closed mqd_t unexpectedly succeeded"); result = 1; } else if (errno != EBADF) { printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n"); result = 1; } memset (&ev, 0x55, sizeof (ev)); ev.sigev_notify = SIGEV_NONE; if (mq_notify (q, &ev) == 0) { puts ("mq_notify on closed mqd_t unexpectedly succeeded"); result = 1; } else if (errno != EBADF) { printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n"); result = 1; } return result; }
static int do_test (void) { int result = 0; char name[sizeof "/tst-mqueue6-" + sizeof (pid_t) * 3]; snprintf (name, sizeof (name), "/tst-mqueue6-%u", getpid ()); struct mq_attr attr = { .mq_maxmsg = 1, .mq_msgsize = 1 }; 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); pthread_attr_t nattr; if (pthread_attr_init (&nattr) || pthread_attr_setguardsize (&nattr, 0)) { puts ("pthread_attr_t setup failed"); result = 1; } fct_guardsize = 1; struct sigevent ev; memset (&ev, 0xaa, sizeof (ev)); ev.sigev_notify = SIGEV_THREAD; ev.sigev_notify_function = fct; ev.sigev_notify_attributes = &nattr; ev.sigev_value.sival_ptr = &q; if (mq_notify (q, &ev) != 0) { printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n"); result = 1; } size_t ps = sysconf (_SC_PAGESIZE); if (pthread_attr_setguardsize (&nattr, 32 * ps)) { puts ("pthread_attr_t setup failed"); result = 1; } if (mq_notify (q, &ev) == 0) { puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded"); result = 1; } else if (errno != EBUSY) { printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } if (fct_cnt != 0) { printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__); result = 1; } result |= mqsend (q); result |= mqrecv (q); result |= mqrecv (q); if (fct_cnt != 1) { printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__); result = 1; } else if (fct_guardsize != 0) { printf ("fct_guardsize %zd != 0\n", fct_guardsize); result = 1; } if (mq_notify (q, &ev) != 0) { printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } if (mq_notify (q, NULL) != 0) { printf ("mq_notify (q, NULL) failed with: %m\n"); result = 1; } memset (&ev, 0x11, sizeof (ev)); ev.sigev_notify = SIGEV_THREAD; ev.sigev_notify_function = fct; ev.sigev_notify_attributes = &nattr; ev.sigev_value.sival_ptr = &q; if (mq_notify (q, &ev) != 0) { printf ("mq_notify (q, { SIGEV_THREAD }) failed with: %m\n"); result = 1; } if (pthread_attr_setguardsize (&nattr, 0)) { puts ("pthread_attr_t setup failed"); result = 1; } if (mq_notify (q, &ev) == 0) { puts ("second mq_notify (q, { SIGEV_NONE }) unexpectedly succeeded"); result = 1; } else if (errno != EBUSY) { printf ("second mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } if (fct_cnt != 1) { printf ("fct called too early (%d on %d)\n", fct_cnt, __LINE__); result = 1; } result |= mqsend (q); result |= mqrecv (q); result |= mqrecv (q); if (fct_cnt != 2) { printf ("fct not called (%d on %d)\n", fct_cnt, __LINE__); result = 1; } else if (fct_guardsize != 32 * ps) { printf ("fct_guardsize %zd != %zd\n", fct_guardsize, 32 * ps); result = 1; } if (mq_notify (q, &ev) != 0) { printf ("third mq_notify (q, { SIGEV_NONE }) failed with: %m\n"); result = 1; } if (mq_notify (q, NULL) != 0) { printf ("mq_notify (q, NULL) failed with: %m\n"); result = 1; } if (pthread_attr_destroy (&nattr) != 0) { puts ("pthread_attr_destroy failed"); 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; } memset (&ev, 0x55, sizeof (ev)); ev.sigev_notify = SIGEV_THREAD; ev.sigev_notify_function = fct; ev.sigev_notify_attributes = NULL; ev.sigev_value.sival_int = 0; if (mq_notify (q, &ev) == 0) { puts ("mq_notify on closed mqd_t unexpectedly succeeded"); result = 1; } else if (errno != EBADF) { printf ("mq_notify on closed mqd_t did not fail with EBADF: %m\n"); result = 1; } if (fct_err) result = 1; return result; }