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