runit_lib.h 11 KB

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