aboutsummaryrefslogtreecommitdiff
path: root/runit/runit_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'runit/runit_lib.c')
-rw-r--r--runit/runit_lib.c552
1 files changed, 4 insertions, 548 deletions
diff --git a/runit/runit_lib.c b/runit/runit_lib.c
index 181d43836..aa1d96002 100644
--- a/runit/runit_lib.c
+++ b/runit/runit_lib.c
@@ -34,207 +34,6 @@ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34#include "libbb.h" 34#include "libbb.h"
35#include "runit_lib.h" 35#include "runit_lib.h"
36 36
37#if 0
38/*** buffer.c ***/
39
40void buffer_init(buffer *s,int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,unsigned len)
41{
42 s->x = buf;
43 s->fd = fd;
44 s->op = op;
45 s->p = 0;
46 s->n = len;
47}
48
49
50/*** buffer_get.c ***/
51
52static int oneread(int (*op)(int fd,char *buf,unsigned len),int fd,char *buf,unsigned len)
53{
54 int r;
55
56 for (;;) {
57 r = op(fd,buf,len);
58 if (r == -1 && errno == EINTR)
59 continue;
60 return r;
61 }
62}
63
64static int getthis(buffer *s,char *buf,unsigned len)
65{
66 if (len > s->p) len = s->p;
67 s->p -= len;
68 memcpy(buf,s->x + s->n,len);
69 s->n += len;
70 return len;
71}
72
73int buffer_feed(buffer *s)
74{
75 int r;
76
77 if (s->p)
78 return s->p;
79 r = oneread(s->op,s->fd,s->x,s->n);
80 if (r <= 0)
81 return r;
82 s->p = r;
83 s->n -= r;
84 if (s->n > 0)
85 memmove(s->x + s->n,s->x,r);
86 return r;
87}
88
89int buffer_bget(buffer *s,char *buf,unsigned len)
90{
91 int r;
92
93 if (s->p > 0)
94 return getthis(s,buf,len);
95 if (s->n <= len)
96 return oneread(s->op,s->fd,buf,s->n);
97 r = buffer_feed(s);
98 if (r <= 0)
99 return r;
100 return getthis(s,buf,len);
101}
102
103int buffer_get(buffer *s,char *buf,unsigned len)
104{
105 int r;
106
107 if (s->p > 0)
108 return getthis(s,buf,len);
109 if (s->n <= len)
110 return oneread(s->op,s->fd,buf,len);
111 r = buffer_feed(s);
112 if (r <= 0)
113 return r;
114 return getthis(s,buf,len);
115}
116
117char *buffer_peek(buffer *s)
118{
119 return s->x + s->n;
120}
121
122void buffer_seek(buffer *s,unsigned len)
123{
124 s->n += len;
125 s->p -= len;
126}
127
128
129/*** buffer_put.c ***/
130
131static int allwrite(int (*op)(int fd,char *buf,unsigned len),int fd,const char *buf,unsigned len)
132{
133 int w;
134
135 while (len) {
136 w = op(fd,(char*)buf,len);
137 if (w == -1) {
138 if (errno == EINTR)
139 continue;
140 return -1; /* note that some data may have been written */
141 }
142 /* if (w == 0) ; luser's fault */
143 buf += w;
144 len -= w;
145 }
146 return 0;
147}
148
149int buffer_flush(buffer *s)
150{
151 int p;
152
153 p = s->p;
154 if (!p) return 0;
155 s->p = 0;
156 return allwrite(s->op,s->fd,s->x,p);
157}
158
159int buffer_putalign(buffer *s,const char *buf,unsigned len)
160{
161 unsigned n;
162
163 while (len > (n = s->n - s->p)) {
164 memcpy(s->x + s->p,buf,n);
165 s->p += n;
166 buf += n;
167 len -= n;
168 if (buffer_flush(s) == -1) return -1;
169 }
170 /* now len <= s->n - s->p */
171 memcpy(s->x + s->p,buf,len);
172 s->p += len;
173 return 0;
174}
175
176int buffer_put(buffer *s,const char *buf,unsigned len)
177{
178 unsigned n;
179
180 n = s->n;
181 if (len > n - s->p) {
182 if (buffer_flush(s) == -1) return -1;
183 /* now s->p == 0 */
184 if (n < BUFFER_OUTSIZE) n = BUFFER_OUTSIZE;
185 while (len > s->n) {
186 if (n > len) n = len;
187 if (allwrite(s->op,s->fd,buf,n) == -1) return -1;
188 buf += n;
189 len -= n;
190 }
191 }
192 /* now len <= s->n - s->p */
193 memcpy(s->x + s->p,buf,len);
194 s->p += len;
195 return 0;
196}
197
198int buffer_putflush(buffer *s,const char *buf,unsigned len)
199{
200 if (buffer_flush(s) == -1)
201 return -1;
202 return allwrite(s->op,s->fd,buf,len);
203}
204
205int buffer_putsalign(buffer *s,const char *buf)
206{
207 return buffer_putalign(s,buf,strlen(buf));
208}
209
210int buffer_puts(buffer *s,const char *buf)
211{
212 return buffer_put(s,buf,strlen(buf));
213}
214
215int buffer_putsflush(buffer *s,const char *buf)
216{
217 return buffer_putflush(s,buf,strlen(buf));
218}
219
220
221/*** buffer_read.c ***/
222
223int buffer_unixread(int fd,char *buf,unsigned len)
224{
225 return read(fd,buf,len);
226}
227
228
229/*** buffer_write.c ***/
230
231int buffer_unixwrite(int fd,char *buf,unsigned len)
232{
233 return write(fd,buf,len);
234}
235#endif
236
237
238/*** byte_chr.c ***/ 37/*** byte_chr.c ***/
239 38
240unsigned byte_chr(char *s,unsigned n,int c) 39unsigned byte_chr(char *s,unsigned n,int c)
@@ -290,16 +89,6 @@ int fd_move(int to,int from)
290} 89}
291 90
292 91
293#if 0
294/*** fifo.c ***/
295
296int fifo_make(const char *fn,int mode)
297{
298 return mkfifo(fn, mode);
299}
300#endif
301
302
303/*** fmt_ptime.c ***/ 92/*** fmt_ptime.c ***/
304 93
305void fmt_ptime30nul(char *s, struct taia *ta) { 94void fmt_ptime30nul(char *s, struct taia *ta) {
@@ -344,60 +133,6 @@ unsigned fmt_taia25(char *s, struct taia *t) {
344} 133}
345 134
346 135
347#ifdef UNUSED
348/*** fmt_uint.c ***/
349
350unsigned fmt_uint(char *s,unsigned u)
351{
352 return fmt_ulong(s,u);
353}
354
355
356/*** fmt_uint0.c ***/
357
358unsigned fmt_uint0(char *s,unsigned u,unsigned n)
359{
360 unsigned len;
361 len = fmt_uint(FMT_LEN, u);
362 while (len < n) {
363 if (s)
364 *s++ = '0';
365 ++len;
366 }
367 if (s)
368 fmt_uint(s, u);
369 return len;
370}
371#endif
372
373
374#ifdef UNUSED
375/*** fmt_ulong.c ***/
376
377unsigned fmt_ulong(char *s,unsigned long u)
378{
379 unsigned len; unsigned long q;
380 len = 1; q = u;
381 while (q > 9) { ++len; q /= 10; }
382 if (s) {
383 s += len;
384 do { *--s = '0' + (u % 10); u /= 10; } while (u); /* handles u == 0 */
385 }
386 return len;
387}
388#endif
389
390
391#ifdef UNUSED
392/*** tai_now.c ***/
393
394void tai_now(struct tai *t)
395{
396 tai_unix(t, time(NULL));
397}
398#endif
399
400
401/*** tai_pack.c ***/ 136/*** tai_pack.c ***/
402 137
403static /* as it isn't used anywhere else */ 138static /* as it isn't used anywhere else */
@@ -447,8 +182,6 @@ void tai_unpack(const char *s,struct tai *t)
447 182
448/*** taia_add.c ***/ 183/*** taia_add.c ***/
449 184
450/* XXX: breaks tai encapsulation */
451
452void taia_add(struct taia *t,const struct taia *u,const struct taia *v) 185void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
453{ 186{
454 t->sec.x = u->sec.x + v->sec.x; 187 t->sec.x = u->sec.x + v->sec.x;
@@ -465,34 +198,8 @@ void taia_add(struct taia *t,const struct taia *u,const struct taia *v)
465} 198}
466 199
467 200
468#ifdef UNUSED
469/*** taia_frac.c ***/
470
471double taia_frac(const struct taia *t)
472{
473 return (t->atto * 0.000000001 + t->nano) * 0.000000001;
474}
475
476
477/*** taia_approx.c ***/
478
479double taia_approx(const struct taia *t)
480{
481 return t->sec->x + taia_frac(t);
482}
483#endif
484
485static
486uint64_t taia2millisec(const struct taia *t)
487{
488 return (t->sec.x * 1000) + (t->nano / 1000000);
489}
490
491
492/*** taia_less.c ***/ 201/*** taia_less.c ***/
493 202
494/* XXX: breaks tai encapsulation */
495
496int taia_less(const struct taia *t, const struct taia *u) 203int taia_less(const struct taia *t, const struct taia *u)
497{ 204{
498 if (t->sec.x < u->sec.x) return 1; 205 if (t->sec.x < u->sec.x) return 1;
@@ -539,8 +246,6 @@ void taia_pack(char *s, const struct taia *t)
539 246
540/*** taia_sub.c ***/ 247/*** taia_sub.c ***/
541 248
542/* XXX: breaks tai encapsulation */
543
544void taia_sub(struct taia *t, const struct taia *u, const struct taia *v) 249void taia_sub(struct taia *t, const struct taia *u, const struct taia *v)
545{ 250{
546 unsigned long unano = u->nano; 251 unsigned long unano = u->nano;
@@ -572,70 +277,15 @@ void taia_uint(struct taia *t, unsigned s)
572} 277}
573 278
574 279
575/*** stralloc_cat.c ***/ 280/*** iopause.c ***/
576#if 0
577
578int stralloc_cat(stralloc *sato,const stralloc *safrom)
579{
580 return stralloc_catb(sato,safrom->s,safrom->len);
581}
582
583
584/*** stralloc_catb.c ***/
585
586int stralloc_catb(stralloc *sa,const char *s,unsigned n)
587{
588 if (!sa->s) return stralloc_copyb(sa,s,n);
589 if (!stralloc_readyplus(sa,n + 1)) return 0;
590 memcpy(sa->s + sa->len,s,n);
591 sa->len += n;
592 sa->s[sa->len] = 'Z'; /* ``offensive programming'' */
593 return 1;
594}
595
596
597/*** stralloc_cats.c ***/
598
599int stralloc_cats(stralloc *sa,const char *s)
600{
601 return stralloc_catb(sa,s,strlen(s));
602}
603
604
605/*** stralloc_eady.c ***/
606
607GEN_ALLOC_ready(stralloc,char,s,len,a,i,n,x,30,stralloc_ready)
608GEN_ALLOC_readyplus(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus)
609
610
611/*** stralloc_opyb.c ***/
612
613int stralloc_copyb(stralloc *sa,const char *s,unsigned n)
614{
615 if (!stralloc_ready(sa,n + 1)) return 0;
616 memcpy(sa->s,s,n);
617 sa->len = n;
618 sa->s[n] = 'Z'; /* ``offensive programming'' */
619 return 1;
620}
621
622
623/*** stralloc_opys.c ***/
624 281
625int stralloc_copys(stralloc *sa,const char *s) 282static
283uint64_t taia2millisec(const struct taia *t)
626{ 284{
627 return stralloc_copyb(sa,s,strlen(s)); 285 return (t->sec.x * 1000) + (t->nano / 1000000);
628} 286}
629 287
630 288
631/*** stralloc_pend.c ***/
632
633GEN_ALLOC_append(stralloc,char,s,len,a,i,n,x,30,stralloc_readyplus,stralloc_append)
634
635#endif /* stralloc */
636
637/*** iopause.c ***/
638
639void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp) 289void iopause(iopause_fd *x,unsigned len,struct taia *deadline,struct taia *stamp)
640{ 290{
641 int millisecs; 291 int millisecs;
@@ -711,130 +361,6 @@ int open_write(const char *fn)
711} 361}
712 362
713 363
714/*** openreadclose.c ***/
715#if 0
716int openreadclose(const char *fn,stralloc *sa,unsigned bufsize)
717{
718 int fd;
719 fd = open_read(fn);
720 if (fd == -1) {
721 if (errno == ENOENT) return 0;
722 return -1;
723 }
724 if (readclose(fd,sa,bufsize) == -1) return -1;
725 return 1;
726}
727#endif
728
729
730/*** pathexec_env.c ***/
731#if 0
732static stralloc plus;
733static stralloc tmp;
734
735int pathexec_env(const char *s,const char *t)
736{
737 if (!s) return 1;
738 if (!stralloc_copys(&tmp,s)) return 0;
739 if (t) {
740 if (!stralloc_cats(&tmp,"=")) return 0;
741 if (!stralloc_cats(&tmp,t)) return 0;
742 }
743 if (!stralloc_0(&tmp)) return 0;
744 return stralloc_cat(&plus,&tmp);
745}
746
747void pathexec(char **argv)
748{
749 char **e;
750 unsigned elen;
751 unsigned i;
752 unsigned j;
753 unsigned split;
754 unsigned t;
755
756 if (!stralloc_cats(&plus,"")) return;
757
758 elen = 0;
759 for (i = 0;environ[i];++i)
760 ++elen;
761 for (i = 0;i < plus.len;++i)
762 if (!plus.s[i])
763 ++elen;
764
765 e = malloc((elen + 1) * sizeof(char *));
766 if (!e) return;
767
768 elen = 0;
769 for (i = 0;environ[i];++i)
770 e[elen++] = environ[i];
771
772 j = 0;
773 for (i = 0;i < plus.len;++i)
774 if (!plus.s[i]) {
775 split = str_chr(plus.s + j,'=');
776 for (t = 0;t < elen;++t)
777 if (memcmp(plus.s + j,e[t],split) == 0)
778 if (e[t][split] == '=') {
779 --elen;
780 e[t] = e[elen];
781 break;
782 }
783 if (plus.s[j + split])
784 e[elen++] = plus.s + j;
785 j = i + 1;
786 }
787 e[elen] = 0;
788
789 pathexec_run(*argv,argv,e);
790 free(e);
791}
792#endif
793
794/*** pathexec_run.c ***/
795#if 0
796static stralloc tmp;
797
798void pathexec_run(const char *file,char *const *argv,char *const *envp)
799{
800 const char *path;
801 unsigned split;
802 int savederrno;
803
804 if (file[str_chr(file,'/')]) {
805 execve(file,argv,envp);
806 return;
807 }
808
809 path = getenv("PATH");
810 if (!path) path = "/bin:/usr/bin";
811
812 savederrno = 0;
813 for (;;) {
814 split = str_chr(path,':');
815 if (!stralloc_copyb(&tmp,path,split)) return;
816 if (!split)
817 if (!stralloc_cats(&tmp,".")) return;
818 if (!stralloc_cats(&tmp,"/")) return;
819 if (!stralloc_cats(&tmp,file)) return;
820 if (!stralloc_0(&tmp)) return;
821
822 execve(tmp.s,argv,envp);
823 if (errno != ENOENT) {
824 savederrno = errno;
825 if ((errno != EACCES) && (errno != EPERM) && (errno != EISDIR)) return;
826 }
827
828 if (!path[split]) {
829 if (savederrno) errno = savederrno;
830 return;
831 }
832 path += split;
833 path += 1;
834 }
835}
836#endif
837
838/*** pmatch.c ***/ 364/*** pmatch.c ***/
839 365
840unsigned pmatch(const char *p, const char *s, unsigned len) { 366unsigned pmatch(const char *p, const char *s, unsigned len) {
@@ -878,62 +404,6 @@ unsigned pmatch(const char *p, const char *s, unsigned len) {
878} 404}
879 405
880 406
881#if 0
882/*** prot.c ***/
883
884int prot_gid(int gid)
885{
886 gid_t x = gid;
887 if (setgroups(1,&x) == -1) return -1;
888 return setgid(gid); /* _should_ be redundant, but on some systems it isn't */
889}
890
891int prot_uid(int uid)
892{
893 return setuid(uid);
894}
895#endif
896
897
898/*** readclose.c ***/
899#if 0
900int readclose_append(int fd,stralloc *sa,unsigned bufsize)
901{
902 int r;
903 for (;;) {
904 if (!stralloc_readyplus(sa,bufsize)) { close(fd); return -1; }
905 r = read(fd,sa->s + sa->len,bufsize);
906 if (r == -1) if (errno == EINTR) continue;
907 if (r <= 0) { close(fd); return r; }
908 sa->len += r;
909 }
910}
911
912int readclose(int fd,stralloc *sa,unsigned bufsize)
913{
914 if (!stralloc_copys(sa,"")) { close(fd); return -1; }
915 return readclose_append(fd,sa,bufsize);
916}
917#endif
918
919#if 0
920/*** scan_ulong.c ***/
921
922unsigned scan_ulong(const char *s,unsigned long *u)
923{
924 unsigned pos = 0;
925 unsigned long result = 0;
926 unsigned long c;
927 while ((c = (unsigned long) (unsigned char) (s[pos] - '0')) < 10) {
928 result = result * 10 + c;
929 ++pos;
930 }
931 *u = result;
932 return pos;
933}
934#endif
935
936
937#ifdef UNUSED 407#ifdef UNUSED
938/*** seek_set.c ***/ 408/*** seek_set.c ***/
939 409
@@ -944,20 +414,6 @@ int seek_set(int fd,seek_pos pos)
944#endif 414#endif
945 415
946 416
947/*** sig.c ***/
948
949//int sig_alarm = SIGALRM;
950//int sig_child = SIGCHLD;
951//int sig_cont = SIGCONT;
952//int sig_hangup = SIGHUP;
953//int sig_int = SIGINT;
954//int sig_pipe = SIGPIPE;
955//int sig_term = SIGTERM;
956
957//void (*sig_defaulthandler)(int) = SIG_DFL;
958//void (*sig_ignorehandler)(int) = SIG_IGN;
959
960
961/*** sig_block.c ***/ 417/*** sig_block.c ***/
962 418
963void sig_block(int sig) 419void sig_block(int sig)