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 #include <sys/syscall.h>
45 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
49 * This is part of the STATS replies. There is no offical numeric for this
50 * since this isnt an official command, in much the same way as HASH isnt.
51 * It is also possible that some systems wont support this call or have
52 * different field names for "struct rusage".
55 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
70 hzz = sysconf(_SC_CLK_TCK);
75 assert(0 != enumerator);
76 if (getrusage(RUSAGE_SELF, &rus) == -1)
79 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
83 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
85 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
86 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
87 (*enumerator)(cptr, buf);
89 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
91 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
92 rus.ru_isrss / (uptime * hzz));
93 (*enumerator)(cptr, buf);
95 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
96 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
97 (*enumerator)(cptr, buf);
99 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
100 (*enumerator)(cptr, buf);
102 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
103 (*enumerator)(cptr, buf);
105 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
106 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
107 (*enumerator)(cptr, buf);
109 #else /* HAVE_GETRUSAGE */
113 int hzz = 1, ticpermin;
114 int umin, smin, usec, ssec;
116 assert(0 != enumerator);
118 hzz = sysconf(_SC_CLK_TCK);
120 ticpermin = hzz * 60;
122 umin = tmsbuf.tms_utime / ticpermin;
123 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
124 smin = tmsbuf.tms_stime / ticpermin;
125 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
127 mins = (secs / 60) + umin + smin;
130 if (times(&tmsbuf) == -1)
132 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
134 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
135 mins, secs, umin, usec, smin, ssec);
136 (*enumerator)(cptr, buf);
137 #endif /* HAVE_TIMES */
138 #endif /* HAVE_GETRUSAGE */
142 int os_get_sockerr(int fd)
145 #if defined(SO_ERROR)
146 unsigned int len = sizeof(err);
147 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
155 * Set the client connection into non-blocking mode. If your
156 * system doesn't support this, you can make this a dummy
157 * function (and get all the old problems that plagued the
158 * blocking version of IRC--not a problem if you are a
159 * lightly loaded node...)
161 int os_set_nonblocking(int fd)
169 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
170 * these ioctl's. There are quite a few variations on them,
171 * as can be seen by the PCS one. They are *NOT* all the same.
172 * Heed this well. - Avalon.
181 /* This portion of code might also apply to NeXT. -LynX */
184 if (ioctl(fd, FIONBIO, &res) == -1)
187 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
189 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
199 int os_set_reuseaddr(int fd)
201 unsigned int opt = 1;
202 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
203 (const char*) &opt, sizeof(opt)));
206 int os_set_sockbufs(int fd, unsigned int size)
208 unsigned int opt = size;
209 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
210 (const char*) &opt, sizeof(opt)) &&
211 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
212 (const char*) &opt, sizeof(opt)));
215 int os_disable_options(int fd)
217 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
218 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
225 * Try and find the correct name to use with getrlimit() for setting the max.
226 * number of files allowed to be open by this process.
229 #define RLIMIT_FD_MAX RLIMIT_FDMAX
232 #define RLIMIT_FD_MAX RLIMIT_NOFILE
234 #ifdef RLIMIT_OPEN_MAX
235 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
242 int os_set_fdlimit(unsigned int max_descriptors)
244 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
247 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
248 if (limit.rlim_max < MAXCONNECTIONS)
249 return limit.rlim_max;
250 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
251 return setrlimit(RLIMIT_FD_MAX, &limit);
253 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
257 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
258 unsigned int* count_out)
262 assert(0 != count_out);
266 if (0 < (res = recv(fd, buf, length, 0))) {
267 *count_out = (unsigned) res;
271 if (EWOULDBLOCK == errno || EAGAIN == errno)
277 * 0 == client closed the connection
283 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
284 unsigned int* length_out, struct sockaddr_in* sin_out)
287 unsigned int len = sizeof(struct sockaddr_in);
289 assert(0 != length_out);
290 assert(0 != sin_out);
294 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
296 if (EWOULDBLOCK == errno || ENOMEM == errno)
304 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
305 unsigned int* count_out)
309 assert(0 != count_out);
313 if (-1 < (res = send(fd, buf, length, 0))) {
314 *count_out = (unsigned) res;
317 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
318 ENOMEM == errno || ENOBUFS == errno)
323 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
324 unsigned int* count_out)
328 struct iovec iov[IOV_MAX];
331 assert(0 != count_in);
332 assert(0 != count_out);
338 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
340 if (-1 < (res = writev(fd, iov, count))) {
341 *count_out = (unsigned) res;
344 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
345 ENOMEM == errno || ENOBUFS == errno)
351 int os_connect_nonb(int fd, const struct sockaddr_in* sin)
353 if (connect(fd, (struct sockaddr*) sin, sizeof(struct sockaddr_in))) {
354 if (errno != EINPROGRESS)
360 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
362 unsigned int len = sizeof(struct sockaddr_in);
363 assert(0 != sin_out);
364 return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
367 int os_get_peername(int fd, struct sockaddr_in* sin_out)
369 unsigned int len = sizeof(struct sockaddr_in);
370 assert(0 != sin_out);
371 return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
374 int os_set_listen(int fd, int backlog)
376 return (0 == listen(fd, backlog));