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