File: crontabs.c

package info (click to toggle)
bcron 0.09-13
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 496 kB
  • sloc: ansic: 2,055; sh: 419; makefile: 110
file content (79 lines) | stat: -rw-r--r-- 1,812 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <sysdeps.h>
#include <string.h>

#include <adt/ghash.h>
#include <msg/msg.h>
#include <msg/wrap.h>

#include "bcron.h"

static void crontabp_free(struct crontab* c)
{
  job_free(c->jobs);
}

GHASH_DEFN(crontabs,const char*,struct crontab,
	   adt_hashsp,adt_cmpsp,adt_copysp,0,adt_freesp,crontabp_free);

struct ghash crontabs;

static str path;

static void unload(const char* name)
{
  msg3("Unloading '", name, "'");
  crontabs_remove(&crontabs, &name);
}

static void reload(struct crontabs_entry* entry)
{
  const char* name = entry->key;
  msg3("Reloading '", name, "'");
  wrap_str(str_copy2s(&path, CRONTAB_DIR "/", name));
  if (!crontab_import(&entry->data, path.s, name[0] == ':' ? 0 : name))
    warn3("Reloading '", name, "' failed, using old table");
}

static void load(const char* name)
{
  struct crontab c;
  msg3("Loading '", name, "'");
  wrap_str(str_copy2s(&path, CRONTAB_DIR "/", name));
  memset(&c, 0, sizeof c);
  if (crontab_import(&c, path.s, name[0] == ':' ? 0 : name)) {
    if (!crontabs_add(&crontabs, &name, &c))
      die_oom(111);
  }
  else
    warn3("Loading '", name, "' failed");
}

void crontabs_load(void)
{
  DIR* dir;
  const direntry* de;
  struct crontabs_entry* entry;
  struct ghashiter iter;
  const char* name;
  struct ministat st;

  if ((dir = opendir(CRONTAB_DIR)) == 0)
    die1sys(111, "Could not open crontabs directory");
  while ((de = readdir(dir)) != 0) {
    name = de->d_name;
    if (name[0] != '.') {
      if ((entry = crontabs_get(&crontabs, &name)) == 0)
	load(name);
    }
  }
  closedir(dir);

  ghashiter_loop(&iter, &crontabs) {
    entry = iter.entry;
    ministat2(CRONTAB_DIR, entry->key, &st);
    if (!st.exists)
      unload(entry->key);
    else if (memcmp(&st, &entry->data.stat, sizeof st) != 0)
      reload(entry);
  }
}