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