summaryrefslogtreecommitdiff
path: root/sysexec.c
blob: d8722f4c8d64ed6a4d23163f5f2a31b0ca2e2c39 (plain) (blame)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*
 *  rhdropbox
 *
 *  Copyright (C) 2009 Christian Pointner <equinox@helsinki.at>
 *
 *  This file is part of rhdropbox.
 *
 *  rhdropbox is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  any later version.
 *
 *  rhdropbox is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with rhdropbox. If not, see <http://www.gnu.org/licenses/>.
 */

#include "datatypes.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/wait.h>
#include <sys/select.h>
#include <stdlib.h>
#include <string.h>

#include "sysexec.h"
#include "log.h"

void child_list_init(child_list_t* list)
{
  if(!list)
    return;
  
  list->first_ = NULL;
}

void child_list_clear(child_list_t* list)
{
  if(!list)
    return;

  while(list->first_) {
    child_list_element_t* tmp;
    tmp = list->first_;
    list->first_ = tmp->next_;
    if(tmp->script_)
      free(tmp->script_);
    free(tmp);
  }
}

int child_list_add(child_list_t* list, pid_t pid, const char* script, int err_fd)
{
  if(!list)
    return -1;

  if(!list->first_) {
    list->first_ = malloc(sizeof(child_list_element_t));
    if(!list->first_)
      return -2;

    list->first_->next_ = 0;
    list->first_->pid_ = pid;
    list->first_->err_fd_ = err_fd;
    list->first_->script_ = strdup(script);
    if(!list->first_->script_) {
      free(list->first_);
      list->first_ = NULL;
      return -2;
    }
  }
  else {
    child_list_element_t* tmp = list->first_;
    while(tmp->next_)
      tmp = tmp->next_;

    tmp->next_  = malloc(sizeof(child_list_element_t));
    if(!tmp->next_)
      return -2;

    tmp->next_->next_ = 0;
    tmp->next_->pid_ = pid;
    tmp->next_->err_fd_ = err_fd;
    tmp->next_->script_ = strdup(script);
    if(!tmp->next_->script_) {
      free(tmp->next_);
      tmp->next_ = NULL;
      return -2;
    }
  }
  return 0;
}

void child_list_rm(child_list_t* list, pid_t pid)
{
  if(!list)
    return;
  
  child_list_element_t* tmp = NULL;
  if(list->first_->pid_ == pid) {
    tmp = list->first_;
    list->first_ = list->first_->next_;
    
    if(tmp->script_)
      free(tmp->script_);
    free(tmp);
    return;
  }

  child_list_element_t* prev = list->first_;
  tmp = list->first_->next_;
  while(tmp) {
    if(tmp->pid_ == pid) {
      prev->next_ = tmp->next_;

      if(tmp->script_)
        free(tmp->script_);
      free(tmp);
      return;      
    }
    prev = tmp;
    tmp = tmp->next_;
  }
}

child_list_element_t* child_list_find(child_list_t* list, pid_t pid)
{
  if(!list)
    return NULL;
  
  child_list_element_t* tmp = list->first_;
  while(tmp) {
    if(tmp->pid_ == pid)
      return tmp;
    tmp = tmp->next_;
  }
  
  return NULL;
}

int rh_exec(const char* script, char* const argv[], char* const evp[], child_list_t* child_lst)
{
  if(!script)
    return -1;

  int pipefd[2];
  if(pipe(pipefd) == -1) {
    log_printf(ERROR, "executing script '%s' pipe() error: %s", script, strerror(errno));
    return -1;
  }

  pid_t pid;
  pid = fork();
  if(pid == -1) {
    log_printf(ERROR, "executing script '%s' fork() error: %s", script, strerror(errno));
    return -1;
  }

  if(!pid) {
    int fd;
    for (fd=getdtablesize();fd>=0;--fd) // close all file descriptors
      if(fd != pipefd[1]) close(fd);

    fd = open("/dev/null",O_RDWR);        // stdin
    if(fd == -1)
      log_printf(WARNING, "can't open stdin");
    else {
      if(dup(fd) == -1)   // stdout
        log_printf(WARNING, "can't open stdout");
      if(dup(fd) == -1)   // stderr
        log_printf(WARNING, "can't open stderr");
    }
    execve(script, argv, evp);
        // if execve returns, an error occurred, but logging doesn't work 
        // because we closed all file descriptors, so just write errno to
        // pipe and call exit
    write(pipefd[1], (void*)(&errno), sizeof(errno));
    exit(-1);
  }
  close(pipefd[1]);

  log_printf(INFO, "called script '%s' with pid %d", script, pid);
  return child_list_add(child_lst, pid, script, pipefd[0]);
}

int rh_waitpid(child_list_t* child_lst)
{
  int status = 0;
  pid_t pid = waitpid(-1, &status, WNOHANG);
  if(!pid || (pid < 0 && errno == ECHILD))
    return 0;
  if(pid < 0) {
    log_printf(ERROR, "waitpid returned with error: %s", strerror(errno));
    return pid;
  }

  child_list_element_t* child = child_list_find(child_lst, pid);
  if(!child) {
    log_printf(ERROR, "waitpid returned unknown child pid (%d)", pid);
    return 0;
  }

  fd_set rfds;
  FD_ZERO(&rfds);
  FD_SET(child->err_fd_, &rfds);
  struct timeval tv = { 0 , 0 };
  if(select(child->err_fd_+1, &rfds, NULL, NULL, &tv) == 1) {
    int err = 0;
    if(read(child->err_fd_, (void*)(&err), sizeof(err)) >= sizeof(err)) {
      log_printf(INFO, "script '%s' exec() error: %s", child->script_, strerror(err));
      close(child->script_);
      return -1;
    }
  }
  if(WIFEXITED(status))
    log_printf(INFO, "script '%s' (pid %d) returned %d", child->script_, child->pid_, WEXITSTATUS(status));
  else if(WIFSIGNALED(status))
    log_printf(INFO, "script '%s' (pid %d) terminated after signal %d", child->script_, child->pid_, WTERMSIG(status));
  else
    log_printf(INFO, "executing script '%s' (pid %d): unkown error", child->script_, child->pid_);

  close(child->err_fd_);
  return status;
}