Subversion Repositories public

Rev

Rev 47 | Rev 55 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 47 Rev 51
1
1
2
/// inotify cron daemon user tables implementation
2
/// inotify cron daemon user tables implementation
3
/**
3
/**
4
 * \file usertable.cpp
4
 * \file usertable.cpp
5
 *
5
 *
6
 * inotify cron system
6
 * inotify cron system
7
 *
7
 *
8
 * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz>
8
 * Copyright (C) 2006 Lukas Jelinek, <lukas@aiken.cz>
9
 *
9
 *
10
 * This program is free software; you can use it, redistribute
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
11
 * it and/or modify it under the terms of the GNU General Public
12
 * License, version 2 (see LICENSE-GPL).
12
 * License, version 2 (see LICENSE-GPL).
13
 *  
13
 *  
14
 */
14
 */
15
15
16
16
17
#include <pwd.h>
17
#include <pwd.h>
18
#include <syslog.h>
18
#include <syslog.h>
19
#include <errno.h>
19
#include <errno.h>
20
#include <sys/wait.h>
20
#include <sys/wait.h>
21
21
22
#include "usertable.h"
22
#include "usertable.h"
23
23
24
24
25
PROC_LIST UserTable::s_procList;
25
PROC_LIST UserTable::s_procList;
26
26
27
27
28
void on_proc_done(InotifyWatch* pW)
28
void on_proc_done(InotifyWatch* pW)
29
{
29
{
30
  pW->SetEnabled(true);
30
  pW->SetEnabled(true);
31
}
31
}
32
32
33
33
34
EventDispatcher::EventDispatcher(Inotify* pIn)
34
EventDispatcher::EventDispatcher(Inotify* pIn)
35
{
35
{
36
 m_pIn = pIn;
36
 m_pIn = pIn;
37
}
37
}
38
38
39
void EventDispatcher::DispatchEvent(InotifyEvent& rEvt)
39
void EventDispatcher::DispatchEvent(InotifyEvent& rEvt)
40
{
40
{
41
  if (m_pIn == NULL)
41
  if (m_pIn == NULL)
42
    return;
42
    return;
43
   
43
   
44
  InotifyWatch* pW = rEvt.GetWatch();
44
  InotifyWatch* pW = rEvt.GetWatch();
45
  if (pW == NULL)
45
  if (pW == NULL)
46
    return;
46
    return;
47
   
47
   
48
  UserTable* pT = FindTable(pW);
48
  UserTable* pT = FindTable(pW);
49
  if (pT == NULL)
49
  if (pT == NULL)
50
    return;
50
    return;
51
   
51
   
52
  pT->OnEvent(rEvt);
52
  pT->OnEvent(rEvt);
53
}
53
}
54
 
54
 
55
void EventDispatcher::Register(InotifyWatch* pWatch, UserTable* pTab)
55
void EventDispatcher::Register(InotifyWatch* pWatch, UserTable* pTab)
56
{
56
{
57
  if (pWatch != NULL && pTab != NULL)
57
  if (pWatch != NULL && pTab != NULL)
58
    m_maps.insert(IWUT_MAP::value_type(pWatch, pTab));
58
    m_maps.insert(IWUT_MAP::value_type(pWatch, pTab));
59
}
59
}
60
 
60
 
61
void EventDispatcher::Unregister(InotifyWatch* pWatch)
61
void EventDispatcher::Unregister(InotifyWatch* pWatch)
62
{
62
{
63
  IWUT_MAP::iterator it = m_maps.find(pWatch);
63
  IWUT_MAP::iterator it = m_maps.find(pWatch);
64
  if (it == m_maps.end())
64
  if (it == m_maps.end())
65
    return;
65
    return;
66
   
66
   
67
  m_maps.erase(it);
67
  m_maps.erase(it);
68
}
68
}
69
 
69
 
70
void EventDispatcher::UnregisterAll(UserTable* pTab)
70
void EventDispatcher::UnregisterAll(UserTable* pTab)
71
{
71
{
72
  IWUT_MAP::iterator it = m_maps.begin();
72
  IWUT_MAP::iterator it = m_maps.begin();
73
  while (it != m_maps.end()) {
73
  while (it != m_maps.end()) {
74
    if ((*it).second == pTab) {
74
    if ((*it).second == pTab) {
75
      IWUT_MAP::iterator it2 = it;
75
      IWUT_MAP::iterator it2 = it;
76
      it++;
76
      it++;
77
      m_maps.erase(it2);
77
      m_maps.erase(it2);
78
    }
78
    }
79
    else {
79
    else {
80
      it++;
80
      it++;
81
    }
81
    }
82
  }
82
  }
83
}
83
}
84
84
85
UserTable* EventDispatcher::FindTable(InotifyWatch* pW)
85
UserTable* EventDispatcher::FindTable(InotifyWatch* pW)
86
{
86
{
87
  IWUT_MAP::iterator it = m_maps.find(pW);
87
  IWUT_MAP::iterator it = m_maps.find(pW);
88
  if (it == m_maps.end())
88
  if (it == m_maps.end())
89
    return NULL;
89
    return NULL;
90
   
90
   
91
  return (*it).second;
91
  return (*it).second;
92
}
92
}
93
93
94
94
95
95
96
96
97
UserTable::UserTable(Inotify* pIn, EventDispatcher* pEd, const std::string& rUser)
97
UserTable::UserTable(Inotify* pIn, EventDispatcher* pEd, const std::string& rUser)
98
: m_user(rUser)
98
: m_user(rUser)
99
{
99
{
100
  m_pIn = pIn;
100
  m_pIn = pIn;
101
  m_pEd = pEd;
101
  m_pEd = pEd;
102
}
102
}
103
103
104
UserTable::~UserTable()
104
UserTable::~UserTable()
105
{
105
{
106
  Dispose();
106
  Dispose();
107
}
107
}
108
 
108
 
109
void UserTable::Load()
109
void UserTable::Load()
110
{
110
{
111
  m_tab.Load(InCronTab::GetUserTablePath(m_user));
111
  m_tab.Load(InCronTab::GetUserTablePath(m_user));
112
 
112
 
113
  int cnt = m_tab.GetCount();
113
  int cnt = m_tab.GetCount();
114
  for (int i=0; i<cnt; i++) {
114
  for (int i=0; i<cnt; i++) {
115
    InCronTabEntry& rE = m_tab.GetEntry(i);
115
    InCronTabEntry& rE = m_tab.GetEntry(i);
116
    InotifyWatch* pW = new InotifyWatch(rE.GetPath(), rE.GetMask());
116
    InotifyWatch* pW = new InotifyWatch(rE.GetPath(), rE.GetMask());
117
    try {
117
    try {
118
      m_pIn->Add(pW);
118
      m_pIn->Add(pW);
119
      m_pEd->Register(pW, this);
119
      m_pEd->Register(pW, this);
120
      m_map.insert(IWCE_MAP::value_type(pW, &rE));
120
      m_map.insert(IWCE_MAP::value_type(pW, &rE));
121
    } catch (InotifyException e) {
121
    } catch (InotifyException e) {
122
      syslog(LOG_ERR, "cannot create watch for user %s", m_user.c_str());
122
      syslog(LOG_ERR, "cannot create watch for user %s", m_user.c_str());
123
      delete pW;
123
      delete pW;
124
    }
124
    }
125
  }
125
  }
126
}
126
}
127
127
128
void UserTable::Dispose()
128
void UserTable::Dispose()
129
{
129
{
130
  IWCE_MAP::iterator it = m_map.begin();
130
  IWCE_MAP::iterator it = m_map.begin();
131
  while (it != m_map.end()) {
131
  while (it != m_map.end()) {
132
    InotifyWatch* pW = (*it).first;
132
    InotifyWatch* pW = (*it).first;
133
    m_pEd->Unregister(pW);
133
    m_pEd->Unregister(pW);
134
    m_pIn->Remove(pW);
134
    m_pIn->Remove(pW);
135
    delete pW;
135
    delete pW;
136
    it++;
136
    it++;
137
  }
137
  }
138
 
138
 
139
  m_map.clear();
139
  m_map.clear();
140
}
140
}
141
 
141
 
142
void UserTable::OnEvent(InotifyEvent& rEvt)
142
void UserTable::OnEvent(InotifyEvent& rEvt)
143
{
143
{
144
  InotifyWatch* pW = rEvt.GetWatch();
144
  InotifyWatch* pW = rEvt.GetWatch();
145
  InCronTabEntry* pE = FindEntry(pW);
145
  InCronTabEntry* pE = FindEntry(pW);
146
 
146
 
147
  if (pE == NULL)
147
  if (pE == NULL)
148
    return;
148
    return;
149
 
149
 
150
  std::string cmd;
150
  std::string cmd;
151
  const std::string& cs = pE->GetCmd();
151
  const std::string& cs = pE->GetCmd();
152
  size_t pos = 0;
152
  size_t pos = 0;
153
  size_t oldpos = 0;
153
  size_t oldpos = 0;
154
  size_t len = cs.length();
154
  size_t len = cs.length();
155
  while ((pos = cs.find('$', oldpos)) != std::string::npos) {
155
  while ((pos = cs.find('$', oldpos)) != std::string::npos) {
156
    if (pos < len - 1) {
156
    if (pos < len - 1) {
157
      size_t px = pos + 1;
157
      size_t px = pos + 1;
158
      if (cs[px] == '$') {
158
      if (cs[px] == '$') {
159
        cmd.append(cs.substr(oldpos, pos-oldpos+1));
159
        cmd.append(cs.substr(oldpos, pos-oldpos+1));
160
        oldpos = pos + 2;
160
        oldpos = pos + 2;
161
      }
161
      }
162
      else if (cs[px] == '@') {
162
      else if (cs[px] == '@') {
163
        cmd.append(cs.substr(oldpos, pos-oldpos));
163
        cmd.append(cs.substr(oldpos, pos-oldpos));
164
        cmd.append(pW->GetPath());
164
        cmd.append(pW->GetPath());
165
        oldpos = pos + 2;
165
        oldpos = pos + 2;
166
      }
166
      }
167
      else if (cs[px] == '#') {
167
      else if (cs[px] == '#') {
168
        cmd.append(cs.substr(oldpos, pos-oldpos));
168
        cmd.append(cs.substr(oldpos, pos-oldpos));
169
        cmd.append(rEvt.GetName());
169
        cmd.append(rEvt.GetName());
170
        oldpos = pos + 2;
170
        oldpos = pos + 2;
171
      }
171
      }
172
      else {
172
      else {
173
        cmd.append(cs.substr(oldpos, pos-oldpos));
173
        cmd.append(cs.substr(oldpos, pos-oldpos));
174
        oldpos = pos + 1;
174
        oldpos = pos + 1;
175
      }
175
      }
176
    }
176
    }
177
    else {
177
    else {
178
      cmd.append(cs.substr(oldpos, pos-oldpos));
178
      cmd.append(cs.substr(oldpos, pos-oldpos));
179
      oldpos = pos + 1;
179
      oldpos = pos + 1;
180
    }
180
    }
181
  }    
181
  }    
182
  cmd.append(cs.substr(oldpos));
182
  cmd.append(cs.substr(oldpos));
183
 
183
 
184
  int argc;
184
  int argc;
185
  char** argv;
185
  char** argv;
186
  if (!PrepareArgs(cmd, argc, argv)) {
186
  if (!PrepareArgs(cmd, argc, argv)) {
187
    syslog(LOG_ERR, "cannot prepare command arguments");
187
    syslog(LOG_ERR, "cannot prepare command arguments");
188
    return;
188
    return;
189
  }
189
  }
190
 
190
 
191
  syslog(LOG_INFO, "(%s) CMD (%s)", m_user.c_str(), cmd.c_str());
191
  syslog(LOG_INFO, "(%s) CMD (%s)", m_user.c_str(), cmd.c_str());
192
 
192
 
193
  if (pE->IsNoLoop())
193
  if (pE->IsNoLoop())
194
    pW->SetEnabled(false);
194
    pW->SetEnabled(false);
195
 
195
 
196
  ProcData_t pd;
196
  ProcData_t pd;
197
  pd.pid = fork();
197
  pd.pid = fork();
198
  if (pd.pid == 0) {
198
  if (pd.pid == 0) {
199
   
199
   
200
    struct passwd* pwd = getpwnam(m_user.c_str());
200
    struct passwd* pwd = getpwnam(m_user.c_str());
201
    if (pwd == NULL)
201
    if (    pwd == NULL                 // user not found
202
      _exit(1);
-
 
203
     
-
 
204
    if (setuid(pwd->pw_uid) != 0)
202
        ||  setuid(pwd->pw_uid) != 0    // setting UID failed
205
      _exit(1);
-
 
206
   
-
 
207
    if (execvp(argv[0], argv) != 0) {
203
        ||  execvp(argv[0], argv) != 0) // exec failed
-
 
204
    {
208
      _exit(1);
205
      _exit(1);
209
    }
206
    }
210
  }
207
  }
211
  else if (pd.pid > 0) {
208
  else if (pd.pid > 0) {
212
    if (pE->IsNoLoop()) {
209
    if (pE->IsNoLoop()) {
213
      pd.onDone = on_proc_done;
210
      pd.onDone = on_proc_done;
214
      pd.pWatch = pW;
211
      pd.pWatch = pW;
215
    }
212
    }
216
    else {
213
    else {
217
      pd.onDone = NULL;
214
      pd.onDone = NULL;
218
      pd.pWatch = NULL;
215
      pd.pWatch = NULL;
219
    }
216
    }
220
   
217
   
221
    s_procList.push_back(pd);
218
    s_procList.push_back(pd);
222
  }
219
  }
223
  else {
220
  else {
224
    if (pE->IsNoLoop())
221
    if (pE->IsNoLoop())
225
      pW->SetEnabled(true);
222
      pW->SetEnabled(true);
226
     
223
     
227
    syslog(LOG_ERR, "cannot fork process: %s", strerror(errno));
224
    syslog(LOG_ERR, "cannot fork process: %s", strerror(errno));
228
  }
225
  }
-
 
226
 
-
 
227
  CleanupArgs(argc, argv);
229
}
228
}
230
229
231
InCronTabEntry* UserTable::FindEntry(InotifyWatch* pWatch)
230
InCronTabEntry* UserTable::FindEntry(InotifyWatch* pWatch)
232
{
231
{
233
  IWCE_MAP::iterator it = m_map.find(pWatch);
232
  IWCE_MAP::iterator it = m_map.find(pWatch);
234
  if (it == m_map.end())
233
  if (it == m_map.end())
235
    return NULL;
234
    return NULL;
236
   
235
   
237
  return (*it).second;
236
  return (*it).second;
238
}
237
}
239
238
240
bool UserTable::PrepareArgs(const std::string& rCmd, int& argc, char**& argv)
239
bool UserTable::PrepareArgs(const std::string& rCmd, int& argc, char**& argv)
241
{
240
{
242
  if (rCmd.empty())
241
  if (rCmd.empty())
243
    return false;
242
    return false;
244
   
243
   
245
  StringTokenizer tok(rCmd, ' ');
244
  StringTokenizer tok(rCmd, ' ');
246
  std::deque<std::string> args;
245
  std::deque<std::string> args;
247
  while (tok.HasMoreTokens()) {
246
  while (tok.HasMoreTokens()) {
248
    args.push_back(tok.GetNextToken());
247
    args.push_back(tok.GetNextToken());
249
  }
248
  }
250
 
249
 
251
  if (args.empty())
250
  if (args.empty())
252
    return false;
251
    return false;
253
 
252
 
254
  argc = (int) args.size();
253
  argc = (int) args.size();
255
  argv = new char*[argc+1];
254
  argv = new char*[argc+1];
256
  argv[argc] = NULL;
255
  argv[argc] = NULL;
257
 
256
 
258
  for (int i=0; i<argc; i++) {
257
  for (int i=0; i<argc; i++) {
259
    const std::string& s = args[i];
258
    const std::string& s = args[i];
260
    size_t len = s.length();
259
    size_t len = s.length();
261
    argv[i] = new char[len+1];
260
    argv[i] = new char[len+1];
262
    strcpy(argv[i], s.c_str());
261
    strcpy(argv[i], s.c_str());
263
  }
262
  }
264
 
263
 
265
  return true;
264
  return true;
266
}
265
}
-
 
266
-
 
267
void UserTable::CleanupArgs(int argc, char** argv)
-
 
268
{
-
 
269
  for (int i=0; i<argc; i++) {
-
 
270
    delete[] argv[i];
-
 
271
  }
-
 
272
 
-
 
273
  delete[] argv;
-
 
274
}
267
275
268
void UserTable::FinishDone()
276
void UserTable::FinishDone()
269
{
277
{
270
  PROC_LIST::iterator it = s_procList.begin();
278
  PROC_LIST::iterator it = s_procList.begin();
271
  while (it != s_procList.end()) {
279
  while (it != s_procList.end()) {
272
    ProcData_t& pd = *it;
280
    ProcData_t& pd = *it;
273
    int status = 0;
281
    int status = 0;
274
    int res = waitpid(pd.pid, &status, WNOHANG);
282
    int res = waitpid(pd.pid, &status, WNOHANG);
275
    if (res == pd.pid && (WIFEXITED(status) || WIFSIGNALED(status))) {
283
    if (res == pd.pid && (WIFEXITED(status) || WIFSIGNALED(status))) {
276
      if (pd.onDone != NULL)
284
      if (pd.onDone != NULL)
277
        (*pd.onDone)(pd.pWatch);
285
        (*pd.onDone)(pd.pWatch);
278
      it = s_procList.erase(it);
286
      it = s_procList.erase(it);
279
    }
287
    }
280
    else {
288
    else {
281
      it++;
289
      it++;
282
    }
290
    }
283
  }  
291
  }  
284
}
292
}
285
293
286
294
287
 
295