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