* bus/dir-watch-default.c, bus/dir-watch-dnotify.c,
[platform/upstream/dbus.git] / bus / dir-watch-dnotify.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dir-watch-dnotify.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 #define _GNU_SOURCE
27 #include <stdlib.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30
31 #include <dbus/dbus-internals.h>
32 #include "dir-watch.h"
33
34 #define MAX_DIRS_TO_WATCH 128
35
36 /* use a static array to avoid handling OOM */
37 static int fds[MAX_DIRS_TO_WATCH];
38 static int num_fds = 0;
39
40 void
41 bus_watch_directory (const char *dir, BusContext *context)
42 {
43   int fd;
44
45   _dbus_assert (dir != NULL);
46
47   if (num_fds >= MAX_DIRS_TO_WATCH )
48     {
49       _dbus_warn ("Cannot watch config directory '%s'. Already watching %d directories\n", dir, MAX_DIRS_TO_WATCH);
50       goto out;
51     }
52
53   fd = open (dir, O_RDONLY);
54   if (fd < 0)
55     {
56       _dbus_warn ("Cannot open directory '%s'; error '%s'\n", dir, _dbus_strerror (errno));
57       goto out;
58     }
59
60   if (fcntl (fd, F_NOTIFY, DN_DELETE|DN_RENAME|DN_MODIFY) == -1)
61     {
62       _dbus_warn ("Cannot setup D_NOTIFY for '%s' error '%s'\n", dir, _dbus_strerror (errno));
63       close (fd);
64       goto out;
65     }
66   
67   fds[num_fds++] = fd;
68   _dbus_verbose ("Added watch on config directory '%s'\n", dir);
69
70  out:
71   ;
72 }
73
74 void 
75 bus_drop_all_directory_watches (void)
76 {
77   int i;
78  
79   _dbus_verbose ("Dropping all watches on config directories\n");
80  
81   for (i = 0; i < num_fds; i++)
82     {
83       if (close (fds[i]) != 0)
84         {
85           _dbus_verbose ("Error closing fd %d for config directory watch\n", fds[i]);
86         }
87     }
88   
89   num_fds = 0;
90 }