-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathf5gs.c
257 lines (247 loc) · 7.86 KB
/
f5gs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* This is F5 Graceful Scaling helper daemon.
*
* The f5gs has BSD 2-clause license which also known as "Simplified
* BSD License" or "FreeBSD License".
*
* Copyright 2013- Sami Kerola. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the
* distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR AND CONTRIBUTORS OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing
* official policies, either expressed or implied, of Sami Kerola.
*/
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "close-stream.h"
#include "closeout.h"
#include "progname.h"
#include "f5gs.h"
/* keep functions in the order that allows skipping the function
* definition lines */
static void __attribute__((__noreturn__))
usage(FILE *restrict out)
{
fputs("\nUsage:\n", out);
fprintf(out, " %s [options]\n", program_invocation_short_name);
fputs("\nOptions:\n", out);
fputs(" -e, --enable set enable status\n", out);
fputs(" -m, --maintenance set maintenance status\n", out);
fputs(" -d, --disable set disable status\n", out);
fputs("\n", out);
fputs(" -s, --server start up daemon\n", out);
fputs(" -a, --address <addr> address (ip or name) daemon will listen\n", out);
fprintf(out, " -p, --port <port> daemon tcp port (default: %s)\n", F5GS_TCP_PORT);
fprintf(out, " --statedir <dir> status dir path (default: %s)\n", F5GS_RUNDIR);
fputs(" --foreground run daemon in foreground\n", out);
fputs(" -q, --quiet do not print status, use exit values\n", out);
fputs(" --reason <text> add explanation to status change\n", out);
fputs(" --why query reason, and when status was changed\n", out);
fputs(" --force run pre and post script and ignore return values\n", out);
fputs(" --no-scripts do not run pre or post status change scripts\n", out);
fputs("\n", out);
fputs(" -h, --help display this help and exit\n", out);
fputs(" -V, --version output version information and exit\n", out);
fputs("\n", out);
fprintf(out, "For more details see %s(8).\n", PACKAGE_NAME);
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
}
static char *construct_pid_file(struct runtime_config *restrict rtc)
{
char *path;
void *p;
char s[INET6_ADDRSTRLEN];
int separator = 0, ret;
const char *last_slash = strrchr(rtc->state_dir, '/');
if (last_slash && *(last_slash + 1) != '\0' && *(last_slash + 1) != '/')
separator = 1;
else if (!last_slash)
separator = 1;
if (inet_ntop(rtc->res->ai_family, rtc->res->ai_addr->sa_data, s, sizeof(s)) == NULL)
err(EXIT_FAILURE, "inet_ntop failed");
switch (rtc->res->ai_family) {
case AF_INET:
p = &((struct sockaddr_in *)rtc->res->ai_addr)->sin_addr;
break;
case AF_INET6:
p = &((struct sockaddr_in6 *)rtc->res->ai_addr)->sin6_addr;
break;
default:
abort();
}
if (inet_ntop(rtc->res->ai_family, p, s, sizeof(s)) == NULL)
err(EXIT_FAILURE, "inet_ntop failed");
ret = asprintf(&path, "%s%s%s:%d", rtc->state_dir, separator ? "/" : "", s,
ntohs(((struct sockaddr_in *)(rtc->res->ai_addr))->sin_port));
if (ret < 0)
err(EXIT_FAILURE, "asprintf failed");
ret =
asprintf(&rtc->mq_name, "/%s_%s:%d", PACKAGE_NAME, s,
ntohs(((struct sockaddr_in *)(rtc->res->ai_addr))->sin_port));
if (ret < 0)
err(EXIT_FAILURE, "asprintf failed");
return path;
}
int main(const int argc, char **argv)
{
static struct runtime_config rtc = {
.state_dir = F5GS_RUNDIR,
.new_state = STATE_UNKNOWN,
0
};
int c, server = 0, retval = EXIT_SUCCESS;
const char *address = NULL, *port = F5GS_TCP_PORT;
struct addrinfo hints = {
.ai_family = AF_UNSPEC,
.ai_socktype = SOCK_STREAM,
.ai_flags = AI_PASSIVE
};
enum {
STATEDIR_OPT = CHAR_MAX + 1,
REASON_OPT,
WHY_OPT,
FORCE_OPT,
NO_SCRIPTS_OPT,
FOREGROUND_OPT
};
static const struct option longopts[] = {
{"disable", no_argument, NULL, 'd'},
{"maintenance", no_argument, NULL, 'm'},
{"enable", no_argument, NULL, 'e'},
{"server", no_argument, NULL, 's'},
{"address", required_argument, NULL, 'a'},
{"port", required_argument, NULL, 'p'},
{"statedir", required_argument, NULL, STATEDIR_OPT},
{"quiet", no_argument, NULL, 'q'},
{"reason", required_argument, NULL, REASON_OPT},
{"why", no_argument, NULL, WHY_OPT},
{"force", no_argument, NULL, NO_SCRIPTS_OPT},
{"no-scripts", no_argument, NULL, NO_SCRIPTS_OPT},
{"foreground", no_argument, NULL, FOREGROUND_OPT},
{"version", no_argument, NULL, 'V'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
set_program_name(argv[0]);
atexit(close_stdout);
rtc.argv = argv;
while ((c = getopt_long(argc, argv, "dmesa:p:qVh", longopts, NULL)) != -1) {
switch (c) {
case 'd':
rtc.new_state = STATE_DISABLE;
break;
case 'm':
rtc.new_state = STATE_MAINTENANCE;
break;
case 'e':
rtc.new_state = STATE_ENABLE;
break;
case 's':
server = 1;
break;
case 'a':
address = optarg;
break;
case 'p':
port = optarg;
break;
case STATEDIR_OPT:
rtc.state_dir = optarg;
break;
case 'q':
rtc.quiet = 1;
break;
case REASON_OPT:
if (REASON_TEXT <= strnlen(optarg, REASON_TEXT)) {
warnx("too long reason, truncating to %d characters", REASON_TEXT - 1);
optarg[REASON_TEXT - 1] = '\0';
}
rtc.new_reason = optarg;
break;
case WHY_OPT:
rtc.why = 1;
break;
case FORCE_OPT:
rtc.force = 1;
break;
case NO_SCRIPTS_OPT:
rtc.no_scripts = 1;
break;
case FOREGROUND_OPT:
rtc.run_foreground = 1;
break;
case 'V':
printf("%s version %s", PACKAGE_NAME, PACKAGE_VERSION);
#ifdef HAVE_LIBSYSTEMD
puts(" with systemd support");
#else
putc('\n', stdout);
#endif
return EXIT_SUCCESS;
case 'h':
usage(stdout);
default:
usage(stderr);
}
}
if (0 < argc - optind)
errx(EXIT_FAILURE, "too many arguments");
c = getaddrinfo(address, port, &hints, &rtc.res);
if (c) {
if (rtc.quiet)
exit(STATE_UNKNOWN);
else
errx(EXIT_FAILURE, "getaddrinfo: %s port %s: %s", address, port, gai_strerror(c));
}
rtc.pid_file = construct_pid_file(&rtc);
/* run server */
if (server) {
start_server(&rtc);
return EXIT_SUCCESS;
}
/* change server state */
if (rtc.new_state != STATE_UNKNOWN)
retval = set_server_status(&rtc);
/* request server state */
if (rtc.quiet && retval == EXIT_SUCCESS)
retval = get_quiet_server_status(&rtc);
else {
if (address)
printf("%s: ", address);
printf("current status is: %s\n", get_server_status(&rtc));
}
/* clean up and exit */
freeaddrinfo(rtc.res);
free(rtc.pid_file);
return retval;
}