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