Skip to content

Commit

Permalink
mdns: die iff the MDNS advertising process dies
Browse files Browse the repository at this point in the history
and not if any other child does!
  • Loading branch information
abrasive committed Jun 3, 2013
1 parent d52125e commit c5b1f97
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion mdns.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <unistd.h>
#include "common.h"

static int mdns_pid = 0;
int mdns_pid = 0;

void mdns_unregister(void) {
if (mdns_pid)
Expand Down
2 changes: 2 additions & 0 deletions mdns.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef _MDNS_H
#define _MDNS_H

extern int mdns_pid;

void mdns_unregister(void);
void mdns_register(void);

Expand Down
9 changes: 8 additions & 1 deletion rtsp.c
Original file line number Diff line number Diff line change
Expand Up @@ -791,7 +791,14 @@ void rtsp_listen_loop(void) {
printf("Listening for connections.\n");

int acceptfd;
while (select(maxfd+1, &fds, 0, 0, 0) >= 0) {
while (1) {
ret = select(maxfd+1, &fds, 0, 0, 0);
if (ret<0) {
if (errno==EINTR)
continue;
break;
}

for (i=0; i<nsock; i++) {
if (FD_ISSET(sockfd[i], &fds)) {
acceptfd = sockfd[i];
Expand Down
13 changes: 13 additions & 0 deletions shairport.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <unistd.h>
#include <memory.h>
#include <openssl/md5.h>
#include <sys/wait.h>
#include "common.h"
#include "rtsp.h"
#include "mdns.h"
Expand All @@ -50,6 +51,15 @@ static void sig_shutdown(int foo, siginfo_t *bar, void *baz) {
shairport_shutdown();
}

static void sig_child(int foo, siginfo_t *bar, void *baz) {
pid_t pid;
while ((pid = waitpid((pid_t)-1, 0, WNOHANG)) > 0) {
if (pid == mdns_pid && !shutting_down) {
die("MDNS child process died unexpectedly!");
}
}
}

void usage(char *progname) {
printf("Usage: %s [options...] [-- [output options...]]\n\n", progname);
printf("Available options:\n"
Expand Down Expand Up @@ -128,6 +138,9 @@ void signal_setup(void) {
sa.sa_sigaction = &sig_shutdown;
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);

sa.sa_flags = SA_SIGINFO;
sa.sa_sigaction = &sig_child;
sigaction(SIGCHLD, &sa, NULL);
}

Expand Down

0 comments on commit c5b1f97

Please sign in to comment.