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.
25 #define _XOPEN_SOURCE 500 /**< make limits.h #define IOV_MAX */
26 #define __EXTENSIONS__ 1 /**< make Solaris netinet/in.h know IPv6 */
28 #include "ircd_osdep.h"
34 /* Include file dependency notes:
35 * FreeBSD requires struct timeval from sys/time.h before struct
36 * rusage in sys/resource.h.
37 * Solaris requires sys/time.h before struct rusage (indirectly) in
46 #include <sys/ioctl.h>
47 #include <sys/types.h>
49 #include <netinet/in.h>
50 #include <sys/resource.h>
51 #include <sys/socket.h>
55 #include <sys/param.h>
63 #define IOV_MAX 16 /**< minimum required length of an iovec array */
67 #include <sys/syscall.h>
68 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
72 /** Native socket address type. */
73 #define sockaddr_native sockaddr_in6
74 /** Field name inside sockaddr_native to find address family. */
75 #define sn_family sin6_family
77 /** Convert native socket address to IRC format.
78 * @param[in] v6 Native socket address.
79 * @param[out] irc IRC format socket address.
81 void sockaddr_to_irc(const struct sockaddr_in6 *v6, struct irc_sockaddr *irc)
83 if (v6->sin6_family == AF_INET6) {
84 memcpy(&irc->addr.in6_16[0], &v6->sin6_addr, sizeof(v6->sin6_addr));
85 irc->port = ntohs(v6->sin6_port);
87 else if (v6->sin6_family == AF_INET) {
88 const struct sockaddr_in *v4 = (const struct sockaddr_in*)v6;
89 memset(&irc->addr, 0, 5*sizeof(int16_t));
90 irc->addr.in6_16[5] = 0xffff;
91 memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
92 irc->port = ntohs(v4->sin_port);
94 else assert(0 && "Unhandled native address family");
97 /** Convert IRC socket address to native format.
98 * @param[out] v6 Native socket address.
99 * @param[in] irc IRC socket address.
100 * @param[in] persist If non-zero, and \a irc is an IPv4 address,
101 * create an AF_INET size address.
102 * @return Length of address written to \a v6.
104 int sockaddr_from_irc(struct sockaddr_in6 *v6, const struct irc_sockaddr *irc, int persist)
106 memset(v6, 0, sizeof(*v6));
108 memset(v6, 0, sizeof(v6));
109 v6->sin6_family = AF_INET6;
112 else if (persist && irc_in_addr_is_ipv4(&irc->addr)) {
113 struct sockaddr_in *v4 = (struct sockaddr_in*)v6;
114 v4->sin_family = AF_INET;
115 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
116 v4->sin_port = htons(irc->port);
120 v6->sin6_family = AF_INET6;
121 memcpy(&v6->sin6_addr, &irc->addr.in6_16[0], sizeof(v6->sin6_addr));
122 v6->sin6_port = htons(irc->port);
128 #define sockaddr_native sockaddr_in
129 #define sn_family sin_family
131 void sockaddr_to_irc(const struct sockaddr_in *v4, struct irc_sockaddr *irc)
133 assert(v4->sin_family == AF_INET);
134 memset(&irc->addr, 0, 6*sizeof(irc->addr.in6_16[0]));
135 memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
136 irc->port = ntohs(v4->sin_port);
139 int sockaddr_from_irc(struct sockaddr_in *v4, const struct irc_sockaddr *irc, int persist)
141 v4->sin_family = AF_INET;
143 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));
144 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
145 v4->sin_port = htons(irc->port);
147 memset(&v4, 0, sizeof(v4));
156 * This is part of the STATS replies. There is no offical numeric for this
157 * since this isnt an official command, in much the same way as HASH isnt.
158 * It is also possible that some systems wont support this call or have
159 * different field names for "struct rusage".
162 /** Send resource usage information to a client.
163 * @param[in] cptr Client requesting information.
164 * @param[in] uptime Wall time in seconds since the server started.
165 * @param[in] enumerator Function to call to send a line to \a cptr.
166 * @return Zero if some usage reports could not be sent, non-zero on success.
168 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
170 #ifdef HAVE_GETRUSAGE
183 hzz = sysconf(_SC_CLK_TCK);
188 assert(0 != enumerator);
189 if (getrusage(RUSAGE_SELF, &rus) == -1)
192 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
196 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
197 (long)(secs / 60), (long)(secs % 60),
198 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
199 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
200 (*enumerator)(cptr, buf);
202 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
204 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
205 rus.ru_isrss / (uptime * hzz));
206 (*enumerator)(cptr, buf);
208 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
209 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
210 (*enumerator)(cptr, buf);
212 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
213 (*enumerator)(cptr, buf);
215 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
216 (*enumerator)(cptr, buf);
218 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
219 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
220 (*enumerator)(cptr, buf);
222 #else /* HAVE_GETRUSAGE */
227 int hzz = 1, ticpermin;
228 int umin, smin, usec, ssec;
230 assert(0 != enumerator);
232 hzz = sysconf(_SC_CLK_TCK);
234 ticpermin = hzz * 60;
236 umin = tmsbuf.tms_utime / ticpermin;
237 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
238 smin = tmsbuf.tms_stime / ticpermin;
239 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
241 mins = (secs / 60) + umin + smin;
244 if (times(&tmsbuf) == -1)
246 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
248 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
249 mins, secs, umin, usec, smin, ssec);
250 (*enumerator)(cptr, buf);
251 #endif /* HAVE_TIMES */
252 #endif /* HAVE_GETRUSAGE */
256 /** Look up the most recent socket error for a socket file descriptor.
257 * @param[in] fd File descriptor to check.
258 * @return Error code from the socket, or 0 if the OS does not support this.
260 int os_get_sockerr(int fd)
263 #if defined(SO_ERROR)
264 unsigned int len = sizeof(err);
265 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
270 /** Set a file descriptor to non-blocking mode.
271 * @param[in] fd %Socket file descriptor.
272 * @return Non-zero on success, or zero on failure.
274 int os_set_nonblocking(int fd)
282 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
283 * these ioctl's. There are quite a few variations on them,
284 * as can be seen by the PCS one. They are *NOT* all the same.
285 * Heed this well. - Avalon.
294 /* This portion of code might also apply to NeXT. -LynX */
297 if (ioctl(fd, FIONBIO, &res) == -1)
300 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
302 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
308 /** Mark a socket's address as reusable.
309 * @param[in] fd %Socket file descriptor to manipulate.
310 * @return Non-zero on success, or zero on failure.
312 int os_set_reuseaddr(int fd)
314 unsigned int opt = 1;
315 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
316 (const char*) &opt, sizeof(opt)));
319 /** Set a socket's send and receive buffer sizes.
320 * @param[in] fd %Socket file descriptor to manipulate.
321 * @param[in] ssize New send buffer size.
322 * @param[in] rsize New receive buffer size.
323 * @return Non-zero on success, or zero on failure.
325 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
327 unsigned int sopt = ssize;
328 unsigned int ropt = rsize;
329 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
330 (const char*) &ropt, sizeof(ropt)) &&
331 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
332 (const char*) &sopt, sizeof(sopt)));
335 /** Set a socket's "type of service" value.
336 * @param[in] fd %Socket file descriptor to manipulate.
337 * @param[in] tos New type of service value to use.
338 * @return Non-zero on success, or zero on failure.
340 int os_set_tos(int fd,int tos)
342 #if defined(IP_TOS) && defined(IPPROTO_IP)
343 unsigned int opt = tos;
344 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
350 /** Disable IP options on a socket.
351 * @param[in] fd %Socket file descriptor to manipulate.
352 * @return Non-zero on success, or zero on failure.
354 int os_disable_options(int fd)
356 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
357 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
364 * Try and find the correct name to use with getrlimit() for setting the max.
365 * number of files allowed to be open by this process.
368 #define RLIMIT_FD_MAX RLIMIT_FDMAX
371 #define RLIMIT_FD_MAX RLIMIT_NOFILE
373 #ifdef RLIMIT_OPEN_MAX
374 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
381 /** Set file descriptor limit for the process.
382 * @param[in] max_descriptors Ideal number of file descriptors.
383 * @return Zero on success; -1 on error; positive number of possible
384 * file descriptors if \a max_descriptors is too high.
386 int os_set_fdlimit(unsigned int max_descriptors)
388 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
391 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
392 if (limit.rlim_max < max_descriptors)
393 return limit.rlim_max;
394 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
395 return setrlimit(RLIMIT_FD_MAX, &limit);
397 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
401 /** Attempt to read from a non-blocking socket.
402 * @param[in] fd File descriptor to read from.
403 * @param[out] buf Output buffer to read into.
404 * @param[in] length Number of bytes to read.
405 * @param[out] count_out Receives number of bytes actually read.
406 * @return An IOResult value indicating status.
408 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
409 unsigned int* count_out)
413 assert(0 != count_out);
417 if (0 < (res = recv(fd, buf, length, 0))) {
418 *count_out = (unsigned) res;
422 if (EWOULDBLOCK == errno || EAGAIN == errno
435 * 0 == client closed the connection
441 /** Attempt to read from a non-blocking UDP socket.
442 * @param[in] fd File descriptor to read from.
443 * @param[out] buf Output buffer to read into.
444 * @param[in] length Number of bytes to read.
445 * @param[out] length_out Receives number of bytes actually read.
446 * @param[out] addr_out Peer address that sent the message.
447 * @return An IOResult value indicating status.
449 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
450 unsigned int* length_out,
451 struct irc_sockaddr* addr_out)
453 struct sockaddr_native addr;
454 unsigned int len = sizeof(addr);
457 assert(0 != length_out);
458 assert(0 != addr_out);
462 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) &addr, &len);
464 if (EWOULDBLOCK == errno || ENOMEM == errno
475 sockaddr_to_irc(&addr, addr_out);
480 /** Attempt to write on a non-blocking UDP socket.
481 * @param[in] fd File descriptor to write to.
482 * @param[in] buf Output buffer to send from.
483 * @param[in] length Number of bytes to write.
484 * @param[out] count_out Receives number of bytes actually written.
485 * @param[in] flags Flags for call to sendto().
486 * @param[in] peer Destination address of the message.
487 * @return An IOResult value indicating status.
489 IOResult os_sendto_nonb(int fd, const char* buf, unsigned int length,
490 unsigned int* count_out, unsigned int flags,
491 const struct irc_sockaddr* peer)
493 struct sockaddr_native addr;
500 size = sockaddr_from_irc(&addr, peer, 1);
501 if (-1 < (res = sendto(fd, buf, length, flags, (struct sockaddr*)&addr, size))) {
503 *count_out = (unsigned) res;
506 else if (EWOULDBLOCK == errno || EAGAIN == errno
518 /** Attempt to write on a connected socket.
519 * @param[in] fd File descriptor to write to.
520 * @param[in] buf Output buffer to send from.
521 * @param[in] length Number of bytes to write.
522 * @param[out] count_out Receives number of bytes actually written.
523 * @return An IOResult value indicating status.
525 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
526 unsigned int* count_out)
530 assert(0 != count_out);
534 if (-1 < (res = send(fd, buf, length, 0))) {
535 *count_out = (unsigned) res;
538 else if (EWOULDBLOCK == errno || EAGAIN == errno
550 /** Attempt a vectored write on a connected socket.
551 * @param[in] fd File descriptor to write to.
552 * @param[in] buf Message queue to send from.
553 * @param[out] count_in Number of bytes mapped from \a buf.
554 * @param[out] count_out Receives number of bytes actually written.
555 * @return An IOResult value indicating status.
557 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
558 unsigned int* count_out)
562 struct iovec iov[IOV_MAX];
565 assert(0 != count_in);
566 assert(0 != count_out);
572 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
574 if (-1 < (res = writev(fd, iov, count))) {
575 *count_out = (unsigned) res;
578 else if (EWOULDBLOCK == errno || EAGAIN == errno
591 /** Open a TCP or UDP socket on a particular address.
592 * @param[in] local Local address to bind to.
593 * @param[in] type SOCK_STREAM or SOCK_DGRAM.
594 * @param[in] port_name Port name (used in error diagnostics).
595 * @return Bound descriptor, or -1 on error.
597 int os_socket(const struct irc_sockaddr* local, int type, const char* port_name)
599 struct sockaddr_native addr;
602 size = sockaddr_from_irc(&addr, local, 1);
603 fd = socket(addr.sn_family, type, 0);
605 report_error(SOCKET_ERROR_MSG, port_name, errno);
608 if (fd > MAXCLIENTS - 1) {
609 report_error(CONNLIMIT_ERROR_MSG, port_name, 0);
613 if (!os_set_reuseaddr(fd)) {
614 report_error(REUSEADDR_ERROR_MSG, port_name, errno);
618 if (!os_set_nonblocking(fd)) {
619 report_error(NONB_ERROR_MSG, port_name, errno);
624 if (bind(fd, (struct sockaddr*)&addr, size)) {
625 report_error(BIND_ERROR_MSG, port_name, errno);
633 /** Accept a connection on a socket.
634 * @param[in] fd Listening file descriptor.
635 * @param[out] peer Peer address of connection.
636 * @return File descriptor for accepted connection.
638 int os_accept(int fd, struct irc_sockaddr* peer)
640 struct sockaddr_native addr;
644 addrlen = sizeof(addr);
645 new_fd = accept(fd, (struct sockaddr*)&addr, &addrlen);
647 memset(peer, 0, sizeof(*peer));
649 sockaddr_to_irc(&addr, peer);
653 /** Start a non-blocking connection.
654 * @param[in] fd Disconnected file descriptor.
655 * @param[in] sin Target address for connection.
656 * @return IOResult code indicating status.
658 IOResult os_connect_nonb(int fd, const struct irc_sockaddr* sin)
660 struct sockaddr_native addr;
663 size = sockaddr_from_irc(&addr, sin, 1);
664 if (connect(fd, (struct sockaddr*) &addr, size))
665 return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
669 /** Get local address of a socket.
670 * @param[in] fd File descriptor to operate on.
671 * @param[out] sin_out Receives local socket address.
672 * @return Non-zero on success; zero on error.
674 int os_get_sockname(int fd, struct irc_sockaddr* sin_out)
676 struct sockaddr_native addr;
677 unsigned int len = sizeof(addr);
679 assert(0 != sin_out);
680 if (getsockname(fd, (struct sockaddr*) &addr, &len))
682 sockaddr_to_irc(&addr, sin_out);
686 /** Get remote address of a socket.
687 * @param[in] fd File descriptor to operate on.
688 * @param[out] sin_out Receives remote socket address.
689 * @return Non-zero on success; zero on error.
691 int os_get_peername(int fd, struct irc_sockaddr* sin_out)
693 struct sockaddr_native addr;
694 unsigned int len = sizeof(addr);
696 assert(0 != sin_out);
697 if (getpeername(fd, (struct sockaddr*) &addr, &len))
699 sockaddr_to_irc(&addr, sin_out);
703 /** Start listening on a socket.
704 * @param[in] fd Disconnected file descriptor.
705 * @param[in] backlog Maximum number of un-accept()ed connections to keep.
706 * @return Non-zero on success; zero on error.
708 int os_set_listen(int fd, int backlog)
710 return (0 == listen(fd, backlog));