• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dir-watch-kqueue.c  OS specific directory change notification for message bus
3  *
4  * Copyright (C) 2003 Red Hat, Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23 
24 #include <config.h>
25 
26 #include <sys/types.h>
27 #include <sys/event.h>
28 #include <sys/time.h>
29 #include <signal.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include "bus.h"
33 #include <dbus/dbus-watch.h>
34 
35 #include <dbus/dbus-internals.h>
36 #include "dir-watch.h"
37 
38 #define MAX_DIRS_TO_WATCH 128
39 
40 static int kq = -1;
41 static int fds[MAX_DIRS_TO_WATCH];
42 static int num_fds = 0;
43 static DBusWatch *watch = NULL;
44 static DBusLoop *loop = NULL;
45 
46 static dbus_bool_t
_kqueue_watch_callback(DBusWatch * watch,unsigned int condition,void * data)47 _kqueue_watch_callback (DBusWatch *watch, unsigned int condition, void *data)
48 {
49   return dbus_watch_handle (watch, condition);
50 }
51 
52 static dbus_bool_t
_handle_kqueue_watch(DBusWatch * watch,unsigned int flags,void * data)53 _handle_kqueue_watch (DBusWatch *watch, unsigned int flags, void *data)
54 {
55   struct kevent ev;
56   struct timespec nullts = { 0, 0 };
57   int res;
58   pid_t pid;
59 
60   res = kevent (kq, NULL, 0, &ev, 1, &nullts);
61 
62   /* Sleep for half a second to avoid a race when files are install(1)'d
63    * to system.d. */
64   usleep(500000);
65 
66   if (res > 0)
67     {
68       pid = getpid ();
69       _dbus_verbose ("Sending SIGHUP signal on reception of a kevent\n");
70       (void) kill (pid, SIGHUP);
71     }
72   else if (res < 0 && errno == EBADF)
73     {
74       kq = -1;
75       if (watch != NULL)
76 	{
77 	  _dbus_loop_remove_watch (loop, watch, _kqueue_watch_callback, NULL);
78           _dbus_watch_unref (watch);
79 	  watch = NULL;
80 	}
81       pid = getpid ();
82       _dbus_verbose ("Sending SIGHUP signal since kqueue has been closed\n");
83       (void) kill (pid, SIGHUP);
84     }
85 
86   return TRUE;
87 }
88 
89 void
bus_watch_directory(const char * dir,BusContext * context)90 bus_watch_directory (const char *dir, BusContext *context)
91 {
92   int fd;
93   struct kevent ev;
94 
95   _dbus_assert (dir != NULL);
96 
97   if (kq < 0)
98     {
99 
100       kq = kqueue ();
101       if (kq < 0)
102         {
103           _dbus_warn ("Cannot create kqueue; error '%s'\n", _dbus_strerror (errno));
104 	  goto out;
105 	}
106 
107         loop = bus_context_get_loop (context);
108 
109         watch = _dbus_watch_new (kq, DBUS_WATCH_READABLE, TRUE,
110                                  _handle_kqueue_watch, NULL, NULL);
111 
112 	if (watch == NULL)
113           {
114             _dbus_warn ("Unable to create kqueue watch\n");
115 	    close (kq);
116 	    kq = -1;
117 	    goto out;
118 	  }
119 
120 	if (!_dbus_loop_add_watch (loop, watch, _kqueue_watch_callback,
121                                    NULL, NULL))
122           {
123             _dbus_warn ("Unable to add reload watch to main loop");
124 	    close (kq);
125 	    kq = -1;
126 	    _dbus_watch_unref (watch);
127 	    watch = NULL;
128             goto out;
129 	  }
130     }
131 
132   if (num_fds >= MAX_DIRS_TO_WATCH )
133     {
134       _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH);
135       goto out;
136     }
137 
138   fd = open (dir, O_RDONLY);
139   if (fd < 0)
140     {
141       _dbus_warn ("Cannot open directory '%s'; error '%s'\n", dir, _dbus_strerror (errno));
142       goto out;
143     }
144 
145   EV_SET (&ev, fd, EVFILT_VNODE, EV_ADD | EV_ENABLE | EV_CLEAR,
146           NOTE_DELETE | NOTE_EXTEND | NOTE_WRITE | NOTE_RENAME, 0, 0);
147   if (kevent (kq, &ev, 1, NULL, 0, NULL) == -1)
148     {
149       _dbus_warn ("Cannot setup a kevent for '%s'; error '%s'\n", dir, _dbus_strerror (errno));
150       close (fd);
151       goto out;
152     }
153 
154   fds[num_fds++] = fd;
155   _dbus_verbose ("Added kqueue watch on config directory '%s'\n", dir);
156 
157  out:
158   ;
159 }
160 
161 void
bus_drop_all_directory_watches(void)162 bus_drop_all_directory_watches (void)
163 {
164   int i;
165 
166   _dbus_verbose ("Dropping all watches on config directories\n");
167 
168   for (i = 0; i < num_fds; i++)
169     {
170       if (close (fds[i]) != 0)
171         {
172           _dbus_verbose ("Error closing fd %d for config directory watch\n", fds[i]);
173 	}
174     }
175 
176   num_fds = 0;
177 }
178