3
0

runit_lib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 Denys 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. unsigned byte_chr(char *s,unsigned n,int c)
  32. {
  33. char ch;
  34. char *t;
  35. ch = c;
  36. t = s;
  37. for (;;) {
  38. if (!n) break;
  39. if (*t == ch) break;
  40. ++t;
  41. --n;
  42. }
  43. return t - s;
  44. }
  45. #ifdef UNUSED
  46. static /* as it isn't used anywhere else */
  47. void tai_pack(char *s, const struct tai *t)
  48. {
  49. uint64_t x;
  50. x = t->x;
  51. s[7] = x & 255; x >>= 8;
  52. s[6] = x & 255; x >>= 8;
  53. s[5] = x & 255; x >>= 8;
  54. s[4] = x & 255; x >>= 8;
  55. s[3] = x & 255; x >>= 8;
  56. s[2] = x & 255; x >>= 8;
  57. s[1] = x & 255; x >>= 8;
  58. s[0] = x;
  59. }
  60. void tai_unpack(const char *s,struct tai *t)
  61. {
  62. uint64_t x;
  63. x = (unsigned char) s[0];
  64. x <<= 8; x += (unsigned char) s[1];
  65. x <<= 8; x += (unsigned char) s[2];
  66. x <<= 8; x += (unsigned char) s[3];
  67. x <<= 8; x += (unsigned char) s[4];
  68. x <<= 8; x += (unsigned char) s[5];
  69. x <<= 8; x += (unsigned char) s[6];
  70. x <<= 8; x += (unsigned char) s[7];
  71. t->x = x;
  72. }
  73. void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
  74. {
  75. t->sec.x = u->sec.x + v->sec.x;
  76. t->nano = u->nano + v->nano;
  77. t->atto = u->atto + v->atto;
  78. if (t->atto > 999999999UL) {
  79. t->atto -= 1000000000UL;
  80. ++t->nano;
  81. }
  82. if (t->nano > 999999999UL) {
  83. t->nano -= 1000000000UL;
  84. ++t->sec.x;
  85. }
  86. }
  87. int taia_less(const struct taia *t, const struct taia *u)
  88. {
  89. if (t->sec.x < u->sec.x) return 1;
  90. if (t->sec.x > u->sec.x) return 0;
  91. if (t->nano < u->nano) return 1;
  92. if (t->nano > u->nano) return 0;
  93. return t->atto < u->atto;
  94. }
  95. void taia_now(struct taia *t)
  96. {
  97. struct timeval now;
  98. gettimeofday(&now, NULL);
  99. tai_unix(&t->sec, now.tv_sec);
  100. t->nano = 1000 * now.tv_usec + 500;
  101. t->atto = 0;
  102. }
  103. /* UNUSED
  104. void taia_pack(char *s, const struct taia *t)
  105. {
  106. unsigned long x;
  107. tai_pack(s, &t->sec);
  108. s += 8;
  109. x = t->atto;
  110. s[7] = x & 255; x >>= 8;
  111. s[6] = x & 255; x >>= 8;
  112. s[5] = x & 255; x >>= 8;
  113. s[4] = x;
  114. x = t->nano;
  115. s[3] = x & 255; x >>= 8;
  116. s[2] = x & 255; x >>= 8;
  117. s[1] = x & 255; x >>= 8;
  118. s[0] = x;
  119. }
  120. */
  121. void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
  122. {
  123. unsigned long unano = u->nano;
  124. unsigned long uatto = u->atto;
  125. t->sec.x = u->sec.x - v->sec.x;
  126. t->nano = unano - v->nano;
  127. t->atto = uatto - v->atto;
  128. if (t->atto > uatto) {
  129. t->atto += 1000000000UL;
  130. --t->nano;
  131. }
  132. if (t->nano > unano) {
  133. t->nano += 1000000000UL;
  134. --t->sec.x;
  135. }
  136. }
  137. /* XXX: breaks tai encapsulation */
  138. void taia_uint(struct taia *t, unsigned s)
  139. {
  140. t->sec.x = s;
  141. t->nano = 0;
  142. t->atto = 0;
  143. }
  144. static
  145. uint64_t taia2millisec(const struct taia *t)
  146. {
  147. return (t->sec.x * 1000) + (t->nano / 1000000);
  148. }
  149. void iopause(iopause_fd *x, unsigned len, struct taia *deadline, struct taia *stamp)
  150. {
  151. int millisecs;
  152. int i;
  153. if (taia_less(deadline, stamp))
  154. millisecs = 0;
  155. else {
  156. uint64_t m;
  157. struct taia t;
  158. t = *stamp;
  159. taia_sub(&t, deadline, &t);
  160. millisecs = m = taia2millisec(&t);
  161. if (m > 1000) millisecs = 1000;
  162. millisecs += 20;
  163. }
  164. for (i = 0; i < len; ++i)
  165. x[i].revents = 0;
  166. poll(x, len, millisecs);
  167. /* XXX: some kernels apparently need x[0] even if len is 0 */
  168. /* XXX: how to handle EAGAIN? are kernels really this dumb? */
  169. /* XXX: how to handle EINVAL? when exactly can this happen? */
  170. }
  171. #endif
  172. int lock_ex(int fd)
  173. {
  174. return flock(fd,LOCK_EX);
  175. }
  176. int lock_exnb(int fd)
  177. {
  178. return flock(fd,LOCK_EX | LOCK_NB);
  179. }
  180. int open_append(const char *fn)
  181. {
  182. return open(fn, O_WRONLY|O_NDELAY|O_APPEND|O_CREAT, 0600);
  183. }
  184. int open_read(const char *fn)
  185. {
  186. return open(fn, O_RDONLY|O_NDELAY);
  187. }
  188. int open_trunc(const char *fn)
  189. {
  190. return open(fn,O_WRONLY | O_NDELAY | O_TRUNC | O_CREAT,0644);
  191. }
  192. int open_write(const char *fn)
  193. {
  194. return open(fn, O_WRONLY|O_NDELAY);
  195. }
  196. unsigned pmatch(const char *p, const char *s, unsigned len)
  197. {
  198. for (;;) {
  199. char c = *p++;
  200. if (!c) return !len;
  201. switch (c) {
  202. case '*':
  203. c = *p;
  204. if (!c) return 1;
  205. for (;;) {
  206. if (!len) return 0;
  207. if (*s == c) break;
  208. ++s;
  209. --len;
  210. }
  211. continue;
  212. case '+':
  213. c = *p++;
  214. if (c != *s) return 0;
  215. for (;;) {
  216. if (!len) return 1;
  217. if (*s != c) break;
  218. ++s;
  219. --len;
  220. }
  221. continue;
  222. /*
  223. case '?':
  224. if (*p == '?') {
  225. if (*s != '?') return 0;
  226. ++p;
  227. }
  228. ++s; --len;
  229. continue;
  230. */
  231. default:
  232. if (!len) return 0;
  233. if (*s != c) return 0;
  234. ++s;
  235. --len;
  236. continue;
  237. }
  238. }
  239. return 0;
  240. }