chiark / gitweb /
Merge and end branch-hostside-wip-2008-01-25 PROPERLY; cvs up -j branch-hostside...
[trains.git] / hostside / obc.c
1 /*
2  * daemons
3  * output buffer chains
4  */
5
6 #include <assert.h>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <stdarg.h>
10 #include <string.h>
11
12 #include "daemons.h"
13 #include "../layout/dlist.h"
14
15 struct OutBuffer {
16   OutBuffer *back, *next;
17   char *m;
18   int l;
19 };
20
21 int obc_tryflush(OutBufferChain *ch) {
22   OutBuffer *ob;
23   int r;
24   
25   for (;;) {
26     ob= ch->obs.head;
27     if (!ob) {
28       events->cancel_fd(events, ch->fd, OOP_WRITE);
29       return 0;
30     }
31     if (ch->done_of_head == ob->l) {
32       LIST_UNLINK(ch->obs, ob);
33       free(ob->m);
34       free(ob);
35       ch->done_of_head= 0;
36       continue;
37     }
38     r= write(ch->fd, ob->m + ch->done_of_head, ob->l - ch->done_of_head);
39     if (r==-1) {
40       if (errno==EINTR) continue;
41       if (errno==EWOULDBLOCK) return errno;
42       ch->error(ch,"write",strerror(errno));
43       return errno;
44     }
45     assert(r>=0);
46     ch->done_of_head += r;
47     ch->total -= r;
48     assert(ch->done_of_head <= ob->l);
49   }
50 }
51
52 static void *writeable(oop_source *evts, int fd,
53                        oop_event evt, void *ch_v) {
54   OutBufferChain *ch= ch_v;
55   assert(fd == ch->fd);
56   assert(evt == OOP_WRITE);
57   obc_tryflush(ch);
58   return OOP_CONTINUE;
59 }
60
61 static void addlink(OutBufferChain *ch, OutBuffer *ob) {
62   if (!ch->obs.head)
63     events->on_fd(events, ch->fd, OOP_WRITE, writeable, ch);
64   LIST_LINK_TAIL(ch->obs, ob);
65   ch->total += ob->l;
66   obc_tryflush(ch);
67   if (ch->total > ch->limit) {
68     char what[128];
69     snprintf(what,sizeof(what)-1,"`%.*s...'", ob->l,ob->m);
70     what[sizeof(what)-1]= 0;
71     ch->error(ch,"buffer limit exceeded",what);
72   }
73 }
74
75 void obc_init(OutBufferChain *ch) {
76   int r;
77   ch->done_of_head= 0;
78   ch->total= 0;
79   if (!ch->limit) ch->limit= 128*1024;
80   r= oop_fd_nonblock(ch->fd, 1);
81   if (r) diee("nonblock(OutBufferChain->fd,1)");
82   LIST_INIT(ch->obs);
83 }
84
85 void ovprintf(OutBufferChain *ch, const char *fmt, va_list al) {
86   OutBuffer *ob;
87
88   ob= mmalloc(sizeof(*ob));
89   ob->l= vasprintf(&ob->m, fmt, al);  if (ob->l <= 0) diem();
90   addlink(ch, ob);
91 }
92
93 void oprintf(OutBufferChain *ch, const char *msg, ...) {
94   va_list al;
95   va_start(al,msg);
96   ovprintf(ch,msg,al);
97   va_end(al);
98 }
99
100 void voerror(OutBufferChain *ch, const char *fmt, va_list al) {
101   oprintf(ch,"error ");
102   ovprintf(ch,fmt,al);
103   owrite(ch,"\n",1);
104 }
105
106 void owrite(OutBufferChain *ch, const char *data, int l) {
107   OutBuffer *ob;
108   ob= mmalloc(sizeof(*ob));
109   ob->l= l;
110   ob->m= mmalloc(l);
111   memcpy(ob->m, data, l);
112   addlink(ch,ob);
113 }