Author: Kev <klmitch@mit.edu>
[ircu2.10.12-pk.git] / ircd / os_generic.c
1 /*
2  * IRC - Internet Relay Chat, ircd/os_generic.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 #define _XOPEN_SOURCE   /* make limits.h #define IOV_MAX */
23
24 #include "ircd_osdep.h"
25 #include "config.h"
26 #include "msgq.h"
27
28 #include <assert.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <limits.h>
32 #include <netinet/in.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <sys/ioctl.h>
36 #include <sys/types.h>
37 #include <sys/resource.h>
38 #include <sys/socket.h>
39 #include <sys/time.h>
40 #include <sys/uio.h>
41
42 #if 0
43 #include <unistd.h>
44 #endif
45
46 #ifdef HPUX
47 #include <sys/syscall.h>
48 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
49 #endif
50
51 /*
52  * This is part of the STATS replies. There is no offical numeric for this
53  * since this isnt an official command, in much the same way as HASH isnt.
54  * It is also possible that some systems wont support this call or have
55  * different field names for "struct rusage".
56  * -avalon
57  */
58 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
59 {
60   char buf[256];
61 #ifdef HAVE_GETRUSAGE
62   struct rusage rus;
63   time_t secs;
64
65 #ifdef  hz
66 #  define hzz hz
67 #else
68 #  ifdef HZ
69 #    define hzz HZ
70 #  else
71   int hzz = 1;
72 #  ifdef HPUX
73   hzz = sysconf(_SC_CLK_TCK);
74 #  endif
75 #endif
76 #endif
77
78   assert(0 != enumerator);
79   if (getrusage(RUSAGE_SELF, &rus) == -1)
80     return 0;
81
82   secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
83   if (secs == 0)
84     secs = 1;
85
86   sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
87           secs / 60, secs % 60,
88           rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
89           rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
90   (*enumerator)(cptr, buf);
91
92   sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
93           rus.ru_maxrss,
94           rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
95           rus.ru_isrss / (uptime * hzz));
96   (*enumerator)(cptr, buf);
97
98   sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
99           rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
100   (*enumerator)(cptr, buf);
101
102   sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
103   (*enumerator)(cptr, buf);
104   
105   sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
106   (*enumerator)(cptr, buf);
107
108   sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
109           rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
110   (*enumerator)(cptr, buf);
111
112 #else /* HAVE_GETRUSAGE */
113 #if HAVE_TIMES
114   struct tms tmsbuf;
115   time_t secs, mins;
116   int hzz = 1, ticpermin;
117   int umin, smin, usec, ssec;
118
119   assert(0 != enumerator);
120 #ifdef HPUX
121   hzz = sysconf(_SC_CLK_TCK);
122 #endif
123   ticpermin = hzz * 60;
124
125   umin = tmsbuf.tms_utime / ticpermin;
126   usec = (tmsbuf.tms_utime % ticpermin) / (float)hzz;
127   smin = tmsbuf.tms_stime / ticpermin;
128   ssec = (tmsbuf.tms_stime % ticpermin) / (float)hzz;
129   secs = usec + ssec;
130   mins = (secs / 60) + umin + smin;
131   secs %= hzz;
132
133   if (times(&tmsbuf) == -1)
134     return 0;
135   secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
136
137   sprintf(buf, "CPU Secs %d:%d User %d:%d System %d:%d", 
138           mins, secs, umin, usec, smin, ssec);
139   (*enumerator)(cptr, buf);
140 #endif /* HAVE_TIMES */
141 #endif /* HAVE_GETRUSAGE */
142   return 1;
143 }
144
145 int os_get_sockerr(int fd)
146 {
147   int    err = 0;
148 #if defined(SO_ERROR)
149   unsigned int len = sizeof(err);
150   getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
151 #endif
152   return err;
153 }
154
155 /*
156  * set_non_blocking
157  *
158  * Set the client connection into non-blocking mode. If your
159  * system doesn't support this, you can make this a dummy
160  * function (and get all the old problems that plagued the
161  * blocking version of IRC--not a problem if you are a
162  * lightly loaded node...)
163  */
164 int os_set_nonblocking(int fd)
165 {
166   int res;
167 #ifndef NBLOCK_SYSV
168   int nonb = 0;
169 #endif
170
171   /*
172    * NOTE: consult ALL your relevant manual pages *BEFORE* changing
173    * these ioctl's. There are quite a few variations on them,
174    * as can be seen by the PCS one. They are *NOT* all the same.
175    * Heed this well. - Avalon.
176    */
177 #ifdef  NBLOCK_POSIX
178   nonb |= O_NONBLOCK;
179 #endif
180 #ifdef  NBLOCK_BSD
181   nonb |= O_NDELAY;
182 #endif
183 #ifdef  NBLOCK_SYSV
184   /* This portion of code might also apply to NeXT. -LynX */
185   res = 1;
186
187   if (ioctl(fd, FIONBIO, &res) == -1)
188     return 0;
189 #else
190   if ((res = fcntl(fd, F_GETFL, 0)) == -1)
191     return 0;
192   else if (fcntl(fd, F_SETFL, res | nonb) == -1)
193     return 0;
194 #endif
195   return 1;
196 }
197
198
199 /*
200  *  set_sock_opts
201  */
202 int os_set_reuseaddr(int fd)
203 {
204   unsigned int opt = 1;
205   return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 
206                           (const char*) &opt, sizeof(opt)));
207 }
208
209 int os_set_sockbufs(int fd, unsigned int size)
210 {
211   unsigned int opt = size;
212   return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF, 
213                           (const char*) &opt, sizeof(opt)) &&
214           0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 
215                           (const char*) &opt, sizeof(opt)));
216 }
217
218 int os_disable_options(int fd)
219 {
220 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
221   return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
222 #else
223   return 1;
224 #endif
225 }
226
227 /*
228  * Try and find the correct name to use with getrlimit() for setting the max.
229  * number of files allowed to be open by this process.
230  */
231 #ifdef RLIMIT_FDMAX
232 #define RLIMIT_FD_MAX   RLIMIT_FDMAX
233 #else
234 #ifdef RLIMIT_NOFILE
235 #define RLIMIT_FD_MAX RLIMIT_NOFILE
236 #else
237 #ifdef RLIMIT_OPEN_MAX
238 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
239 #else
240 #undef RLIMIT_FD_MAX
241 #endif
242 #endif
243 #endif
244
245 int os_set_fdlimit(unsigned int max_descriptors)
246 {
247 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
248   struct rlimit limit;
249
250   if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
251     if (limit.rlim_max < MAXCONNECTIONS)
252       return limit.rlim_max;
253     limit.rlim_cur = limit.rlim_max;    /* make soft limit the max */
254     return setrlimit(RLIMIT_FD_MAX, &limit);
255   }
256 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
257   return 0;
258 }
259
260 IOResult os_recv_nonb(int fd, char* buf, unsigned int length, 
261                  unsigned int* count_out)
262 {
263   int res;
264   assert(0 != buf);
265   assert(0 != count_out);
266   *count_out = 0;
267   errno = 0;
268
269   if (0 < (res = recv(fd, buf, length, 0))) {
270     *count_out = (unsigned) res;
271     return IO_SUCCESS;
272   }
273   else if (res < 0) {
274     if (EWOULDBLOCK == errno || EAGAIN == errno)
275       return IO_BLOCKED;
276     else
277       return IO_FAILURE;
278   } 
279   /*
280    * 0   == client closed the connection
281    * < 1 == error
282    */
283   return IO_FAILURE;
284 }
285
286 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length, 
287                           unsigned int* length_out, struct sockaddr_in* sin_out)
288 {
289   int    res;
290   unsigned int len = sizeof(struct sockaddr_in);
291   assert(0 != buf);
292   assert(0 != length_out);
293   assert(0 != sin_out);
294   errno = 0;
295
296   res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
297   if (-1 == res) {
298     if (EWOULDBLOCK == errno || ENOMEM == errno)
299       return IO_BLOCKED;
300     return IO_FAILURE;
301   }
302   *length_out = res;
303   return IO_SUCCESS;
304 }
305
306 IOResult os_send_nonb(int fd, const char* buf, unsigned int length, 
307                  unsigned int* count_out)
308 {
309   int res;
310   assert(0 != buf);
311   assert(0 != count_out);
312   *count_out = 0;
313   errno = 0;
314
315   if (-1 < (res = send(fd, buf, length, 0))) {
316     *count_out = (unsigned) res;
317     return IO_SUCCESS;
318   }
319   else if (EWOULDBLOCK == errno || EAGAIN == errno || 
320            ENOMEM == errno || ENOBUFS == errno)
321     return IO_BLOCKED;
322   return IO_FAILURE;
323 }
324
325 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
326                        unsigned int* count_out)
327 {
328   int res;
329   int count;
330   struct iovec iov[IOV_MAX];
331
332   assert(0 != buf);
333   assert(0 != count_in);
334   assert(0 != count_out);
335
336   *count_in = 0;
337   *count_out = 0;
338   errno = 0;
339
340   count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
341
342   if (-1 < (res = writev(fd, iov, count))) {
343     *count_out = (unsigned) res;
344     return IO_SUCCESS;
345   }
346   else if (EWOULDBLOCK == errno || EAGAIN == errno ||
347            ENOMEM == errno || ENOBUFS == errno)
348     return IO_BLOCKED;
349
350   return IO_FAILURE;
351 }
352
353 int os_connect_nonb(int fd, const struct sockaddr_in* sin)
354 {
355   if (connect(fd, (struct sockaddr*) sin, sizeof(struct sockaddr_in))) {
356     if (errno != EINPROGRESS)
357       return 0;
358   }
359   return 1;
360 }
361       
362 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
363 {
364   unsigned int len = sizeof(struct sockaddr_in);
365   assert(0 != sin_out);
366   return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
367 }
368
369 int os_get_peername(int fd, struct sockaddr_in* sin_out)
370 {
371   unsigned int len = sizeof(struct sockaddr_in);
372   assert(0 != sin_out);
373   return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
374 }
375
376 int os_set_listen(int fd, int backlog)
377 {
378   return (0 == listen(fd, backlog));
379 }
380
381