summaryrefslogtreecommitdiff
path: root/src/sysexec.c
blob: c248ec6e27ed99bb4747a5c3b0a2a7424cd4ebb1 (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
/*
 *  gstdvbbackend
 *
 *  gstdvbbackend is a small programm which captures a given set of dvb
 *  channels from one dvb device and provides the streams via minimal http.
 *
 *
 *  Copyright (C) 2011-2016 Christian Pointner <equinox@spreadspace.org>
 *
 *  This file is part of gstdvbbackend.
 *
 *  gstdvbbackend 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.
 *
 *  gstdvbbackend 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 gstdvbbackend. If not, see <http://www.gnu.org/licenses/>.
 */

#include "datatypes.h"

#include <unistd.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 <stdio.h>
#include <string.h>

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

char** dup_ptrptr(char* const ptrptr[])
{
  if(!ptrptr)
    return NULL;

  int n = 0;
  while(ptrptr[n])
    n++;

  char** my_ptrptr;
  my_ptrptr = malloc((n+1)*sizeof(char*));
  if(!my_ptrptr)
    return NULL;

  int i;
  for(i = 0; i < n; ++i) {
    my_ptrptr[i] = strdup(ptrptr[i]);
    if(!my_ptrptr[i]) {
      i--;
      for(; i >= 0; --i)
        free(my_ptrptr[i]);

      free(my_ptrptr);
      return NULL;
    }
  }

  my_ptrptr[n] = NULL;

  return my_ptrptr;
}

void free_ptrptr(char** ptrptr)
{
  if(!ptrptr)
    return;

  int i;
  for(i = 0; ptrptr[i]; ++i)
    free(ptrptr[i]);

  free(ptrptr);
}

child_t* new_child(const char* script, char* const argv[], char* const evp[])
{
  child_t* new_child;

  new_child = malloc(sizeof(child_t));
  if(!new_child)
    return NULL;

  new_child->pid_ = -1;
  new_child->err_fd_ = -1;
  new_child->script_ = strdup(script);
  if(!new_child->script_) {
    free(new_child);
    return NULL;
  }

  new_child->argv_ = dup_ptrptr(argv);
  if(!new_child->argv_) {
    free(new_child->script_);
    free(new_child);
    return NULL;

  }

  new_child->evp_ = dup_ptrptr(evp);
  if(!new_child->evp_) {
    free_ptrptr(new_child->argv_);
    free(new_child->script_);
    free(new_child);
    return NULL;
  }
  return new_child;
}

void free_child(child_t* child)
{
  if(!child)
    return;

  free_ptrptr(child->argv_);
  free_ptrptr(child->evp_);
  if(child->script_)
    free(child->script_);
  if(child->err_fd_ >= 0) close(child->err_fd_);
  free(child);
}

child_t* rh_exec(const char* script, char* const argv[], char* const evp[])
{
  if(!script)
    return NULL;

  child_t* child = new_child(script, argv, evp);
  if(!child)
    return NULL;

  int pipefd[2];
  if(pipe(pipefd) == -1) {
    log_printf(ERROR, "executing script '%s' failed: pipe() error: %s", child->script_, strerror(errno)); // TODO: thread safe strerror
    free_child(child);
    return NULL;
  }

  pid_t pid;
  pid = fork();
  if(pid == -1) {
    log_printf(ERROR, "executing script '%s' failed: fork() error: %s", child->script_, strerror(errno)); // TODO: thread safe strerror
    close(pipefd[0]);
    close(pipefd[1]);
    free_child(child);
    return NULL;
  }

  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(child->script_, child->argv_, child->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
    int ret = write(pipefd[1], (void*)(&errno), sizeof(errno));
    if(ret == -1) exit(-1);
    exit(-1);
  }
  close(pipefd[1]);

  child->pid_ = pid;
  child->err_fd_ = pipefd[0];

  log_printf(INFO, "called script '%s' with pid %d", child->script_, child->pid_);

  return child;
}

int rh_waitpid(child_t* child, int* status_return)
{
  int status = 0;
  pid_t pid = waitpid(child->pid_, &status, WNOHANG);
  if(!pid || (pid < 0 && errno == ECHILD))
    return 0;
  if(pid < 0) {
    log_printf(ERROR, "waitpid returned with error: %s", strerror(errno)); // TODO: thread safe strerror
    return pid;
  }

  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)); // TODO: thread safe strerror
      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_);

  if(status_return) *status_return = status;

  return 1;
}