1 | /* |
---|
2 | * Copyright (c) 2018 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * embedded brains GmbH |
---|
5 | * Dornierstr. 4 |
---|
6 | * 82178 Puchheim |
---|
7 | * Germany |
---|
8 | * <rtems@embedded-brains.de> |
---|
9 | * |
---|
10 | * Redistribution and use in source and binary forms, with or without |
---|
11 | * modification, are permitted provided that the following conditions |
---|
12 | * are met: |
---|
13 | * 1. Redistributions of source code must retain the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer. |
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
16 | * notice, this list of conditions and the following disclaimer in the |
---|
17 | * documentation and/or other materials provided with the distribution. |
---|
18 | * |
---|
19 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
---|
20 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
21 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
22 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
---|
23 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
24 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
25 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
26 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
27 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
28 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
29 | * SUCH DAMAGE. |
---|
30 | */ |
---|
31 | |
---|
32 | #include <rtems.h> |
---|
33 | #include <rtems/thread.h> |
---|
34 | #include <rtems/netcmds-config.h> |
---|
35 | #include <machine/rtems-bsd-commands.h> |
---|
36 | |
---|
37 | #include <string.h> |
---|
38 | #include <stdlib.h> |
---|
39 | #include <syslog.h> |
---|
40 | |
---|
41 | struct argc_argv { |
---|
42 | size_t argc; |
---|
43 | char **argv; |
---|
44 | }; |
---|
45 | |
---|
46 | static bool racoon_initialized = false; |
---|
47 | rtems_recursive_mutex racoon_mutex = |
---|
48 | RTEMS_RECURSIVE_MUTEX_INITIALIZER("racoon"); |
---|
49 | |
---|
50 | static void |
---|
51 | clean_up_args(struct argc_argv *args) |
---|
52 | { |
---|
53 | if (args != NULL) { |
---|
54 | if (args->argv != NULL) { |
---|
55 | for (int i = 0; i < args->argc; ++i) { |
---|
56 | if (args->argv[i] != NULL) { |
---|
57 | free(args->argv[i]); |
---|
58 | } |
---|
59 | } |
---|
60 | free(args->argv); |
---|
61 | } |
---|
62 | free(args); |
---|
63 | } |
---|
64 | } |
---|
65 | |
---|
66 | static void |
---|
67 | racoon_task(rtems_task_argument arg) |
---|
68 | { |
---|
69 | int exit_code; |
---|
70 | struct argc_argv *args = (struct argc_argv *) arg; |
---|
71 | |
---|
72 | exit_code = rtems_bsd_command_racoon(args->argc, args->argv); |
---|
73 | if (exit_code != EXIT_SUCCESS) { |
---|
74 | syslog(LOG_ERR, "racoon exited with %d\n", exit_code); |
---|
75 | } |
---|
76 | |
---|
77 | clean_up_args(args); |
---|
78 | rtems_task_delete(RTEMS_SELF); |
---|
79 | } |
---|
80 | |
---|
81 | rtems_status_code |
---|
82 | rtems_bsd_racoon_daemon(int argc, const char **argv, rtems_task_priority prio) |
---|
83 | { |
---|
84 | rtems_id id; |
---|
85 | rtems_status_code sc = RTEMS_SUCCESSFUL; |
---|
86 | struct argc_argv *args = NULL; |
---|
87 | |
---|
88 | rtems_recursive_mutex_lock(&racoon_mutex); |
---|
89 | |
---|
90 | if (racoon_initialized) { |
---|
91 | sc = RTEMS_INCORRECT_STATE; |
---|
92 | } |
---|
93 | |
---|
94 | if (sc == RTEMS_SUCCESSFUL) { |
---|
95 | args = malloc(sizeof(struct argc_argv)); |
---|
96 | if (args == NULL) { |
---|
97 | sc = RTEMS_NO_MEMORY; |
---|
98 | } |
---|
99 | } |
---|
100 | |
---|
101 | if (sc == RTEMS_SUCCESSFUL) { |
---|
102 | args->argc = argc; |
---|
103 | args->argv = calloc(argc + 1, sizeof(char*)); |
---|
104 | if (args->argv == NULL) { |
---|
105 | sc = RTEMS_NO_MEMORY; |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | if (sc == RTEMS_SUCCESSFUL) { |
---|
110 | for (int i = 0; i < argc; ++i) { |
---|
111 | args->argv[i] = strdup(argv[i]); |
---|
112 | if (args->argv[i] == NULL) { |
---|
113 | sc = RTEMS_NO_MEMORY; |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | if (sc == RTEMS_SUCCESSFUL) { |
---|
119 | sc = rtems_task_create( |
---|
120 | rtems_build_name('R', 'A', 'C', 'N'), |
---|
121 | prio, |
---|
122 | 32 * 1024, |
---|
123 | RTEMS_DEFAULT_MODES, |
---|
124 | RTEMS_FLOATING_POINT, |
---|
125 | &id |
---|
126 | ); |
---|
127 | } |
---|
128 | |
---|
129 | if (sc == RTEMS_SUCCESSFUL) { |
---|
130 | sc = rtems_task_start(id, racoon_task, |
---|
131 | (rtems_task_argument)args); |
---|
132 | } |
---|
133 | |
---|
134 | if (sc == RTEMS_SUCCESSFUL) { |
---|
135 | racoon_initialized = true; |
---|
136 | } else { |
---|
137 | clean_up_args(args); |
---|
138 | } |
---|
139 | |
---|
140 | rtems_recursive_mutex_unlock(&racoon_mutex); |
---|
141 | |
---|
142 | return (sc); |
---|
143 | } |
---|