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