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