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