4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
34 #include <avahi-common/gccmacro.h>
37 static AvahiSimplePoll *simple_poll = NULL;
38 static struct sigaction old_sigint_sa, old_sigterm_sa;
39 static int pipe_fds[2] = { -1, -1 };
40 static AvahiWatch *watch = NULL;
42 static int set_nonblock(int fd) {
47 if ((n = fcntl(fd, F_GETFL)) < 0)
53 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
56 static void handler(int s) {
57 write(pipe_fds[1], &s, sizeof(s));
60 static void close_pipe_fds(void) {
66 pipe_fds[0] = pipe_fds[1] = -1;
69 static void watch_callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
74 assert(fd == pipe_fds[0]);
75 assert(event == AVAHI_WATCH_IN);
77 l = read(fd, &s, sizeof(s));
78 assert(l == sizeof(s));
80 fprintf(stderr, "Got %s, quitting.\n", s == SIGINT ? "SIGINT" : "SIGTERM");
81 avahi_simple_poll_quit(simple_poll);
84 int sigint_install(AvahiSimplePoll *spoll) {
90 assert(pipe_fds[0] == -1 && pipe_fds[1] == -1);
92 if (pipe(pipe_fds) < 0) {
93 fprintf(stderr, "pipe() failed: %s\n", strerror(errno));
97 set_nonblock(pipe_fds[0]);
98 set_nonblock(pipe_fds[1]);
100 memset(&sa, 0, sizeof(sa));
101 sa.sa_handler = handler;
102 sa.sa_flags = SA_RESTART;
104 if (sigaction(SIGINT, &sa, &old_sigint_sa) < 0) {
105 fprintf(stderr, "sigaction() failed: %s\n", strerror(errno));
110 if (sigaction(SIGTERM, &sa, &old_sigterm_sa) < 0) {
111 sigaction(SIGINT, &old_sigint_sa, NULL);
112 fprintf(stderr, "sigaction() failed: %s\n", strerror(errno));
117 p = avahi_simple_poll_get(spoll);
118 watch = p->watch_new(p, pipe_fds[0], AVAHI_WATCH_IN, watch_callback, NULL);
125 void sigint_uninstall(void) {
130 sigaction(SIGTERM, &old_sigterm_sa, NULL);
131 sigaction(SIGINT, &old_sigint_sa, NULL);
139 p = avahi_simple_poll_get(simple_poll);
141 p->watch_free(watch);