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