summaryrefslogtreecommitdiff
path: root/runit/runit_lib.h
blob: d306164ea11484b2b9e1ae313e1b156655236206 (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
/*
Copyright (c) 2001-2006, Gerrit Pape
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
   3. The name of the author may not be used to endorse or promote products
      derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*** buffer.h ***/

typedef struct buffer {
	char *x;
	unsigned p;
	unsigned n;
	int fd;
	int (*op)(int fd,char *buf,unsigned len);
} buffer;

#define BUFFER_INIT(op,fd,buf,len) { (buf), 0, (len), (fd), (op) }
#define BUFFER_INSIZE 8192
#define BUFFER_OUTSIZE 8192

extern void buffer_init(buffer *,int (*)(int fd,char *buf,unsigned len),int,char *,unsigned);

extern int buffer_flush(buffer *);
extern int buffer_put(buffer *,const char *,unsigned);
extern int buffer_putalign(buffer *,const char *,unsigned);
extern int buffer_putflush(buffer *,const char *,unsigned);
extern int buffer_puts(buffer *,const char *);
extern int buffer_putsalign(buffer *,const char *);
extern int buffer_putsflush(buffer *,const char *);

#define buffer_PUTC(s,c) \
	( ((s)->n != (s)->p) \
		? ( (s)->x[(s)->p++] = (c), 0 ) \
		: buffer_put((s),&(c),1) \
	)

extern int buffer_get(buffer *,char *,unsigned);
extern int buffer_bget(buffer *,char *,unsigned);
extern int buffer_feed(buffer *);

extern char *buffer_peek(buffer *);
extern void buffer_seek(buffer *,unsigned);

#define buffer_PEEK(s) ( (s)->x + (s)->n )
#define buffer_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) )

#define buffer_GETC(s,c) \
	( ((s)->p > 0) \
		? ( *(c) = (s)->x[(s)->n], buffer_SEEK((s),1), 1 ) \
		: buffer_get((s),(c),1) \
	)

extern int buffer_copy(buffer *,buffer *);

extern int buffer_unixread(int,char *,unsigned);
/* Actually, int buffer_unixwrite(int,const char *,unsigned),
	 but that 'const' will produce warnings... oh well */
extern int buffer_unixwrite(int,char *,unsigned);


/*** byte.h ***/

extern unsigned byte_chr(char *s,unsigned n,int c);


/*** coe.h ***/

extern int coe(int);


/*** direntry.h ***/

#define direntry struct dirent


/*** fd.h ***/

extern int fd_copy(int,int);
extern int fd_move(int,int);


/*** fifo.h ***/

extern int fifo_make(const char *,int);


/*** fmt.h ***/

#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
#define FMT_LEN ((char *) 0) /* convenient abbreviation */

extern unsigned fmt_uint(char *,unsigned);
extern unsigned fmt_uint0(char *,unsigned,unsigned);
extern unsigned fmt_xint(char *,unsigned);
extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned);
extern unsigned fmt_ushort(char *,unsigned short);
extern unsigned fmt_xshort(char *,unsigned short);
extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short);
extern unsigned fmt_ulong(char *,unsigned long);
extern unsigned fmt_xlong(char *,unsigned long);
extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long);

extern unsigned fmt_plusminus(char *,int);
extern unsigned fmt_minus(char *,int);
extern unsigned fmt_0x(char *,int);

extern unsigned fmt_str(char *,const char *);
extern unsigned fmt_strn(char *,const char *,unsigned);


/*** tai.h ***/

struct tai {
	uint64_t x;
} ;

#define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))

extern void tai_now(struct tai *);

#define tai_approx(t) ((double) ((t)->x))

extern void tai_add(struct tai *,const struct tai *,const struct tai *);
extern void tai_sub(struct tai *,const struct tai *,const struct tai *);
#define tai_less(t,u) ((t)->x < (u)->x)

#define TAI_PACK 8
extern void tai_pack(char *,const struct tai *);
extern void tai_unpack(const char *,struct tai *);

extern void tai_uint(struct tai *,unsigned);


/*** taia.h ***/

struct taia {
	struct tai sec;
	unsigned long nano; /* 0...999999999 */
	unsigned long atto; /* 0...999999999 */
} ;

extern void taia_tai(const struct taia *,struct tai *);

extern void taia_now(struct taia *);

extern double taia_approx(const struct taia *);
extern double taia_frac(const struct taia *);

extern void taia_add(struct taia *,const struct taia *,const struct taia *);
extern void taia_addsec(struct taia *,const struct taia *,int);
extern void taia_sub(struct taia *,const struct taia *,const struct taia *);
extern void taia_half(struct taia *,const struct taia *);
extern int taia_less(const struct taia *,const struct taia *);

#define TAIA_PACK 16
extern void taia_pack(char *,const struct taia *);
extern void taia_unpack(const char *,struct taia *);

#define TAIA_FMTFRAC 19
extern unsigned taia_fmtfrac(char *,const struct taia *);

extern void taia_uint(struct taia *,unsigned);


/*** fmt_ptime.h ***/

#define FMT_PTIME 30

extern unsigned fmt_ptime(char *, struct taia *);
extern unsigned fmt_taia(char *, struct taia *);


/*** gen_alloc.h ***/

#define GEN_ALLOC_typedef(ta,type,field,len,a) \
	typedef struct ta { type *field; unsigned len; unsigned a; } ta;


/*** gen_allocdefs.h ***/

#define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
int ta_ready(ta *x,unsigned n) \
{ unsigned i; \
	if (x->field) { \
		i = x->a; \
		if (n > i) { \
			x->a = base + n + (n >> 3); \
			x->field = realloc(x->field,x->a * sizeof(type)); \
			if (x->field) return 1; \
			x->a = i; return 0; } \
		return 1; } \
	x->len = 0; \
	return !!(x->field = malloc((x->a = n) * sizeof(type))); }

#define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
int ta_rplus(ta *x,unsigned n) \
{ unsigned i; \
	if (x->field) { \
		i = x->a; n += x->len; \
		if (n > i) { \
			x->a = base + n + (n >> 3); \
			x->field = realloc(x->field,x->a * sizeof(type)); \
			if (x->field) return 1; \
			x->a = i; return 0; } \
		return 1; } \
	x->len = 0; \
	return !!(x->field = malloc((x->a = n) * sizeof(type))); }

#define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
int ta_append(ta *x,const type *i) \
{ if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }


/*** stralloc.h ***/

GEN_ALLOC_typedef(stralloc,char,s,len,a)

extern int stralloc_ready(stralloc *,unsigned);
extern int stralloc_readyplus(stralloc *,unsigned);
extern int stralloc_copy(stralloc *,const stralloc *);
extern int stralloc_cat(stralloc *,const stralloc *);
extern int stralloc_copys(stralloc *,const char *);
extern int stralloc_cats(stralloc *,const char *);
extern int stralloc_copyb(stralloc *,const char *,unsigned);
extern int stralloc_catb(stralloc *,const char *,unsigned);
extern int stralloc_append(stralloc *,const char *); /* beware: this takes a pointer to 1 char */
extern int stralloc_starts(stralloc *,const char *);

#define stralloc_0(sa) stralloc_append(sa,"")

extern int stralloc_catulong0(stralloc *,unsigned long,unsigned);
extern int stralloc_catlong0(stralloc *,long,unsigned);

#define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
#define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
#define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
#define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))


/*** iopause.h ***/

typedef struct pollfd iopause_fd;
#define IOPAUSE_READ POLLIN
#define IOPAUSE_WRITE POLLOUT

extern void iopause(iopause_fd *,unsigned,struct taia *,struct taia *);


/*** lock.h ***/

extern int lock_ex(int);
extern int lock_un(int);
extern int lock_exnb(int);


/*** ndelay.h ***/

extern int ndelay_on(int);
extern int ndelay_off(int);


/*** open.h ***/

extern int open_read(const char *);
extern int open_excl(const char *);
extern int open_append(const char *);
extern int open_trunc(const char *);
extern int open_write(const char *);


/*** openreadclose.h ***/

extern int openreadclose(const char *,stralloc *,unsigned);


/*** pathexec.h ***/

extern void pathexec_run(const char *,char *const *,char *const *);
extern int pathexec_env(const char *,const char *);
extern void pathexec(char **);


/*** pmatch.h ***/

extern unsigned pmatch(const char *, const char *, unsigned);


/*** prot.h ***/

extern int prot_gid(int);
extern int prot_uid(int);


/*** readclose.h ***/

extern int readclose_append(int,stralloc *,unsigned);
extern int readclose(int,stralloc *,unsigned);


/*** scan.h ***/

extern unsigned scan_uint(const char *,unsigned *);
extern unsigned scan_xint(const char *,unsigned *);
extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *);
extern unsigned scan_ushort(const char *,unsigned short *);
extern unsigned scan_xshort(const char *,unsigned short *);
extern unsigned scan_nbbshort(const char *,unsigned,unsigned,unsigned,unsigned short *);
extern unsigned scan_ulong(const char *,unsigned long *);
extern unsigned scan_xlong(const char *,unsigned long *);
extern unsigned scan_nbblong(const char *,unsigned,unsigned,unsigned,unsigned long *);

extern unsigned scan_plusminus(const char *,int *);
extern unsigned scan_0x(const char *,unsigned *);

extern unsigned scan_whitenskip(const char *,unsigned);
extern unsigned scan_nonwhitenskip(const char *,unsigned);
extern unsigned scan_charsetnskip(const char *,const char *,unsigned);
extern unsigned scan_noncharsetnskip(const char *,const char *,unsigned);

extern unsigned scan_strncmp(const char *,const char *,unsigned);
extern unsigned scan_memcmp(const char *,const char *,unsigned);

extern unsigned scan_long(const char *,long *);
extern unsigned scan_8long(const char *,unsigned long *);


/*** seek.h ***/

typedef unsigned long seek_pos;

extern seek_pos seek_cur(int);

extern int seek_set(int,seek_pos);
extern int seek_end(int);

extern int seek_trunc(int,seek_pos);

#define seek_begin(fd) (seek_set((fd),(seek_pos) 0))


/*** sig.h ***/

extern int sig_alarm;
extern int sig_child;
extern int sig_cont;
extern int sig_hangup;
extern int sig_int;
extern int sig_pipe;
extern int sig_term;

extern void (*sig_defaulthandler)(int);
extern void (*sig_ignorehandler)(int);

extern void sig_catch(int,void (*)(int));
#define sig_ignore(s) (sig_catch((s),sig_ignorehandler))
#define sig_uncatch(s) (sig_catch((s),sig_defaulthandler))

extern void sig_block(int);
extern void sig_unblock(int);
extern void sig_blocknone(void);
extern void sig_pause(void);

extern void sig_dfl(int);


/*** str.h ***/

extern unsigned str_chr(const char *,int);  /* never returns NULL */

#define str_diff(s,t) strcmp((s),(t))
#define str_equal(s,t) (!strcmp((s),(t)))


/*** wait.h ***/

extern int wait_pid(int *wstat, int pid);
extern int wait_nohang(int *wstat);

#define wait_crashed(w) ((w) & 127)
#define wait_exitcode(w) ((w) >> 8)
#define wait_stopsig(w) ((w) >> 8)
#define wait_stopped(w) (((w) & 127) == 127)