runit_lib.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977
  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. /* Busyboxed by Denis Vlasenko <vda.linux@googlemail.com> */
  25. /* Collected into one file from runit's many tiny files */
  26. /* TODO: review, eliminate unneeded stuff, move good stuff to libbb */
  27. #include <sys/poll.h>
  28. #include <sys/file.h>
  29. #include "libbb.h"
  30. #include "runit_lib.h"
  31. /*** buffer.c ***/
  32. void buffer_init(buffer *s,int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,unsigned len)
  33. {
  34. s->x = buf;
  35. s->fd = fd;
  36. s->op = op;
  37. s->p = 0;
  38. s->n = len;
  39. }
  40. /*** buffer_get.c ***/
  41. static int oneread(int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,unsigned len)
  42. {
  43. int r;
  44. for (;;) {
  45. r = op(fd,buf,len);
  46. if (r == -1) if (errno == EINTR) continue;
  47. return r;
  48. }
  49. }
  50. static int getthis(buffer *s,char *buf,unsigned len)
  51. {
  52. if (len > s->p) len = s->p;
  53. s->p -= len;
  54. memcpy(buf,s->x + s->n,len);
  55. s->n += len;
  56. return len;
  57. }
  58. int buffer_feed(buffer *s)
  59. {
  60. int r;
  61. if (s->p) return s->p;
  62. r = oneread(s->op,s->fd,s->x,s->n);
  63. if (r <= 0) return r;
  64. s->p = r;
  65. s->n -= r;
  66. if (s->n > 0) memmove(s->x + s->n,s->x,r);
  67. return r;
  68. }
  69. int buffer_bget(buffer *s,char *buf,unsigned len)
  70. {
  71. int r;
  72. if (s->p > 0) return getthis(s,buf,len);
  73. if (s->n <= len) return oneread(s->op,s->fd,buf,s->n);
  74. r = buffer_feed(s); if (r <= 0) return r;
  75. return getthis(s,buf,len);
  76. }
  77. int buffer_get(buffer *s,char *buf,unsigned len)
  78. {
  79. int r;
  80. if (s->p > 0) return getthis(s,buf,len);
  81. if (s->n <= len) return oneread(s->op,s->fd,buf,len);
  82. r = buffer_feed(s); if (r <= 0) return r;
  83. return getthis(s,buf,len);
  84. }
  85. char *buffer_peek(buffer *s)
  86. {
  87. return s->x + s->n;
  88. }
  89. void buffer_seek(buffer *s,unsigned len)
  90. {
  91. s->n += len;
  92. s->p -= len;
  93. }
  94. /*** buffer_put.c ***/
  95. static int allwrite(int (*op)(int fd,char *buf,unsigned len),int fd,const char *buf,unsigned len)
  96. {
  97. int w;
  98. while (len) {
  99. w = op(fd,(char*)buf,len);
  100. if (w == -1) {
  101. if (errno == EINTR) continue;
  102. return -1; /* note that some data may have been written */
  103. }
  104. if (w == 0) ; /* luser's fault */
  105. buf += w;
  106. len -= w;
  107. }
  108. return 0;
  109. }
  110. int buffer_flush(buffer *s)
  111. {
  112. int p;
  113. p = s->p;
  114. if (!p) return 0;
  115. s->p = 0;
  116. return allwrite(s->op,s->fd,s->x,p);
  117. }
  118. int buffer_putalign(buffer *s,const char *buf,unsigned len)
  119. {
  120. unsigned n;
  121. while (len > (n = s->n - s->p)) {
  122. memcpy(s->x + s->p,buf,n);
  123. s->p += n;
  124. buf += n;
  125. len -= n;
  126. if (buffer_flush(s) == -1) return -1;
  127. }
  128. /* now len <= s->n - s->p */
  129. memcpy(s->x + s->p,buf,len);
  130. s->p += len;
  131. return 0;
  132. }
  133. int buffer_put(buffer *s,const char *buf,unsigned len)
  134. {
  135. unsigned n;
  136. n = s->n;
  137. if (len > n - s->p) {
  138. if (buffer_flush(s) == -1) return -1;
  139. /* now s->p == 0 */
  140. if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE;
  141. while (len > s->n) {
  142. if (n > len) n = len;
  143. if (allwrite(s->op,s->fd,buf,n) == -1) return -1;
  144. buf += n;
  145. len -= n;
  146. }
  147. }
  148. /* now len <= s->n - s->p */
  149. memcpy(s->x + s->p,buf,len);
  150. s->p += len;
  151. return 0;
  152. }
  153. int buffer_putflush(buffer *s,const char *buf,unsigned len)
  154. {
  155. if (buffer_flush(s) == -1) return -1;
  156. return allwrite(s->op,s->fd,buf,len);
  157. }
  158. int buffer_putsalign(buffer *s,const char *buf)
  159. {
  160. return buffer_putalign(s,buf,strlen(buf));
  161. }
  162. int buffer_puts(buffer *s,const char *buf)
  163. {
  164. return buffer_put(s,buf,strlen(buf));
  165. }
  166. int buffer_putsflush(buffer *s,const char *buf)
  167. {
  168. return buffer_putflush(s,buf,strlen(buf));
  169. }
  170. /*** buffer_read.c ***/
  171. int buffer_unixread(int fd,char *buf,unsigned len)
  172. {
  173. return read(fd,buf,len);
  174. }
  175. /*** buffer_write.c ***/
  176. int buffer_unixwrite(int fd,char *buf,unsigned len)
  177. {
  178. return write(fd,buf,len);
  179. }
  180. /*** byte_chr.c ***/
  181. unsigned byte_chr(char *s,unsigned n,int c)
  182. {
  183. char ch;
  184. char *t;
  185. ch = c;
  186. t = s;
  187. for (;;) {
  188. if (!n) break; if (*t == ch) break; ++t; --n;
  189. if (!n) break; if (*t == ch) break; ++t; --n;
  190. if (!n) break; if (*t == ch) break; ++t; --n;
  191. if (!n) break; if (*t == ch) break; ++t; --n;
  192. }
  193. return t - s;
  194. }
  195. /*** coe.c ***/
  196. int coe(int fd)
  197. {
  198. return fcntl(fd,F_SETFD,FD_CLOEXEC);
  199. }
  200. /*** fd_copy.c ***/
  201. int fd_copy(int to,int from)
  202. {
  203. if (to == from) return 0;
  204. if (fcntl(from,F_GETFL,0) == -1) return -1;
  205. close(to);
  206. if (fcntl(from,F_DUPFD,to) == -1) return -1;
  207. return 0;
  208. }
  209. /*** fd_move.c ***/
  210. int fd_move(int to,int from)
  211. {
  212. if (to == from) return 0;
  213. if (fd_copy(to,from) == -1) return -1;
  214. close(from);
  215. return 0;
  216. }
  217. /*** fifo.c ***/
  218. int fifo_make(const char *fn,int mode) { return mkfifo(fn,mode); }
  219. /*** fmt_ptime.c ***/
  220. unsigned fmt_ptime(char *s, struct taia *ta) {
  221. struct tm *t;
  222. unsigned long u;
  223. if (ta->sec.x < 4611686018427387914ULL) return 0; /* impossible? */
  224. u = ta->sec.x -4611686018427387914ULL;
  225. if (!(t = gmtime((time_t*)&u))) return 0;
  226. fmt_ulong(s, 1900 + t->tm_year);
  227. s[4] = '-'; fmt_uint0(&s[5], t->tm_mon+1, 2);
  228. s[7] = '-'; fmt_uint0(&s[8], t->tm_mday, 2);
  229. s[10] = '_'; fmt_uint0(&s[11], t->tm_hour, 2);
  230. s[13] = ':'; fmt_uint0(&s[14], t->tm_min, 2);
  231. s[16] = ':'; fmt_uint0(&s[17], t->tm_sec, 2);
  232. s[19] = '.'; fmt_uint0(&s[20], ta->nano, 9);
  233. return 25;
  234. }
  235. unsigned fmt_taia(char *s, struct taia *t) {
  236. static char pack[TAIA_PACK];
  237. taia_pack(pack, t);
  238. *s++ = '@';
  239. bin2hex(s, pack, 12);
  240. return 25;
  241. }
  242. /*** fmt_uint.c ***/
  243. unsigned fmt_uint(char *s,unsigned u)
  244. {
  245. return fmt_ulong(s,u);
  246. }
  247. /*** fmt_uint0.c ***/
  248. unsigned fmt_uint0(char *s,unsigned u,unsigned n)
  249. {
  250. unsigned len;
  251. len = fmt_uint(FMT_LEN,u);
  252. while (len < n) { if (s) *s++ = '0'; ++len; }
  253. if (s) fmt_uint(s,u);
  254. return len;
  255. }
  256. /*** fmt_ulong.c ***/
  257. unsigned fmt_ulong(char *s,unsigned long u)
  258. {
  259. unsigned len; unsigned long q;
  260. len = 1; q = u;
  261. while (q > 9) { ++len; q /= 10; }
  262. if (s) {
  263. s += len;
  264. do { *--s = '0' + (u % 10); u /= 10; } while (u); /* handles u == 0 */
  265. }
  266. return len;
  267. }
  268. /*** tai_now.c ***/
  269. void tai_now(struct tai *t)
  270. {
  271. tai_unix(t,time((time_t *) 0));
  272. }
  273. /*** tai_pack.c ***/
  274. void tai_pack(char *s,const struct tai *t)
  275. {
  276. uint64_t x;
  277. x = t->x;
  278. s[7] = x & 255; x >>= 8;
  279. s[6] = x & 255; x >>= 8;
  280. s[5] = x & 255; x >>= 8;
  281. s[4] = x & 255; x >>= 8;
  282. s[3] = x & 255; x >>= 8;
  283. s[2] = x & 255; x >>= 8;
  284. s[1] = x & 255; x >>= 8;
  285. s[0] = x;
  286. }
  287. /*** tai_sub.c ***/
  288. void tai_sub(struct tai *t,const struct tai *u,const struct tai *v)
  289. {
  290. t->x = u->x - v->x;
  291. }
  292. /*** tai_unpack.c ***/
  293. void tai_unpack(const char *s,struct tai *t)
  294. {
  295. uint64_t x;
  296. x = (unsigned char) s[0];
  297. x <<= 8; x += (unsigned char) s[1];
  298. x <<= 8; x += (unsigned char) s[2];
  299. x <<= 8; x += (unsigned char) s[3];
  300. x <<= 8; x += (unsigned char) s[4];
  301. x <<= 8; x += (unsigned char) s[5];
  302. x <<= 8; x += (unsigned char) s[6];
  303. x <<= 8; x += (unsigned char) s[7];
  304. t->x = x;
  305. }
  306. /*** taia_add.c ***/
  307. /* XXX: breaks tai encapsulation */
  308. void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
  309. {
  310. t->sec.x = u->sec.x + v->sec.x;
  311. t->nano = u->nano + v->nano;
  312. t->atto = u->atto + v->atto;
  313. if (t->atto > 999999999UL) {
  314. t->atto -= 1000000000UL;
  315. ++t->nano;
  316. }
  317. if (t->nano > 999999999UL) {
  318. t->nano -= 1000000000UL;
  319. ++t->sec.x;
  320. }
  321. }
  322. /*** taia_approx.c ***/
  323. double taia_approx(const struct taia *t)
  324. {
  325. return tai_approx(&t->sec) + taia_frac(t);
  326. }
  327. /*** taia_frac.c ***/
  328. double taia_frac(const struct taia *t)
  329. {
  330. return (t->atto * 0.000000001 + t->nano) * 0.000000001;
  331. }
  332. /*** taia_less.c ***/
  333. /* XXX: breaks tai encapsulation */
  334. int taia_less(const struct taia *t,const struct taia *u)
  335. {
  336. if (t->sec.x < u->sec.x) return 1;
  337. if (t->sec.x > u->sec.x) return 0;
  338. if (t->nano < u->nano) return 1;
  339. if (t->nano > u->nano) return 0;
  340. return t->atto < u->atto;
  341. }
  342. /*** taia_now.c ***/
  343. void taia_now(struct taia *t)
  344. {
  345. struct timeval now;
  346. gettimeofday(&now,(struct timezone *) 0);
  347. tai_unix(&t->sec,now.tv_sec);
  348. t->nano = 1000 * now.tv_usec + 500;
  349. t->atto = 0;
  350. }
  351. /*** taia_pack.c ***/
  352. void taia_pack(char *s,const struct taia *t)
  353. {
  354. unsigned long x;
  355. tai_pack(s,&t->sec);
  356. s += 8;
  357. x = t->atto;
  358. s[7] = x & 255; x >>= 8;
  359. s[6] = x & 255; x >>= 8;
  360. s[5] = x & 255; x >>= 8;
  361. s[4] = x;
  362. x = t->nano;
  363. s[3] = x & 255; x >>= 8;
  364. s[2] = x & 255; x >>= 8;
  365. s[1] = x & 255; x >>= 8;
  366. s[0] = x;
  367. }
  368. /*** taia_sub.c ***/
  369. /* XXX: breaks tai encapsulation */
  370. void taia_sub(struct taia *t,const struct taia *u,const struct taia *v)
  371. {
  372. unsigned long unano = u->nano;
  373. unsigned long uatto = u->atto;
  374. t->sec.x = u->sec.x - v->sec.x;
  375. t->nano = unano - v->nano;
  376. t->atto = uatto - v->atto;
  377. if (t->atto > uatto) {
  378. t->atto += 1000000000UL;
  379. --t->nano;
  380. }
  381. if (t->nano > unano) {
  382. t->nano += 1000000000UL;
  383. --t->sec.x;
  384. }
  385. }
  386. /*** taia_uint.c ***/
  387. /* XXX: breaks tai encapsulation */
  388. void taia_uint(struct taia *t,unsigned s)
  389. {
  390. t->sec.x = s;
  391. t->nano = 0;
  392. t->atto = 0;
  393. }
  394. /*** stralloc_cat.c ***/
  395. #if 0
  396. int stralloc_cat(stralloc *sato,const stralloc *safrom)
  397. {
  398. return stralloc_catb(sato,safrom->s,safrom->len);
  399. }
  400. /*** stralloc_catb.c ***/
  401. int stralloc_catb(stralloc *sa,const char *s,unsigned n)
  402. {
  403. if (!sa->s) return stralloc_copyb(sa,s,n);
  404. if (!stralloc_readyplus(sa,n + 1)) return 0;
  405. memcpy(sa->s + sa->len,s,n);
  406. sa->len += n;
  407. sa->s[sa->len] = 'Z'; /* ``offensive programming'' */
  408. return 1;
  409. }
  410. /*** stralloc_cats.c ***/
  411. int stralloc_cats(stralloc *sa,const char *s)
  412. {
  413. return stralloc_catb(sa,s,strlen(s));
  414. }
  415. /*** stralloc_eady.c ***/
  416. GEN_ALLOC_ready(stralloc,char,s,len,a,i,n,x,30,stralloc_ready)
  417. GEN_ALLOC_readyplus(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus)
  418. /*** stralloc_opyb.c ***/
  419. int stralloc_copyb(stralloc *sa,const char *s,unsigned n)
  420. {
  421. if (!stralloc_ready(sa,n + 1)) return 0;
  422. memcpy(sa->s,s,n);
  423. sa->len = n;
  424. sa->s[n] = 'Z'; /* ``offensive programming'' */
  425. return 1;
  426. }
  427. /*** stralloc_opys.c ***/
  428. int stralloc_copys(stralloc *sa,const char *s)
  429. {
  430. return stralloc_copyb(sa,s,strlen(s));
  431. }
  432. /*** stralloc_pend.c ***/
  433. GEN_ALLOC_append(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus,stralloc_append)
  434. #endif /* stralloc */
  435. /*** iopause.c ***/
  436. void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp)
  437. {
  438. struct taia t;
  439. int millisecs;
  440. double d;
  441. int i;
  442. if (taia_less(deadline,stamp))
  443. millisecs = 0;
  444. else {
  445. t = *stamp;
  446. taia_sub(&t,deadline,&t);
  447. d = taia_approx(&t);
  448. if (d > 1000.0) d = 1000.0;
  449. millisecs = d * 1000.0 + 20.0;
  450. }
  451. for (i = 0;i < len;++i)
  452. x[i].revents = 0;
  453. poll(x,len,millisecs);
  454. /* XXX: some kernels apparently need x[0] even if len is 0 */
  455. /* XXX: how to handle EAGAIN? are kernels really this dumb? */
  456. /* XXX: how to handle EINVAL? when exactly can this happen? */
  457. }
  458. /*** lock_ex.c ***/
  459. int lock_ex(int fd)
  460. {
  461. return flock(fd,LOCK_EX);
  462. }
  463. /*** lock_exnb.c ***/
  464. int lock_exnb(int fd)
  465. {
  466. return flock(fd,LOCK_EX | LOCK_NB);
  467. }
  468. /*** open_append.c ***/
  469. int open_append(const char *fn)
  470. {
  471. return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  472. }
  473. /*** open_read.c ***/
  474. int open_read(const char *fn)
  475. {
  476. return open(fn, O_RDONLY|O_NDELAY);
  477. }
  478. /*** open_trunc.c ***/
  479. int open_trunc(const char *fn)
  480. {
  481. return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644);
  482. }
  483. /*** open_write.c ***/
  484. int open_write(const char *fn)
  485. {
  486. return open(fn, O_WRONLY|O_NDELAY);
  487. }
  488. /*** openreadclose.c ***/
  489. #if 0
  490. int openreadclose(const char *fn,stralloc *sa,unsigned bufsize)
  491. {
  492. int fd;
  493. fd = open_read(fn);
  494. if (fd == -1) {
  495. if (errno == ENOENT) return 0;
  496. return -1;
  497. }
  498. if (readclose(fd,sa,bufsize) == -1) return -1;
  499. return 1;
  500. }
  501. #endif
  502. /*** pathexec_env.c ***/
  503. #if 0
  504. static stralloc plus;
  505. static stralloc tmp;
  506. int pathexec_env(const char *s,const char *t)
  507. {
  508. if (!s) return 1;
  509. if (!stralloc_copys(&tmp,s)) return 0;
  510. if (t) {
  511. if (!stralloc_cats(&tmp,"=")) return 0;
  512. if (!stralloc_cats(&tmp,t)) return 0;
  513. }
  514. if (!stralloc_0(&tmp)) return 0;
  515. return stralloc_cat(&plus,&tmp);
  516. }
  517. void pathexec(char **argv)
  518. {
  519. char **e;
  520. unsigned elen;
  521. unsigned i;
  522. unsigned j;
  523. unsigned split;
  524. unsigned t;
  525. if (!stralloc_cats(&plus,"")) return;
  526. elen = 0;
  527. for (i = 0;environ[i];++i)
  528. ++elen;
  529. for (i = 0;i < plus.len;++i)
  530. if (!plus.s[i])
  531. ++elen;
  532. e = malloc((elen + 1) * sizeof(char *));
  533. if (!e) return;
  534. elen = 0;
  535. for (i = 0;environ[i];++i)
  536. e[elen++] = environ[i];
  537. j = 0;
  538. for (i = 0;i < plus.len;++i)
  539. if (!plus.s[i]) {
  540. split = str_chr(plus.s + j,'=');
  541. for (t = 0;t < elen;++t)
  542. if (memcmp(plus.s + j,e[t],split) == 0)
  543. if (e[t][split] == '=') {
  544. --elen;
  545. e[t] = e[elen];
  546. break;
  547. }
  548. if (plus.s[j + split])
  549. e[elen++] = plus.s + j;
  550. j = i + 1;
  551. }
  552. e[elen] = 0;
  553. pathexec_run(*argv,argv,e);
  554. free(e);
  555. }
  556. #endif
  557. /*** pathexec_run.c ***/
  558. #if 0
  559. static stralloc tmp;
  560. void pathexec_run(const char *file,char *const *argv,char *const *envp)
  561. {
  562. const char *path;
  563. unsigned split;
  564. int savederrno;
  565. if (file[str_chr(file,'/')]) {
  566. execve(file,argv,envp);
  567. return;
  568. }
  569. path = getenv("PATH");
  570. if (!path) path = "/bin:/usr/bin";
  571. savederrno = 0;
  572. for (;;) {
  573. split = str_chr(path,':');
  574. if (!stralloc_copyb(&tmp,path,split)) return;
  575. if (!split)
  576. if (!stralloc_cats(&tmp,".")) return;
  577. if (!stralloc_cats(&tmp,"/")) return;
  578. if (!stralloc_cats(&tmp,file)) return;
  579. if (!stralloc_0(&tmp)) return;
  580. execve(tmp.s,argv,envp);
  581. if (errno != ENOENT) {
  582. savederrno = errno;
  583. if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR)) return;
  584. }
  585. if (!path[split]) {
  586. if (savederrno) errno = savederrno;
  587. return;
  588. }
  589. path += split;
  590. path += 1;
  591. }
  592. }
  593. #endif
  594. /*** pmatch.c ***/
  595. unsigned pmatch(const char *p, const char *s, unsigned len) {
  596. for (;;) {
  597. char c = *p++;
  598. if (!c) return !len;
  599. switch (c) {
  600. case '*':
  601. if (!(c = *p)) return 1;
  602. for (;;) {
  603. if (!len) return 0;
  604. if (*s == c) break;
  605. ++s; --len;
  606. }
  607. continue;
  608. case '+':
  609. if ((c = *p++) != *s) return 0;
  610. for (;;) {
  611. if (!len) return 1;
  612. if (*s != c) break;
  613. ++s; --len;
  614. }
  615. continue;
  616. /*
  617. case '?':
  618. if (*p == '?') {
  619. if (*s != '?') return 0;
  620. ++p;
  621. }
  622. ++s; --len;
  623. continue;
  624. */
  625. default:
  626. if (!len) return 0;
  627. if (*s != c) return 0;
  628. ++s; --len;
  629. continue;
  630. }
  631. }
  632. return 0;
  633. }
  634. /*** prot.c ***/
  635. int prot_gid(int gid)
  636. {
  637. gid_t x = gid;
  638. if (setgroups(1,&x) == -1) return -1;
  639. return setgid(gid); /* _should_ be redundant, but on some systems it isn't */
  640. }
  641. int prot_uid(int uid)
  642. {
  643. return setuid(uid);
  644. }
  645. /*** readclose.c ***/
  646. #if 0
  647. int readclose_append(int fd,stralloc *sa,unsigned bufsize)
  648. {
  649. int r;
  650. for (;;) {
  651. if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; }
  652. r = read(fd,sa->s + sa->len,bufsize);
  653. if (r == -1) if (errno == EINTR) continue;
  654. if (r <= 0) { close(fd); return r; }
  655. sa->len += r;
  656. }
  657. }
  658. int readclose(int fd,stralloc *sa,unsigned bufsize)
  659. {
  660. if (!stralloc_copys(sa,"")) { close(fd); return -1; }
  661. return readclose_append(fd,sa,bufsize);
  662. }
  663. #endif
  664. /*** scan_ulong.c ***/
  665. unsigned scan_ulong(const char *s,unsigned long *u)
  666. {
  667. unsigned pos = 0;
  668. unsigned long result = 0;
  669. unsigned long c;
  670. while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) {
  671. result = result * 10 + c;
  672. ++pos;
  673. }
  674. *u = result;
  675. return pos;
  676. }
  677. /*** seek_set.c ***/
  678. int seek_set(int fd,seek_pos pos)
  679. {
  680. if (lseek(fd,(off_t) pos,SEEK_SET) == -1) return -1; return 0;
  681. }
  682. /*** sig.c ***/
  683. int sig_alarm = SIGALRM;
  684. int sig_child = SIGCHLD;
  685. int sig_cont = SIGCONT;
  686. int sig_hangup = SIGHUP;
  687. int sig_int = SIGINT;
  688. int sig_pipe = SIGPIPE;
  689. int sig_term = SIGTERM;
  690. void (*sig_defaulthandler)(int) = SIG_DFL;
  691. void (*sig_ignorehandler)(int) = SIG_IGN;
  692. /*** sig_block.c ***/
  693. void sig_block(int sig)
  694. {
  695. sigset_t ss;
  696. sigemptyset(&ss);
  697. sigaddset(&ss,sig);
  698. sigprocmask(SIG_BLOCK,&ss,(sigset_t *) 0);
  699. }
  700. void sig_unblock(int sig)
  701. {
  702. sigset_t ss;
  703. sigemptyset(&ss);
  704. sigaddset(&ss,sig);
  705. sigprocmask(SIG_UNBLOCK,&ss,(sigset_t *) 0);
  706. }
  707. void sig_blocknone(void)
  708. {
  709. sigset_t ss;
  710. sigemptyset(&ss);
  711. sigprocmask(SIG_SETMASK,&ss,(sigset_t *) 0);
  712. }
  713. /*** sig_catch.c ***/
  714. void sig_catch(int sig,void (*f)(int))
  715. {
  716. struct sigaction sa;
  717. sa.sa_handler = f;
  718. sa.sa_flags = 0;
  719. sigemptyset(&sa.sa_mask);
  720. sigaction(sig,&sa,(struct sigaction *) 0);
  721. }
  722. /*** sig_pause.c ***/
  723. void sig_pause(void)
  724. {
  725. sigset_t ss;
  726. sigemptyset(&ss);
  727. sigsuspend(&ss);
  728. }
  729. /*** str_chr.c ***/
  730. unsigned str_chr(const char *s,int c)
  731. {
  732. char ch;
  733. const char *t;
  734. ch = c;
  735. t = s;
  736. for (;;) {
  737. if (!*t) break; if (*t == ch) break; ++t;
  738. if (!*t) break; if (*t == ch) break; ++t;
  739. if (!*t) break; if (*t == ch) break; ++t;
  740. if (!*t) break; if (*t == ch) break; ++t;
  741. }
  742. return t - s;
  743. }
  744. /*** wait_nohang.c ***/
  745. int wait_nohang(int *wstat)
  746. {
  747. return waitpid(-1,wstat,WNOHANG);
  748. }
  749. /*** wait_pid.c ***/
  750. int wait_pid(int *wstat, int pid)
  751. {
  752. int r;
  753. do
  754. r = waitpid(pid,wstat,0);
  755. while ((r == -1) && (errno == EINTR));
  756. return r;
  757. }