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