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 #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 <netinet/in.h>
34 #include <stdio.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/types.h>
38 #include <sys/resource.h>
39 #include <sys/socket.h>
40 #include <sys/time.h>
41 #include <sys/uio.h>
42
43 #if 0
44 #include <unistd.h>
45 #endif
46
47 #ifndef IOV_MAX
48 #define IOV_MAX 16      /* minimum required */
49 #endif
50
51 #ifdef HPUX
52 #include <sys/syscall.h>
53 #define getrusage(a,b) syscall(SYS_GETRUSAGE, a, b)
54 #endif
55
56 /*
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".
61  * -avalon
62  */
63 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
64 {
65   char buf[256];
66 #ifdef HAVE_GETRUSAGE
67   struct rusage rus;
68   time_t secs;
69
70 #ifdef  hz
71 #  define hzz hz
72 #else
73 #  ifdef HZ
74 #    define hzz HZ
75 #  else
76   int hzz = 1;
77 #  ifdef HPUX
78   hzz = sysconf(_SC_CLK_TCK);
79 #  endif
80 #endif
81 #endif
82
83   assert(0 != enumerator);
84   if (getrusage(RUSAGE_SELF, &rus) == -1)
85     return 0;
86
87   secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
88   if (secs == 0)
89     secs = 1;
90
91   sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
92           secs / 60, secs % 60,
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);
96
97   sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
98           rus.ru_maxrss,
99           rus.ru_ixrss / (uptime * hzz), rus.ru_idrss / (uptime * hzz),
100           rus.ru_isrss / (uptime * hzz));
101   (*enumerator)(cptr, buf);
102
103   sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
104           rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
105   (*enumerator)(cptr, buf);
106
107   sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
108   (*enumerator)(cptr, buf);
109   
110   sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
111   (*enumerator)(cptr, buf);
112
113   sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
114           rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
115   (*enumerator)(cptr, buf);
116
117 #else /* HAVE_GETRUSAGE */
118 #if HAVE_TIMES
119   struct tms tmsbuf;
120   time_t secs, mins;
121   int hzz = 1, ticpermin;
122   int umin, smin, usec, ssec;
123
124   assert(0 != enumerator);
125 #ifdef HPUX
126   hzz = sysconf(_SC_CLK_TCK);
127 #endif
128   ticpermin = hzz * 60;
129
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;
134   secs = usec + ssec;
135   mins = (secs / 60) + umin + smin;
136   secs %= hzz;
137
138   if (times(&tmsbuf) == -1)
139     return 0;
140   secs = tmsbuf.tms_utime + tmsbuf.tms_stime;
141
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 */
147   return 1;
148 }
149
150 int os_get_sockerr(int fd)
151 {
152   int    err = 0;
153 #if defined(SO_ERROR)
154   unsigned int len = sizeof(err);
155   getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
156 #endif
157   return err;
158 }
159
160 /*
161  * set_non_blocking
162  *
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...)
168  */
169 int os_set_nonblocking(int fd)
170 {
171   int res;
172 #ifndef NBLOCK_SYSV
173   int nonb = 0;
174 #endif
175
176   /*
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.
181    */
182 #ifdef  NBLOCK_POSIX
183   nonb |= O_NONBLOCK;
184 #endif
185 #ifdef  NBLOCK_BSD
186   nonb |= O_NDELAY;
187 #endif
188 #ifdef  NBLOCK_SYSV
189   /* This portion of code might also apply to NeXT. -LynX */
190   res = 1;
191
192   if (ioctl(fd, FIONBIO, &res) == -1)
193     return 0;
194 #else
195   if ((res = fcntl(fd, F_GETFL, 0)) == -1)
196     return 0;
197   else if (fcntl(fd, F_SETFL, res | nonb) == -1)
198     return 0;
199 #endif
200   return 1;
201 }
202
203
204 /*
205  *  set_sock_opts
206  */
207 int os_set_reuseaddr(int fd)
208 {
209   unsigned int opt = 1;
210   return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 
211                           (const char*) &opt, sizeof(opt)));
212 }
213
214 int os_set_sockbufs(int fd, unsigned int size)
215 {
216   unsigned int opt = size;
217   return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF, 
218                           (const char*) &opt, sizeof(opt)) &&
219           0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 
220                           (const char*) &opt, sizeof(opt)));
221 }
222
223 int os_disable_options(int fd)
224 {
225 #if defined(IP_OPTIONS) && defined(IPPROTO_IP)
226   return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
227 #else
228   return 1;
229 #endif
230 }
231
232 /*
233  * Try and find the correct name to use with getrlimit() for setting the max.
234  * number of files allowed to be open by this process.
235  */
236 #ifdef RLIMIT_FDMAX
237 #define RLIMIT_FD_MAX   RLIMIT_FDMAX
238 #else
239 #ifdef RLIMIT_NOFILE
240 #define RLIMIT_FD_MAX RLIMIT_NOFILE
241 #else
242 #ifdef RLIMIT_OPEN_MAX
243 #define RLIMIT_FD_MAX RLIMIT_OPEN_MAX
244 #else
245 #undef RLIMIT_FD_MAX
246 #endif
247 #endif
248 #endif
249
250 int os_set_fdlimit(unsigned int max_descriptors)
251 {
252 #if defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX)
253   struct rlimit limit;
254
255   if (!getrlimit(RLIMIT_FD_MAX, &limit)) {
256     if (limit.rlim_max < MAXCONNECTIONS)
257       return limit.rlim_max;
258     limit.rlim_cur = limit.rlim_max;    /* make soft limit the max */
259     return setrlimit(RLIMIT_FD_MAX, &limit);
260   }
261 #endif /* defined(HAVE_SETRLIMIT) && defined(RLIMIT_FD_MAX) */
262   return 0;
263 }
264
265 IOResult os_recv_nonb(int fd, char* buf, unsigned int length, 
266                  unsigned int* count_out)
267 {
268   int res;
269   assert(0 != buf);
270   assert(0 != count_out);
271   *count_out = 0;
272   errno = 0;
273
274   if (0 < (res = recv(fd, buf, length, 0))) {
275     *count_out = (unsigned) res;
276     return IO_SUCCESS;
277   }
278   else if (res < 0) {
279     if (EWOULDBLOCK == errno || EAGAIN == errno)
280       return IO_BLOCKED;
281     else
282       return IO_FAILURE;
283   } 
284   /*
285    * 0   == client closed the connection
286    * < 1 == error
287    */
288   return IO_FAILURE;
289 }
290
291 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length, 
292                           unsigned int* length_out, struct sockaddr_in* sin_out)
293 {
294   int    res;
295   unsigned int len = sizeof(struct sockaddr_in);
296   assert(0 != buf);
297   assert(0 != length_out);
298   assert(0 != sin_out);
299   errno = 0;
300
301   res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
302   if (-1 == res) {
303     if (EWOULDBLOCK == errno || ENOMEM == errno)
304       return IO_BLOCKED;
305     return IO_FAILURE;
306   }
307   *length_out = res;
308   return IO_SUCCESS;
309 }
310
311 IOResult os_send_nonb(int fd, const char* buf, unsigned int length, 
312                  unsigned int* count_out)
313 {
314   int res;
315   assert(0 != buf);
316   assert(0 != count_out);
317   *count_out = 0;
318   errno = 0;
319
320   if (-1 < (res = send(fd, buf, length, 0))) {
321     *count_out = (unsigned) res;
322     return IO_SUCCESS;
323   }
324   else if (EWOULDBLOCK == errno || EAGAIN == errno || 
325            ENOMEM == errno || ENOBUFS == errno)
326     return IO_BLOCKED;
327   return IO_FAILURE;
328 }
329
330 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
331                        unsigned int* count_out)
332 {
333   int res;
334   int count;
335   struct iovec iov[IOV_MAX];
336
337   assert(0 != buf);
338   assert(0 != count_in);
339   assert(0 != count_out);
340
341   *count_in = 0;
342   *count_out = 0;
343   errno = 0;
344
345   count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
346
347   if (-1 < (res = writev(fd, iov, count))) {
348     *count_out = (unsigned) res;
349     return IO_SUCCESS;
350   }
351   else if (EWOULDBLOCK == errno || EAGAIN == errno ||
352            ENOMEM == errno || ENOBUFS == errno)
353     return IO_BLOCKED;
354
355   return IO_FAILURE;
356 }
357
358 int os_connect_nonb(int fd, const struct sockaddr_in* sin)
359 {
360   if (connect(fd, (struct sockaddr*) sin, sizeof(struct sockaddr_in))) {
361     if (errno != EINPROGRESS)
362       return 0;
363   }
364   return 1;
365 }
366       
367 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
368 {
369   unsigned int len = sizeof(struct sockaddr_in);
370   assert(0 != sin_out);
371   return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
372 }
373
374 int os_get_peername(int fd, struct sockaddr_in* sin_out)
375 {
376   unsigned int len = sizeof(struct sockaddr_in);
377   assert(0 != sin_out);
378   return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
379 }
380
381 int os_set_listen(int fd, int backlog)
382 {
383   return (0 == listen(fd, backlog));
384 }
385
386