1 // -*-c++-*-
2 /* $Id$ */
3 
4 /*
5  *
6  * Copyright (C) 1998 David Mazieres (dm@uun.org)
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2, or (at
11  * your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21  * USA
22  *
23  */
24 
25 #ifndef _ASYNC_AMISC_H_
26 #define _ASYNC_AMISC_H_ 1
27 
28 #include "sysconf.h"
29 #include "err.h"
30 #include "callback.h"
31 #include "serial.h"
32 
33 /* getopt declarations */
34 extern char *optarg;
35 extern int optind;
36 
37 /* Common callback types */
38 typedef callback<void>::ref cbv;
39 typedef callback<void, int>::ref cbi;
40 typedef callback<void, str>::ref cbs;
41 typedef callback<void, bool>::ref cbb;
42 extern cbs cbs_null;
43 extern cbb cbb_null;
44 extern cbv cbv_null;
45 extern cbi cbi_null;
46 
47 /* arandom.c */
48 extern "C" {
49   extern u_int32_t (*arandom_fn) ();
50   u_int32_t arandom ();
51 }
52 
53 /* straux.C */
54 char *mempbrk (char *, const char *, int);
55 char *xstrsep (char **, const char *);
56 char *strnnsep (char **, const char *);
57 
58 /* socket.C */
59 extern in_addr inet_bindaddr;
60 int inetsocket (int, u_int16_t = 0, u_int32_t = INADDR_ANY);
61 int inetsocket_resvport (int, u_int32_t = INADDR_ANY);
62 int unixsocket (const char *);
63 int unixsocket_connect (const char *);
64 bool isunixsocket (int);
65 void close_on_exec (int, bool = true);
66 int _make_async (int);
67 void make_async (int);
68 void make_sync (int);
69 void tcp_nodelay (int);
70 void tcp_abort (int);
71 bool addreq (const sockaddr *, const sockaddr *, socklen_t);
72 
73 /* sigio.C */
74 int sigio_set (int fd);
75 int sigio_clear (int fd);
76 
77 /* passfd.c */
78 #include "rwfd.h"
79 
80 /* myname.C */
81 str myname ();
82 str mydomain ();
83 
84 /* myaddrs.C */
85 bool myipaddrs (vec<in_addr> *res);
86 
87 /* fdwait.C */
88 enum selop { selread = 0, selwrite = 1 };
89 int fdwait (int fd, bool r, bool w, timeval *tvp);
90 int fdwait (int fd, selop op, timeval *tvp = NULL);
91 
92 /* spawn.C */
93 extern str execdir;
94 #ifdef MAINTAINER
95 extern str builddir;		// For running programs in place
96 extern str buildtmpdir;		// For creating files (e.g. prog.pid)
97 #endif /* MAINTAINER */
98 pid_t afork ();
99 str fix_exec_path (str path, str dir = NULL);
100 str find_program (const char *program);
101 str find_program_plus_libsfs (const char *program);
102 pid_t spawn (const char *, char *const *,
103 	     int in = 0, int out = 1, int err = 2,
104 	     cbv::ptr postforkcb = NULL, char *const *env = NULL);
105 inline pid_t
106 spawn (const char *path, const char *const *av,
107        int in = 0, int out = 1, int err = 2, cbv::ptr postforkcb = NULL,
108        char *const *env = NULL)
109 {
110   return spawn (path, const_cast<char *const *> (av),
111 		in, out, err, postforkcb, env);
112 }
113 pid_t aspawn (const char *, char *const *,
114 	      int in = 0, int out = 1, int err = 2,
115 	      cbv::ptr postforkcb = NULL, char *const *env = NULL);
116 inline pid_t
117 aspawn (const char *path, const char *const *av,
118 	int in = 0, int out = 1, int err = 2, cbv::ptr postforkcb = NULL,
119 	char *const *env = NULL)
120 {
121   return aspawn (path, const_cast<char *const *> (av),
122 		 in, out, err, postforkcb, env);
123 }
124 
125 /* lockfile.C */
126 bool stat_unchanged (const struct stat *sb1, const struct stat *sb2);
127 class lockfile {
128 protected:
129   bool islocked;
130   int fd;
131 
132   ~lockfile ();
133   bool openit ();
134   void closeit ();
135   bool fdok ();
136 
137 public:
138   const str path;
139 
lockfile(const str & p)140   lockfile (const str &p) : islocked (false), fd (-1), path (p) {}
locked()141   bool locked () const { return islocked; }
getfd()142   int getfd () const { return fd; }
143   bool acquire (bool wait = false);
144   void release ();
ok()145   bool ok () { return islocked && fdok (); }
146   static ptr<lockfile> alloc (const str &path, bool wait = false);
147 };
148 
149 /* daemonize.C */
150 extern str syslog_priority;
151 void daemonize (str tag = NULL);
152 void start_logger (str tag = NULL);
153 int start_logger (const str &pri, const str &tag, const str &line,
154 		  const str &logfile, int flags, mode_t mode);
155 
156 /* Random usefull operators */
157 #include "keyfunc.h"
158 
159 inline in_addr
inaddr_cast(const void * p)160 inaddr_cast (const void *p)
161 {
162   in_addr a;
163   memcpy (&a, p, sizeof (a));
164   return a;
165 }
166 
167 inline bool
168 operator== (const in_addr &a, const in_addr &b)
169 {
170   return a.s_addr == b.s_addr;
171 }
172 inline bool
173 operator!= (const in_addr &a, const in_addr &b)
174 {
175   return a.s_addr != b.s_addr;
176 }
177 template<> struct hashfn<in_addr> {
178   hashfn () {}
179   hash_t operator() (const in_addr a) const
180     { return a.s_addr; }
181 };
182 
183 inline bool
184 operator== (const sockaddr_in &a, const sockaddr_in &b)
185 {
186   return a.sin_port == b.sin_port && a.sin_addr.s_addr == b.sin_addr.s_addr;
187 }
188 inline bool
189 operator!= (const sockaddr_in &a, const sockaddr_in &b)
190 {
191   return a.sin_port != b.sin_port || a.sin_addr.s_addr != b.sin_addr.s_addr;
192 }
193 template<> struct hashfn<sockaddr_in> {
194   hashfn () {}
195   hash_t operator() (const sockaddr_in &a) const
196     { return ntohs (a.sin_port) << 16 ^ htonl (a.sin_addr.s_addr); }
197 };
198 
199 inline bool
200 operator== (const timespec &a, const timespec &b)
201 {
202   return a.tv_nsec == b.tv_nsec && a.tv_sec == b.tv_sec;
203 }
204 inline bool
205 operator!= (const timespec &a, const timespec &b)
206 {
207   return a.tv_nsec != b.tv_nsec || a.tv_sec != b.tv_sec;
208 }
209 
210 inline int
211 tscmp (const timespec &a, const timespec &b)
212 {
213   if (a.tv_sec < b.tv_sec)
214     return -1;
215   if (b.tv_sec < a.tv_sec)
216     return 1;
217   if (a.tv_nsec < b.tv_nsec)
218     return -1;
219   return b.tv_nsec < a.tv_nsec;
220 };
221 inline bool
222 operator< (const timespec &a, const timespec &b)
223 {
224   return tscmp (a, b) < 0;
225 }
226 inline bool
227 operator<= (const timespec &a, const timespec &b)
228 {
229   return tscmp (a, b) <= 0;
230 }
231 inline bool
232 operator> (const timespec &a, const timespec &b)
233 {
234   return tscmp (a, b) > 0;
235 }
236 inline bool
237 operator>= (const timespec &a, const timespec &b)
238 {
239   return tscmp (a, b) >= 0;
240 }
241 
242 inline timespec
243 operator+ (const timespec &a, const timespec &b)
244 {
245   timespec ts;
246   ts.tv_sec = a.tv_sec + b.tv_sec;
247   if ((ts.tv_nsec = a.tv_nsec + b.tv_nsec) > 1000000000) {
248     ts.tv_nsec -= 1000000000;
249     ++ts.tv_sec;
250   }
251   return ts;
252 }
253 
254 inline timespec
255 operator- (const timespec &a, const timespec &b)
256 {
257   timespec ts;
258   ts.tv_sec = a.tv_sec - b.tv_sec;
259   if (a.tv_nsec > b.tv_nsec)
260     ts.tv_nsec = a.tv_nsec - b.tv_nsec;
261   else {
262     ts.tv_nsec = a.tv_nsec + 1000000000 - b.tv_nsec;
263     --ts.tv_sec;
264   }
265   return ts;
266 }
267 
268 #endif /* !_ASYNC_AMISC_H_ */
269