2 * Copyright (c) 1997 Brian Somers <brian@Awfulhak.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/param.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <sys/socket.h>
42 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
43 #include <sys/module.h>
46 #if !defined(__FreeBSD__) || __FreeBSD__ < 3
51 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
57 #define issep(c) ((c) == '\t' || (c) == ' ')
59 #if defined(__NetBSD__) || __FreeBSD__ < 3
63 #if defined(__FreeBSD__)
64 static int initdone; /* srandomdev() call is only required once */
71 srandom((time(NULL)^getpid())+random());
77 fullread(int fd, void *v, size_t n)
81 for (total = 0; total < n; total += got)
82 switch ((got = read(fd, (char *)v + total, n - total))) {
98 { PHYS_INTERACTIVE, "interactive" },
99 { PHYS_AUTO, "auto" },
100 { PHYS_DIRECT, "direct" },
101 { PHYS_DEDICATED, "dedicated" },
102 { PHYS_DDIAL, "ddial" },
103 { PHYS_BACKGROUND, "background" },
104 { PHYS_FOREGROUND, "foreground" },
114 for (m = 0; modes[m].mode; m++)
115 if (modes[m].mode == mode)
116 return modes[m].name;
122 Nam2mode(const char *name)
128 for (m = 0; modes[m].mode; m++)
129 if (!strncasecmp(name, modes[m].name, len)) {
130 if (modes[m].name[len] == '\0')
131 return modes[m].mode;
137 return got == -1 ? 0 : modes[got].mode;
141 GetIpAddr(const char *cp)
143 struct in_addr ipaddr;
145 if (!strcasecmp(cp, "default"))
146 ipaddr.s_addr = INADDR_ANY;
147 else if (inet_aton(cp, &ipaddr) == 0) {
150 /* Any illegal characters ? */
151 for (ptr = cp; *ptr != '\0'; ptr++)
152 if (!isalnum(*ptr) && strchr("-.", *ptr) == NULL)
158 hp = gethostbyname(cp);
159 if (hp && hp->h_addrtype == AF_INET)
160 memcpy(&ipaddr, hp->h_addr, hp->h_length);
162 ipaddr.s_addr = INADDR_NONE;
164 ipaddr.s_addr = INADDR_NONE;
170 static const struct speeds {
219 #ifndef _POSIX_SOURCE
236 { 115200, B115200, },
239 { 230400, B230400, },
242 { 460800, B460800, },
245 { 921600, B921600, },
253 #endif /* _POSIX_SOURCE */
258 SpeedToUnsigned(speed_t speed)
260 const struct speeds *sp;
262 for (sp = speeds; sp->nspeed; sp++) {
263 if (sp->speed == speed) {
271 UnsignedToSpeed(unsigned nspeed)
273 const struct speeds *sp;
275 for (sp = speeds; sp->nspeed; sp++) {
276 if (sp->nspeed == nspeed) {
284 findblank(char *p, int flags)
291 if (flags & PARSE_REDUCE) {
292 memmove(p, p + 1, strlen(p));
297 } else if (*p == '"') {
298 memmove(p, p + 1, strlen(p));
299 instring = !instring;
301 } else if (!instring && (issep(*p) ||
302 (*p == '#' && !(flags & PARSE_NOHASH))))
307 return instring ? NULL : p;
311 MakeArgs(char *script, char **pvect, int maxargs, int flags)
317 script += strspn(script, " \t");
318 if (*script == '#' && !(flags & PARSE_NOHASH)) {
323 if (nargs >= maxargs - 1)
327 script = findblank(script, flags);
330 else if (!(flags & PARSE_NOHASH) && *script == '#')
341 NumStr(long val, char *buf, size_t sz)
343 static char result[23]; /* handles 64 bit numbers */
345 if (buf == NULL || sz == 0) {
349 snprintf(buf, sz, "<%ld>", val);
354 HexStr(long val, char *buf, size_t sz)
356 static char result[21]; /* handles 64 bit numbers */
358 if (buf == NULL || sz == 0) {
362 snprintf(buf, sz, "<0x%lx>", val);
369 static char num[12]; /* Used immediately if returned */
370 static const char * const desc[] = {
371 "normal", "start", "sock", "modem", "dial", "dead", "done",
372 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
373 "redial", "reconnect"
376 if (ex >= 0 && ex < (int)(sizeof desc / sizeof *desc))
378 snprintf(num, sizeof num, "%d", ex);
383 SetTitle(const char *title)
387 else if (title[0] == '-' && title[1] != '\0')
388 setproctitle("-%s", title + 1);
390 setproctitle("%s", title);
396 return (fd_set *)malloc(howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
402 memset(s, '\0', howmany(getdtablesize(), NFDBITS) * sizeof (fd_mask));
406 Concatinate(char *buf, size_t sz, int argc, const char *const *argv)
412 for (pos = i = 0; i < argc; i++) {
413 n = snprintf(buf + pos, sz - pos, "%s%s", i ? " " : "", argv[i]);
418 if ((pos += n) >= sz)
423 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
425 loadmodules(int how, const char *module, ...)
430 va_start(ap, module);
431 while (module != NULL) {
432 if (modfind(module) == -1) {
433 if (ID0kldload(module) == -1) {
434 if (how == LOAD_VERBOSLY)
435 log_Printf(LogWARN, "%s: Cannot load module\n", module);
439 module = va_arg(ap, const char *);
446 loadmodules(int how __unused, const char *module __unused, ...)