2 This file is part of avahi.
4 avahi is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 avahi is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
32 #include <avahi-common/gccmacro.h>
35 static AvahiSimplePoll *simple_poll = NULL;
36 static struct sigaction old_sigint_sa, old_sigterm_sa;
37 static int pipe_fds[2] = { -1, -1 };
38 static AvahiWatch *watch = NULL;
40 static int set_nonblock(int fd) {
45 if ((n = fcntl(fd, F_GETFL)) < 0)
51 return fcntl(fd, F_SETFL, n|O_NONBLOCK);
54 static void handler(int s) {
55 write(pipe_fds[1], &s, sizeof(s));
58 static void close_pipe_fds(void) {
64 pipe_fds[0] = pipe_fds[1] = -1;
67 static void watch_callback(AvahiWatch *w, int fd, AvahiWatchEvent event, AVAHI_GCC_UNUSED void *userdata) {
72 assert(fd == pipe_fds[0]);
73 assert(event == AVAHI_WATCH_IN);
75 l = read(fd, &s, sizeof(s));
76 assert(l == sizeof(s));
78 fprintf(stderr, "Got %s, quitting.\n", s == SIGINT ? "SIGINT" : "SIGTERM");
79 avahi_simple_poll_quit(simple_poll);
82 int sigint_install(AvahiSimplePoll *spoll) {
88 assert(pipe_fds[0] == -1 && pipe_fds[1] == -1);
90 if (pipe(pipe_fds) < 0) {
91 fprintf(stderr, "pipe() failed: %s\n", strerror(errno));
95 set_nonblock(pipe_fds[0]);
96 set_nonblock(pipe_fds[1]);
98 memset(&sa, 0, sizeof(sa));
99 sa.sa_handler = handler;
100 sa.sa_flags = SA_RESTART;
102 if (sigaction(SIGINT, &sa, &old_sigint_sa) < 0) {
103 fprintf(stderr, "sigaction() failed: %s\n", strerror(errno));
108 if (sigaction(SIGTERM, &sa, &old_sigterm_sa) < 0) {
109 sigaction(SIGINT, &old_sigint_sa, NULL);
110 fprintf(stderr, "sigaction() failed: %s\n", strerror(errno));
115 p = avahi_simple_poll_get(spoll);
116 watch = p->watch_new(p, pipe_fds[0], AVAHI_WATCH_IN, watch_callback, NULL);
123 void sigint_uninstall(void) {
128 sigaction(SIGTERM, &old_sigterm_sa, NULL);
129 sigaction(SIGINT, &old_sigint_sa, NULL);
137 p = avahi_simple_poll_get(simple_poll);
139 p->watch_free(watch);