/mandos/trunk

To get this branch, use:
bzr branch http://bzr.recompile.se/loggerhead/mandos/trunk

« back to all changes in this revision

Viewing changes to plugbasedclient.c

  • Committer: Teddy Hogeborn
  • Date: 2008-08-01 06:56:27 UTC
  • Revision ID: teddy@fukt.bsnet.se-20080801065627-2c7k4ydefjgacgnq
* plugins.d/plugbasedclient.c (set_cloexec_flag): New function.
  (main): Use the set_cloexec_flag function.  Move inserting of global
  arguments to before the fork.

Show diffs side-by-side

added added

removed removed

Lines of Context:
106
106
  p->argv[p->argc] = NULL;
107
107
}
108
108
 
 
109
/*
 
110
 * Based on the example in the GNU LibC manual chapter 13.13 "File
 
111
 * Descriptor Flags".
 
112
 * *Note File Descriptor Flags:(libc)Descriptor Flags.
 
113
 */
 
114
int set_cloexec_flag(int fd)
 
115
{
 
116
  int ret = fcntl(fd, F_GETFD, 0);
 
117
  /* If reading the flags failed, return error indication now. */
 
118
  if(ret < 0){
 
119
    return ret;
 
120
  }
 
121
  /* Store modified flag word in the descriptor. */
 
122
  return fcntl(fd, F_SETFD, ret | FD_CLOEXEC);
 
123
}
 
124
 
 
125
 
109
126
#define BUFFER_SIZE 256
110
127
 
111
128
const char *argp_program_version =
204
221
  if(debug){
205
222
    for(plugin *p = plugin_list; p != NULL; p=p->next){
206
223
      fprintf(stderr, "Plugin: %s has %d arguments\n",
207
 
              p->name ? p->name : "Global", p->argc);
 
224
              p->name ? p->name : "Global", p->argc - 1);
208
225
      for(char **a = p->argv; *a != NULL; a++){
209
226
        fprintf(stderr, "\tArg: %s\n", *a);
210
227
      }
212
229
  }
213
230
  
214
231
  dir = opendir(plugindir);
215
 
  {
216
 
    /* Set the FD_CLOEXEC flag on the directory */
217
 
    int fd = dirfd(dir);
218
 
    ret = fcntl (fd, F_GETFD, 0);
219
 
    if(ret < 0){
220
 
      perror("fcntl F_GETFD");
221
 
      goto end;
222
 
    }
223
 
    ret = fcntl(fd, F_SETFD, FD_CLOEXEC | ret);
224
 
    if(ret < 0){
225
 
      perror("fcntl F_SETFD");
226
 
      goto end;
227
 
    }
 
232
  /* Set the FD_CLOEXEC flag on the directory */
 
233
  ret = set_cloexec_flag(dirfd(dir));
 
234
  if(ret < 0){
 
235
    perror("set_cloexec_flag");
 
236
    goto end;
228
237
  }
229
238
  
230
239
  if(dir == NULL){
322
331
      perror(argv[0]);
323
332
      goto end;
324
333
    }
 
334
    plugin *p = getplugin(dirst->d_name, &plugin_list);
 
335
    {
 
336
      /* Add global arguments to argument list for this plugin */
 
337
      plugin *g = getplugin(NULL, &plugin_list);
 
338
      for(char **a = g->argv + 1; *a != NULL; a++){
 
339
        addargument(p, *a);
 
340
      }
 
341
    }
325
342
    pid_t pid = fork();
326
343
    if(pid == 0){
327
344
      /* this is the child process */
329
346
      close(pipefd[0]); /* close unused read end of pipe */
330
347
      dup2(pipefd[1], STDOUT_FILENO); /* replace our stdout */
331
348
      
332
 
      plugin *p = getplugin(dirst->d_name, &plugin_list);
333
 
      plugin *g = getplugin(NULL, &plugin_list);
334
 
      for(char **a = g->argv + 1; *a != NULL; a++){
335
 
        addargument(p, *a);
336
 
      }
337
349
      if(execv(filename, p->argv) < 0){
338
350
        perror(argv[0]);
339
351
        close(pipefd[1]);