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