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