BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file bprocess_example.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stddef.h>
31 #include <unistd.h>
32  
33 #include <misc/debug.h>
34 #include <base/DebugObject.h>
35 #include <base/BLog.h>
36 #include <system/BReactor.h>
37 #include <system/BUnixSignal.h>
38 #include <system/BTime.h>
39 #include <system/BProcess.h>
40  
41 BReactor reactor;
42 BUnixSignal unixsignal;
43 BProcessManager manager;
44 BProcess process;
45  
46 static void unixsignal_handler (void *user, int signo);
47 static void process_handler (void *user, int normally, uint8_t normally_exit_status);
48  
49 int main (int argc, char **argv)
50 {
51 if (argc <= 0) {
52 return 1;
53 }
54  
55 int ret = 1;
56  
57 if (argc < 2) {
58 printf("Usage: %s <program> [argument ...]\n", argv[0]);
59 goto fail0;
60 }
61  
62 char *program = argv[1];
63  
64 // init time
65 BTime_Init();
66  
67 // init logger
68 BLog_InitStdout();
69  
70 // init reactor (event loop)
71 if (!BReactor_Init(&reactor)) {
72 DEBUG("BReactor_Init failed");
73 goto fail1;
74 }
75  
76 // choose signals to catch
77 sigset_t set;
78 sigemptyset(&set);
79 sigaddset(&set, SIGINT);
80 sigaddset(&set, SIGTERM);
81  
82 // init BUnixSignal for catching signals
83 if (!BUnixSignal_Init(&unixsignal, &reactor, set, unixsignal_handler, NULL)) {
84 DEBUG("BUnixSignal_Init failed");
85 goto fail2;
86 }
87  
88 // init process manager
89 if (!BProcessManager_Init(&manager, &reactor)) {
90 DEBUG("BProcessManager_Init failed");
91 goto fail3;
92 }
93  
94 char **p_argv = argv + 1;
95  
96 // map fds 0, 1, 2 in child to fds 0, 1, 2 in parent
97 int fds[] = { 0, 1, 2, -1 };
98 int fds_map[] = { 0, 1, 2 };
99  
100 // start child process
101 if (!BProcess_InitWithFds(&process, &manager, process_handler, NULL, program, p_argv, NULL, fds, fds_map)) {
102 DEBUG("BProcess_Init failed");
103 goto fail4;
104 }
105  
106 // enter event loop
107 ret = BReactor_Exec(&reactor);
108  
109 BProcess_Free(&process);
110 fail4:
111 BProcessManager_Free(&manager);
112 fail3:
113 BUnixSignal_Free(&unixsignal, 0);
114 fail2:
115 BReactor_Free(&reactor);
116 fail1:
117 BLog_Free();
118 fail0:
119 DebugObjectGlobal_Finish();
120  
121 return ret;
122 }
123  
124 void unixsignal_handler (void *user, int signo)
125 {
126 DEBUG("received %s, terminating child", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
127  
128 // send SIGTERM to child
129 BProcess_Terminate(&process);
130 }
131  
132 void process_handler (void *user, int normally, uint8_t normally_exit_status)
133 {
134 DEBUG("process terminated");
135  
136 int ret = (normally ? normally_exit_status : 1);
137  
138 // return from event loop
139 BReactor_Quit(&reactor, ret);
140 }