/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
1
/*  -*- coding: utf-8 -*- */
2
/*
3
 * Mandos plugin runner - Run Mandos plugins
4
 *
28 by Teddy Hogeborn
* server.conf: New file.
5
 * Copyright © 2007-2008 Teddy Hogeborn & Björn Påhlsson
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
6
 * 
7
 * This program is free software: you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 * 
12
 * This program is distributed in the hope that it will be useful, but
13
 * WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * General Public License for more details.
16
 * 
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program.  If not, see
19
 * <http://www.gnu.org/licenses/>.
20
 * 
28 by Teddy Hogeborn
* server.conf: New file.
21
 * Contact the authors at <mandos@fukt.bsnet.se>.
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
22
 */
23
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
24
#define _GNU_SOURCE		/* TEMP_FAILURE_RETRY() */
25
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
26
#include <stdio.h>		/* popen(), fileno(), fprintf(),
27
				   stderr, STDOUT_FILENO */
28
#include <iso646.h>		/* and, or, not */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
29
#include <sys/types.h>	        /* DIR, opendir(), stat(),
30
				   struct stat, waitpid(),
31
				   WIFEXITED(), WEXITSTATUS(),
32
				   wait() */
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
33
#include <sys/wait.h>		/* wait() */
34
#include <dirent.h>		/* DIR, struct dirent, opendir(),
35
				   readdir(), closedir() */
36
#include <sys/stat.h>		/* struct stat, stat(), S_ISREG() */
37
#include <unistd.h>		/* struct stat, stat(), S_ISREG(),
38
				   fcntl() */
39
#include <fcntl.h>		/* fcntl() */
40
#include <stddef.h>		/* NULL */
41
#include <stdlib.h>		/* EXIT_FAILURE */
42
#include <sys/select.h>		/* fd_set, select(), FD_ZERO(),
43
				   FD_SET(), FD_ISSET() */
44
#include <string.h>		/* strlen(), strcpy(), strcat() */
45
#include <stdbool.h>		/* true */
46
#include <sys/wait.h>		/* waitpid(), WIFEXITED(),
47
				   WEXITSTATUS() */
48
#include <errno.h>		/* errno */
49
#include <argp.h>		/* struct argp_option,
50
				   struct argp_state, struct argp,
51
				   argp_parse() */
13 by Björn Påhlsson
Added following support:
52
53
struct process;
54
55
typedef struct process{
56
  pid_t pid;
57
  int fd;
58
  char *buffer;
21 by Teddy Hogeborn
* Makefile (CFLAGS): Changed to use $(WARN), $(DEBUG), $(COVERAGE) and
59
  size_t buffer_size;
60
  size_t buffer_length;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
61
  bool eof;
62
  bool completed;
63
  int status;
13 by Björn Påhlsson
Added following support:
64
  struct process *next;
65
} process;
66
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
67
typedef struct plugin{
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
68
  char *name;			/* can be NULL or any plugin name */
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
69
  char **argv;
70
  int argc;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
71
  bool disabled;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
72
  struct plugin *next;
73
} plugin;
74
75
plugin *getplugin(char *name, plugin **plugin_list){
76
  for (plugin *p = *plugin_list; p != NULL; p = p->next){
77
    if ((p->name == name)
78
	or (p->name and name and (strcmp(p->name, name) == 0))){
79
      return p;
80
    }
81
  }
82
  /* Create a new plugin */
83
  plugin *new_plugin = malloc(sizeof(plugin));
84
  if (new_plugin == NULL){
85
    perror("malloc");
86
    exit(EXIT_FAILURE);
87
  }
88
  new_plugin->name = name;
89
  new_plugin->argv = malloc(sizeof(char *) * 2);
90
  if (new_plugin->argv == NULL){
91
    perror("malloc");
92
    exit(EXIT_FAILURE);
93
  }
94
  new_plugin->argv[0] = name;
95
  new_plugin->argv[1] = NULL;
96
  new_plugin->argc = 1;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
97
  new_plugin->disabled = false;
24.1.5 by Björn Påhlsson
plugbasedclient:
98
  new_plugin->next = *plugin_list;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
99
  /* Append the new plugin to the list */
100
  *plugin_list = new_plugin;
101
  return new_plugin;
102
}
103
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
104
void addargument(plugin *p, char *arg){
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
105
  p->argv[p->argc] = arg;
106
  p->argv = realloc(p->argv, sizeof(char *) * (size_t)(p->argc + 2));
107
  if (p->argv == NULL){
108
    perror("malloc");
109
    exit(EXIT_FAILURE);
110
  }
111
  p->argc++;
112
  p->argv[p->argc] = NULL;
113
}
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
114
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
115
/*
116
 * Based on the example in the GNU LibC manual chapter 13.13 "File
117
 * Descriptor Flags".
118
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
119
 */
120
int set_cloexec_flag(int fd)
121
{
122
  int ret = fcntl(fd, F_GETFD, 0);
123
  /* If reading the flags failed, return error indication now. */
124
  if(ret < 0){
125
    return ret;
126
  }
127
  /* Store modified flag word in the descriptor. */
128
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
129
}
130
13 by Björn Påhlsson
Added following support:
131
#define BUFFER_SIZE 256
132
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
133
const char *argp_program_version = "plugbasedclient 0.9";
134
const char *argp_program_bug_address = "<mandos@fukt.bsnet.se>";
135
136
process *process_list = NULL;
137
138
/* Mark a process as completed when it exits, and save its exit
139
   status. */
140
void handle_sigchld(__attribute__((unused)) int sig){
141
  process *proc = process_list;
142
  int status;
143
  pid_t pid = wait(&status);
144
  while(proc != NULL and proc->pid != pid){
145
    proc = proc->next;    
146
  }
147
  if(proc == NULL){
148
    /* Process not found in process list */
149
    return;
150
  }
151
  proc->status = status;
152
  proc->completed = true;
153
}
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
154
13 by Björn Påhlsson
Added following support:
155
int main(int argc, char *argv[]){
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
156
  const char *plugindir = "/conf/conf.d/mandos/plugins.d";
24.1.5 by Björn Påhlsson
plugbasedclient:
157
  size_t d_name_len;
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
158
  DIR *dir = NULL;
13 by Björn Påhlsson
Added following support:
159
  struct dirent *dirst;
160
  struct stat st;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
161
  fd_set rfds_all;
13 by Björn Påhlsson
Added following support:
162
  int ret, maxfd = 0;
24.1.6 by Björn Påhlsson
plugbasedclient
163
  uid_t uid = 65534;
164
  gid_t gid = 65534;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
165
  bool debug = false;
166
  int exitstatus = EXIT_SUCCESS;
24.1.7 by Björn Påhlsson
merge
167
  struct sigaction old_sigchld_action;
168
  struct sigaction sigchld_action = { .sa_handler = handle_sigchld,
169
				      .sa_flags = SA_NOCLDSTOP };
170
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
171
  /* Establish a signal handler */
172
  sigemptyset(&sigchld_action.sa_mask);
173
  ret = sigaddset(&sigchld_action.sa_mask, SIGCHLD);
174
  if(ret < 0){
175
    perror("sigaddset");
176
    exit(EXIT_FAILURE);
177
  }
178
  ret = sigaction(SIGCHLD, &sigchld_action, &old_sigchld_action);
179
  if(ret < 0){
180
    perror("sigaction");
181
    exit(EXIT_FAILURE);
182
  }
183
  
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
184
  /* The options we understand. */
185
  struct argp_option options[] = {
186
    { .name = "global-options", .key = 'g',
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
187
      .arg = "OPTION[,OPTION[,...]]",
188
      .doc = "Options passed to all plugins" },
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
189
    { .name = "options-for", .key = 'o',
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
190
      .arg = "PLUGIN:OPTION[,OPTION[,...]]",
191
      .doc = "Options passed only to specified plugin" },
192
    { .name = "disable", .key = 'd',
193
      .arg = "PLUGIN",
194
      .doc = "Disable a specific plugin", .group = 1 },
24.1.5 by Björn Påhlsson
plugbasedclient:
195
    { .name = "plugin-dir", .key = 128,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
196
      .arg = "DIRECTORY",
197
      .doc = "Specify a different plugin directory", .group = 2 },
24.1.6 by Björn Påhlsson
plugbasedclient
198
    { .name = "userid", .key = 129,
24.1.7 by Björn Påhlsson
merge
199
      .arg = "ID", .flags = 0,
200
      .doc = "User ID the plugins will run as", .group = 2 },
24.1.6 by Björn Påhlsson
plugbasedclient
201
    { .name = "groupid", .key = 130,
24.1.7 by Björn Påhlsson
merge
202
      .arg = "ID", .flags = 0,
203
      .doc = "Group ID the plugins will run as", .group = 2 },
204
    { .name = "debug", .key = 131,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
205
      .doc = "Debug mode", .group = 3 },
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
206
    { .name = NULL }
207
  };
208
  
209
  error_t parse_opt (int key, char *arg, struct argp_state *state) {
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
210
    /* Get the INPUT argument from `argp_parse', which we know is a
211
       pointer to our plugin list pointer. */
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
212
    plugin **plugins = state->input;
213
    switch (key) {
214
    case 'g':
215
      if (arg != NULL){
216
	char *p = strtok(arg, ",");
217
	do{
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
218
	  addargument(getplugin(NULL, plugins), p);
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
219
	  p = strtok(NULL, ",");
220
	} while (p);
221
      }
222
      break;
223
    case 'o':
224
      if (arg != NULL){
225
	char *name = strtok(arg, ":");
226
	char *p = strtok(NULL, ":");
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
227
	if(p){
228
	  p = strtok(p, ",");
229
	  do{
230
	    addargument(getplugin(name, plugins), p);
231
	    p = strtok(NULL, ",");
232
	  } while (p);
233
	}
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
234
      }
235
      break;
24.1.5 by Björn Påhlsson
plugbasedclient:
236
    case 'd':
237
      if (arg != NULL){
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
238
	getplugin(arg, plugins)->disabled = true;
24.1.5 by Björn Påhlsson
plugbasedclient:
239
      }
240
      break;
241
    case 128:
242
      plugindir = arg;
243
      break;
24.1.6 by Björn Påhlsson
plugbasedclient
244
    case 129:
245
      uid = (uid_t)strtol(arg, NULL, 10);
246
      break;
247
    case 130:
248
      gid = (gid_t)strtol(arg, NULL, 10);
249
      break;
24.1.7 by Björn Påhlsson
merge
250
    case 131:
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
251
      debug = true;
252
      break;
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
253
    case ARGP_KEY_ARG:
254
      argp_usage (state);
255
      break;
256
    case ARGP_KEY_END:
257
      break;
258
    default:
259
      return ARGP_ERR_UNKNOWN;
260
    }
261
    return 0;
262
  }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
263
  
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
264
  plugin *plugin_list = NULL;
265
  
266
  struct argp argp = { .options = options, .parser = parse_opt,
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
267
		       .args_doc = "",
268
		       .doc = "Mandos plugin runner -- Run plugins" };
269
  
24.1.7 by Björn Påhlsson
merge
270
  argp_parse (&argp, argc, argv, 0, 0, &plugin_list);  
271
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
272
  if(debug){
273
    for(plugin *p = plugin_list; p != NULL; p=p->next){
274
      fprintf(stderr, "Plugin: %s has %d arguments\n",
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
275
	      p->name ? p->name : "Global", p->argc - 1);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
276
      for(char **a = p->argv; *a != NULL; a++){
277
	fprintf(stderr, "\tArg: %s\n", *a);
278
      }
279
    }
280
  }
24.1.5 by Björn Påhlsson
plugbasedclient:
281
24.1.6 by Björn Påhlsson
plugbasedclient
282
  ret = setuid(uid);
283
  if (ret == -1){
284
    perror("setuid");
285
  }
24.1.7 by Björn Påhlsson
merge
286
  
24.1.6 by Björn Påhlsson
plugbasedclient
287
  setgid(gid);
288
  if (ret == -1){
289
    perror("setuid");
290
  }
291
  
13 by Björn Påhlsson
Added following support:
292
  dir = opendir(plugindir);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
293
  if(dir == NULL){
294
    perror("Could not open plugin dir");
295
    exitstatus = EXIT_FAILURE;
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
296
    goto end;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
297
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
298
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
299
  /* Set the FD_CLOEXEC flag on the directory, if possible */
300
  {
301
    int dir_fd = dirfd(dir);
302
    if(dir_fd >= 0){
303
      ret = set_cloexec_flag(dir_fd);
304
      if(ret < 0){
305
	perror("set_cloexec_flag");
306
	exitstatus = EXIT_FAILURE;
307
	goto end;
308
      }
309
    }
310
  }
24.1.1 by Björn Påhlsson
Added syntax and support for plugbasedclient arguments and how they
311
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
312
  FD_ZERO(&rfds_all);
13 by Björn Påhlsson
Added following support:
313
  
314
  while(true){
315
    dirst = readdir(dir);
316
    
317
    // All directory entries have been processed
318
    if(dirst == NULL){
319
      break;
320
    }
321
    
322
    d_name_len = strlen(dirst->d_name);
323
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
324
    // Ignore dotfiles, backup files and other junk
325
    {
326
      bool bad_name = false;
327
      
328
      const char const *bad_prefixes[] = { ".", "#", NULL };
329
      
330
      const char const *bad_suffixes[] = { "~", "#", ".dpkg-new",
331
					   ".dpkg-old",
332
					   ".dpkg-divert", NULL };
333
      for(const char **pre = bad_prefixes; *pre != NULL; pre++){
334
	size_t pre_len = strlen(*pre);
335
	if((d_name_len >= pre_len)
336
	   and strncmp((dirst->d_name), *pre, pre_len) == 0){
337
	  if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
338
	    fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
339
		    " with bad prefix %s\n", dirst->d_name, *pre);
340
	  }
341
	  bad_name = true;
342
	  break;
343
	}
344
      }
345
      
346
      if(bad_name){
347
	continue;
348
      }
349
      
350
      for(const char **suf = bad_suffixes; *suf != NULL; suf++){
351
	size_t suf_len = strlen(*suf);
352
	if((d_name_len >= suf_len)
353
	   and (strcmp((dirst->d_name)+d_name_len-suf_len, *suf)
354
		== 0)){
355
	  if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
356
	    fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
357
		    " with bad suffix %s\n", dirst->d_name, *suf);
358
	  }
359
	  bad_name = true;
360
	  break;
361
	}
362
      }
363
      
364
      if(bad_name){
365
	continue;
366
      }
13 by Björn Påhlsson
Added following support:
367
    }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
368
    
24.1.5 by Björn Påhlsson
plugbasedclient:
369
    char *filename = malloc(d_name_len + strlen(plugindir) + 2);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
370
    if (filename == NULL){
371
      perror("malloc");
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
372
      exitstatus = EXIT_FAILURE;
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
373
      goto end;
374
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
375
    strcpy(filename, plugindir); /* Spurious warning */
376
    strcat(filename, "/");	/* Spurious warning */
377
    strcat(filename, dirst->d_name); /* Spurious warning */
378
    
13 by Björn Påhlsson
Added following support:
379
    stat(filename, &st);
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
380
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
381
    if (not S_ISREG(st.st_mode)	or (access(filename, X_OK) != 0)){
382
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
383
	fprintf(stderr, "Ignoring plugin dir entry \"%s\""
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
384
		" with bad type or mode\n", filename);
385
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
386
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
387
      continue;
388
    }
389
    if(getplugin(dirst->d_name, &plugin_list)->disabled){
390
      if(debug){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
391
	fprintf(stderr, "Ignoring disabled plugin \"%s\"\n",
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
392
		dirst->d_name);
393
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
394
      free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
395
      continue;
396
    }
32 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
397
    plugin *p = getplugin(dirst->d_name, &plugin_list);
398
    {
399
      /* Add global arguments to argument list for this plugin */
400
      plugin *g = getplugin(NULL, &plugin_list);
401
      for(char **a = g->argv + 1; *a != NULL; a++){
402
	addargument(p, *a);
403
      }
404
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
405
    int pipefd[2]; 
406
    ret = pipe(pipefd);
407
    if (ret == -1){
408
      perror("pipe");
409
      exitstatus = EXIT_FAILURE;
410
      goto end;
411
    }
412
    ret = set_cloexec_flag(pipefd[0]);
413
    if(ret < 0){
414
      perror("set_cloexec_flag");
415
      exitstatus = EXIT_FAILURE;
416
      goto end;
417
    }
418
    ret = set_cloexec_flag(pipefd[1]);
419
    if(ret < 0){
420
      perror("set_cloexec_flag");
421
      exitstatus = EXIT_FAILURE;
422
      goto end;
423
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
424
    /* Block SIGCHLD until process is safely in process list */
425
    ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
426
    if(ret < 0){
427
      perror("sigprocmask");
428
      exitstatus = EXIT_FAILURE;
429
      goto end;
430
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
431
    // Starting a new process to be watched
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
432
    pid_t pid = fork();
433
    if(pid == 0){
434
      /* this is the child process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
435
      ret = sigaction(SIGCHLD, &old_sigchld_action, NULL);
436
      if(ret < 0){
437
	perror("sigaction");
438
	_exit(EXIT_FAILURE);
439
      }
440
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
441
      if(ret < 0){
442
	perror("sigprocmask");
443
	_exit(EXIT_FAILURE);
444
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
445
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
446
      
447
      if(dirfd(dir) < 0){
448
	/* If dir has no file descriptor, we could not set FD_CLOEXEC
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
449
	   above and must now close it manually here. */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
450
	closedir(dir);
33 by Teddy Hogeborn
* plugins.d/plugbasedclient.c (main): Close the pipe fd after dup2:ing
451
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
452
      if(execv(filename, p->argv) < 0){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
453
	perror("execv");
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
454
	_exit(EXIT_FAILURE);
455
      }
456
      /* no return */
457
    }
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
458
    /* parent process */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
459
    free(filename);
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
460
    close(pipefd[1]);		/* close unused write end of pipe */
461
    process *new_process = malloc(sizeof(process));
462
    if (new_process == NULL){
463
      perror("malloc");
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
464
      ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
465
      if(ret < 0){
466
	perror("sigprocmask");
467
      }
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
468
      exitstatus = EXIT_FAILURE;
469
      goto end;
470
    }
471
    
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
472
    *new_process = (struct process){ .pid = pid,
473
				     .fd = pipefd[0],
474
				     .next = process_list };
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
475
    // List handling
476
    process_list = new_process;
477
    /* Unblock SIGCHLD so signal handler can be run if this process
478
       has already completed */
479
    ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask, NULL);
480
    if(ret < 0){
481
      perror("sigprocmask");
482
      exitstatus = EXIT_FAILURE;
483
      goto end;
484
    }
485
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
486
    FD_SET(new_process->fd, &rfds_all);
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
487
    
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
488
    if (maxfd < new_process->fd){
489
      maxfd = new_process->fd;
490
    }
491
    
13 by Björn Påhlsson
Added following support:
492
  }
493
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
494
  /* Free the plugin list */
495
  for(plugin *next; plugin_list != NULL; plugin_list = next){
496
    next = plugin_list->next;
497
    free(plugin_list->argv);
498
    free(plugin_list);
499
  }
500
  
13 by Björn Påhlsson
Added following support:
501
  closedir(dir);
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
502
  dir = NULL;
13 by Björn Påhlsson
Added following support:
503
  
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
504
  if (process_list == NULL){
505
    fprintf(stderr, "No plugin processes started, exiting\n");
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
506
    exitstatus = EXIT_FAILURE;
507
    goto end;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
508
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
509
  while(process_list){
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
510
    fd_set rfds = rfds_all;
511
    int select_ret = select(maxfd+1, &rfds, NULL, NULL, NULL);
512
    if (select_ret == -1){
513
      perror("select");
514
      exitstatus = EXIT_FAILURE;
515
      goto end;
516
    }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
517
    /* OK, now either a process completed, or something can be read
518
       from one of them */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
519
    for(process *proc = process_list; proc ; proc = proc->next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
520
      /* Is this process completely done? */
521
      if(proc->eof and proc->completed){
522
	/* Only accept the plugin output if it exited cleanly */
523
	if(not WIFEXITED(proc->status)
524
	   or WEXITSTATUS(proc->status) != 0){
525
	  /* Bad exit by plugin */
526
	  if(debug){
527
	    if(WIFEXITED(proc->status)){
528
	      fprintf(stderr, "Plugin %d exited with status %d\n",
529
		      proc->pid, WEXITSTATUS(proc->status));
530
	    } else if(WIFSIGNALED(proc->status)) {
531
	      fprintf(stderr, "Plugin %d killed by signal %d\n",
532
		      proc->pid, WTERMSIG(proc->status));
533
	    } else if(WCOREDUMP(proc->status)){
534
	      fprintf(stderr, "Plugin %d dumped core\n", proc->pid);
535
	    }
536
	  }
537
	  /* Remove the plugin */
538
	  FD_CLR(proc->fd, &rfds_all);
539
	  /* Block signal while modifying process_list */
540
	  ret = sigprocmask (SIG_BLOCK, &sigchld_action.sa_mask, NULL);
541
	  if(ret < 0){
542
	    perror("sigprocmask");
543
	    exitstatus = EXIT_FAILURE;
544
	    goto end;
545
	  }
546
	  /* Delete this process entry from the list */
547
	  if(process_list == proc){
548
	    /* First one - simple */
549
	    process_list = proc->next;
550
	  } else {
551
	    /* Second one or later */
552
	    for(process *p = process_list; p != NULL; p = p->next){
553
	      if(p->next == proc){
554
		p->next = proc->next;
555
		break;
556
	      }
557
	    }
558
	  }
559
	  /* We are done modifying process list, so unblock signal */
560
	  ret = sigprocmask (SIG_UNBLOCK, &sigchld_action.sa_mask,
561
			     NULL);
562
	  if(ret < 0){
563
	    perror("sigprocmask");
564
	  }
565
	  free(proc->buffer);
566
	  free(proc);
567
	  /* We deleted this process from the list, so we can't go
568
	     proc->next.  Therefore, start over from the beginning of
569
	     the process list */
570
	  break;
571
	}
572
	/* This process exited nicely, so print its buffer */
573
	for(size_t written = 0; written < proc->buffer_length;
574
	    written += (size_t)ret){
575
	  ret = TEMP_FAILURE_RETRY(write(STDOUT_FILENO,
576
					 proc->buffer + written,
577
					 proc->buffer_length
578
					 - written));
579
	  if(ret < 0){
580
	    perror("write");
581
	    exitstatus = EXIT_FAILURE;
582
	    goto end;
583
	  }
584
	}
585
	goto end;
586
      }
587
      /* This process has not completed.  Does it have any output? */
588
      if(proc->eof or not FD_ISSET(proc->fd, &rfds)){
589
	/* This process had nothing to say at this time */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
590
	continue;
591
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
592
      /* Before reading, make the process' data buffer large enough */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
593
      if(proc->buffer_length + BUFFER_SIZE > proc->buffer_size){
594
	proc->buffer = realloc(proc->buffer, proc->buffer_size
595
			       + (size_t) BUFFER_SIZE);
596
	if (proc->buffer == NULL){
597
	  perror("malloc");
598
	  exitstatus = EXIT_FAILURE;
599
	  goto end;
600
	}
601
	proc->buffer_size += BUFFER_SIZE;
602
      }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
603
      /* Read from the process */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
604
      ret = read(proc->fd, proc->buffer + proc->buffer_length,
605
		 BUFFER_SIZE);
606
      if(ret < 0){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
607
	/* Read error from this process; ignore the error */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
608
	continue;
609
      }
610
      if(ret == 0){
611
	/* got EOF */
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
612
	proc->eof = true;
613
      } else {
614
	proc->buffer_length += (size_t) ret;
13 by Björn Påhlsson
Added following support:
615
      }
616
    }
617
  }
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
618
  if(process_list == NULL){
619
    fprintf(stderr, "All plugin processes failed, exiting\n");
620
    exitstatus = EXIT_FAILURE;
621
  }
13 by Björn Påhlsson
Added following support:
622
  
623
 end:
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
624
  /* Restore old signal handler */
625
  sigaction(SIGCHLD, &old_sigchld_action, NULL);
626
  
627
  /* Free the plugin list */
628
  for(plugin *next; plugin_list != NULL; plugin_list = next){
629
    next = plugin_list->next;
630
    free(plugin_list->argv);
631
    free(plugin_list);
632
  }
633
  
634
  if(dir != NULL){
635
    closedir(dir);
636
  }
637
  
638
  /* Free the process list and kill the processes */
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
639
  for(process *next; process_list != NULL; process_list = next){
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
640
    next = process_list->next;
34 by Teddy Hogeborn
* plugbasedclient.c (main): Check if plugin dir could be opened. Set
641
    close(process_list->fd);
642
    kill(process_list->pid, SIGTERM);
643
    free(process_list->buffer);
644
    free(process_list);
13 by Björn Påhlsson
Added following support:
645
  }
646
  
35 by Teddy Hogeborn
* plugbasedclient.c (struct process): New fields "eof", "completed",
647
  /* Wait for any remaining child processes to terminate */
648
  do{
649
    ret = wait(NULL);
650
  } while(ret >= 0);
651
  if(errno != ECHILD){
652
    perror("wait");
653
  }
654
  
31 by Teddy Hogeborn
* plugins.d/plugbasedclient.c: Update include file comments.
655
  return exitstatus;
13 by Björn Påhlsson
Added following support:
656
}