2 * IRC - Internet Relay Chat, ircd/os_generic.c
3 * Copyright (C) 1999 Thomas Helvey
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 1, or (at your option)
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * @brief Implementation of OS-dependent operations.
21 * @version $Id: os_generic.c 1767 2007-02-28 22:38:50Z entrope $
26 /* Solaris requires C99 support for SUSv3, but C99 support breaks other
27 * parts of the build. So fall back to SUSv2, but request IPv6 support
28 * by defining __EXTENSIONS__.
30 #define _XOPEN_SOURCE 500
31 #define __EXTENSIONS__ 1
32 #elif defined(__FreeBSD__) && __FreeBSD__ >= 5
33 /* FreeBSD 6.0 requires SUSv3 to support IPv6 -- but if you ask for
34 * that specifically (by defining _XOPEN_SOURCE to anything at all),
35 * they cleverly hide IPPROTO_IPV6. If you don't ask for anything,
36 * they give you everything.
39 #define _XOPEN_SOURCE 600
42 #include "ircd_osdep.h"
49 /* Include file dependency notes:
50 * FreeBSD requires struct timeval from sys/time.h before struct
51 * rusage in sys/resource.h.
52 * Solaris requires sys/time.h before struct rusage (indirectly) in
55 /* #include <assert.h> -- Now using assert in ircd_log.h */
61 #include <sys/ioctl.h>
62 #include <sys/types.h>
64 #include <netinet/in.h>
65 #include <sys/resource.h>
66 #include <sys/socket.h>
70 #include <sys/param.h>
77 #if defined(IPV6_BINDV6ONLY) &&!defined(IPV6_V6ONLY)
78 # define IPV6_V6ONLY IPV6_BINDV6ONLY
82 #define IOV_MAX 16 /**< minimum required length of an iovec array */
86 #include <sys/syscall.h>
87 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
90 static int is_blocked(int error)
92 return EWOULDBLOCK == error
102 static void sockaddr_in_to_irc(const struct sockaddr_in *v4,
103 struct irc_sockaddr *irc)
105 memset(&irc->addr, 0, 5*sizeof(int16_t));
106 irc->addr.in6_16[5] = 0xffff;
107 memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
108 irc->port = ntohs(v4->sin_port);
113 /** Native socket address type. */
114 #define sockaddr_native sockaddr_in6
115 /** Field name inside sockaddr_native to find address family. */
116 #define sn_family sin6_family
118 /** Convert native socket address to IRC format.
119 * @param[in] v6 Native socket address.
120 * @param[out] irc IRC format socket address.
122 void sockaddr_to_irc(const struct sockaddr_in6 *v6, struct irc_sockaddr *irc)
124 if (v6->sin6_family == AF_INET6) {
125 memcpy(&irc->addr.in6_16[0], &v6->sin6_addr, sizeof(v6->sin6_addr));
126 irc->port = ntohs(v6->sin6_port);
128 else if (v6->sin6_family == AF_INET) {
129 sockaddr_in_to_irc((struct sockaddr_in *)v6, irc);
131 else assert(0 && "Unhandled native address family");
134 /** Convert IRC socket address to native format.
135 * @param[out] v6 Native socket address.
136 * @param[in] irc IRC socket address.
137 * @param[in] compat_fd If non-negative, an FD specifying address family.
138 * @return Length of address written to \a v6.
140 int sockaddr_from_irc(struct sockaddr_in6 *v6, const struct irc_sockaddr *irc, int compat_fd, int family)
142 struct sockaddr_in6 sin6;
148 /* accept whatever user specified */
149 } else if ((0 <= compat_fd)
150 && (0 == getsockname(compat_fd, (struct sockaddr*)&sin6, &slen)))
151 family = sin6.sin6_family;
152 else if ((irc == &VirtualHost_v4) || irc_in_addr_is_ipv4(&irc->addr))
157 memset(v6, 0, sizeof(*v6));
158 if (family == AF_INET) {
159 struct sockaddr_in *v4 = (struct sockaddr_in*)v6;
160 v4->sin_family = AF_INET;
161 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
162 v4->sin_port = htons(irc->port);
166 v6->sin6_family = AF_INET6;
167 memcpy(&v6->sin6_addr, &irc->addr.in6_16[0], sizeof(v6->sin6_addr));
168 v6->sin6_port = htons(irc->port);
174 #define sockaddr_native sockaddr_in
175 #define sn_family sin_family
176 #define sockaddr_to_irc sockaddr_in_to_irc
178 int sockaddr_from_irc(struct sockaddr_in *v4, const struct irc_sockaddr *irc, int compat_fd, int family)
181 memset(v4, 0, sizeof(*v4));
182 v4->sin_family = AF_INET;
184 assert(!irc->addr.in6_16[0] && !irc->addr.in6_16[1] && !irc->addr.in6_16[2] && !irc->addr.in6_16[3] && !irc->addr.in6_16[4] && (!irc->addr.in6_16[5] || irc->addr.in6_16[5] == 0xffff));
185 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
186 v4->sin_port = htons(irc->port);
188 (void)compat_fd; (void)family;
195 /** Send resource usage information to an enumerator function.
196 * @param[in] cptr Client requesting information.
197 * @param[in] uptime Wall time in seconds since the server started.
198 * @param[in] enumerator Function to call to send a line to \a cptr.
199 * @return Zero if some usage reports could not be sent, non-zero on success.
201 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
203 #ifdef HAVE_GETRUSAGE
216 hzz = sysconf(_SC_CLK_TCK);
221 assert(0 != enumerator);
222 if (getrusage(RUSAGE_SELF, &rus) == -1)
225 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
229 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
230 (long)(secs / 60), (long)(secs % 60),
231 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
232 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
233 (*enumerator)(cptr, buf);
235 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
237 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
238 rus.ru_isrss / (uptime * hzz));
239 (*enumerator)(cptr, buf);
241 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
242 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
243 (*enumerator)(cptr, buf);
245 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
246 (*enumerator)(cptr, buf);
248 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
249 (*enumerator)(cptr, buf);
251 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
252 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
253 (*enumerator)(cptr, buf);
255 #else /* HAVE_GETRUSAGE */
260 int hzz = 1, ticpermin;
261 int umin, smin, usec, ssec;
263 assert(0 != enumerator);
265 hzz = sysconf(_SC_CLK_TCK);
267 ticpermin = hzz * 60;
269 umin = tmsbuf.tms_utime / ticpermin;
270 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
271 smin = tmsbuf.tms_stime / ticpermin;
272 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
274 mins = (secs / 60) + umin + smin;
277 if (times(&tmsbuf) == -1)
279 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
281 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
282 mins, secs, umin, usec, smin, ssec);
283 (*enumerator)(cptr, buf);
284 #endif /* HAVE_TIMES */
285 #endif /* HAVE_GETRUSAGE */
290 /** Look up the most recent socket error for a socket file descriptor.
291 * @param[in] fd File descriptor to check.
292 * @return Error code from the socket, or 0 if the OS does not support this.
294 int os_get_sockerr(int fd)
297 #if defined(SO_ERROR)
298 unsigned int len = sizeof(err);
299 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
304 /** Set a file descriptor to non-blocking mode.
305 * @param[in] fd %Socket file descriptor.
306 * @return Non-zero on success, or zero on failure.
308 int os_set_nonblocking(int fd)
316 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
317 * these ioctl's. There are quite a few variations on them,
318 * as can be seen by the PCS one. They are *NOT* all the same.
319 * Heed this well. - Avalon.
328 /* This portion of code might also apply to NeXT. -LynX */
331 if (ioctl(fd, FIONBIO, &res) == -1)
334 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
336 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
342 /** Mark a socket's address as reusable.
343 * @param[in] fd %Socket file descriptor to manipulate.
344 * @return Non-zero on success, or zero on failure.
346 int os_set_reuseaddr(int fd)
348 unsigned int opt = 1;
349 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
350 (const char*) &opt, sizeof(opt)));
353 /** Set a socket's send and receive buffer sizes.
354 * @param[in] fd %Socket file descriptor to manipulate.
355 * @param[in] ssize New send buffer size.
356 * @param[in] rsize New receive buffer size.
357 * @return Non-zero on success, or zero on failure.
359 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
361 unsigned int sopt = ssize;
362 unsigned int ropt = rsize;
363 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
364 (const char*) &ropt, sizeof(ropt)) &&
365 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
366 (const char*) &sopt, sizeof(sopt)));
369 /** Set a socket's "type of service" value.
370 * @param[in] fd %Socket file descriptor to manipulate.
371 * @param[in] tos New type of service value to use.
372 * @return Non-zero on success, or zero on failure.
374 int os_set_tos(int fd,int tos)
376 #if defined(IP_TOS) && defined(IPPROTO_IP)
377 unsigned int opt = tos;
378 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
384 /** Disable IP options on a socket.
385 * @param[in] fd %Socket file descriptor to manipulate.
386 * @return Non-zero on success, or zero on failure.
388 int os_disable_options(int fd)
390 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
391 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
398 * Try and find the correct name to use with getrlimit() for setting the max.
399 * number of files allowed to be open by this process.
402 #define RLIMIT_FD_MAX RLIMIT_FDMAX
405 #define RLIMIT_FD_MAX RLIMIT_NOFILE
407 #ifdef RLIMIT_OPEN_MAX
408 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
415 /** Set file descriptor limit for the process.
416 * @param[in] max_descriptors Ideal number of file descriptors.
417 * @return Zero on success; -1 on error; positive number of possible
418 * file descriptors if \a max_descriptors is too high.
420 int os_set_fdlimit(unsigned int max_descriptors)
422 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
425 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
426 if (limit.rlim_max < max_descriptors)
427 return limit.rlim_max;
428 if(limit.rlim_max == RLIM_INFINITY)
429 limit.rlim_cur = max_descriptors;
431 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
432 return setrlimit(RLIMIT_FD_MAX, &limit);
434 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
438 /** Attempt to read from a non-blocking socket.
439 * @param[in] fd File descriptor to read from.
440 * @param[out] buf Output buffer to read into.
441 * @param[in] length Number of bytes to read.
442 * @param[out] count_out Receives number of bytes actually read.
443 * @return An IOResult value indicating status.
445 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
446 unsigned int* count_out)
450 assert(0 != count_out);
452 if (0 < (res = recv(fd, buf, length, 0))) {
453 *count_out = (unsigned) res;
455 } else if (res == 0) {
457 errno = 0; /* or ECONNRESET? */
461 return is_blocked(errno) ? IO_BLOCKED : IO_FAILURE;
465 /** Attempt to read from a non-blocking UDP socket.
466 * @param[in] fd File descriptor to read from.
467 * @param[out] buf Output buffer to read into.
468 * @param[in] length Number of bytes to read.
469 * @param[out] length_out Receives number of bytes actually read.
470 * @param[out] addr_out Peer address that sent the message.
471 * @return An IOResult value indicating status.
473 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
474 unsigned int* length_out,
475 struct irc_sockaddr* addr_out)
477 struct sockaddr_native addr;
478 unsigned int len = sizeof(addr);
481 assert(0 != length_out);
482 assert(0 != addr_out);
484 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) &addr, &len);
486 sockaddr_to_irc(&addr, addr_out);
491 return is_blocked(errno) ? IO_BLOCKED : IO_FAILURE;
495 /** Attempt to write on a non-blocking UDP socket.
496 * @param[in] fd File descriptor to write to.
497 * @param[in] buf Output buffer to send from.
498 * @param[in] length Number of bytes to write.
499 * @param[out] count_out Receives number of bytes actually written.
500 * @param[in] flags Flags for call to sendto().
501 * @param[in] peer Destination address of the message.
502 * @return An IOResult value indicating status.
504 IOResult os_sendto_nonb(int fd, const char* buf, unsigned int length,
505 unsigned int* count_out, unsigned int flags,
506 const struct irc_sockaddr* peer)
508 struct sockaddr_native addr;
512 size = sockaddr_from_irc(&addr, peer, fd, 0);
513 assert((addr.sn_family == AF_INET) == irc_in_addr_is_ipv4(&peer->addr));
514 if (-1 < (res = sendto(fd, buf, length, flags, (struct sockaddr*)&addr, size))) {
516 *count_out = (unsigned) res;
521 return is_blocked(errno) ? IO_BLOCKED : IO_FAILURE;
525 /** Attempt to write on a connected socket.
526 * @param[in] fd File descriptor to write to.
527 * @param[in] buf Output buffer to send from.
528 * @param[in] length Number of bytes to write.
529 * @param[out] count_out Receives number of bytes actually written.
530 * @return An IOResult value indicating status.
532 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
533 unsigned int* count_out)
537 assert(0 != count_out);
539 if (-1 < (res = send(fd, buf, length, 0))) {
540 *count_out = (unsigned) res;
544 return is_blocked(errno) ? IO_BLOCKED : IO_FAILURE;
548 /** Attempt a vectored write on a connected socket.
549 * @param[in] fd File descriptor to write to.
550 * @param[in] buf Message queue to send from.
551 * @param[out] count_in Number of bytes mapped from \a buf.
552 * @param[out] count_out Receives number of bytes actually written.
553 * @return An IOResult value indicating status.
555 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
556 unsigned int* count_out)
560 struct iovec iov[IOV_MAX];
563 assert(0 != count_in);
564 assert(0 != count_out);
567 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
569 if (-1 < (res = writev(fd, iov, count))) {
570 *count_out = (unsigned) res;
574 return is_blocked(errno) ? IO_BLOCKED : IO_FAILURE;
578 /** Open a TCP or UDP socket on a particular address.
579 * @param[in] local Local address to bind to.
580 * @param[in] type SOCK_STREAM or SOCK_DGRAM.
581 * @param[in] port_name Port name (used in error diagnostics).
582 * @param[in] family A specific address family to use, or 0 for automatic.
583 * @return Bound descriptor, or -1 on error.
585 int os_socket(const struct irc_sockaddr* local, int type, const char* port_name, int family)
587 struct sockaddr_native addr;
591 size = sockaddr_from_irc(&addr, local, -1, family);
592 fd = socket(addr.sn_family, type, 0);
594 report_error(SOCKET_ERROR_MSG, port_name, errno);
597 if (fd > MAXCLIENTS - 1) {
598 report_error(CONNLIMIT_ERROR_MSG, port_name, 0);
602 if (!os_set_reuseaddr(fd)) {
603 report_error(REUSEADDR_ERROR_MSG, port_name, errno);
607 if (!os_set_nonblocking(fd)) {
608 report_error(NONB_ERROR_MSG, port_name, errno);
613 #if defined(IPV6_V6ONLY)
615 if (family == AF_INET6 && irc_in_addr_unspec(&local->addr))
616 setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof(on));
618 if (bind(fd, (struct sockaddr*)&addr, size)) {
619 report_error(BIND_ERROR_MSG, port_name, errno);
627 /** Accept a connection on a socket.
628 * @param[in] fd Listening file descriptor.
629 * @param[out] peer Peer address of connection.
630 * @return File descriptor for accepted connection.
632 int os_accept(int fd, struct irc_sockaddr* peer)
634 struct sockaddr_native addr;
638 addrlen = sizeof(addr);
639 new_fd = accept(fd, (struct sockaddr*)&addr, &addrlen);
641 memset(peer, 0, sizeof(*peer));
643 sockaddr_to_irc(&addr, peer);
647 /** Start a non-blocking connection.
648 * @param[in] fd Disconnected file descriptor.
649 * @param[in] sin Target address for connection.
650 * @return IOResult code indicating status.
652 IOResult os_connect_nonb(int fd, const struct irc_sockaddr* sin)
654 struct sockaddr_native addr;
657 size = sockaddr_from_irc(&addr, sin, fd, 0);
658 if (0 == connect(fd, (struct sockaddr*) &addr, size))
660 else if (errno == EINPROGRESS)
666 /** Get local address of a socket.
667 * @param[in] fd File descriptor to operate on.
668 * @param[out] sin_out Receives local socket address.
669 * @return Non-zero on success; zero on error.
671 int os_get_sockname(int fd, struct irc_sockaddr* sin_out)
673 struct sockaddr_native addr;
674 unsigned int len = sizeof(addr);
676 assert(0 != sin_out);
677 if (getsockname(fd, (struct sockaddr*) &addr, &len))
679 sockaddr_to_irc(&addr, sin_out);
683 /** Get remote address of a socket.
684 * @param[in] fd File descriptor to operate on.
685 * @param[out] sin_out Receives remote socket address.
686 * @return Non-zero on success; zero on error.
688 int os_get_peername(int fd, struct irc_sockaddr* sin_out)
690 struct sockaddr_native addr;
691 unsigned int len = sizeof(addr);
693 assert(0 != sin_out);
694 if (getpeername(fd, (struct sockaddr*) &addr, &len))
696 sockaddr_to_irc(&addr, sin_out);
700 /** Start listening on a socket.
701 * @param[in] fd Disconnected file descriptor.
702 * @param[in] backlog Maximum number of un-accept()ed connections to keep.
703 * @return Non-zero on success; zero on error.
705 int os_set_listen(int fd, int backlog)
707 return (0 == listen(fd, backlog));
710 /** Allocate a connected pair of local sockets.
711 * @param[out] sv Array of two file descriptors.
712 * @return Zero on success; non-zero number on error.
714 int os_socketpair(int sv[2])
716 return socketpair(AF_UNIX, SOCK_STREAM, 0, sv);