File: process.c

package info (click to toggle)
udpcast 20120424-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, forky, sid, stretch, trixie
  • size: 876 kB
  • ctags: 1,011
  • sloc: ansic: 7,713; sh: 2,838; perl: 227; makefile: 114
file content (87 lines) | stat: -rw-r--r-- 1,816 bytes parent folder | download | duplicates (4)
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
#include "udpc_process.h"
#include "log.h"

#ifndef __MINGW32__

#include <unistd.h>
#include <string.h>
#include <errno.h>

static void dupFd(int src, int target) {
  if(src != target) {
    close(target);
    if(dup2(src, target) < 0)
      udpc_fatal(1, "dup2 %d->%d: %s\n", src, target, strerror(errno));
    close(src);
  }
}

int open2(int in, int out, char **arg, int closeFd) {
  int pid;
  switch( (pid=fork()) ) {
  case 0: /* child */

    dupFd(in,0);
    dupFd(out,1);
 
    if(closeFd != -1)
      close(closeFd);
    
    execvp(arg[0], arg);
    udpc_fatal(1, "exec %s: %s\n", arg[0], strerror(errno));
  case -1: /* fork error */
    perror("fork");
    return -1;
  default: /* Father: just return */
    return pid;
  }
}

#else /* __MINGW32__ */

#include <errno.h>
#include <stdio.h>
#include <string.h>

/* Thanks http://lists.gnu.org/archive/html/groff/2003-07/msg00107.html */
static int dupFd(int src, int target, int *savedFd) {
  *savedFd = -1;

  if(src != target) {
    if ((*savedFd = dup (target)) < 0)
      udpc_fatal(1, "dup parent_fd %d %s", target, strerror(errno));
    
    if (dup2 (src, target) < 0)
      udpc_fatal(1, "dup2 child_fd %d %s", target, strerror(errno));    
  }

  return 0;
}

static int restoreFd(int src, int target) {
  if(src == -1 || src == target)
    /* Do nothing... */
    return 0;
  if (dup2 (src, target) < 0)
    udpc_fatal(1, "restore child_fd %d %s", target, strerror(errno));  
  close(src);
  return 0;
}

int open2(int in, int out, char **arg, int closeFd) {
  int parent_in=-1;
  int parent_out=-1;
  int child;

  dupFd(in, 0, &parent_in);
  dupFd(out, 1, &parent_out);

  child = _spawnvp(P_NOWAIT, arg[0], (const char* const*)&arg[1]);
 
  restoreFd(parent_in, 0);
  restoreFd(parent_out, 1);
  return child;
}


#endif /* __MINGW32__ */