Forward port SOCKSENDBUF, SOCKRECVBUF features from 2.10.11.
[ircu2.10.12-pk.git] / ircd / os_linux.c
1 /*
2  * IRC - Internet Relay Chat, ircd/os_linux.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 "config.h"
23
24 #define _XOPEN_SOURCE   /* make limits.h #define IOV_MAX */
25
26 #include "ircd_osdep.h"
27 #include "msgq.h"
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <limits.h>
33 #include <stdio.h>
34 #include <netinet/in.h>
35 #include <string.h>
36 #include <sys/ioctl.h>
37 #include <sys/resource.h>
38 #include <sys/socket.h>
39 #include <sys/types.h>
40 #include <sys/times.h>
41 #include <sys/uio.h>
42 #include <sys/param.h>
43 #include <unistd.h>
44
45 /*
46  * This is part of the STATS replies. There is no offical numeric for this
47  * since this isnt an official command, in much the same way as HASH isnt.
48  * It is also possible that some systems wont support this call or have
49  * different field names for "struct rusage".
50  * -avalon
51  */
52 int os_get_rusage(struct Client *cptr, int uptime, EnumFn enumerator)
53 {
54   char buf[256];
55   struct rusage rus;
56   struct tms tmsbuf;
57   time_t secs;
58   time_t mins;
59   int umin;
60   int smin;
61   int usec;
62   int ssec;
63   int ticpermin = HZ * 60;
64   unsigned int tick_count = uptime * HZ;
65
66   if (0 == tick_count)
67     ++tick_count;
68
69   assert(0 != enumerator);
70   if (getrusage(RUSAGE_SELF, &rus) == -1)
71     return 0;
72
73   secs = rus.ru_utime.tv_sec + rus.ru_stime.tv_sec;
74   if (secs == 0)
75     secs = 1;
76
77   sprintf(buf, "CPU Secs %ld:%ld User %ld:%ld System %ld:%ld",
78           secs / 60, secs % 60,
79           rus.ru_utime.tv_sec / 60, rus.ru_utime.tv_sec % 60,
80           rus.ru_stime.tv_sec / 60, rus.ru_stime.tv_sec % 60);
81   (*enumerator)(cptr, buf);
82
83   sprintf(buf, "RSS %ld ShMem %ld Data %ld Stack %ld",
84           rus.ru_maxrss,
85           rus.ru_ixrss / tick_count, rus.ru_idrss / tick_count,
86           rus.ru_isrss / tick_count);
87   (*enumerator)(cptr, buf);
88
89   sprintf(buf, "Swaps %ld Reclaims %ld Faults %ld",
90           rus.ru_nswap, rus.ru_minflt, rus.ru_majflt);
91   (*enumerator)(cptr, buf);
92
93   sprintf(buf, "Block in %ld out %ld", rus.ru_inblock, rus.ru_oublock);
94   (*enumerator)(cptr, buf);
95   
96   sprintf(buf, "Msg Rcv %ld Send %ld", rus.ru_msgrcv, rus.ru_msgsnd);
97   (*enumerator)(cptr, buf);
98
99   sprintf(buf, "Signals %ld Context Vol. %ld Invol %ld",
100           rus.ru_nsignals, rus.ru_nvcsw, rus.ru_nivcsw);
101   (*enumerator)(cptr, buf);
102
103   if (times(&tmsbuf) == -1)
104     return 0;
105
106   umin = tmsbuf.tms_utime / ticpermin;
107   usec = (tmsbuf.tms_utime % ticpermin) / (float)HZ;
108   smin = tmsbuf.tms_stime / ticpermin;
109   ssec = (tmsbuf.tms_stime % ticpermin) / (float)HZ;
110   secs = usec + ssec;
111   mins = (secs / 60) + umin + smin;
112   secs %= HZ;
113
114   sprintf(buf, "CPU Secs %ld:%ld User %d:%d System %d:%d", 
115           mins, secs, umin, usec, smin, ssec);
116   (*enumerator)(cptr, buf);
117   return 1;
118 }
119
120 int os_get_sockerr(int fd)
121 {
122   int    err = 0;
123   unsigned int len = sizeof(err);
124   getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &len);
125   return err;
126 }
127
128 /*
129  * set_non_blocking
130  *
131  * Set the client connection into non-blocking mode. If your
132  * system doesn't support this, you can make this a dummy
133  * function (and get all the old problems that plagued the
134  * blocking version of IRC--not a problem if you are a
135  * lightly loaded node...)
136  */
137 int os_set_nonblocking(int fd)
138 {
139   int res = 1;
140   return (0 == ioctl(fd, FIONBIO, &res));
141 }
142
143
144 /*
145  *  set_sock_opts
146  */
147 int os_set_reuseaddr(int fd)
148 {
149   unsigned int opt = 1;
150   return (0 == setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)));
151 }
152
153 int os_set_sockbufs(int fd, unsigned int ssize, unsigned int rsize)
154 {
155   unsigned int sopt = ssize;
156   unsigned int ropt = rsize;
157   return (0 == setsockopt(fd, SOL_SOCKET, SO_RCVBUF, 
158                           (const char*) &ropt, sizeof(ropt)) &&
159           0 == setsockopt(fd, SOL_SOCKET, SO_SNDBUF, 
160                           (const char*) &sopt, sizeof(sopt)));
161 }
162
163 int os_set_tos(int fd,int tos)
164 {
165   unsigned int opt = tos;
166   return (0 == setsockopt(fd, SOL_IP, IP_TOS, &opt, sizeof(opt)));
167 }
168
169 int os_disable_options(int fd)
170 {
171   return (0 == setsockopt(fd, IPPROTO_IP, IP_OPTIONS, NULL, 0));
172 }
173
174 int os_set_fdlimit(unsigned int max_descriptors)
175 {
176   struct rlimit limit;
177
178   if (!getrlimit(RLIMIT_NOFILE, &limit)) {
179     if (limit.rlim_max < max_descriptors)
180       return limit.rlim_max;
181     limit.rlim_cur = limit.rlim_max;    /* make soft limit the max */
182     return setrlimit(RLIMIT_NOFILE, &limit);
183   }
184   return 0;
185 }
186
187 /*
188  * os_recv_nonb - non blocking read of a connection
189  * returns:
190  *  1  if data was read or socket is blocked (recoverable error)
191  *    count_out > 0 if data was read
192  *   
193  *  0  if socket closed from other end
194  *  -1 if an unrecoverable error occurred
195  */
196 IOResult os_recv_nonb(int fd, char* buf, unsigned int length, 
197                  unsigned int* count_out)
198 {
199   int res;
200   assert(0 != buf);
201   assert(0 != count_out);
202   *count_out = 0;
203   errno = 0;
204
205   if (0 < (res = recv(fd, buf, length, 0))) {
206     *count_out = (unsigned) res;
207     return IO_SUCCESS;
208   }
209   else if (res < 0) {
210     if (EWOULDBLOCK == errno || EAGAIN == errno)
211       return IO_BLOCKED;
212     else
213       return IO_FAILURE;
214   } 
215   /*
216    * 0   == client closed the connection
217    * < 1 == error
218    */
219   return IO_FAILURE;
220 }
221
222 IOResult os_recvfrom_nonb(int fd, char* buf, unsigned int length, 
223                           unsigned int* length_out, struct sockaddr_in* sin_out)
224 {
225   int    res;
226   unsigned int len = sizeof(struct sockaddr_in);
227   assert(0 != buf);
228   assert(0 != length_out);
229   assert(0 != sin_out);
230   errno = 0;
231
232   res = recvfrom(fd, buf, length, 0, (struct sockaddr*) sin_out, &len);
233   if (-1 == res) {
234     if (EWOULDBLOCK == errno || ENOMEM == errno)
235       return IO_BLOCKED;
236     return IO_FAILURE;
237   }
238   *length_out = res;
239   return IO_SUCCESS;
240 }
241
242 /*
243  * os_send_nonb - non blocking read of a connection
244  * returns:
245  *  1  if data was written
246  *    count_out contains amount written
247  *   
248  *  0  if write call blocked, recoverable error
249  *  -1 if an unrecoverable error occurred
250  */
251 IOResult os_send_nonb(int fd, const char* buf, unsigned int length, 
252                  unsigned int* count_out)
253 {
254   int res;
255   assert(0 != buf);
256   assert(0 != count_out);
257   *count_out = 0;
258   errno = 0;
259
260   if (-1 < (res = send(fd, buf, length, 0))) {
261     *count_out = (unsigned) res;
262     return IO_SUCCESS;
263   }
264   else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
265     return IO_BLOCKED;
266
267   return IO_FAILURE;
268 }
269
270 /*
271  * os_sendv_nonb - non blocking writev to a connection
272  * returns:
273  *  1  if data was written
274  *    count_out contains amount written
275  *   
276  *  0  if write call blocked, recoverable error
277  *  -1 if an unrecoverable error occurred
278  */
279 IOResult os_sendv_nonb(int fd, struct MsgQ* buf, unsigned int* count_in,
280                        unsigned int* count_out)
281 {
282   int res;
283   int count;
284   struct iovec iov[IOV_MAX];
285
286   assert(0 != buf);
287   assert(0 != count_in);
288   assert(0 != count_out);
289
290   *count_in = 0;
291   *count_out = 0;
292   errno = 0;
293
294   count = msgq_mapiov(buf, iov, IOV_MAX, count_in);
295
296   if (-1 < (res = writev(fd, iov, count))) {
297     *count_out = (unsigned) res;
298     return IO_SUCCESS;
299   }
300   else if (EAGAIN == errno || ENOMEM == errno || ENOBUFS == errno)
301     return IO_BLOCKED;
302
303   return IO_FAILURE;
304 }
305
306
307 IOResult os_connect_nonb(int fd, const struct sockaddr_in* sin)
308 {
309   if (connect(fd, (const struct sockaddr*) sin, sizeof(struct sockaddr_in)))
310     return (errno == EINPROGRESS) ? IO_BLOCKED : IO_FAILURE;
311   return IO_SUCCESS;
312 }
313       
314 int os_get_sockname(int fd, struct sockaddr_in* sin_out)
315 {
316   unsigned int len = sizeof(struct sockaddr_in);
317   assert(0 != sin_out);
318   return (0 == getsockname(fd, (struct sockaddr*) sin_out, &len));
319 }
320
321 int os_get_peername(int fd, struct sockaddr_in* sin_out)
322 {
323   unsigned int len = sizeof(struct sockaddr_in);
324   assert(0 != sin_out);
325   return (0 == getpeername(fd, (struct sockaddr*) sin_out, &len));
326 }
327
328 int os_set_listen(int fd, int backlog)
329 {
330   /*
331    * for linux 2.2 backlog is the number of connections ready to be accepted
332    * not the max syn requests, there is a kernel tweak there to set the max
333    * syn request queue length
334    */
335   return (0 == listen(fd, backlog));
336 }
337