File: job.c

package info (click to toggle)
bcron 0.10-3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 504 kB
  • ctags: 353
  • sloc: ansic: 2,161; sh: 611; makefile: 108
file content (58 lines) | stat: -rw-r--r-- 1,433 bytes parent folder | download
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
#include <sysdeps.h>
#include <systime.h>
#include <ctype.h>
#include <string.h>

#include <msg/msg.h>
#include <msg/wrap.h>
#include <str/env.h>
#include <str/iter.h>
#include <str/str.h>

#include "bcron.h"

void job_free(struct job* job)
{
  if (job) {
    job_free(job->next);
    if (job->runas != 0)
      free((char*)job->runas);
    if (job->command != 0)
      free((char*)job->command);
    str_free(&job->environ);
    free(job);
  }
}

struct job* job_new(const struct job_timespec* times,
		    const char* runas,
		    const char* command,
		    const str* environ)
{
  struct job* this;
  if ((this = malloc(sizeof *this)) == 0)
    die_oom(111);
  memset(this, 0, sizeof *this);
  memcpy(&this->times, times, sizeof *times);
  if ((runas && (this->runas = strdup(runas)) == 0)
      || (this->command = strdup(command)) == 0
      || !str_copy(&this->environ, environ))
    die_oom(111);
  return this;
}

void job_exec(struct job* job)
{
  static str packet;
  static unsigned long id = 1;
  const char *runas = job->runas != 0 ? job->runas : "";
  debugf(DEBUG_JOBS, "{Running: (}s{) }s", runas, job->command);
  packet.len = 0;
  str_catu(&packet, id++);
  str_catc(&packet, 0);
  str_catb(&packet, runas, strlen(runas) + 1);
  str_catb(&packet, job->command, strlen(job->command) + 1);
  str_cat(&packet, &job->environ);
  if (sendpacket(1, &packet) == -1)
    die1sys(111, "Could not send job to bcron-exec");
}