SURFEX v8.1
General documentation of Surfex
spawn.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include <stdio.h>
4 #include <errno.h>
5 #include <sys/wait.h>
6 #include <sys/types.h>
7 #include <unistd.h>
8 
9 void spawn_ (const char * cmd, int * kret, const unsigned long cmd_len)
10 {
11  char _cmd[cmd_len+1];
12  pid_t pid;
13 
14  *kret = 0;
15 
16 
17  memset (_cmd, 0, sizeof (_cmd));
18  strncpy (_cmd, cmd, cmd_len);
19 
20  fprintf (stderr, "SPAWN: %s\n", _cmd);
21 
22  /* We use vfork because it requires
23  * less memory than plain fork */
24 
25  errno = 0;
26 
27 #ifdef LINUX
28  pid = vfork ();
29 
30  if (pid < 0)
31  {
32  perror ("SPAWN");
33  goto error;
34  }
35  else if (pid > 0)
36  {
37  int status;
38  waitpid (pid, &status, 0);
39  if (! WIFEXITED (status))
40  goto error;
41  if (WEXITSTATUS (status))
42  goto error;
43  }
44  else
45  {
46  execl (_cmd, _cmd, (char *)NULL);
47  _exit (1);
48  }
49 #else
50 
51  if (system (_cmd) != 0)
52  goto error;
53 
54 #endif
55 
56  return;
57 
58 error:
59 
60  *kret = 1;
61 
62  return;
63 }
64 
65 
66 
67 
void spawn_(const char *cmd, int *kret, const unsigned long cmd_len)
Definition: spawn.c:9
pid_t pid
Definition: opfla_perfmon.c:22