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