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.
22 #define _XOPEN_SOURCE /* make limits.h #define IOV_MAX */
24 #include "ircd_osdep.h"
29 #include <sys/types.h>
31 #include <sys/resource.h>
35 #include <netinet/in.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
44 #define IOV_MAX 16 /* minimum required */
48 #include <sys/syscall.h>
49 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
53 * This is part of the STATS replies. There is no offical numeric for this
54 * since this isnt an official command, in much the same way as HASH isnt.
55 * It is also possible that some systems wont support this call or have
56 * different field names for "struct rusage".
59 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
74 hzz = sysconf(_SC_CLK_TCK);
79 assert(0 != enumerator);
80 if (getrusage(RUSAGE_SELF, &rus) == -1)
83 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
87 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
89 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
90 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
91 (*enumerator)(cptr, buf);
93 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
95 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
96 rus.ru_isrss / (uptime * hzz));
97 (*enumerator)(cptr, buf);
99 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
100 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
101 (*enumerator)(cptr, buf);
103 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
104 (*enumerator)(cptr, buf);
106 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
107 (*enumerator)(cptr, buf);
109 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
110 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
111 (*enumerator)(cptr, buf);
113 #else /* HAVE_GETRUSAGE */
117 int hzz = 1, ticpermin;
118 int umin, smin, usec, ssec;
120 assert(0 != enumerator);
122 hzz = sysconf(_SC_CLK_TCK);
124 ticpermin = hzz * 60;
126 umin = tmsbuf.tms_utime / ticpermin;
127 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
128 smin = tmsbuf.tms_stime / ticpermin;
129 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
131 mins = (secs / 60) + umin + smin;
134 if (times(&tmsbuf) == -1)
136 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
138 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
139 mins, secs, umin, usec, smin, ssec);
140 (*enumerator)(cptr, buf);
141 #endif /* HAVE_TIMES */
142 #endif /* HAVE_GETRUSAGE */
146 int os_get_sockerr(int fd)
149 #if defined(SO_ERROR)
150 unsigned int len = sizeof(err);
151 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
159 * Set the client connection into non-blocking mode. If your
160 * system doesn't support this, you can make this a dummy
161 * function (and get all the old problems that plagued the
162 * blocking version of IRC--not a problem if you are a
163 * lightly loaded node...)
165 int os_set_nonblocking(int fd)
173 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
174 * these ioctl's. There are quite a few variations on them,
175 * as can be seen by the PCS one. They are *NOT* all the same.
176 * Heed this well. - Avalon.
185 /* This portion of code might also apply to NeXT. -LynX */
188 if (ioctl(fd, FIONBIO, &res) == -1)
191 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
193 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
203 int os_set_reuseaddr(int fd)
205 unsigned int opt = 1;
206 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
207 (const char*) &opt, sizeof(opt)));
210 int os_set_sockbufs(int fd, unsigned int size)
212 unsigned int opt = size;
213 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
214 (const char*) &opt, sizeof(opt)) &&
215 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
216 (const char*) &opt, sizeof(opt)));
219 int os_set_tos(int fd,int tos)
221 unsigned int opt = tos;
222 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
225 int os_disable_options(int fd)
227 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
228 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
235 * Try and find the correct name to use with getrlimit() for setting the max.
236 * number of files allowed to be open by this process.
239 #define RLIMIT_FD_MAX RLIMIT_FDMAX
242 #define RLIMIT_FD_MAX RLIMIT_NOFILE
244 #ifdef RLIMIT_OPEN_MAX
245 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
252 int os_set_fdlimit(unsigned int max_descriptors)
254 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
257 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
258 if (limit.rlim_max < MAXCONNECTIONS)
259 return limit.rlim_max;
260 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
261 return setrlimit(RLIMIT_FD_MAX, &limit);
263 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
267 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
268 unsigned int* count_out)
272 assert(0 != count_out);
276 if (0 < (res = recv(fd, buf, length, 0))) {
277 *count_out = (unsigned) res;
281 if (EWOULDBLOCK == errno || EAGAIN == errno)
287 * 0 == client closed the connection
293 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
294 unsigned int* length_out, struct sockaddr_in* sin_out)
297 unsigned int len = sizeof(struct sockaddr_in);
299 assert(0 != length_out);
300 assert(0 != sin_out);
304 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
306 if (EWOULDBLOCK == errno || ENOMEM == errno)
314 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
315 unsigned int* count_out)
319 assert(0 != count_out);
323 if (-1 < (res = send(fd, buf, length, 0))) {
324 *count_out = (unsigned) res;
327 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
328 ENOMEM == errno || ENOBUFS == errno)
333 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
334 unsigned int* count_out)
338 struct iovec iov[IOV_MAX];
341 assert(0 != count_in);
342 assert(0 != count_out);
348 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
350 if (-1 < (res = writev(fd, iov, count))) {
351 *count_out = (unsigned) res;
354 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
355 ENOMEM == errno || ENOBUFS == errno)
361 int os_connect_nonb(int fd, const struct sockaddr_in* sin)
363 if (connect(fd, (struct sockaddr*) sin, sizeof(struct sockaddr_in))) {
364 if (errno != EINPROGRESS)
370 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
372 unsigned int len = sizeof(struct sockaddr_in);
373 assert(0 != sin_out);
374 return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
377 int os_get_peername(int fd, struct sockaddr_in* sin_out)
379 unsigned int len = sizeof(struct sockaddr_in);
380 assert(0 != sin_out);
381 return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
384 int os_set_listen(int fd, int backlog)
386 return (0 == listen(fd, backlog));