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"
33 #include <netinet/in.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/resource.h>
39 #include <sys/socket.h>
48 #define IOV_MAX 16 /* minimum required */
52 #include <sys/syscall.h>
53 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
57 * This is part of the STATS replies. There is no offical numeric for this
58 * since this isnt an official command, in much the same way as HASH isnt.
59 * It is also possible that some systems wont support this call or have
60 * different field names for "struct rusage".
63 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
78 hzz = sysconf(_SC_CLK_TCK);
83 assert(0 != enumerator);
84 if (getrusage(RUSAGE_SELF, &rus) == -1)
87 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
91 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
93 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
94 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
95 (*enumerator)(cptr, buf);
97 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
99 rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
100 rus.ru_isrss / (uptime * hzz));
101 (*enumerator)(cptr, buf);
103 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
104 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
105 (*enumerator)(cptr, buf);
107 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
108 (*enumerator)(cptr, buf);
110 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
111 (*enumerator)(cptr, buf);
113 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
114 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
115 (*enumerator)(cptr, buf);
117 #else /* HAVE_GETRUSAGE */
121 int hzz = 1, ticpermin;
122 int umin, smin, usec, ssec;
124 assert(0 != enumerator);
126 hzz = sysconf(_SC_CLK_TCK);
128 ticpermin = hzz * 60;
130 umin = tmsbuf.tms_utime / ticpermin;
131 usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
132 smin = tmsbuf.tms_stime / ticpermin;
133 ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
135 mins = (secs / 60) + umin + smin;
138 if (times(&tmsbuf) == -1)
140 secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
142 sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d",
143 mins, secs, umin, usec, smin, ssec);
144 (*enumerator)(cptr, buf);
145 #endif /* HAVE_TIMES */
146 #endif /* HAVE_GETRUSAGE */
150 int os_get_sockerr(int fd)
153 #if defined(SO_ERROR)
154 unsigned int len = sizeof(err);
155 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
163 * Set the client connection into non-blocking mode. If your
164 * system doesn't support this, you can make this a dummy
165 * function (and get all the old problems that plagued the
166 * blocking version of IRC--not a problem if you are a
167 * lightly loaded node...)
169 int os_set_nonblocking(int fd)
177 * NOTE: consult ALL your relevant manual pages *BEFORE* changing
178 * these ioctl's. There are quite a few variations on them,
179 * as can be seen by the PCS one. They are *NOT* all the same.
180 * Heed this well. - Avalon.
189 /* This portion of code might also apply to NeXT. -LynX */
192 if (ioctl(fd, FIONBIO, &res) == -1)
195 if ((res = fcntl(fd, F_GETFL, 0)) == -1)
197 else if (fcntl(fd, F_SETFL, res | nonb) == -1)
207 int os_set_reuseaddr(int fd)
209 unsigned int opt = 1;
210 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR,
211 (const char*) &opt, sizeof(opt)));
214 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
216 unsigned int sopt = ssize;
217 unsigned int ropt = rsize;
218 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
219 (const char*) &ropt, sizeof(ropt)) &&
220 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
221 (const char*) &sopt, sizeof(sopt)));
224 int os_set_tos(int fd,int tos)
226 #if defined(IP_TOS) && defined(IPPROTO_IP)
227 unsigned int opt = tos;
228 return (0 == setsockopt(fd, IPPROTO_IP, IP_TOS, &opt, sizeof(opt)));
234 int os_disable_options(int fd)
236 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
237 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
244 * Try and find the correct name to use with getrlimit() for setting the max.
245 * number of files allowed to be open by this process.
248 #define RLIMIT_FD_MAX RLIMIT_FDMAX
251 #define RLIMIT_FD_MAX RLIMIT_NOFILE
253 #ifdef RLIMIT_OPEN_MAX
254 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
261 int os_set_fdlimit(unsigned int max_descriptors)
263 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
266 if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
267 if (limit.rlim_max < MAXCONNECTIONS)
268 return limit.rlim_max;
269 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
270 return setrlimit(RLIMIT_FD_MAX, &limit);
272 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
276 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
277 unsigned int* count_out)
281 assert(0 != count_out);
285 if (0 < (res = recv(fd, buf, length, 0))) {
286 *count_out = (unsigned) res;
290 if (EWOULDBLOCK == errno || EAGAIN == errno)
296 * 0 == client closed the connection
302 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
303 unsigned int* length_out, struct sockaddr_in* sin_out)
306 unsigned int len = sizeof(struct sockaddr_in);
308 assert(0 != length_out);
309 assert(0 != sin_out);
312 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
314 if (EWOULDBLOCK == errno || ENOMEM == errno)
322 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
323 unsigned int* count_out)
327 assert(0 != count_out);
331 if (-1 < (res = send(fd, buf, length, 0))) {
332 *count_out = (unsigned) res;
335 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
336 ENOMEM == errno || ENOBUFS == errno)
341 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
342 unsigned int* count_out)
346 struct iovec iov[IOV_MAX];
349 assert(0 != count_in);
350 assert(0 != count_out);
356 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
358 if (-1 < (res = writev(fd, iov, count))) {
359 *count_out = (unsigned) res;
362 else if (EWOULDBLOCK == errno || EAGAIN == errno ||
363 ENOMEM == errno || ENOBUFS == errno)
369 IOResult os_connect_nonb(int fd, const struct sockaddr_in* sin)
371 if (connect(fd, (struct sockaddr*) sin, sizeof(struct sockaddr_in)))
372 return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
376 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
378 unsigned int len = sizeof(struct sockaddr_in);
379 assert(0 != sin_out);
380 return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
383 int os_get_peername(int fd, struct sockaddr_in* sin_out)
385 unsigned int len = sizeof(struct sockaddr_in);
386 assert(0 != sin_out);
387 return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
390 int os_set_listen(int fd, int backlog)
392 return (0 == listen(fd, backlog));