BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file ncdudevmonitor_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 <string.h> |
||
31 | |||
32 | #include <system/BTime.h> |
||
33 | #include <base/BLog.h> |
||
34 | #include <system/BReactor.h> |
||
35 | #include <system/BSignal.h> |
||
36 | #include <system/BProcess.h> |
||
37 | #include <system/BNetwork.h> |
||
38 | #include <udevmonitor/NCDUdevMonitor.h> |
||
39 | |||
40 | BReactor reactor; |
||
41 | BProcessManager manager; |
||
42 | NCDUdevMonitor monitor; |
||
43 | |||
44 | static void signal_handler (void *user); |
||
45 | static void monitor_handler_event (void *unused); |
||
46 | static void monitor_handler_error (void *unused, int is_error); |
||
47 | |||
48 | int main (int argc, char **argv) |
||
49 | { |
||
50 | int ret = 1; |
||
51 | |||
52 | if (argc < 2 || (strcmp(argv[1], "monitor_udev") && strcmp(argv[1], "monitor_kernel") && strcmp(argv[1], "info"))) { |
||
53 | fprintf(stderr, "Usage: %s <monitor_udev/monitor_kernel/info>\n", (argc > 0 ? argv[0] : NULL)); |
||
54 | goto fail0; |
||
55 | } |
||
56 | |||
57 | int mode; |
||
58 | if (!strcmp(argv[1], "monitor_udev")) { |
||
59 | mode = NCDUDEVMONITOR_MODE_MONITOR_UDEV; |
||
60 | } else if (!strcmp(argv[1], "monitor_kernel")) { |
||
61 | mode = NCDUDEVMONITOR_MODE_MONITOR_KERNEL; |
||
62 | } else { |
||
63 | mode = NCDUDEVMONITOR_MODE_INFO; |
||
64 | } |
||
65 | |||
66 | if (!BNetwork_GlobalInit()) { |
||
67 | DEBUG("BNetwork_GlobalInit failed"); |
||
68 | goto fail0; |
||
69 | } |
||
70 | |||
71 | BTime_Init(); |
||
72 | |||
73 | BLog_InitStdout(); |
||
74 | |||
75 | if (!BReactor_Init(&reactor)) { |
||
76 | DEBUG("BReactor_Init failed"); |
||
77 | goto fail1; |
||
78 | } |
||
79 | |||
80 | if (!BSignal_Init(&reactor, signal_handler, NULL)) { |
||
81 | DEBUG("BSignal_Init failed"); |
||
82 | goto fail2; |
||
83 | } |
||
84 | |||
85 | if (!BProcessManager_Init(&manager, &reactor)) { |
||
86 | DEBUG("BProcessManager_Init failed"); |
||
87 | goto fail3; |
||
88 | } |
||
89 | |||
90 | if (!NCDUdevMonitor_Init(&monitor, &reactor, &manager, mode, NULL, |
||
91 | monitor_handler_event, |
||
92 | monitor_handler_error |
||
93 | )) { |
||
94 | DEBUG("NCDUdevMonitor_Init failed"); |
||
95 | goto fail4; |
||
96 | } |
||
97 | |||
98 | ret = BReactor_Exec(&reactor); |
||
99 | |||
100 | NCDUdevMonitor_Free(&monitor); |
||
101 | fail4: |
||
102 | BProcessManager_Free(&manager); |
||
103 | fail3: |
||
104 | BSignal_Finish(); |
||
105 | fail2: |
||
106 | BReactor_Free(&reactor); |
||
107 | fail1: |
||
108 | BLog_Free(); |
||
109 | fail0: |
||
110 | DebugObjectGlobal_Finish(); |
||
111 | |||
112 | return ret; |
||
113 | } |
||
114 | |||
115 | void signal_handler (void *user) |
||
116 | { |
||
117 | DEBUG("termination requested"); |
||
118 | |||
119 | BReactor_Quit(&reactor, 1); |
||
120 | } |
||
121 | |||
122 | void monitor_handler_event (void *unused) |
||
123 | { |
||
124 | // accept event |
||
125 | NCDUdevMonitor_Done(&monitor); |
||
126 | |||
127 | if (NCDUdevMonitor_IsReadyEvent(&monitor)) { |
||
128 | printf("ready\n"); |
||
129 | return; |
||
130 | } |
||
131 | |||
132 | printf("event\n"); |
||
133 | |||
134 | int num_props = NCDUdevMonitor_GetNumProperties(&monitor); |
||
135 | for (int i = 0; i < num_props; i++) { |
||
136 | const char *name; |
||
137 | const char *value; |
||
138 | NCDUdevMonitor_GetProperty(&monitor, i, &name, &value); |
||
139 | printf(" %s=%s\n", name, value); |
||
140 | } |
||
141 | } |
||
142 | |||
143 | void monitor_handler_error (void *unused, int is_error) |
||
144 | { |
||
145 | if (is_error) { |
||
146 | DEBUG("monitor error"); |
||
147 | } else { |
||
148 | DEBUG("monitor finished"); |
||
149 | } |
||
150 | |||
151 | BReactor_Quit(&reactor, (is_error ? 1 : 0)); |
||
152 | } |