2 * IRC - Internet Relay Chat, ircd/os_linux.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"
32 #include <netinet/in.h>
34 #include <sys/ioctl.h>
35 #include <sys/resource.h>
36 #include <sys/socket.h>
37 #include <sys/types.h>
38 #include <sys/times.h>
40 #include <sys/param.h>
46 * This is part of the STATS replies. There is no offical numeric for this
47 * since this isnt an official command, in much the same way as HASH isnt.
48 * It is also possible that some systems wont support this call or have
49 * different field names for "struct rusage".
52 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
63 int ticpermin = HZ * 60;
64 unsigned int tick_count = uptime * HZ;
69 assert(0 != enumerator);
70 if (getrusage(RUSAGE_SELF, &rus) == -1)
73 secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
77 sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
79 rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
80 rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
81 (*enumerator)(cptr, buf);
83 sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
85 rus.ru_ixrss / tick_count, rus.ru_idrss / tick_count,
86 rus.ru_isrss / tick_count);
87 (*enumerator)(cptr, buf);
89 sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
90 rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
91 (*enumerator)(cptr, buf);
93 sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
94 (*enumerator)(cptr, buf);
96 sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
97 (*enumerator)(cptr, buf);
99 sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
100 rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
101 (*enumerator)(cptr, buf);
103 if (times(&tmsbuf) == -1)
106 umin = tmsbuf.tms_utime / ticpermin;
107 usec = (tmsbuf.tms_utime % ticpermin) / (float)HZ;
108 smin = tmsbuf.tms_stime / ticpermin;
109 ssec = (tmsbuf.tms_stime % ticpermin) / (float)HZ;
111 mins = (secs / 60) + umin + smin;
114 sprintf(buf, "CPU Secs %ld:%ld User %d:%d System %d:%d",
115 mins, secs, umin, usec, smin, ssec);
116 (*enumerator)(cptr, buf);
120 int os_get_sockerr(int fd)
123 unsigned int len = sizeof(err);
124 getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
131 * Set the client connection into non-blocking mode. If your
132 * system doesn't support this, you can make this a dummy
133 * function (and get all the old problems that plagued the
134 * blocking version of IRC--not a problem if you are a
135 * lightly loaded node...)
137 int os_set_nonblocking(int fd)
140 return (0 == ioctl(fd, FIONBIO, &res));
147 int os_set_reuseaddr(int fd)
149 unsigned int opt = 1;
150 return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)));
153 int os_set_sockbufs(int fd, unsigned int size)
155 unsigned int opt = size;
156 return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) &&
157 0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)));
160 int os_set_tos(int fd,int tos)
162 unsigned int opt = tos;
163 return (0 == setsockopt(fd, SOL_IP, IP_TOS, &opt, sizeof(opt)));
166 int os_disable_options(int fd)
168 return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
171 int os_set_fdlimit(unsigned int max_descriptors)
175 if (!getrlimit(RLIMIT_NOFILE, &limit)) {
176 if (limit.rlim_max < max_descriptors)
177 return limit.rlim_max;
178 limit.rlim_cur = limit.rlim_max; /* make soft limit the max */
179 return setrlimit(RLIMIT_NOFILE, &limit);
185 * os_recv_nonb - non blocking read of a connection
187 * 1 if data was read or socket is blocked (recoverable error)
188 * count_out > 0 if data was read
190 * 0 if socket closed from other end
191 * -1 if an unrecoverable error occurred
193 IOResult os_recv_nonb(int fd, char* buf, unsigned int length,
194 unsigned int* count_out)
198 assert(0 != count_out);
202 if (0 < (res = recv(fd, buf, length, 0))) {
203 *count_out = (unsigned) res;
207 if (EWOULDBLOCK == errno || EAGAIN == errno)
213 * 0 == client closed the connection
219 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length,
220 unsigned int* length_out, struct sockaddr_in* sin_out)
223 unsigned int len = sizeof(struct sockaddr_in);
225 assert(0 != length_out);
226 assert(0 != sin_out);
229 res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
231 if (EWOULDBLOCK == errno || ENOMEM == errno)
240 * os_send_nonb - non blocking read of a connection
242 * 1 if data was written
243 * count_out contains amount written
245 * 0 if write call blocked, recoverable error
246 * -1 if an unrecoverable error occurred
248 IOResult os_send_nonb(int fd, const char* buf, unsigned int length,
249 unsigned int* count_out)
253 assert(0 != count_out);
257 if (-1 < (res = send(fd, buf, length, 0))) {
258 *count_out = (unsigned) res;
261 else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
268 * os_sendv_nonb - non blocking writev to a connection
270 * 1 if data was written
271 * count_out contains amount written
273 * 0 if write call blocked, recoverable error
274 * -1 if an unrecoverable error occurred
276 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
277 unsigned int* count_out)
281 struct iovec iov[IOV_MAX];
284 assert(0 != count_in);
285 assert(0 != count_out);
291 count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
293 if (-1 < (res = writev(fd, iov, count))) {
294 *count_out = (unsigned) res;
297 else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
304 int os_connect_nonb(int fd, const struct sockaddr_in* sin)
306 if (connect(fd, (const struct sockaddr*) sin, sizeof(struct sockaddr_in))) {
307 if (errno != EINPROGRESS)
313 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
315 unsigned int len = sizeof(struct sockaddr_in);
316 assert(0 != sin_out);
317 return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
320 int os_get_peername(int fd, struct sockaddr_in* sin_out)
322 unsigned int len = sizeof(struct sockaddr_in);
323 assert(0 != sin_out);
324 return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
327 int os_set_listen(int fd, int backlog)
330 * for linux 2.2 backlog is the number of connections ready to be accepted
331 * not the max syn requests, there is a kernel tweak there to set the max
332 * syn request queue length
334 return (0 == listen(fd, backlog));