summaryrefslogtreecommitdiffstats
path: root/src/exec.c
blob: 2e8b33bde929cef43b55b5d08f0254c268dc8d53 (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
129
130
131
132
133
134
135
136
137
138
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <signal.h>
#include <sys/wait.h>
#include <limits.h>
#include <fcntl.h>
#include <stdio.h>

#include <libdaemon/dlog.h>
#include <libdaemon/dsignal.h>

#include "exec.h"

int log_exec(const char *dir, const char *prog, const char *arg) {
    pid_t pid;
    int p[2];
    unsigned n = 0;
    static char buf[256];
    int sigfd, r;
    fd_set fds;

    daemon_log(LOG_INFO, "Running '%s %s'", prog, arg);
    
    if (pipe(p) < 0) {
        daemon_log(LOG_ERR, "pipe() failed: %s", strerror(errno));
        return -1;
    }

    if ((pid = fork()) < 0) {
        daemon_log(LOG_ERR, "fork() failed: %s", strerror(errno));
        return -1;
            
    } else if (pid == 0) {
        dup2(p[1], 1);
        dup2(p[1], 2);
        
        if (p[0] > 2)
            close(p[0]);

        if (p[1] > 2)
            close(p[1]);

        close(0);
        open("/dev/null", O_RDONLY);

	umask(0022); // Set up a sane umask

        if (dir && chdir(dir) < 0) {
            daemon_log(LOG_WARNING, "Failed to change to directory '%s'", dir);
            chdir("/");
        }

        execl(prog, prog, arg, 0);

        daemon_log(LOG_ERR, "execl(%s) failed: %s\n", prog, strerror(errno));
        
        _exit(EXIT_FAILURE);
    }

    close(p[1]);

    FD_ZERO(&fds);
    FD_SET(p[0], &fds);
    FD_SET(sigfd = daemon_signal_fd(), &fds);

    n = 0;

    for (;;) {
        fd_set qfds = fds;

        if (select(FD_SETSIZE, &qfds, NULL, NULL, NULL) < 0) {

            if (errno == EINTR)
                continue;

            daemon_log(LOG_ERR, "select() failed: %s", strerror(errno));
            return -1;
        }


        if (FD_ISSET(p[0], &qfds)) {
            char c;

            if (read(p[0], &c, 1) != 1)
                break;

            buf[n] = c;
            
            if (c == '\n' || n >= sizeof(buf) - 2) {
                if (c != '\n') n++;
                buf[n] = 0;

                if (buf[0]) 
                     daemon_log(LOG_INFO, "client: %s", buf); 
            
                n = 0;
            } else
                n++;
        }

        if (FD_ISSET(sigfd, &qfds)) {
            int sig;
            
            if ((sig = daemon_signal_next()) < 0) {
                daemon_log(LOG_ERR, "daemon_signal_next(): %s", strerror(errno));
                break;
            }

            if (sig != SIGCHLD) {
                daemon_log(LOG_WARNING, "Killing child.");
                kill(pid, SIGTERM);
            }
                
            break;
        }
    }

    if (n > 0) {
        buf[n] = 0;
         daemon_log(LOG_WARNING, "client: %s", buf); 
    }

    waitpid(pid, &r, 0);

    close(p[0]);

    if (!WIFEXITED(r) || WEXITSTATUS(r) != 0) {
        daemon_log(LOG_ERR, "Program execution failed, return value is %i.", WEXITSTATUS(r));
        return -1;
    } else {
        daemon_log(LOG_INFO, "Program executed successfully.");
        return 0;
    }
}