summaryrefslogtreecommitdiff
path: root/daemonize.c
blob: 635ce2bf1a9f4c120cf33ebfe2467a31125fae4c (plain)
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <fcntl.h>

const struct option longopts[] = {
  {"chdir", no_argument, NULL, 'd'},
  {"noclose", no_argument, NULL, 'n'},
  {"log-stdout", required_argument, NULL, '1'},
  {"log-stderr", required_argument, NULL, '2'},
  {0, 0, 0, 0}
};
const char* optstring = "dn1:2:";

void q (char* m) {
    fprintf(stderr, "%s\n", m);
    exit(1);
}

void qusage () {
    q("[daemonize] Usage: daemonize [-d|--chdir] [-n|--noclose] [-1|--log-stdout /path/to/log] [-2|--log-stderr /path/to/log] <command>");
}

void qtoofewargs () {
    fprintf(stderr, "[daemonize] Too few arguments.\n");
    qusage();
}

void qinvalidarg (char* arg) {
    fprintf(stderr, "[daemonize] Unrecognized argument: %s\n", arg);
    qusage();
}

char* lowerdup (char* a) {
    char* b = strdup(a);
    int i;
    for (i = 0; b[i] != 0; i++) {
        b[i] = tolower(b[i]);
    }
    return b;
}

struct opts {
    int nochdir;
    int noclose;
    FILE* stdlog;
    FILE* errlog;
};

void parseargs(int argc, char** argv, struct opts* savedopts) {
    if (argc < 2) qtoofewargs();
    int c;

    while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
        switch (c) {
            case 'd':
                savedopts->nochdir = false;
                break;
            case 'n':
                savedopts->noclose = true;
                break;
            case '1':
                savedopts->stdlog = fopen(optarg, "a");
                if (!savedopts->stdlog) q("[daemonize] Unable to open stdout log, quitting.");
                break;
            case '2':
                savedopts->errlog = fopen(optarg, "a");
                if (!savedopts->errlog) q("[daemonize] Unable to open stderr log, quitting.");
                break;
            default:
                // getopt will have printed an error already
                qusage();
    }
  }
}

int main (int argc, char** argv) {
    struct opts options = {true, false, NULL, NULL};
    parseargs(argc, argv, &options);

    int cmdlen = 0, buff_size = 256;
    char* buffer = malloc(buff_size);

    while (optind < argc) {
        int arglen = strlen(argv[optind]);
        while (cmdlen+arglen+1 > buff_size) {
            buff_size *= 2;
            buffer = realloc(buffer, buff_size);
            if (buffer == NULL) q("[daemonize] Out of memory parsing arguments.");
        }
        strncpy(&buffer[cmdlen], argv[optind++], arglen);
        cmdlen += arglen+1;
        buffer[cmdlen-1] = ' ';
    }
    buffer[cmdlen-1] = 0;

    if (cmdlen < 1) qtoofewargs();

    // Set up logs as appropriate
    int logs = options.stdlog || options.errlog;
    if (options.stdlog) {
        dup2(fileno(options.stdlog), STDOUT_FILENO);
        fclose(options.stdlog);
    }
    if (options.errlog) {
        dup2(fileno(options.errlog), STDERR_FILENO);
        fclose(options.errlog);
    }

    if (logs && !options.noclose) {
        int devnull_out = open("/dev/null", O_WRONLY);
        int devnull_in = open("/dev/null", O_RDONLY);
        if (!options.errlog)
            dup2(devnull_out, STDERR_FILENO);
        if (!options.stdlog)
            dup2(devnull_out, STDOUT_FILENO);
        dup2(devnull_in, STDIN_FILENO);
        close(devnull_in);
        close(devnull_out);
    }

    daemon(options.nochdir, options.noclose | logs);
    system(buffer);
}