2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/param.h>
34 #include <netinet/in.h>
35 #include <arpa/inet.h>
36 #include <sys/socket.h>
44 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
45 #include <sys/module.h>
53 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
59 #define issep(c) ((c) == '\t' || (c) == ' ')
65 srandom((time(NULL)^getpid())+random());
70 fullread(int fd, void *v, size_t n)
74 for (total = 0; total < n; total += got)
75 switch ((got = read(fd, (char *)v + total, n - total))) {
91 { PHYS_INTERACTIVE, "interactive" },
92 { PHYS_AUTO, "auto" },
93 { PHYS_DIRECT, "direct" },
94 { PHYS_DEDICATED, "dedicated" },
95 { PHYS_DDIAL, "ddial" },
96 { PHYS_BACKGROUND, "background" },
97 { PHYS_FOREGROUND, "foreground" },
107 for (m = 0; modes[m].mode; m++)
108 if (modes[m].mode == mode)
109 return modes[m].name;
115 Nam2mode(const char *name)
121 for (m = 0; modes[m].mode; m++)
122 if (!strncasecmp(name, modes[m].name, len)) {
123 if (modes[m].name[len] == '\0')
124 return modes[m].mode;
130 return got == -1 ? 0 : modes[got].mode;
134 GetIpAddr(const char *cp)
136 struct in_addr ipaddr;
138 if (!strcasecmp(cp, "default"))
139 ipaddr.s_addr = INADDR_ANY;
140 else if (inet_aton(cp, &ipaddr) == 0) {
143 /* Any illegal characters ? */
144 for (ptr = cp; *ptr != '\0'; ptr++)
145 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
151 hp = gethostbyname(cp);
152 if (hp && hp->h_addrtype == AF_INET)
153 memcpy(&ipaddr, hp->h_addr, hp->h_length);
155 ipaddr.s_addr = INADDR_NONE;
157 ipaddr.s_addr = INADDR_NONE;
163 static const struct speeds {
212 #ifndef _POSIX_SOURCE
229 { 115200, B115200, },
232 { 230400, B230400, },
235 { 460800, B460800, },
238 { 921600, B921600, },
246 #endif /* _POSIX_SOURCE */
251 SpeedToUnsigned(speed_t speed)
253 const struct speeds *sp;
255 for (sp = speeds; sp->nspeed; sp++) {
256 if (sp->speed == speed) {
264 UnsignedToSpeed(unsigned nspeed)
266 const struct speeds *sp;
268 for (sp = speeds; sp->nspeed; sp++) {
269 if (sp->nspeed == nspeed) {
277 findblank(char *p, int flags)
284 if (flags & PARSE_REDUCE) {
285 memmove(p, p + 1, strlen(p));
290 } else if (*p == '"') {
291 memmove(p, p + 1, strlen(p));
292 instring = !instring;
294 } else if (!instring && (issep(*p) ||
295 (*p == '#' && !(flags & PARSE_NOHASH))))
300 return instring ? NULL : p;
304 MakeArgs(char *script, char **pvect, int maxargs, int flags)
310 script += strspn(script, " \t");
311 if (*script == '#' && !(flags & PARSE_NOHASH)) {
316 if (nargs >= maxargs - 1)
320 script = findblank(script, flags);
323 else if (!(flags & PARSE_NOHASH) && *script == '#')
334 NumStr(long val, char *buf, size_t sz)
336 static char result[23]; /* handles 64 bit numbers */
338 if (buf == NULL || sz == 0) {
342 snprintf(buf, sz, "<%ld>", val);
347 HexStr(long val, char *buf, size_t sz)
349 static char result[21]; /* handles 64 bit numbers */
351 if (buf == NULL || sz == 0) {
355 snprintf(buf, sz, "<0x%lx>", val);
362 static char num[12]; /* Used immediately if returned */
363 static const char * const desc[] = {
364 "normal", "start", "sock", "modem", "dial", "dead", "done",
365 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
366 "redial", "reconnect"
369 if (ex >= 0 && ex < (int)(sizeof desc / sizeof *desc))
371 snprintf(num, sizeof num, "%d", ex);
376 SetTitle(const char *title)
380 else if (title[0] == '-' && title[1] != '\0')
381 setproctitle("-%s", title + 1);
383 setproctitle("%s", title);
389 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
395 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
399 Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
405 for (pos = i = 0; i < argc; i++) {
406 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
411 if ((pos += n) >= sz)
416 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
418 loadmodules(int how, const char *module, ...)
423 va_start(ap, module);
424 while (module != NULL) {
425 if (modfind(module) == -1) {
426 if (ID0kldload(module) == -1) {
427 if (how == LOAD_VERBOSLY)
428 log_Printf(LogWARN, "%s: Cannot load module\n", module);
432 module = va_arg(ap, const char *);
439 loadmodules(int how __unused, const char *module __unused, ...)