missing piece of prev commit: ndelay_off
[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 extern unsigned fmt_ptime(char *, struct taia *);
191 extern unsigned fmt_taia(char *, struct taia *);
192
193
194 /*** gen_alloc.h ***/
195
196 #define GEN_ALLOC_typedef(ta,type,field,len,a) \
197         typedef struct ta { type *field; unsigned len; unsigned a; } ta;
198
199
200 /*** gen_allocdefs.h ***/
201
202 #define GEN_ALLOC_ready(ta,type,field,len,a,i,n,x,base,ta_ready) \
203 int ta_ready(ta *x,unsigned n) \
204 { unsigned i; \
205         if (x->field) { \
206                 i = x->a; \
207                 if (n > i) { \
208                         x->a = base + n + (n >> 3); \
209                         x->field = realloc(x->field,x->a * sizeof(type)); \
210                         if (x->field) return 1; \
211                         x->a = i; return 0; } \
212                 return 1; } \
213         x->len = 0; \
214         return !!(x->field = malloc((x->a = n) * sizeof(type))); }
215
216 #define GEN_ALLOC_readyplus(ta,type,field,len,a,i,n,x,base,ta_rplus) \
217 int ta_rplus(ta *x,unsigned n) \
218 { unsigned i; \
219         if (x->field) { \
220                 i = x->a; n += x->len; \
221                 if (n > i) { \
222                         x->a = base + n + (n >> 3); \
223                         x->field = realloc(x->field,x->a * sizeof(type)); \
224                         if (x->field) return 1; \
225                         x->a = i; return 0; } \
226                 return 1; } \
227         x->len = 0; \
228         return !!(x->field = malloc((x->a = n) * sizeof(type))); }
229
230 #define GEN_ALLOC_append(ta,type,field,len,a,i,n,x,base,ta_rplus,ta_append) \
231 int ta_append(ta *x,const type *i) \
232 { if (!ta_rplus(x,1)) return 0; x->field[x->len++] = *i; return 1; }
233
234
235 /*** stralloc.h ***/
236 #if 0
237 GEN_ALLOC_typedef(stralloc,char,s,len,a)
238
239 extern int stralloc_ready(stralloc *,unsigned);
240 extern int stralloc_readyplus(stralloc *,unsigned);
241 extern int stralloc_copy(stralloc *,const stralloc *);
242 extern int stralloc_cat(stralloc *,const stralloc *);
243 extern int stralloc_copys(stralloc *,const char *);
244 extern int stralloc_cats(stralloc *,const char *);
245 extern int stralloc_copyb(stralloc *,const char *,unsigned);
246 extern int stralloc_catb(stralloc *,const char *,unsigned);
247 extern int stralloc_append(stralloc *,const char *); /* beware: this takes a pointer to 1 char */
248 extern int stralloc_starts(stralloc *,const char *);
249
250 #define stralloc_0(sa) stralloc_append(sa,"")
251
252 extern int stralloc_catulong0(stralloc *,unsigned long,unsigned);
253 extern int stralloc_catlong0(stralloc *,long,unsigned);
254
255 #define stralloc_catlong(sa,l) (stralloc_catlong0((sa),(l),0))
256 #define stralloc_catuint0(sa,i,n) (stralloc_catulong0((sa),(i),(n)))
257 #define stralloc_catint0(sa,i,n) (stralloc_catlong0((sa),(i),(n)))
258 #define stralloc_catint(sa,i) (stralloc_catlong0((sa),(i),0))
259 #endif
260
261 /*** iopause.h ***/
262
263 typedef struct pollfd iopause_fd;
264 #define IOPAUSE_READ POLLIN
265 #define IOPAUSE_WRITE POLLOUT
266
267 extern void iopause(iopause_fd *,unsigned,struct taia *,struct taia *);
268
269
270 /*** lock.h ***/
271
272 extern int lock_ex(int);
273 extern int lock_un(int);
274 extern int lock_exnb(int);
275
276
277 /*** open.h ***/
278
279 extern int open_read(const char *);
280 extern int open_excl(const char *);
281 extern int open_append(const char *);
282 extern int open_trunc(const char *);
283 extern int open_write(const char *);
284
285
286 /*** openreadclose.h ***/
287 #if 0
288 extern int openreadclose(const char *,stralloc *,unsigned);
289 #endif
290
291 /*** pathexec.h ***/
292
293 extern void pathexec_run(const char *,char *const *,char *const *);
294 extern int pathexec_env(const char *,const char *);
295 extern void pathexec(char **);
296
297
298 /*** pmatch.h ***/
299
300 extern unsigned pmatch(const char *, const char *, unsigned);
301
302
303 /*** prot.h ***/
304
305 extern int prot_gid(int);
306 extern int prot_uid(int);
307
308
309 /*** readclose.h ***/
310 #if 0
311 extern int readclose_append(int,stralloc *,unsigned);
312 extern int readclose(int,stralloc *,unsigned);
313 #endif
314
315 /*** scan.h ***/
316
317 extern unsigned scan_uint(const char *,unsigned *);
318 extern unsigned scan_xint(const char *,unsigned *);
319 extern unsigned scan_nbbint(const char *,unsigned,unsigned,unsigned,unsigned *);
320 extern unsigned scan_ushort(const char *,unsigned short *);
321 extern unsigned scan_xshort(const char *,unsigned short *);
322 extern unsigned scan_nbbshort(const char *,unsigned,unsigned,unsigned,unsigned short *);
323 extern unsigned scan_ulong(const char *,unsigned long *);
324 extern unsigned scan_xlong(const char *,unsigned long *);
325 extern unsigned scan_nbblong(const char *,unsigned,unsigned,unsigned,unsigned long *);
326
327 extern unsigned scan_plusminus(const char *,int *);
328 extern unsigned scan_0x(const char *,unsigned *);
329
330 extern unsigned scan_whitenskip(const char *,unsigned);
331 extern unsigned scan_nonwhitenskip(const char *,unsigned);
332 extern unsigned scan_charsetnskip(const char *,const char *,unsigned);
333 extern unsigned scan_noncharsetnskip(const char *,const char *,unsigned);
334
335 extern unsigned scan_strncmp(const char *,const char *,unsigned);
336 extern unsigned scan_memcmp(const char *,const char *,unsigned);
337
338 extern unsigned scan_long(const char *,long *);
339 extern unsigned scan_8long(const char *,unsigned long *);
340
341
342 /*** seek.h ***/
343
344 typedef unsigned long seek_pos;
345
346 extern seek_pos seek_cur(int);
347
348 extern int seek_set(int,seek_pos);
349 extern int seek_end(int);
350
351 extern int seek_trunc(int,seek_pos);
352
353 #define seek_begin(fd) (seek_set((fd),(seek_pos) 0))
354
355
356 /*** sig.h ***/
357
358 extern int sig_alarm;
359 extern int sig_child;
360 extern int sig_cont;
361 extern int sig_hangup;
362 extern int sig_int;
363 extern int sig_pipe;
364 extern int sig_term;
365
366 extern void (*sig_defaulthandler)(int);
367 extern void (*sig_ignorehandler)(int);
368
369 extern void sig_catch(int,void (*)(int));
370 #define sig_ignore(s) (sig_catch((s),sig_ignorehandler))
371 #define sig_uncatch(s) (sig_catch((s),sig_defaulthandler))
372
373 extern void sig_block(int);
374 extern void sig_unblock(int);
375 extern void sig_blocknone(void);
376 extern void sig_pause(void);
377
378 extern void sig_dfl(int);
379
380
381 /*** str.h ***/
382
383 extern unsigned str_chr(const char *,int);  /* never returns NULL */
384
385 #define str_diff(s,t) strcmp((s),(t))
386 #define str_equal(s,t) (!strcmp((s),(t)))
387
388
389 /*** wait.h ***/
390
391 extern int wait_pid(int *wstat, int pid);
392 extern int wait_nohang(int *wstat);
393
394 #define wait_crashed(w) ((w) & 127)
395 #define wait_exitcode(w) ((w) >> 8)
396 #define wait_stopsig(w) ((w) >> 8)
397 #define wait_stopped(w) (((w) & 127) == 127)