c8ae5bcd92953985d778f22a5a664cdc309f600a
[wsti_so.git] / src / bootstrap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3
4 /* strerror, and errno family.. */
5 #include <errno.h>
6
7 /* exec family.. */
8 #include <unistd.h>
9
10 /** Number of processes to spawn */
11 #define PROCESS_NUMBER 3
12
13 char * processes[PROCESS_NUMBER] = {
14         "process1",
15         "process2",
16         "process3"
17 };
18
19 /**
20  * Bootstrap program used for starting all three main processes.
21  */
22 int main(void) {
23         /**
24          * Variable storing Process ID. In our case we need only to check if
25          * it's equal 0, to exec new program.
26          */
27         pid_t pid;
28
29         /** Index of the current process to spawn. */
30         int i = 0;
31
32         /** Error (theoretically) returned by execl. */
33         int err = 0;
34
35         for (; i < PROCESS_NUMBER; i++) {
36                 fprintf(stderr, "[%s] Forking process %d\n", "bootstrap", i);
37                 pid = fork();
38
39                 /* If it is child fork */
40                 if (pid == 0) {
41                         fprintf(stderr, "[%s] Forked process %d has id: %d\n", "bootstrap", i, getpid());
42
43                         /*
44                          * Create new session for this process, so it won't close
45                          * after the parent process exit.
46                          */
47                         setsid();
48                         err = execl(processes[i], NULL);
49
50                         /*
51                          * According to manual, this will only occur
52                          * if there was an error in execl
53                          */
54                         if (err == -1) {
55                                 fprintf(stderr, "[%s] Something went wrong when spawning %s. Error: %s\n",
56                                         "bootstrap", processes[i], strerror(errno));
57                         }
58                 }
59                 /* There was an error when forking */
60                 else if (pid < 0) {
61                         fprintf(stderr, "[%s] Something went wrong when forking %s. Error: %s\n",
62                                 "bootstrap", processes[i], strerror(errno));
63                 }
64         }
65
66         /* All processes should be now spawned. Close bootstrap program. */
67
68         return 0;
69 }