BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file ncdudevmanager_test.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 <stdlib.h>
31 #include <string.h>
32 #include <stdio.h>
33  
34 #include <misc/debug.h>
35 #include <system/BTime.h>
36 #include <base/BLog.h>
37 #include <system/BReactor.h>
38 #include <system/BUnixSignal.h>
39 #include <system/BProcess.h>
40 #include <system/BNetwork.h>
41 #include <udevmonitor/NCDUdevManager.h>
42  
43 BReactor reactor;
44 BUnixSignal usignal;
45 BProcessManager manager;
46 NCDUdevManager umanager;
47 NCDUdevClient client;
48  
49 static void signal_handler (void *user, int signo);
50 static void client_handler (void *unused, char *devpath, int have_map, BStringMap map);
51  
52 int main (int argc, char **argv)
53 {
54 if (!(argc == 1 || (argc == 2 && !strcmp(argv[1], "--no-udev")))) {
55 fprintf(stderr, "Usage: %s [--no-udev]\n", (argc > 0 ? argv[0] : NULL));
56 goto fail0;
57 }
58  
59 int no_udev = (argc == 2);
60  
61 if (!BNetwork_GlobalInit()) {
62 DEBUG("BNetwork_GlobalInit failed");
63 goto fail0;
64 }
65  
66 BTime_Init();
67  
68 BLog_InitStdout();
69  
70 if (!BReactor_Init(&reactor)) {
71 DEBUG("BReactor_Init failed");
72 goto fail1;
73 }
74  
75 sigset_t set;
76 sigemptyset(&set);
77 sigaddset(&set, SIGINT);
78 sigaddset(&set, SIGTERM);
79 sigaddset(&set, SIGHUP);
80 if (!BUnixSignal_Init(&usignal, &reactor, set, signal_handler, NULL)) {
81 fprintf(stderr, "BUnixSignal_Init failed\n");
82 goto fail2;
83 }
84  
85 if (!BProcessManager_Init(&manager, &reactor)) {
86 DEBUG("BProcessManager_Init failed");
87 goto fail3;
88 }
89  
90 NCDUdevManager_Init(&umanager, no_udev, &reactor, &manager);
91  
92 NCDUdevClient_Init(&client, &umanager, NULL, client_handler);
93  
94 BReactor_Exec(&reactor);
95  
96 NCDUdevClient_Free(&client);
97  
98 NCDUdevManager_Free(&umanager);
99  
100 BProcessManager_Free(&manager);
101 fail3:
102 BUnixSignal_Free(&usignal, 0);
103 fail2:
104 BReactor_Free(&reactor);
105 fail1:
106 BLog_Free();
107 fail0:
108 DebugObjectGlobal_Finish();
109  
110 return 1;
111 }
112  
113 static void signal_handler (void *user, int signo)
114 {
115 if (signo == SIGHUP) {
116 fprintf(stderr, "received SIGHUP, restarting client\n");
117  
118 NCDUdevClient_Free(&client);
119 NCDUdevClient_Init(&client, &umanager, NULL, client_handler);
120 } else {
121 fprintf(stderr, "received %s, exiting\n", (signo == SIGINT ? "SIGINT" : "SIGTERM"));
122  
123 // exit event loop
124 BReactor_Quit(&reactor, 1);
125 }
126 }
127  
128 void client_handler (void *unused, char *devpath, int have_map, BStringMap map)
129 {
130 printf("event %s\n", devpath);
131  
132 if (!have_map) {
133 printf(" no map\n");
134 } else {
135 printf(" map:\n");
136  
137 const char *name = BStringMap_First(&map);
138 while (name) {
139 printf(" %s=%s\n", name, BStringMap_Get(&map, name));
140 name = BStringMap_Next(&map, name);
141 }
142 }
143  
144 const BStringMap *cache_map = NCDUdevManager_Query(&umanager, devpath);
145 if (!cache_map) {
146 printf(" no cache\n");
147 } else {
148 printf(" cache:\n");
149  
150 const char *name = BStringMap_First(cache_map);
151 while (name) {
152 printf(" %s=%s\n", name, BStringMap_Get(cache_map, name));
153 name = BStringMap_Next(cache_map, name);
154 }
155 }
156  
157 if (have_map) {
158 BStringMap_Free(&map);
159 }
160 free(devpath);
161 }