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