Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / os_linux.c
1 /*
2  * IRC - Internet Relay Chat, ircd/os_linux.c
3  * Copyright (C) 1999 Thomas Helvey
4  *
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)
8  * any later version.
9  *
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.
14  *
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.
18  *
19  * $Id$
20  *
21  */
22 #include "config.h"
23
24 #define _XOPEN_SOURCE   /* make limits.h #define IOV_MAX */
25
26 #include "ircd_osdep.h"
27 #include "msgq.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <netinet/in.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/resource.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
40 #include <sys/times.h>
41 #include <sys/uio.h>
42 #include <sys/param.h>
43 #if 0
44 #include <unistd.h>
45 #endif
46
47 /*
48  * This is part of the STATS replies. There is no offical numeric for this
49  * since this isnt an official command, in much the same way as HASH isnt.
50  * It is also possible that some systems wont support this call or have
51  * different field names for "struct rusage".
52  * -avalon
53  */
54 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
55 {
56   char buf[256];
57   struct rusage rus;
58   struct tms tmsbuf;
59   time_t secs;
60   time_t mins;
61   int umin;
62   int smin;
63   int usec;
64   int ssec;
65   int ticpermin = HZ * 60;
66   unsigned int tick_count = uptime * HZ;
67
68   if (0 == tick_count)
69     ++tick_count;
70
71   assert(0 != enumerator);
72   if (getrusage(RUSAGE_SELF, &rus) == -1)
73     return 0;
74
75   secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
76   if (secs == 0)
77     secs = 1;
78
79   sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
80           secs / 60, secs % 60,
81           rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
82           rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
83   (*enumerator)(cptr, buf);
84
85   sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
86           rus.ru_maxrss,
87           rus.ru_ixrss / tick_count, rus.ru_idrss / tick_count,
88           rus.ru_isrss / tick_count);
89   (*enumerator)(cptr, buf);
90
91   sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
92           rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
93   (*enumerator)(cptr, buf);
94
95   sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
96   (*enumerator)(cptr, buf);
97   
98   sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
99   (*enumerator)(cptr, buf);
100
101   sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
102           rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
103   (*enumerator)(cptr, buf);
104
105   if (times(&tmsbuf) == -1)
106     return 0;
107
108   umin = tmsbuf.tms_utime / ticpermin;
109   usec = (tmsbuf.tms_utime % ticpermin) / (float)HZ;
110   smin = tmsbuf.tms_stime / ticpermin;
111   ssec = (tmsbuf.tms_stime % ticpermin) / (float)HZ;
112   secs = usec + ssec;
113   mins = (secs / 60) + umin + smin;
114   secs %= HZ;
115
116   sprintf(buf, "CPU Secs %ld:%ld User %d:%d System %d:%d", 
117           mins, secs, umin, usec, smin, ssec);
118   (*enumerator)(cptr, buf);
119   return 1;
120 }
121
122 int os_get_sockerr(int fd)
123 {
124   int    err = 0;
125   unsigned int len = sizeof(err);
126   getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
127   return err;
128 }
129
130 /*
131  * set_non_blocking
132  *
133  * Set the client connection into non-blocking mode. If your
134  * system doesn't support this, you can make this a dummy
135  * function (and get all the old problems that plagued the
136  * blocking version of IRC--not a problem if you are a
137  * lightly loaded node...)
138  */
139 int os_set_nonblocking(int fd)
140 {
141   int res = 1;
142   return (0 == ioctl(fd, FIONBIO, &res));
143 }
144
145
146 /*
147  *  set_sock_opts
148  */
149 int os_set_reuseaddr(int fd)
150 {
151   unsigned int opt = 1;
152   return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)));
153 }
154
155 int os_set_sockbufs(int fd, unsigned int size)
156 {
157   unsigned int opt = size;
158   return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &opt, sizeof(opt)) &&
159           0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt)));
160 }
161
162 int os_set_tos(int fd,int tos)
163 {
164   unsigned int opt = tos;
165   return (0 == setsockopt(fd, SOL_IP, IP_TOS, &opt, sizeof(opt)));
166 }
167
168 int os_disable_options(int fd)
169 {
170   return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
171 }
172
173 int os_set_fdlimit(unsigned int max_descriptors)
174 {
175   struct rlimit limit;
176
177   if (!getrlimit(RLIMIT_NOFILE, &limit)) {
178     if (limit.rlim_max < max_descriptors)
179       return limit.rlim_max;
180     limit.rlim_cur = limit.rlim_max;    /* make soft limit the max */
181     return setrlimit(RLIMIT_NOFILE, &limit);
182   }
183   return 0;
184 }
185
186 /*
187  * os_recv_nonb - non blocking read of a connection
188  * returns:
189  *  1  if data was read or socket is blocked (recoverable error)
190  *    count_out > 0 if data was read
191  *   
192  *  0  if socket closed from other end
193  *  -1 if an unrecoverable error occurred
194  */
195 IOResult os_recv_nonb(int fd, char* buf, unsigned int length, 
196                  unsigned int* count_out)
197 {
198   int res;
199   assert(0 != buf);
200   assert(0 != count_out);
201   *count_out = 0;
202   errno = 0;
203
204   if (0 < (res = recv(fd, buf, length, 0))) {
205     *count_out = (unsigned) res;
206     return IO_SUCCESS;
207   }
208   else if (res < 0) {
209     if (EWOULDBLOCK == errno || EAGAIN == errno)
210       return IO_BLOCKED;
211     else
212       return IO_FAILURE;
213   } 
214   /*
215    * 0   == client closed the connection
216    * < 1 == error
217    */
218   return IO_FAILURE;
219 }
220
221 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length, 
222                           unsigned int* length_out, struct sockaddr_in* sin_out)
223 {
224   int    res;
225   unsigned int len = sizeof(struct sockaddr_in);
226   assert(0 != buf);
227   assert(0 != length_out);
228   assert(0 != sin_out);
229   errno = 0;
230
231   res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
232   if (-1 == res) {
233     if (EWOULDBLOCK == errno || ENOMEM == errno)
234       return IO_BLOCKED;
235     return IO_FAILURE;
236   }
237   *length_out = res;
238   return IO_SUCCESS;
239 }
240
241 /*
242  * os_send_nonb - non blocking read of a connection
243  * returns:
244  *  1  if data was written
245  *    count_out contains amount written
246  *   
247  *  0  if write call blocked, recoverable error
248  *  -1 if an unrecoverable error occurred
249  */
250 IOResult os_send_nonb(int fd, const char* buf, unsigned int length, 
251                  unsigned int* count_out)
252 {
253   int res;
254   assert(0 != buf);
255   assert(0 != count_out);
256   *count_out = 0;
257   errno = 0;
258
259   if (-1 < (res = send(fd, buf, length, 0))) {
260     *count_out = (unsigned) res;
261     return IO_SUCCESS;
262   }
263   else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
264     return IO_BLOCKED;
265
266   return IO_FAILURE;
267 }
268
269 /*
270  * os_sendv_nonb - non blocking writev to a connection
271  * returns:
272  *  1  if data was written
273  *    count_out contains amount written
274  *   
275  *  0  if write call blocked, recoverable error
276  *  -1 if an unrecoverable error occurred
277  */
278 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
279                        unsigned int* count_out)
280 {
281   int res;
282   int count;
283   struct iovec iov[IOV_MAX];
284
285   assert(0 != buf);
286   assert(0 != count_in);
287   assert(0 != count_out);
288
289   *count_in = 0;
290   *count_out = 0;
291   errno = 0;
292
293   count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
294
295   if (-1 < (res = writev(fd, iov, count))) {
296     *count_out = (unsigned) res;
297     return IO_SUCCESS;
298   }
299   else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
300     return IO_BLOCKED;
301
302   return IO_FAILURE;
303 }
304
305
306 IOResult os_connect_nonb(int fd, const struct sockaddr_in* sin)
307 {
308   if (connect(fd, (const struct sockaddr*) sin, sizeof(struct sockaddr_in)))
309     return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
310   return IO_SUCCESS;
311 }
312       
313 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
314 {
315   unsigned int len = sizeof(struct sockaddr_in);
316   assert(0 != sin_out);
317   return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
318 }
319
320 int os_get_peername(int fd, struct sockaddr_in* sin_out)
321 {
322   unsigned int len = sizeof(struct sockaddr_in);
323   assert(0 != sin_out);
324   return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
325 }
326
327 int os_set_listen(int fd, int backlog)
328 {
329   /*
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
333    */
334   return (0 == listen(fd, backlog));
335 }
336