runit cleanup part 1
[oweals/busybox.git] / runit / runit_lib.h
1 /*
2 Copyright (c) 2001-2006, Gerrit Pape
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8    1. Redistributions of source code must retain the above copyright notice,
9       this list of conditions and the following disclaimer.
10    2. Redistributions in binary form must reproduce the above copyright
11       notice, this list of conditions and the following disclaimer in the
12       documentation and/or other materials provided with the distribution.
13    3. The name of the author may not be used to endorse or promote products
14       derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*** buffer.h ***/
29
30 typedef struct buffer {
31         char *x;
32         unsigned p;
33         unsigned n;
34         int fd;
35         int (*op)(int fd,char *buf,unsigned len);
36 } buffer;
37
38 #define BUFFER_INIT(op,fd,buf,len) { (buf), 0, (len), (fd), (op) }
39 #define BUFFER_INSIZE 8192
40 #define BUFFER_OUTSIZE 8192
41
42 extern void buffer_init(buffer *,int (*)(int fd,char *buf,unsigned len),int,char *,unsigned);
43
44 extern int buffer_flush(buffer *);
45 extern int buffer_put(buffer *,const char *,unsigned);
46 extern int buffer_putalign(buffer *,const char *,unsigned);
47 extern int buffer_putflush(buffer *,const char *,unsigned);
48 extern int buffer_puts(buffer *,const char *);
49 extern int buffer_putsalign(buffer *,const char *);
50 extern int buffer_putsflush(buffer *,const char *);
51
52 #define buffer_PUTC(s,c) \
53         ( ((s)->n != (s)->p) \
54                 ? ( (s)->x[(s)->p++] = (c), 0 ) \
55                 : buffer_put((s),&(c),1) \
56         )
57
58 extern int buffer_get(buffer *,char *,unsigned);
59 extern int buffer_bget(buffer *,char *,unsigned);
60 extern int buffer_feed(buffer *);
61
62 extern char *buffer_peek(buffer *);
63 extern void buffer_seek(buffer *,unsigned);
64
65 #define buffer_PEEK(s) ( (s)->x + (s)->n )
66 #define buffer_SEEK(s,len) ( ( (s)->p -= (len) ) , ( (s)->n += (len) ) )
67
68 #define buffer_GETC(s,c) \
69         ( ((s)->p > 0) \
70                 ? ( *(c) = (s)->x[(s)->n], buffer_SEEK((s),1), 1 ) \
71                 : buffer_get((s),(c),1) \
72         )
73
74 extern int buffer_copy(buffer *,buffer *);
75
76 extern int buffer_unixread(int,char *,unsigned);
77 /* Actually, int buffer_unixwrite(int,const char *,unsigned),
78          but that 'const' will produce warnings... oh well */
79 extern int buffer_unixwrite(int,char *,unsigned);
80
81
82 /*** byte.h ***/
83
84 extern unsigned byte_chr(char *s,unsigned n,int c);
85
86
87 /*** coe.h ***/
88
89 extern int coe(int);
90
91
92 /*** direntry.h ***/
93
94 #define direntry struct dirent
95
96
97 /*** fd.h ***/
98
99 extern int fd_copy(int,int);
100 extern int fd_move(int,int);
101
102
103 /*** fifo.h ***/
104
105 extern int fifo_make(const char *,int);
106
107
108 /*** fmt.h ***/
109
110 //#define FMT_ULONG 40 /* enough space to hold 2^128 - 1 in decimal, plus \0 */
111 //#define FMT_LEN ((char *) 0) /* convenient abbreviation */
112
113 //extern unsigned fmt_uint(char *,unsigned);
114 //extern unsigned fmt_uint0(char *,unsigned,unsigned);
115 //extern unsigned fmt_xint(char *,unsigned);
116 //extern unsigned fmt_nbbint(char *,unsigned,unsigned,unsigned,unsigned);
117 //extern unsigned fmt_ushort(char *,unsigned short);
118 //extern unsigned fmt_xshort(char *,unsigned short);
119 //extern unsigned fmt_nbbshort(char *,unsigned,unsigned,unsigned,unsigned short);
120 //extern unsigned fmt_ulong(char *,unsigned long);
121 //extern unsigned fmt_xlong(char *,unsigned long);
122 //extern unsigned fmt_nbblong(char *,unsigned,unsigned,unsigned,unsigned long);
123
124 //extern unsigned fmt_plusminus(char *,int);
125 //extern unsigned fmt_minus(char *,int);
126 //extern unsigned fmt_0x(char *,int);
127
128 //extern unsigned fmt_str(char *,const char *);
129 //extern unsigned fmt_strn(char *,const char *,unsigned);
130
131
132 /*** tai.h ***/
133
134 struct tai {
135         uint64_t x;
136 };
137
138 #define tai_unix(t,u) ((void) ((t)->x = 4611686018427387914ULL + (uint64_t) (u)))
139
140 //extern void tai_now(struct tai *);
141
142 //#define tai_approx(t) ((double) ((t)->x))
143
144 //extern void tai_add(struct tai *,const struct tai *,const struct tai *);
145 //extern void tai_sub(struct tai *,const struct tai *,const struct tai *);
146 //#define tai_less(t,u) ((t)->x < (u)->x)
147
148 #define TAI_PACK 8
149 //extern void tai_pack(char *,const struct tai *);
150 extern void tai_unpack(const char *,struct tai *);
151
152 extern void tai_uint(struct tai *,unsigned);
153
154
155 /*** taia.h ***/
156
157 struct taia {
158         struct tai sec;
159         unsigned long nano; /* 0...999999999 */
160         unsigned long atto; /* 0...999999999 */
161 };
162
163 //extern void taia_tai(const struct taia *,struct tai *);
164
165 extern void taia_now(struct taia *);
166
167 //extern double taia_approx(const struct taia *);
168 //extern double taia_frac(const struct taia *);
169
170 extern void taia_add(struct taia *,const struct taia *,const struct taia *);
171 extern void taia_addsec(struct taia *,const struct taia *,int);
172 extern void taia_sub(struct taia *,const struct taia *,const struct taia *);
173 extern void taia_half(struct taia *,const struct taia *);
174 extern int taia_less(const struct taia *,const struct taia *);
175
176 #define TAIA_PACK 16
177 extern void taia_pack(char *,const struct taia *);
178 //extern void taia_unpack(const char *,struct taia *);
179
180 //#define TAIA_FMTFRAC 19
181 //extern unsigned taia_fmtfrac(char *,const struct taia *);
182
183 extern void taia_uint(struct taia *,unsigned);
184
185
186 /*** fmt_ptime.h ***/
187
188 #define FMT_PTIME 30
189
190 /* NUL terminated */
191 extern void fmt_ptime30nul(char *, struct taia *);
192 /* NOT terminated! */
193 extern unsigned fmt_taia25(char *, struct taia *);
194
195
196 #ifdef UNUSED
197 /*** gen_alloc.h ***/
198
199 #define GEN_ALLOC_typedef(ta,type,field,len,a) \
200         typedef struct ta { type *field; unsigned len; unsigned a; } ta;
201
202
203 /*** gen_allocdefs.h ***/
204
205 #define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
206 int ta_ready(ta *x,unsigned n) \
207 { unsigned i; \
208         if (x->field) { \
209                 i = x->a; \
210                 if (n > i) { \
211                         x->a = base + n + (n >> 3); \
212                         x->field = realloc(x->field,x->a * sizeof(type)); \
213                         if (x->field) return 1; \
214                         x->a = i; return 0; } \
215                 return 1; } \
216         x->len = 0; \
217         return !!(x->field = malloc((x->a = n) * sizeof(type))); }
218
219 #define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
220 int ta_rplus(ta *x,unsigned n) \
221 { unsigned i; \
222         if (x->field) { \
223                 i = x->a; n += x->len; \
224                 if (n > i) { \
225                         x->a = base + n + (n >> 3); \
226                         x->field = realloc(x->field,x->a * sizeof(type)); \
227                         if (x->field) return 1; \
228                         x->a = i; return 0; } \
229                 return 1; } \
230         x->len = 0; \
231         return !!(x->field = malloc((x->a = n) * sizeof(type))); }
232
233 #define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
234 int ta_append(ta *x,const type *i) \
235 { if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
236
237
238 /*** stralloc.h ***/
239 GEN_ALLOC_typedef(stralloc,char,s,len,a)
240
241 extern int stralloc_ready(stralloc *,unsigned);
242 extern int stralloc_readyplus(stralloc *,unsigned);
243 extern int stralloc_copy(stralloc *,const stralloc *);
244 extern int stralloc_cat(stralloc *,const stralloc *);
245 extern int stralloc_copys(stralloc *,const char *);
246 extern int stralloc_cats(stralloc *,const char *);
247 extern int stralloc_copyb(stralloc *,const char *,unsigned);
248 extern int stralloc_catb(stralloc *,const char *,unsigned);
249 extern int stralloc_append(stralloc *,const char *); /* beware: this takes a pointer to 1 char */
250 extern int stralloc_starts(stralloc *,const char *);
251
252 #define stralloc_0(sa) stralloc_append(sa,"")
253
254 extern int stralloc_catulong0(stralloc *,unsigned long,unsigned);
255 extern int stralloc_catlong0(stralloc *,long,unsigned);
256
257 #define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
258 #define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
259 #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
260 #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
261 #endif
262
263 /*** iopause.h ***/
264
265 typedef struct pollfd iopause_fd;
266 #define IOPAUSE_READ POLLIN
267 #define IOPAUSE_WRITE POLLOUT
268
269 extern void iopause(iopause_fd *,unsigned,struct taia *,struct taia *);
270
271
272 /*** lock.h ***/
273
274 extern int lock_ex(int);
275 extern int lock_un(int);
276 extern int lock_exnb(int);
277
278
279 /*** open.h ***/
280
281 extern int open_read(const char *);
282 extern int open_excl(const char *);
283 extern int open_append(const char *);
284 extern int open_trunc(const char *);
285 extern int open_write(const char *);
286
287
288 /*** openreadclose.h ***/
289 #if 0
290 extern int openreadclose(const char *,stralloc *,unsigned);
291 #endif
292
293 /*** pathexec.h ***/
294
295 extern void pathexec_run(const char *,char *const *,char *const *);
296 extern int pathexec_env(const char *,const char *);
297 extern void pathexec(char **);
298
299
300 /*** pmatch.h ***/
301
302 extern unsigned pmatch(const char *, const char *, unsigned);
303
304
305 /*** prot.h ***/
306
307 extern int prot_gid(int);
308 extern int prot_uid(int);
309
310
311 /*** readclose.h ***/
312 #if 0
313 extern int readclose_append(int,stralloc *,unsigned);
314 extern int readclose(int,stralloc *,unsigned);
315 #endif
316
317 /*** scan.h ***/
318
319 #if 0
320 extern unsigned scan_uint(const char *,unsigned *);
321 extern unsigned scan_xint(const char *,unsigned *);
322 extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *);
323 extern unsigned scan_ushort(const char *,unsigned short *);
324 extern unsigned scan_xshort(const char *,unsigned short *);
325 extern unsigned scan_nbbshort(const char *,unsigned,unsigned,unsigned,unsigned short *);
326 extern unsigned scan_ulong(const char *,unsigned long *);
327 extern unsigned scan_xlong(const char *,unsigned long *);
328 extern unsigned scan_nbblong(const char *,unsigned,unsigned,unsigned,unsigned long *);
329
330 extern unsigned scan_plusminus(const char *,int *);
331 extern unsigned scan_0x(const char *,unsigned *);
332
333 extern unsigned scan_whitenskip(const char *,unsigned);
334 extern unsigned scan_nonwhitenskip(const char *,unsigned);
335 extern unsigned scan_charsetnskip(const char *,const char *,unsigned);
336 extern unsigned scan_noncharsetnskip(const char *,const char *,unsigned);
337
338 extern unsigned scan_strncmp(const char *,const char *,unsigned);
339 extern unsigned scan_memcmp(const char *,const char *,unsigned);
340
341 extern unsigned scan_long(const char *,long *);
342 extern unsigned scan_8long(const char *,unsigned long *);
343 #endif
344
345
346 /*** seek.h ***/
347
348 typedef unsigned long seek_pos;
349
350 extern seek_pos seek_cur(int);
351
352 //extern int seek_set(int,seek_pos);
353 extern int seek_end(int);
354
355 extern int seek_trunc(int,seek_pos);
356
357 //#define seek_begin(fd) (seek_set((fd),(seek_pos) 0))
358
359
360 /*** sig.h ***/
361
362 //extern int sig_alarm;
363 //extern int sig_child;
364 //extern int sig_cont;
365 //extern int sig_hangup;
366 //extern int sig_int;
367 //extern int sig_pipe;
368 //extern int sig_term;
369
370 extern void sig_catch(int,void (*)(int));
371 #define sig_ignore(s) (sig_catch((s),SIG_IGN))
372 #define sig_uncatch(s) (sig_catch((s),SIG_DFL))
373
374 extern void sig_block(int);
375 extern void sig_unblock(int);
376 extern void sig_blocknone(void);
377 extern void sig_pause(void);
378
379 extern void sig_dfl(int);
380
381
382 /*** str.h ***/
383
384 extern unsigned str_chr(const char *,int);  /* never returns NULL */
385
386 #define str_diff(s,t) strcmp((s),(t))
387 #define str_equal(s,t) (!strcmp((s),(t)))
388
389
390 /*** wait.h ***/
391
392 extern int wait_pid(int *wstat, int pid);
393 extern int wait_nohang(int *wstat);
394
395 #define wait_crashed(w) ((w) & 127)
396 #define wait_exitcode(w) ((w) >> 8)
397 #define wait_stopsig(w) ((w) >> 8)
398 #define wait_stopped(w) (((w) & 127) == 127)