Subversion Repositories public

Rev

Rev 47 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
45 luk 1
 
2
/// inotify cron daemon main file
3
/**
4
 * \file icd-main.cpp
5
 *
6
 * inotify cron system
7
 *
8
 * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz>
9
 *
10
 * This program is free software; you can use it, redistribute
11
 * it and/or modify it under the terms of the GNU General Public
12
 * License, version 2 (see LICENSE-GPL).
13
 *  
14
 */
15
 
16
#include <map>
17
#include <signal.h>
18
#include <wait.h>
19
#include <pwd.h>
20
#include <dirent.h>
21
#include <syslog.h>
22
#include <errno.h>
23
 
24
#include "inotify-cxx.h"
25
#include "incrontab.h"
26
 
27
#include "usertable.h"
28
 
29
#define DAEMON false
30
 
31
#define INCRON_APP_NAME "incrond"
32
#define INCRON_LOG_OPTS (LOG_CONS | LOG_PID)
33
#define INCRON_LOG_FACIL LOG_CRON
34
 
35
 
36
typedef std::map<std::string, UserTable*> SUT_MAP;
37
 
38
 
39
SUT_MAP g_ut;
40
 
41
bool g_fFinish = false;
42
 
43
 
44
void on_signal(int signo)
45
{
46
  g_fFinish = true;
47
}
48
 
49
void on_child(int signo)
50
{
51
  wait(NULL);
52
}
53
 
54
bool check_user(const char* user)
55
{
56
  struct passwd* pw = getpwnam(user);
57
  if (pw == NULL)
58
    return false;
59
 
60
  return InCronTab::CheckUser(user);
61
}
62
 
63
bool load_tables(Inotify* pIn, EventDispatcher* pEd)
64
{
65
  DIR* d = opendir(INCRON_TABLE_BASE);
66
  if (d == NULL) {
67
    syslog(LOG_CRIT, "cannot open table directory: %s", strerror(errno));
68
    return false;
69
  }
70
 
71
  syslog(LOG_NOTICE, "loading user tables");
72
 
73
  struct dirent* pDe = NULL;
74
  while ((pDe = readdir(d)) != NULL) {
75
    std::string un(pDe->d_name);
76
    if (pDe->d_type == DT_REG && un != "." && un != "..") {
77
      if (check_user(pDe->d_name)) {
78
        syslog(LOG_INFO, "loading table for user %s", pDe->d_name);
79
        UserTable* pUt = new UserTable(pIn, pEd, un);
80
        g_ut.insert(SUT_MAP::value_type(un, pUt));
81
        pUt->Load();
82
      }
83
      else {
84
        syslog(LOG_WARNING, "table for invalid user %s found (ignored)", pDe->d_name);
85
      }
86
    }
87
  }
88
 
89
  closedir(d);
90
  return true;
91
}
92
 
93
int main(int argc, char** argv)
94
{
95
  openlog(INCRON_APP_NAME, INCRON_LOG_OPTS, INCRON_LOG_FACIL);
96
 
97
  syslog(LOG_NOTICE, "starting service");
98
 
99
  Inotify in;
100
  EventDispatcher ed(&in);
101
 
102
  if (!load_tables(&in, &ed)) {
103
    closelog();
104
    return 1;
105
  }
106
 
107
  signal(SIGTERM, on_signal);
108
  signal(SIGINT, on_signal);
109
 
110
  signal(SIGCHLD, on_child);
111
 
112
  if (DAEMON)
113
    daemon(0, 0);
114
 
115
  uint32_t wm = IN_CLOSE_WRITE | IN_DELETE | IN_MOVE | IN_DELETE_SELF | IN_UNMOUNT;
116
  InotifyWatch watch(INCRON_TABLE_BASE, wm);
117
  in.Add(watch);
118
 
119
  syslog(LOG_NOTICE, "ready to process filesystem events");
120
 
121
  InotifyEvent e;
122
 
123
  while (!g_fFinish) {
124
    if (in.WaitForEvents()) {
125
      while (in.GetEvent(e)) {
126
 
127
        std::string s;
128
        e.DumpTypes(s);
129
        //syslog(LOG_DEBUG, "EVENT: wd=%i, cookie=%u, name=%s, mask: %s", (int) e.GetDescriptor(), (unsigned) e.GetCookie(), e.GetName().c_str(), s.c_str());
130
 
131
        if (e.GetWatch() == &watch) {
132
          if (e.IsType(IN_DELETE_SELF) || e.IsType(IN_UNMOUNT)) {
133
            syslog(LOG_CRIT, "base directory destroyed, exitting");
134
            g_fFinish = true;
135
          }
136
          else if (!e.GetName().empty()) {
137
            SUT_MAP::iterator it = g_ut.find(e.GetName());
138
            if (it != g_ut.end()) {
139
              UserTable* pUt = (*it).second;
140
              if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) {
141
                syslog(LOG_INFO, "table for user %s changed, reloading", e.GetName().c_str());
142
                pUt->Dispose();
143
                pUt->Load();
144
              }
145
              else if (e.IsType(IN_MOVED_FROM) || e.IsType(IN_DELETE)) {
146
                syslog(LOG_INFO, "table for user %s destroyed, removing", e.GetName().c_str());
147
                delete pUt;
148
                g_ut.erase(it);
149
              }
150
            }
151
            else if (e.IsType(IN_CLOSE_WRITE) || e.IsType(IN_MOVED_TO)) {
152
              if (check_user(e.GetName().c_str())) {
153
                syslog(LOG_INFO, "table for user %s created, loading", e.GetName().c_str());
154
                UserTable* pUt = new UserTable(&in, &ed, e.GetName());
155
                g_ut.insert(SUT_MAP::value_type(e.GetName(), pUt));
156
                pUt->Load();
157
              }
158
            }
159
          }
160
        }
161
        else {
162
          ed.DispatchEvent(e);
163
        }
164
      }
165
    }
166
  }
167
 
168
  syslog(LOG_NOTICE, "stopping service");
169
 
170
  closelog();
171
 
172
  return 0;
173
}