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>
51 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
57 #define issep(c) ((c) == '\t' || (c) == ' ')
63 srandom((time(NULL
)^getpid())+random());
68 fullread(int fd
, void *v
, size_t n
)
72 for (total
= 0; total
< n
; total
+= got
)
73 switch ((got
= read(fd
, (char *)v
+ total
, n
- total
))) {
89 { PHYS_INTERACTIVE
, "interactive" },
90 { PHYS_AUTO
, "auto" },
91 { PHYS_DIRECT
, "direct" },
92 { PHYS_DEDICATED
, "dedicated" },
93 { PHYS_DDIAL
, "ddial" },
94 { PHYS_BACKGROUND
, "background" },
95 { PHYS_FOREGROUND
, "foreground" },
105 for (m
= 0; modes
[m
].mode
; m
++)
106 if (modes
[m
].mode
== mode
)
107 return modes
[m
].name
;
113 Nam2mode(const char *name
)
119 for (m
= 0; modes
[m
].mode
; m
++)
120 if (!strncasecmp(name
, modes
[m
].name
, len
)) {
121 if (modes
[m
].name
[len
] == '\0')
122 return modes
[m
].mode
;
128 return got
== -1 ? 0 : modes
[got
].mode
;
132 GetIpAddr(const char *cp
)
134 struct in_addr ipaddr
;
136 if (!strcasecmp(cp
, "default"))
137 ipaddr
.s_addr
= INADDR_ANY
;
138 else if (inet_aton(cp
, &ipaddr
) == 0) {
141 /* Any illegal characters ? */
142 for (ptr
= cp
; *ptr
!= '\0'; ptr
++)
143 if (!isalnum(*ptr
) && strchr("-.", *ptr
) == NULL
)
149 hp
= gethostbyname(cp
);
150 if (hp
&& hp
->h_addrtype
== AF_INET
)
151 memcpy(&ipaddr
, hp
->h_addr
, hp
->h_length
);
153 ipaddr
.s_addr
= INADDR_NONE
;
155 ipaddr
.s_addr
= INADDR_NONE
;
161 static const struct speeds
{
210 #ifndef _POSIX_SOURCE
227 { 115200, B115200
, },
230 { 230400, B230400
, },
233 { 460800, B460800
, },
236 { 921600, B921600
, },
244 #endif /* _POSIX_SOURCE */
249 SpeedToUnsigned(speed_t speed
)
251 const struct speeds
*sp
;
253 for (sp
= speeds
; sp
->nspeed
; sp
++) {
254 if (sp
->speed
== speed
) {
262 UnsignedToSpeed(unsigned nspeed
)
264 const struct speeds
*sp
;
266 for (sp
= speeds
; sp
->nspeed
; sp
++) {
267 if (sp
->nspeed
== nspeed
) {
275 findblank(char *p
, int flags
)
282 if (flags
& PARSE_REDUCE
) {
283 memmove(p
, p
+ 1, strlen(p
));
288 } else if (*p
== '"') {
289 memmove(p
, p
+ 1, strlen(p
));
290 instring
= !instring
;
292 } else if (!instring
&& (issep(*p
) ||
293 (*p
== '#' && !(flags
& PARSE_NOHASH
))))
298 return instring
? NULL
: p
;
302 MakeArgs(char *script
, char **pvect
, int maxargs
, int flags
)
308 script
+= strspn(script
, " \t");
309 if (*script
== '#' && !(flags
& PARSE_NOHASH
)) {
314 if (nargs
>= maxargs
- 1)
318 script
= findblank(script
, flags
);
321 else if (!(flags
& PARSE_NOHASH
) && *script
== '#')
332 NumStr(long val
, char *buf
, size_t sz
)
334 static char result
[23]; /* handles 64 bit numbers */
336 if (buf
== NULL
|| sz
== 0) {
340 snprintf(buf
, sz
, "<%ld>", val
);
345 HexStr(long val
, char *buf
, size_t sz
)
347 static char result
[21]; /* handles 64 bit numbers */
349 if (buf
== NULL
|| sz
== 0) {
353 snprintf(buf
, sz
, "<0x%lx>", val
);
360 static char num
[12]; /* Used immediately if returned */
361 static const char * const desc
[] = {
362 "normal", "start", "sock", "modem", "dial", "dead", "done",
363 "reboot", "errdead", "hangup", "term", "nodial", "nologin",
364 "redial", "reconnect"
367 if (ex
>= 0 && ex
< (int)(sizeof desc
/ sizeof *desc
))
369 snprintf(num
, sizeof num
, "%d", ex
);
374 SetTitle(const char *title
)
378 else if (title
[0] == '-' && title
[1] != '\0')
379 setproctitle("-%s", title
+ 1);
381 setproctitle("%s", title
);
387 return (fd_set
*)malloc(howmany(getdtablesize(), NFDBITS
) * sizeof (fd_mask
));
393 memset(s
, '\0', howmany(getdtablesize(), NFDBITS
) * sizeof (fd_mask
));
397 Concatinate(char *buf
, size_t sz
, int argc
, const char *const *argv
)
403 for (pos
= i
= 0; i
< argc
; i
++) {
404 n
= snprintf(buf
+ pos
, sz
- pos
, "%s%s", i
? " " : "", argv
[i
]);
409 if ((pos
+= n
) >= sz
)
414 #if defined(__FreeBSD__) && !defined(NOKLDLOAD)
416 loadmodules(int how
, const char *module
, ...)
421 va_start(ap
, module
);
422 while (module
!= NULL
) {
423 if (modfind(module
) == -1) {
424 if (ID0kldload(module
) == -1) {
425 if (how
== LOAD_VERBOSLY
)
426 log_Printf(LogWARN
, "%s: Cannot load module\n", module
);
430 module
= va_arg(ap
, const char *);
437 loadmodules(int how __unused
, const char *module __unused
, ...)