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.
24 #define _XOPEN_SOURCE 500 /* make limits.h #define IOV_MAX */
25 #define __EXTENSIONS__ 1 /* make Solaris netinet/in.h know IPv6 */
27 #include "ircd_osdep.h"
33 /* Include file dependency notes:
34 * FreeBSD requires struct timeval from sys/time.h before struct
35 * rusage in sys/resource.h.
36 * Solaris requires sys/time.h before struct rusage (indirectly) in
45 #include <sys/ioctl.h>
46 #include <sys/types.h>
48 #include <netinet/in.h>
49 #include <sys/resource.h>
50 #include <sys/socket.h>
54 #include <sys/param.h>
62 #define IOV_MAX 16 /* minimum required */
66 #include <sys/syscall.h>
67 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
71 #define sockaddr_native sockaddr_in6
73 void sockaddr_to_irc(const struct sockaddr_in6 *v6, struct irc_sockaddr *irc)
75 assert(v6->sin6_family == AF_INET6);
76 memcpy(&irc->addr.in6_16[0], &v6->sin6_addr, sizeof(v6->sin6_addr));
77 irc->port = ntohs(v6->sin6_port);
80 void sockaddr_from_irc(struct sockaddr_in6 *v6, const struct irc_sockaddr *irc, int persist)
82 memset(v6, 0, sizeof(*v6));
83 v6->sin6_family = AF_INET6;
84 memcpy(&v6->sin6_addr, &irc->addr.in6_16[0], sizeof(v6->sin6_addr));
85 if (persist && irc_in_addr_is_ipv4(&irc->addr))
86 v6->sin6_addr.s6_addr[10] = v6->sin6_addr.s6_addr[11] = '\xff';
87 v6->sin6_port = htons(irc->port);
91 #define sockaddr_native sockaddr_in
93 void sockaddr_to_irc(const struct sockaddr_in *v4, struct irc_sockaddr *irc)
95 assert(v4->sin_family == AF_INET);
96 memset(&irc->addr, 0, 6*sizeof(irc->addr.in6_16[0]));
97 memcpy(&irc->addr.in6_16[6], &v4->sin_addr, sizeof(v4->sin_addr));
98 irc->port = ntohs(v4->sin_port);
101 void sockaddr_from_irc(struct sockaddr_in *v4, const struct irc_sockaddr *irc, int persist)
103 v4->sin_family = AF_INET;
104 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));
105 memcpy(&v4->sin_addr, &irc->addr.in6_16[6], sizeof(v4->sin_addr));
106 v4->sin_port = htons(irc->port);
113 * This is part of the STATS replies. There is no offical numeric for this
114 * since this isnt an official command, in much the same way as HASH isnt.
115 * It is also possible that some systems wont support this call or have
116 * different field names for "struct rusage".
119 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
121 #ifdef HAVE_GETRUSAGE
134 hzz = sysconf(_SC_CLK_TCK);
139 assert(0 != enumerator);
140 if (getrusage(RUSAGE_SELF, &rus) == -1)
143 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
147 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
148 (long)(secs / 60), (long)(secs % 60),
149 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
150 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
151 (*enumerator)(cptr, buf);
153 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
155 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
156 rus.ru_isrss / (uptime * hzz));
157 (*enumerator)(cptr, buf);
159 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
160 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
161 (*enumerator)(cptr, buf);
163 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
164 (*enumerator)(cptr, buf);
166 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
167 (*enumerator)(cptr, buf);
169 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
170 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
171 (*enumerator)(cptr, buf);
173 #else /* HAVE_GETRUSAGE */
178 int hzz = 1, ticpermin;
179 int umin, smin, usec, ssec;
181 assert(0 != enumerator);
183 hzz = sysconf(_SC_CLK_TCK);
185 ticpermin = hzz * 60;
187 umin = tmsbuf.tms_utime / ticpermin;
188 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
189 smin = tmsbuf.tms_stime / ticpermin;
190 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
192 mins = (secs / 60) + umin + smin;
195 if (times(&tmsbuf) == -1)
197 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
199 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
200 mins, secs, umin, usec, smin, ssec);
201 (*enumerator)(cptr, buf);
202 #endif /* HAVE_TIMES */
203 #endif /* HAVE_GETRUSAGE */
207 int os_get_sockerr(int fd)
210 #if defined(SO_ERROR)
211 unsigned int len = sizeof(err);
212 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
220 * Set the client connection into non-blocking mode. If your
221 * system doesn't support this, you can make this a dummy
222 * function (and get all the old problems that plagued the
223 * blocking version of IRC--not a problem if you are a
224 * lightly loaded node...)
226 int os_set_nonblocking(int fd)
234 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
235 * these ioctl's. There are quite a few variations on them,
236 * as can be seen by the PCS one. They are *NOT* all the same.
237 * Heed this well. - Avalon.
246 /* This portion of code might also apply to NeXT. -LynX */
249 if (ioctl(fd, FIONBIO, &res) == -1)
252 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
254 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
264 int os_set_reuseaddr(int fd)
266 unsigned int opt = 1;
267 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
268 (const char*) &opt, sizeof(opt)));
271 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
273 unsigned int sopt = ssize;
274 unsigned int ropt = rsize;
275 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
276 (const char*) &ropt, sizeof(ropt)) &&
277 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
278 (const char*) &sopt, sizeof(sopt)));
281 int os_set_tos(int fd,int tos)
283 #if defined(IP_TOS) && defined(IPPROTO_IP)
284 unsigned int opt = tos;
285 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
291 int os_disable_options(int fd)
293 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
294 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
301 * Try and find the correct name to use with getrlimit() for setting the max.
302 * number of files allowed to be open by this process.
305 #define RLIMIT_FD_MAX RLIMIT_FDMAX
308 #define RLIMIT_FD_MAX RLIMIT_NOFILE
310 #ifdef RLIMIT_OPEN_MAX
311 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
318 int os_set_fdlimit(unsigned int max_descriptors)
320 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
323 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
324 if (limit.rlim_max < MAXCONNECTIONS)
325 return limit.rlim_max;
326 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
327 return setrlimit(RLIMIT_FD_MAX, &limit);
329 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
334 * os_recv_nonb - non blocking read of a connection
336 * 1 if data was read or socket is blocked (recoverable error)
337 * count_out > 0 if data was read
339 * 0 if socket closed from other end
340 * -1 if an unrecoverable error occurred
342 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
343 unsigned int* count_out)
347 assert(0 != count_out);
351 if (0 < (res = recv(fd, buf, length, 0))) {
352 *count_out = (unsigned) res;
356 if (EWOULDBLOCK == errno || EAGAIN == errno
369 * 0 == client closed the connection
375 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
376 unsigned int* length_out,
377 struct irc_sockaddr* addr_out)
379 struct sockaddr_native addr;
380 unsigned int len = sizeof(addr);
383 assert(0 != length_out);
384 assert(0 != addr_out);
388 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) &addr, &len);
390 if (EWOULDBLOCK == errno || ENOMEM == errno
401 sockaddr_to_irc(&addr, addr_out);
407 * os_sendto_nonb - non blocking send to a UDP socket
409 * 1 if data was written, *count_out contains number of bytes
410 * 0 if sendto call blocked
411 * -1 if an unrecoverable error occurred
413 IOResult os_sendto_nonb(int fd, const char* buf, unsigned int length,
414 unsigned int* count_out, unsigned int flags,
415 const struct irc_sockaddr* peer)
417 struct sockaddr_native addr;
424 sockaddr_from_irc(&addr, peer, 1);
425 if (-1 < (res = sendto(fd, buf, length, flags, (struct sockaddr*)&addr, sizeof(addr)))) {
427 *count_out = (unsigned) res;
430 else if (EWOULDBLOCK == errno || EAGAIN == errno
443 * os_send_nonb - non blocking read of a connection
445 * 1 if data was written
446 * count_out contains amount written
448 * 0 if write call blocked, recoverable error
449 * -1 if an unrecoverable error occurred
451 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
452 unsigned int* count_out)
456 assert(0 != count_out);
460 if (-1 < (res = send(fd, buf, length, 0))) {
461 *count_out = (unsigned) res;
464 else if (EWOULDBLOCK == errno || EAGAIN == errno
476 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
477 unsigned int* count_out)
481 struct iovec iov[IOV_MAX];
484 assert(0 != count_in);
485 assert(0 != count_out);
491 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
493 if (-1 < (res = writev(fd, iov, count))) {
494 *count_out = (unsigned) res;
497 else if (EWOULDBLOCK == errno || EAGAIN == errno
510 int os_socket(const struct irc_sockaddr* local, int type, const char* port_name)
518 fd = socket(family, type, 0);
520 report_error(SOCKET_ERROR_MSG, port_name, errno);
523 if (fd > MAXCLIENTS - 1) {
524 report_error(CONNLIMIT_ERROR_MSG, port_name, 0);
528 if (!os_set_reuseaddr(fd)) {
529 report_error(REUSEADDR_ERROR_MSG, port_name, errno);
533 if (!os_set_nonblocking(fd)) {
534 report_error(NONB_ERROR_MSG, port_name, errno);
539 struct sockaddr_native addr;
540 sockaddr_from_irc(&addr, local, 1);
541 if (bind(fd, (struct sockaddr*)&addr, sizeof(addr))) {
542 report_error(BIND_ERROR_MSG, port_name, errno);
550 int os_accept(int fd, struct irc_sockaddr* peer)
552 struct sockaddr_native addr;
556 addrlen = sizeof(addr);
557 new_fd = accept(fd, (struct sockaddr*)&addr, &addrlen);
559 memset(peer, 0, sizeof(*peer));
561 sockaddr_to_irc(&addr, peer);
565 IOResult os_connect_nonb(int fd, const struct irc_sockaddr* sin)
567 struct sockaddr_native addr;
568 sockaddr_from_irc(&addr, sin, 1);
569 if (connect(fd, (struct sockaddr*) &addr, sizeof(addr)))
570 return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
574 int os_get_sockname(int fd, struct irc_sockaddr* sin_out)
576 struct sockaddr_native addr;
577 unsigned int len = sizeof(addr);
579 assert(0 != sin_out);
580 if (getsockname(fd, (struct sockaddr*) &addr, &len))
582 sockaddr_to_irc(&addr, sin_out);
586 int os_get_peername(int fd, struct irc_sockaddr* sin_out)
588 struct sockaddr_native addr;
589 unsigned int len = sizeof(addr);
591 assert(0 != sin_out);
592 if (getpeername(fd, (struct sockaddr*) &addr, &len))
594 sockaddr_to_irc(&addr, sin_out);
598 int os_set_listen(int fd, int backlog)
600 return (0 == listen(fd, backlog));