Debian Codegraph @

src/socket-activate.c

raw symbols SCIP
1 /*
2 * standalone-socket-activate
3 *
4 * Copyright (c) 2019 Peter Pentchev
5 * Copyright (c) 2023- Mark Hindley
6 *
7 * License: GPL-3.0+
8 *
9 * This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include <sys/types.h>
24 #include <sys/socket.h>
25 #include <sys/stat.h>
26 #include <sys/un.h>
27
28 #include <err.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <inttypes.h>
32 #include <limits.h>
33 #include <netdb.h>
34 #include <poll.h>
35 #include <stdarg.h>
36 #include <stdbool.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
41
42 #include <arpa/inet.h>
43 #include <netinet/in.h>
44
45 #define SD_LISTEN_FDS_STARTcxx . . $ `src/socket-activate.c:45:9`! 3
46
47 #ifndef __printflike
48 #if defined(__GNUC__cxx . . $ `<built-in>:8:9`!) && __GNUC__cxx . . $ `<built-in>:8:9`! >= 3
49 #define __printflikecxx . . $ `src/socket-activate.c:49:9`!(x, y) __attribute__((format(printf, (x), (y))))
50 #define __unusedcxx . . $ `src/socket-activate.c:50:9`! __attribute__((unused))
51 #else
52 #define __printflike(x, y)
53 #define __unused
54 #endif
55 #endif
56
57 #define VERSION_STRINGcxx . . $ `src/socket-activate.c:57:9`! "0.4unreleased"
58
59 enum option_id_tcxx . . $ option_id_t# {
60 OPT_LABELcxx . . $ OPT_LABEL.,
61 OPT_MODEcxx . . $ OPT_MODE.,
62 OPT_USERcxx . . $ OPT_USER.,
63 OPT_GROUPcxx . . $ OPT_GROUP.,
64 OPT_BACKLOGcxx . . $ OPT_BACKLOG.,
65 OPTIONS_COUNTcxx . . $ OPTIONS_COUNT.
66 };
67
68 struct option_tcxx . . $ option_t# {
69 const char *valuecxx . . $ option_t#value.;
70 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! handledcxx . . $ option_t#handled.;
71 };
72
73 static const char * const option_namescxx . . $ option_names.[OPTIONS_COUNTcxx . . $ OPTIONS_COUNT.] = {
74 "label",
75 "mode",
76 "user",
77 "group",
78 "backlog",
79 };
80
81 static boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! verbosecxx . . $ verbose.;
82
83 static void
84 usagecxx . . $ usage(b45ddcbc8423892c).(const boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! _ferr)
85 {
86 const char * const s =
87 "Usage:\tsocket-activate [-v | --verbose] --family:[options,...]:address... [--] program [arg...]\n"
88 "\tsocket-activate -V | -h | --version | --help\n"
89 "\tsocket-activate --features\n"
90 "\n"
91 "\t-h\tdisplay program usage information and exit\n"
92 "\t-V\tdisplay program version information and exit\n"
93 "\t-v\tverbose operation; display diagnostic output\n";
94
95 fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(_ferr? stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!: stdoutcxx . . $ `<external>/232824719582829767/stdio.h:154:9`!, "%s", s);
96 if (_ferr)
97 exitcxx . . $ exit(3ca292c338df9bb3).(1);
98 }
99
100 static void
101 versioncxx . . $ version(49f6e7a06ebc5aa8).(void)
102 {
103 putscxx . . $ puts(5c4306d8553b3696).("socket-activate " VERSION_STRINGcxx . . $ `src/socket-activate.c:57:9`!);
104 }
105
106 static void
107 featurescxx . . $ features(49f6e7a06ebc5aa8).(void)
108 {
109 putscxx . . $ puts(5c4306d8553b3696).("Features: socket-activate=" VERSION_STRINGcxx . . $ `src/socket-activate.c:57:9`!);
110 }
111
112 __printflikecxx . . $ `src/socket-activate.c:49:9`!(1, 2)
113 static void
114 debugcxx . . $ debug(326aeb464d16410c).(const char * const fmt, ...)
115 {
116 va_listcxx . . $ va_list# v;
117
118 va_startcxx . . $ `<external>/8105065126723003039/stdarg.h:50:9`!(v, fmt);
119 if (verbosecxx . . $ verbose.)
120 vfprintfcxx . . $ vfprintf(bded311e54d67f9c).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!, fmt, v);
121 va_endcxx . . $ `<external>/8105065126723003039/stdarg.h:52:9`!(v);
122 }
123
124 static long
125 strtolong_fatalcxx . . $ strtolong_fatal(71083d4ee4d9cb41).(const char * const s, const int base)
126 {
127 errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! = 0;
128 char *endp;
129 const long value = strtolcxx . . $ strtol(44bbb87609b212fc).(s, &endp, base);
130 if (*endp != '\0' ||
131 (errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! == ERANGEcxx . . $ `<external>/8987895939074593727/errno-base.h:38:9`! && (value == LONG_MINcxx . . $ `<external>/67164224788392828/limits.h:129:9`! || value == LONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:131:9`!)))
132 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Invalid base %d numeric value '%s'", base, s);
133 return value;
134 }
135
136 static int
137 connect_unixcxx . . $ connect_unix(deec2d11b0702941).(const int socktype, struct option_tcxx . . $ option_t# * const options,
138 char * const address)
139 {
140 const int sock = socketcxx . . $ socket(2326284602b0bb2d).(AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!, socktype, 0);
141 if (sock == -1)
142 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not create a Unix-domain socket");
143
144 mode_tcxx . . $ mode_t# oldmask = 0;
145 long mode = 0;
146 struct sockaddr_uncxx . . $ sockaddr_un# addr = { 0 };
147 addr.sun_familycxx . . $ sockaddr_un#sun_family. = AF_UNIXcxx . . $ `<external>/8962547315054503424/socket.h:95:9`!;
148 const int written = snprintfcxx . . $ snprintf(ea502d73e74c754c).(addr.sun_pathcxx . . $ sockaddr_un#sun_path., sizeof(addr.sun_pathcxx . . $ sockaddr_un#sun_path.),
149 "%s", address);
150 if (written < 0 || (size_t)written >= sizeof(addr.sun_pathcxx . . $ sockaddr_un#sun_path.))
151 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Path too long for a Unix-domain socket");
152 if (unlinkcxx . . $ unlink(5c4306d8553b3696).(address) == -1 && errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`! != ENOENTcxx . . $ `<external>/8987895939074593727/errno-base.h:6:9`!)
153 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Failed to remove existing Unix-domain socket %s", address);
154 /* Set umask for bind */
155 if (options[OPT_MODEcxx . . $ OPT_MODE.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
156 options[OPT_MODEcxx . . $ OPT_MODE.].handledcxx . . $ option_t#handled. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
157 mode = strtolong_fatalcxx . . $ strtolong_fatal(71083d4ee4d9cb41).(options[OPT_MODEcxx . . $ OPT_MODE.].valuecxx . . $ option_t#value., 8);
158 if (mode < 0)
159 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Invalid access mode '%s' for '%s'",
160 options[OPT_MODEcxx . . $ OPT_MODE.].valuecxx . . $ option_t#value., address);
161 oldmask = umaskcxx . . $ umask(2141b656ce2cc063).(~mode);
162 }
163 if (bindcxx . . $ bind(f39c6c650450bfdd).(sock, (const struct sockaddrcxx . . $ sockaddr# *)&addr, sizeof(addr)) == -1)
164 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not bind the Unix-domain socket to %s", address);
165 if (options[OPT_MODEcxx . . $ OPT_MODE.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
166 umaskcxx . . $ umask(2141b656ce2cc063).(oldmask);
167
168 uid_tcxx . . $ uid_t# uid = 0;
169 gid_tcxx . . $ gid_t# gid = 0;
170 if (options[OPT_USERcxx . . $ OPT_USER.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
171 options[OPT_USERcxx . . $ OPT_USER.].handledcxx . . $ option_t#handled. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
172 const long luid = strtolong_fatalcxx . . $ strtolong_fatal(71083d4ee4d9cb41).(options[OPT_USERcxx . . $ OPT_USER.].valuecxx . . $ option_t#value., 10);
173 if (luid < 0)
174 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "The user id must be non-negative");
175 uid = (uid_tcxx . . $ uid_t#)luid;
176
177 if (options[OPT_GROUPcxx . . $ OPT_GROUP.].valuecxx . . $ option_t#value. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
178 gid = getgidcxx . . $ getgid(db52760e79bf9054).();
179 }
180 if (options[OPT_GROUPcxx . . $ OPT_GROUP.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
181 options[OPT_GROUPcxx . . $ OPT_GROUP.].handledcxx . . $ option_t#handled. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
182 const long lgid = strtolong_fatalcxx . . $ strtolong_fatal(71083d4ee4d9cb41).(options[OPT_GROUPcxx . . $ OPT_GROUP.].valuecxx . . $ option_t#value., 10);
183 if (lgid < 0)
184 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "The group id must be non-negative");
185 gid = (gid_tcxx . . $ gid_t#)lgid;
186
187 if (options[OPT_USERcxx . . $ OPT_USER.].valuecxx . . $ option_t#value. == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
188 uid = getuidcxx . . $ getuid(db52760e79bf9054).();
189 }
190 if (options[OPT_USERcxx . . $ OPT_USER.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ||
191 options[OPT_GROUPcxx . . $ OPT_GROUP.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
192 if (chowncxx . . $ chown(a017fb26b85e77bb).(address, uid, gid) == -1)
193 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not set the Unix socket ownership to "
194 "%jd:%jd", (intmax_tcxx . . $ intmax_t#)uid, (intmax_tcxx . . $ intmax_t#)gid);
195
196 return sock;
197 }
198
199 static int
200 connect_inetcxx . . $ connect_inet(deec2d11b0702941).(const int socktype, struct option_tcxx . . $ option_t# * const options __unusedcxx . . $ `src/socket-activate.c:50:9`!,
201 char * const address)
202 {
203 const int proto = socktype == SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`! ? IPPROTO_TCPcxx . . $ `<external>/9581057695539925927/in.h:51:9`! : IPPROTO_UDPcxx . . $ `<external>/9581057695539925927/in.h:57:9`!;
204 char *next = address;
205 const char * const first = strsepcxx . . $ strsep(f4782bac5b8a260e).(&next, "/");
206 const char * const addrstr = next == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? "0.0.0.0" : first;
207 const char * const portstr = next == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? first : next;
208 struct addrinfocxx . . $ addrinfo# hints = {
209 .ai_family = next == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`! : AF_UNSPECcxx . . $ `<external>/8962547315054503424/socket.h:93:9`!,
210 .ai_socktype = socktype,
211 .ai_protocol = proto,
212 .ai_flags = AI_NUMERICHOSTcxx . . $ `<external>/564697342961150458/netdb.h:598:10`! | AI_NUMERICSERVcxx . . $ `<external>/564697342961150458/netdb.h:613:10`! | AI_PASSIVEcxx . . $ `<external>/564697342961150458/netdb.h:596:10`!,
213 };
214 struct addrinfocxx . . $ addrinfo# *res;
215 const int ret = getaddrinfocxx . . $ getaddrinfo(f68af21b9849b03e).(addrstr, portstr, &hints, &res);
216 if (ret != 0)
217 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Could not parse the %s address: %s",
218 address, gai_strerrorcxx . . $ gai_strerror(f7ae7e56280f18ae).(ret));
219 if (res == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
220 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Could not parse the %s address", address);
221
222 if (verbosecxx . . $ verbose.) {
223 char buf[200];
224 short port;
225 const void *vaddr;
226 if (res->ai_familycxx . . $ addrinfo#ai_family. == AF_INETcxx . . $ `<external>/8962547315054503424/socket.h:97:9`!) {
227 const struct sockaddr_incxx . . $ sockaddr_in# * const iaddr =
228 (const struct sockaddr_incxx . . $ sockaddr_in# *)res->ai_addrcxx . . $ addrinfo#ai_addr.;
229 vaddr = &iaddr->sin_addrcxx . . $ sockaddr_in#sin_addr.;
230 port = iaddr->sin_portcxx . . $ sockaddr_in#sin_port.;
231 } else {
232 const struct sockaddr_in6cxx . . $ sockaddr_in6# * const iaddr =
233 (const struct sockaddr_in6cxx . . $ sockaddr_in6# *)res->ai_addrcxx . . $ addrinfo#ai_addr.;
234 vaddr = &iaddr->sin6_addrcxx . . $ sockaddr_in6#sin6_addr.;
235 port = iaddr->sin6_portcxx . . $ sockaddr_in6#sin6_port.;
236 }
237 debugcxx . . $ debug(326aeb464d16410c).("got family %d type %d proto %d len %d "
238 "address %s port %d\n",
239 res->ai_familycxx . . $ addrinfo#ai_family., res->ai_socktypecxx . . $ addrinfo#ai_socktype., res->ai_protocolcxx . . $ addrinfo#ai_protocol.,
240 res->ai_addrlencxx . . $ addrinfo#ai_addrlen.,
241 inet_ntopcxx . . $ inet_ntop(6a923972b45bea03).(res->ai_familycxx . . $ addrinfo#ai_family., vaddr, buf, res->ai_addrlencxx . . $ addrinfo#ai_addrlen.),
242 ntohscxx . . $ `<external>/9581057695539925927/in.h:429:12`!(port));
243 }
244
245 const int sock = socketcxx . . $ socket(2326284602b0bb2d).(res->ai_familycxx . . $ addrinfo#ai_family., res->ai_socktypecxx . . $ addrinfo#ai_socktype.,
246 res->ai_protocolcxx . . $ addrinfo#ai_protocol.);
247 if (sock == -1)
248 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not create a %s socket",
249 socktype == SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`! ? "tcp" : "udp");
250 const int optval = 1;
251 if (setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(sock, SOL_SOCKETcxx . . $ `<external>/9030916627833342711/socket.h:9:9`!, SO_REUSEADDRcxx . . $ `<external>/9030916627833342711/socket.h:12:9`!,
252 &optval, sizeof(optval)) == -1
253 #ifndef __gnu_hurd__
254 || setsockoptcxx . . $ setsockopt(506cc1d0fc70fdf1).(sock, SOL_SOCKETcxx . . $ `<external>/9030916627833342711/socket.h:9:9`!, SO_REUSEPORTcxx . . $ `<external>/9030916627833342711/socket.h:27:9`!,
255 &optval, sizeof(optval)) == -1
256 #endif
257 )
258 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not set the socket options for %s:%s",
259 addrstr, portstr);
260 if (bindcxx . . $ bind(f39c6c650450bfdd).(sock, res->ai_addrcxx . . $ addrinfo#ai_addr., res->ai_addrlencxx . . $ addrinfo#ai_addrlen.) == -1)
261 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not bind to %s:%s", addrstr, portstr);
262
263 return sock;
264 }
265
266
267 struct connect_func_tcxx . . $ connect_func_t# {
268 const char * const familycxx . . $ connect_func_t#family.;
269 int (* const funccxx . . $ connect_func_t#func.)(int, struct option_tcxx . . $ option_t# * const options, char *);
270 const int socktypecxx . . $ connect_func_t#socktype.;
271 };
272
273 static const struct connect_func_tcxx . . $ connect_func_t# connect_functionscxx . . $ connect_functions.[] = {
274 {"unix", connect_unixcxx . . $ connect_unix(deec2d11b0702941)., SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!},
275 {"unix-dgram", connect_unixcxx . . $ connect_unix(deec2d11b0702941)., SOCK_DGRAMcxx . . $ `<external>/5956532346368192945/socket_type.h:31:9`!},
276 {"tcp", connect_inetcxx . . $ connect_inet(deec2d11b0702941)., SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!},
277 {"udp", connect_inetcxx . . $ connect_inet(deec2d11b0702941)., SOCK_DGRAMcxx . . $ `<external>/5956532346368192945/socket_type.h:31:9`!},
278 };
279
280 static inline const struct connect_func_tcxx . . $ connect_func_t# *
281 get_connect_functioncxx . . $ get_connect_function(facba5c2d0de081b).(const char * const family)
282 {
283 for (size_t idx = 0; idx < sizeof(connect_functionscxx . . $ connect_functions.) / sizeof(connect_functionscxx . . $ connect_functions.[0]); idx++)
284 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(family, connect_functionscxx . . $ connect_functions.[idx].familycxx . . $ connect_func_t#family.) == 0)
285 return &connect_functionscxx . . $ connect_functions.[idx];
286 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Invalid socket family '%s'", family);
287 }
288
289 static inline enum option_id_tcxx . . $ option_id_t#
290 get_option_idcxx . . $ get_option_id(1a62a4238dfe086a).(const char * const name)
291 {
292 for (size_t idx = 0; idx < (size_t)OPTIONS_COUNTcxx . . $ OPTIONS_COUNT.; idx++)
293 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(option_namescxx . . $ option_names.[idx], name) == 0)
294 return (enum option_id_tcxx . . $ option_id_t#)idx;
295 return OPTIONS_COUNTcxx . . $ OPTIONS_COUNT.;
296 }
297
298 static const char *
299 add_listenercxx . . $ add_listener(223c544933887702).(const char * const optarg, const int nextfd)
300 {
301 char *next = strdupcxx . . $ strdup(a8b8e32347182212).(optarg);
302 if (next == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
303 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not allocate memory");
304
305 const char * const family = strsepcxx . . $ strsep(f4782bac5b8a260e).(&next, ":");
306 const struct connect_func_tcxx . . $ connect_func_t# * const func = get_connect_functioncxx . . $ get_connect_function(facba5c2d0de081b).(family);
307
308 struct option_tcxx . . $ option_t# * const options = calloccxx . . $ calloc(63a7bf2941bfcd7a).(OPTIONS_COUNTcxx . . $ OPTIONS_COUNT., sizeof(*options));
309 char *optstr = strsepcxx . . $ strsep(f4782bac5b8a260e).(&next, ":");
310 char *current;
311 while (current = strsepcxx . . $ strsep(f4782bac5b8a260e).(&optstr, ","), current != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && strlencxx . . $ strlen(751346f8406fd082).(current)) {
312 const char * const name = strsepcxx . . $ strsep(f4782bac5b8a260e).(&current, "=");
313 if (name[0] == '\0')
314 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Empty option name in '%s'", optarg);
315 const enum option_id_tcxx . . $ option_id_t# idx = get_option_idcxx . . $ get_option_id(1a62a4238dfe086a).(name);
316 if (idx == OPTIONS_COUNTcxx . . $ OPTIONS_COUNT.)
317 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Unknown option '%s' in '%s'", name, optarg);
318
319 if (current[0] == '\0')
320 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "No value for option '%s' in '%s'",
321 name, optarg);
322 options[idx].valuecxx . . $ option_t#value. = current;
323 }
324
325 const char * const label = options[OPT_LABELcxx . . $ OPT_LABEL.].valuecxx . . $ option_t#value.;
326 options[OPT_LABELcxx . . $ OPT_LABEL.].handledcxx . . $ option_t#handled. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
327 char * const address = next;
328 const int fd = func->funccxx . . $ connect_func_t#func.(func->socktypecxx . . $ connect_func_t#socktype., options, address);
329 if (fd != nextfd) {
330 debugcxx . . $ debug(326aeb464d16410c).("moving %s fd %d to %d\n", optarg, fd, nextfd);
331 if (dup2cxx . . $ dup2(9b79fb6aee4c0440).(fd, nextfd) == -1)
332 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not move %s fd %d to %d",
333 optarg, fd, nextfd);
334 if (closecxx . . $ close(7864480464b09eea).(fd) == -1)
335 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not close %s fd %d after "
336 "moving it to %d",
337 optarg, fd, nextfd);
338 /* fd now closed, don't use anymore */
339 } else {
340 debugcxx . . $ debug(326aeb464d16410c).("no need to move %s fd %d\n", optarg, fd);
341 }
342 debugcxx . . $ debug(326aeb464d16410c).("fd %d: label %s, family %s, address %s\n",
343 nextfd, label, family, address);
344
345 if (func->socktypecxx . . $ connect_func_t#socktype. == SOCK_STREAMcxx . . $ `<external>/5956532346368192945/socket_type.h:28:9`!) {
346 int backlog = SOMAXCONNcxx . . $ `<external>/8962547315054503424/socket.h:178:9`!;
347 if (options[OPT_BACKLOGcxx . . $ OPT_BACKLOG.].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!) {
348 options[OPT_BACKLOGcxx . . $ OPT_BACKLOG.].handledcxx . . $ option_t#handled. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
349 long value = strtolong_fatalcxx . . $ strtolong_fatal(71083d4ee4d9cb41).(
350 options[OPT_BACKLOGcxx . . $ OPT_BACKLOG.].valuecxx . . $ option_t#value., 10);
351 if (value < 1)
352 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "The listen backlog for %s must be "
353 "a positive number", optarg);
354 #if LONG_MAXcxx . . $ `<external>/67164224788392828/limits.h:131:9`! > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!
355 if (value > INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!)
356 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "The listen backlog for %s must be "
357 "no more than %d", optarg, INT_MAXcxx . . $ `<external>/67164224788392828/limits.h:120:9`!);
358 #endif
359 backlog = value;
360 }
361 if (listencxx . . $ listen(9b79fb6aee4c0440).(nextfd, backlog) == -1)
362 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not listen for %s", optarg);
363 }
364
365 for (size_t idx = 0; idx < OPTIONS_COUNTcxx . . $ OPTIONS_COUNT.; idx++)
366 if (options[idx].valuecxx . . $ option_t#value. != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! && !options[idx].handledcxx . . $ option_t#handled.)
367 errxcxx . . $ errx(53ecd36ad8a4a0a2).(1, "Unexpected option '%s' in '%s'",
368 option_namescxx . . $ option_names.[idx], optarg);
369 freecxx . . $ free(5d22bdacc48458e8).(options);
370
371 return label != NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? label : "";
372 }
373
374 int
375 maincxx . . $ main(79f8f20494429ff0).(int argc, char * const argv[])
376 {
377 char *fdnames = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
378 boolcxx . . $ `<external>/14268322491693841775/stdbool.h:36:9`! hflag = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!, Vflag = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!, show_features = falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!;
379 int ch;
380 int nextfd = SD_LISTEN_FDS_STARTcxx . . $ `src/socket-activate.c:45:9`!;
381 while ((ch = getoptcxx . . $ getopt(2483c31ff9bab884).(argc, argv, "hVv-:")) != -1) {
382 switch ((char)ch) {
383 case 'h':
384 hflag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
385 break;
386
387 case 'V':
388 Vflag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
389 break;
390
391 case 'v':
392 verbosecxx . . $ verbose. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
393 break;
394
395 case '-':
396 if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(optargcxx . . $ optarg., "help") == 0)
397 hflag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
398 else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(optargcxx . . $ optarg., "version") == 0)
399 Vflag = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
400 else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(optargcxx . . $ optarg., "verbose") == 0)
401 verbosecxx . . $ verbose. = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
402 else if (strcmpcxx . . $ strcmp(5ae29d726ae57f88).(optargcxx . . $ optarg., "features") == 0)
403 show_features = truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!;
404 else {
405 const char * const label =
406 add_listenercxx . . $ add_listener(223c544933887702).(optargcxx . . $ optarg., nextfd);
407 nextfd++;
408 char *newnames = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!;
409 const int res = asprintfcxx . . $ asprintf(70ad09b5e10823c4).(&newnames,
410 "%s%s%s",
411 fdnames == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? "" : fdnames,
412 fdnames == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! ? "" : ":",
413 label);
414 if (res < 0 || newnames == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
415 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not allocate "
416 "the names list");
417 if (fdnames == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`! || strcmpcxx . . $ strcmp(5ae29d726ae57f88).(newnames,fdnames) != 0) {
418 freecxx . . $ free(5d22bdacc48458e8).(fdnames);
419 fdnames = newnames;
420 } else {
421 freecxx . . $ free(5d22bdacc48458e8).(newnames);
422 }
423 }
424 break;
425
426 default:
427 usagecxx . . $ usage(b45ddcbc8423892c).(truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!);
428 /* NOTREACHED */
429 }
430 }
431 if (Vflag)
432 versioncxx . . $ version(49f6e7a06ebc5aa8).();
433 if (hflag)
434 usagecxx . . $ usage(b45ddcbc8423892c).(falsecxx . . $ `<external>/14268322491693841775/stdbool.h:38:9`!);
435 if (show_features)
436 featurescxx . . $ features(49f6e7a06ebc5aa8).();
437 if (Vflag || hflag || show_features)
438 return (0);
439
440 argc -= optindcxx . . $ optind.;
441 argv += optindcxx . . $ optind.;
442 if (argc < 1)
443 usagecxx . . $ usage(b45ddcbc8423892c).(truecxx . . $ `<external>/14268322491693841775/stdbool.h:37:9`!);
444
445 struct pollfdcxx . . $ pollfd# *pfds;
446 pid_tcxx . . $ pid_t# pid;
447 nfds_tcxx . . $ nfds_t# nfds = nextfd - SD_LISTEN_FDS_STARTcxx . . $ `src/socket-activate.c:45:9`!;
448 nfds_tcxx . . $ nfds_t# j = 0;
449 pfds = calloccxx . . $ calloc(63a7bf2941bfcd7a).(nfds, sizeof(struct pollfdcxx . . $ pollfd#));
450 if (pfds == NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/15/include/stddef.h:419:9`!)
451 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Malloc failed");
452
453 for (int fd = SD_LISTEN_FDS_STARTcxx . . $ `src/socket-activate.c:45:9`!; fd < nextfd; fd++) {
454 pfds[j].eventscxx . . $ pollfd#events. = POLLINcxx . . $ `<external>/17223861378049612178/poll.h:25:9`!;
455 pfds[j++].fdcxx . . $ pollfd#fd. = fd;
456 }
457
458 debugcxx . . $ debug(326aeb464d16410c).("Polling %ld fds\n", nfds);
459 const int n = pollcxx . . $ poll(bc0744c797f613bd).(pfds, nfds, -1);
460 if (n == -1)
461 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not wait for incoming connections");
462 debugcxx . . $ debug(326aeb464d16410c).("got %d fds from poll()\n", n);
463 char buf[20];
464 pid = getpidcxx . . $ getpid(b126dc7c1de90089).();
465 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, sizeof(buf), "%d", pid);
466 setenvcxx . . $ setenv(f0be30763a490cce).("LISTEN_PID", buf, 1);
467 snprintfcxx . . $ snprintf(ea502d73e74c754c).(buf, sizeof(buf), "%d", nextfd - SD_LISTEN_FDS_STARTcxx . . $ `src/socket-activate.c:45:9`!);
468 setenvcxx . . $ setenv(f0be30763a490cce).("LISTEN_FDS", buf, 1);
469 setenvcxx . . $ setenv(f0be30763a490cce).("LISTEN_FDNAMES", fdnames, 1);
470 debugcxx . . $ debug(326aeb464d16410c).("Exec %s, PID %d\n", argv[0], pid);
471 execvpcxx . . $ execvp(8a4102a9cf05ffcd).(argv[0], argv);
472 errcxx . . $ err(53ecd36ad8a4a0a2).(EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:92:9`!, "Could not execute '%s'", argv[0]);
473 }