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