]> git.neil.brown.name Git - metad.git/blob - sendlist.c
Assorted reformating
[metad.git] / sendlist.c
1
2 #include        "metad.h"
3 #include        "skip.h"
4
5 /*
6  * 3 - listing
7  *  1 - service
8  *  2 - service-2 with watchoutput and pidfile
9  *  3 - process-1
10  *  4 - process-2 with pipefd or it_forked
11  */
12
13 void send_proc(proc_t p)
14 {
15         if (p->it_forked > 0 || p->pipefd >= 0) {
16                 send_byte(4);
17                 send_int(p->pid);
18                 send_int(p->it_forked);
19                 send_int(p->pipefd);
20         } else {
21                 send_byte(3);           /* process */
22                 send_int(p->pid);
23         }
24         send_int(p->start_time);
25         send_int(p->hold_time);
26         send_int(p->exit_time);
27         send_int(p->status);
28 }
29
30 void send_service(service_t sv)
31 {
32         int i;
33         proc_t *pp;
34         if (sv->watch_output || sv->pidfile)
35                 send_byte(2);
36         else
37                 send_byte(1); /* service */
38         send_str(sv->service);
39         send_int(sv->max_proc);
40         send_str(sv->home_dir);
41         send_str(sv->username);
42         send_str(sv->crash_prog);
43         if (sv->watch_output || sv->pidfile) {
44                 send_int(sv->watch_output);
45                 send_str(sv->pidfile);
46         }
47         send_int(sv->start_cnt);
48         send_int(sv->enabled);
49         send_str(sv->program);
50         for (i=0 ; sv->args[i] ; i++)
51                 ;
52         send_int(i);
53         for (i=0 ; sv->args[i] ; i++)
54                 send_str(sv->args[i]);
55         (sv->class->send_class)(sv);
56         for (pp = skip_first(sv->proc_list) ; pp ; pp = skip_next(pp))
57                 send_proc(*pp);
58 }
59
60 void send_int(int i)
61 {
62         send_byte((i>>24)& 0xff);
63         send_byte((i>>16)& 0xff);
64         send_byte((i>> 8)& 0xff);
65         send_byte((i>> 0)& 0xff);
66 }
67
68 void send_str(char *s)
69 {
70         if (s == NULL)
71                 send_int(0);
72         else {
73                 send_int(strlen(s)+1);
74                 while (*s)
75                         send_byte(*s++);
76                 send_byte(0);
77         }
78 }
79
80 static char *sendbuf = NULL;
81 static int sendsize;
82 static int sendptr;
83
84 void send_byte(int b)
85 {
86         if (sendptr >= sendsize) {
87                 sendsize += 100;
88                 sendbuf = (char*)realloc(sendbuf, sendsize);
89         }
90         sendbuf[sendptr++] = b;
91 }
92
93 void init_return()
94 {
95         sendptr = 0;
96         if (sendbuf == NULL) {
97                 sendsize = 200;
98                 sendbuf = malloc(sendsize);
99         }
100 }
101
102 void do_send(void *con)
103 {
104         set_reply(con, sendbuf, sendptr);
105         sendbuf = NULL;
106         sendptr = 0;
107 }
108
109 char *get_return(int *i)
110 {
111         char *c = sendbuf;
112         *i = sendptr;
113         sendbuf = NULL;
114         sendptr = 0;
115         return c;
116 }