root/src/noit_console.c

Revision 7192210e4ed02f4546970eb41f6da3a167b4316a, 8.1 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 6 years ago)

delegate hooking into the configuration system to other modules

  • Property mode set to 100644
Line 
1 /*
2  * Copyright (c) 2007, OmniTI Computer Consulting, Inc.
3  * All rights reserved.
4  */
5
6 #include "noit_defines.h"
7
8 #include <stdio.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <sys/ioctl.h>
12 #ifdef HAVE_TERMIOS_H
13 #include <termios.h>
14 #endif
15 #ifdef HAVE_UTIL_H
16 #include <util.h>
17 #endif
18 #include <arpa/telnet.h>
19
20 #include "eventer/eventer.h"
21 #include "utils/noit_log.h"
22 #include "noit_listener.h"
23 #include "noit_console.h"
24 #include "noit_tokenizer.h"
25
26 static void
27 nc_telnet_cooker(noit_console_closure_t ncct) {
28   char *tmpbuf, *p, *n;
29   int r;
30
31   tmpbuf = ncct->outbuf;
32   if(ncct->outbuf_len == 0) return;
33
34   p = ncct->outbuf + ncct->outbuf_completed;
35   r = ncct->outbuf_len - ncct->outbuf_completed;
36   n = memchr(p, '\n', r);
37   /* No '\n'? Nothin' to do */
38   if(!n) {
39     ncct->outbuf_cooked = ncct->outbuf_len;
40     return;
41   }
42
43   /* Forget the outbuf -- it is now tmpbuf */
44   ncct->outbuf = NULL;
45   ncct->outbuf_allocd = 0;
46   ncct->outbuf_len = 0;
47   ncct->outbuf_completed = 0;
48   ncct->outbuf_cooked = 0;
49   do {
50     if(n == tmpbuf || *(n-1) != '\r') {
51       nc_write(ncct, p, n-p);   r -= n-p;
52       nc_write(ncct, "\r", 1);
53       p = n;
54     }
55     n = memchr(p+1, '\n', r-1);
56   } while(n);
57   nc_write(ncct, p, r);
58   ncct->outbuf_cooked = ncct->outbuf_len;
59   free(tmpbuf);
60 }
61 int
62 nc_printf(noit_console_closure_t ncct, const char *fmt, ...) {
63   int len;
64   va_list arg;
65   va_start(arg, fmt);
66   len = nc_vprintf(ncct, fmt, arg);
67   va_end(arg);
68   return len;
69 }
70 int
71 nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg) {
72 #ifdef va_copy
73   va_list copy;
74 #endif
75   int lenwanted;
76  
77   if(!ncct->outbuf_allocd) {
78     ncct->outbuf = malloc(4096);
79     if(!ncct->outbuf) return 0;
80     ncct->outbuf_allocd = 4096;
81   }
82   while(1) {
83     char *newbuf;
84 #ifdef va_copy
85     va_copy(copy, arg);
86     lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len,
87                           ncct->outbuf_allocd - ncct->outbuf_len,
88                           fmt, copy);
89     va_end(copy);
90 #else
91     lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len,
92                           ncct->outbuf_allocd - ncct->outbuf_len,
93                           fmt, arg);
94 #endif
95     if(ncct->outbuf_len + lenwanted < ncct->outbuf_allocd) {
96       /* All went well, things are as we want them. */
97       ncct->outbuf_len += lenwanted;
98       return lenwanted;
99     }
100
101     /* We need to enlarge the buffer */
102     lenwanted += ncct->outbuf_len;
103     lenwanted /= 4096;
104     lenwanted += 1;
105     lenwanted *= 4096;
106     newbuf = realloc(ncct->outbuf, lenwanted);
107     if(!newbuf) {
108       return 0;
109     }
110     ncct->outbuf = newbuf;
111     ncct->outbuf_allocd = lenwanted;
112   }
113   return -1;
114 }
115 int
116 nc_write(noit_console_closure_t ncct, void *buf, int len) {
117   if(!ncct->outbuf_allocd) {
118     ncct->outbuf = malloc(len);
119     if(!ncct->outbuf) return 0;
120     ncct->outbuf_allocd = len;
121   }
122   else if(ncct->outbuf_allocd < ncct->outbuf_len + len) {
123     char *newbuf;
124     newbuf = realloc(ncct->outbuf, ncct->outbuf_len + len);
125     if(!newbuf) return 0;
126     ncct->outbuf = newbuf;
127   }
128   memcpy(ncct->outbuf + ncct->outbuf_len, buf, len);
129   ncct->outbuf_len += len;
130   return len;
131 }
132
133 void
134 noit_console_closure_free(noit_console_closure_t ncct) {
135   if(ncct->el) el_end(ncct->el);
136   if(ncct->hist) history_end(ncct->hist);
137   if(ncct->pty_master >= 0) close(ncct->pty_master);
138   if(ncct->pty_slave >= 0) close(ncct->pty_slave);
139   if(ncct->outbuf) free(ncct->outbuf);
140   if(ncct->telnet) noit_console_telnet_free(ncct->telnet);
141   while(ncct->state_stack) {
142     noit_console_state_stack_t *tmp;
143     tmp = ncct->state_stack;
144     ncct->state_stack = tmp->last;
145     free(tmp);
146   }
147   free(ncct);
148 }
149
150 noit_console_closure_t
151 noit_console_closure_alloc() {
152   noit_console_closure_t new_ncct;
153   new_ncct = calloc(1, sizeof(*new_ncct));
154   noit_console_state_push_state(new_ncct, noit_console_state_initial());
155   new_ncct->pty_master = -1;
156   new_ncct->pty_slave = -1;
157   return new_ncct;
158 }
159
160 int
161 noit_console_continue_sending(noit_console_closure_t ncct,
162                               int *mask) {
163   int len;
164   eventer_t e = ncct->e;
165   if(!ncct->outbuf_len) return 0;
166   if(ncct->output_cooker) ncct->output_cooker(ncct);
167   while(ncct->outbuf_len > ncct->outbuf_completed) {
168     len = e->opset->write(e->fd, ncct->outbuf + ncct->outbuf_completed,
169                           ncct->outbuf_len - ncct->outbuf_completed,
170                           mask, e);
171     if(len < 0) {
172       if(errno == EAGAIN) return -1;
173       /* Do something else here? */
174       return -1;
175     }
176     ncct->outbuf_completed += len;
177   }
178   len = ncct->outbuf_len;
179   free(ncct->outbuf);
180   ncct->outbuf = NULL;
181   ncct->outbuf_allocd = ncct->outbuf_len =
182     ncct->outbuf_completed = ncct->outbuf_cooked = 0;
183   return len;
184 }
185
186 void
187 noit_console_init() {
188   el_multi_init();
189   signal(SIGTTOU, SIG_IGN);
190   eventer_name_callback("noit_console", noit_console_handler);
191 }
192
193 void
194 noit_console_dispatch(eventer_t e, const char *buffer,
195                       noit_console_closure_t ncct) {
196   char **cmds;
197   int i, cnt = 32;
198   cmds = alloca(32 * sizeof(*cmds));
199   nc_printf(ncct, "You said: %s\n", buffer);
200   i = noit_tokenize(buffer, cmds, &cnt);
201   if(i>cnt) nc_printf(ncct, "Command length too long.\n");
202   else if(i<0) nc_printf(ncct, "Error at offset: %d\n", 0-i);
203   else noit_console_state_do(ncct, cnt, cmds);
204   for(i=0;i<cnt;i++) {
205     nc_printf(ncct, "[%d] '%s'\n", i, cmds[i]);
206     free(cmds[i]);
207   }
208 }
209
210 int
211 noit_console_handler(eventer_t e, int mask, void *closure,
212                      struct timeval *now) {
213   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
214   int keep_going;
215   noit_console_closure_t ncct = closure;
216
217   if(mask & EVENTER_EXCEPTION || (ncct && ncct->wants_shutdown)) {
218 socket_error:
219     /* Exceptions cause us to simply snip the connection */
220     eventer_remove_fd(e->fd);
221     e->opset->close(e->fd, &newmask, e);
222     if(ncct) noit_console_closure_free(ncct);
223     return 0;
224   }
225
226   if(!ncct) {
227     int on = 1;
228     ncct = closure = e->closure = noit_console_closure_alloc();
229     ncct->e = e;
230     if(openpty(&ncct->pty_master, &ncct->pty_slave, NULL, NULL, NULL) ||
231        ioctl(ncct->pty_master, FIONBIO, &on)) {
232       nc_printf(ncct, "Failed to open pty: %s\n", strerror(errno));
233       ncct->wants_shutdown = 1;
234     }
235     else {
236       HistEvent ev;
237       ncct->hist = history_init();
238       history(ncct->hist, &ev, H_SETSIZE, 500);
239       ncct->el = el_init("noitd", ncct->pty_master, e->fd, e->fd);
240       el_set(ncct->el, EL_EDITOR, "emacs");
241       el_set(ncct->el, EL_HIST, history, ncct->hist);
242       ncct->telnet = noit_console_telnet_alloc(ncct);
243       ncct->output_cooker = nc_telnet_cooker;
244       noit_console_state_init(ncct);
245     }
246   }
247
248   /* If we still have data to send back to the client, this will take
249    * care of that
250    */
251   if(noit_console_continue_sending(ncct, &newmask) < 0) {
252     if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error;
253     return newmask | EVENTER_EXCEPTION;
254   }
255
256   for(keep_going=1 ; keep_going ; ) {
257     int len, plen;
258     char sbuf[4096];
259     const char *buffer;
260
261     keep_going = 0;
262
263     buffer = el_gets(ncct->el, &plen);
264     if(!el_eagain(ncct->el)) keep_going++;
265
266     len = e->opset->read(e->fd, sbuf, sizeof(sbuf)-1, &newmask, e);
267     if(len == 0 || (len < 0 && errno != EAGAIN)) {
268       eventer_remove_fd(e->fd);
269       close(e->fd);
270       return 0;
271     }
272     if(len > 0) {
273       keep_going++;
274       sbuf[len] = '\0';
275       if(ncct->telnet) {
276         noit_console_telnet_telrcv(ncct, sbuf, len);
277         ptyflush(ncct);
278       }
279       else {
280         write(ncct->pty_slave, sbuf, len);
281       }
282     }
283     if(buffer) {
284       char *cmd_buffer;
285       HistEvent ev;
286       cmd_buffer = malloc(plen+1);
287       memcpy(cmd_buffer, buffer, plen);
288       /* chomp */
289       cmd_buffer[plen] = '\0';
290       if(cmd_buffer[plen-1] == '\n') cmd_buffer[plen-1] = '\0';
291       noitL(noit_debug, "IN: '%s'\n", cmd_buffer);
292       history(ncct->hist, &ev, H_ENTER, cmd_buffer);
293       noit_console_dispatch(e, cmd_buffer, ncct);
294       free(cmd_buffer);
295     }
296     if(noit_console_continue_sending(ncct, &newmask) == -1) {
297       if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error;
298       return newmask | EVENTER_EXCEPTION;
299     }
300     if(ncct->wants_shutdown) goto socket_error;
301   }
302   return newmask | EVENTER_EXCEPTION;
303 }
304
Note: See TracBrowser for help on using the browser.