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 /* make limits.h #define IOV_MAX */
26 #include "ircd_osdep.h"
30 #include <sys/types.h>
32 #include <sys/resource.h>
36 #include <netinet/in.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
45 #define IOV_MAX 16 /* minimum required */
49 #include <sys/syscall.h>
50 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
54 * This is part of the STATS replies. There is no offical numeric for this
55 * since this isnt an official command, in much the same way as HASH isnt.
56 * It is also possible that some systems wont support this call or have
57 * different field names for "struct rusage".
60 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
75 hzz = sysconf(_SC_CLK_TCK);
80 assert(0 != enumerator);
81 if (getrusage(RUSAGE_SELF, &rus) == -1)
84 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
88 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
90 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
91 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
92 (*enumerator)(cptr, buf);
94 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
96 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
97 rus.ru_isrss / (uptime * hzz));
98 (*enumerator)(cptr, buf);
100 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
101 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
102 (*enumerator)(cptr, buf);
104 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
105 (*enumerator)(cptr, buf);
107 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
108 (*enumerator)(cptr, buf);
110 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
111 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
112 (*enumerator)(cptr, buf);
114 #else /* HAVE_GETRUSAGE */
119 int hzz = 1, ticpermin;
120 int umin, smin, usec, ssec;
122 assert(0 != enumerator);
124 hzz = sysconf(_SC_CLK_TCK);
126 ticpermin = hzz * 60;
128 umin = tmsbuf.tms_utime / ticpermin;
129 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
130 smin = tmsbuf.tms_stime / ticpermin;
131 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
133 mins = (secs / 60) + umin + smin;
136 if (times(&tmsbuf) == -1)
138 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
140 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
141 mins, secs, umin, usec, smin, ssec);
142 (*enumerator)(cptr, buf);
143 #endif /* HAVE_TIMES */
144 #endif /* HAVE_GETRUSAGE */
148 int os_get_sockerr(int fd)
151 #if defined(SO_ERROR)
152 unsigned int len = sizeof(err);
153 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
161 * Set the client connection into non-blocking mode. If your
162 * system doesn't support this, you can make this a dummy
163 * function (and get all the old problems that plagued the
164 * blocking version of IRC--not a problem if you are a
165 * lightly loaded node...)
167 int os_set_nonblocking(int fd)
175 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
176 * these ioctl's. There are quite a few variations on them,
177 * as can be seen by the PCS one. They are *NOT* all the same.
178 * Heed this well. - Avalon.
187 /* This portion of code might also apply to NeXT. -LynX */
190 if (ioctl(fd, FIONBIO, &res) == -1)
193 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
195 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
205 int os_set_reuseaddr(int fd)
207 unsigned int opt = 1;
208 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
209 (const char*) &opt, sizeof(opt)));
212 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
214 unsigned int sopt = ssize;
215 unsigned int ropt = rsize;
216 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
217 (const char*) &ropt, sizeof(ropt)) &&
218 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
219 (const char*) &sopt, sizeof(sopt)));
222 int os_set_tos(int fd,int tos)
224 unsigned int opt = tos;
225 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
228 int os_disable_options(int fd)
230 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
231 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
238 * Try and find the correct name to use with getrlimit() for setting the max.
239 * number of files allowed to be open by this process.
242 #define RLIMIT_FD_MAX RLIMIT_FDMAX
245 #define RLIMIT_FD_MAX RLIMIT_NOFILE
247 #ifdef RLIMIT_OPEN_MAX
248 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
255 int os_set_fdlimit(unsigned int max_descriptors)
257 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
260 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
261 if (limit.rlim_max < MAXCONNECTIONS)
262 return limit.rlim_max;
263 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
264 return setrlimit(RLIMIT_FD_MAX, &limit);
266 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
270 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
271 unsigned int* count_out)
275 assert(0 != count_out);
279 if (0 < (res = recv(fd, buf, length, 0))) {
280 *count_out = (unsigned) res;
284 if (EWOULDBLOCK == errno || EAGAIN == errno)
290 * 0 == client closed the connection
296 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
297 unsigned int* length_out, struct sockaddr_in* sin_out)
300 unsigned int len = sizeof(struct sockaddr_in);
302 assert(0 != length_out);
303 assert(0 != sin_out);
307 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
309 if (EWOULDBLOCK == errno || ENOMEM == errno)
317 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
318 unsigned int* count_out)
322 assert(0 != count_out);
326 if (-1 < (res = send(fd, buf, length, 0))) {
327 *count_out = (unsigned) res;
330 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
331 ENOMEM == errno || ENOBUFS == errno)
336 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
337 unsigned int* count_out)
341 struct iovec iov[IOV_MAX];
344 assert(0 != count_in);
345 assert(0 != count_out);
351 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
353 if (-1 < (res = writev(fd, iov, count))) {
354 *count_out = (unsigned) res;
357 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
358 ENOMEM == errno || ENOBUFS == errno)
364 IOResult os_connect_nonb(int fd, const struct sockaddr_in* sin)
366 if (connect(fd, (struct sockaddr*) sin, sizeof(struct sockaddr_in)))
367 return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
371 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
373 unsigned int len = sizeof(struct sockaddr_in);
374 assert(0 != sin_out);
375 return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
378 int os_get_peername(int fd, struct sockaddr_in* sin_out)
380 unsigned int len = sizeof(struct sockaddr_in);
381 assert(0 != sin_out);
382 return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
385 int os_set_listen(int fd, int backlog)
387 return (0 == listen(fd, backlog));