root/src/noit_console.c

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

start the framework for a console state machine

  • 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) {
142     noit_console_state_t *tmp;
143     tmp = ncct->state;
144     ncct->state = tmp->stacked;
145     noit_console_state_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   new_ncct->pty_master = -1;
155   new_ncct->pty_slave = -1;
156   return new_ncct;
157 }
158
159 int
160 noit_console_continue_sending(noit_console_closure_t ncct,
161                               int *mask) {
162   int len;
163   eventer_t e = ncct->e;
164   if(!ncct->outbuf_len) return 0;
165   if(ncct->output_cooker) ncct->output_cooker(ncct);
166   while(ncct->outbuf_len > ncct->outbuf_completed) {
167     len = e->opset->write(e->fd, ncct->outbuf + ncct->outbuf_completed,
168                           ncct->outbuf_len - ncct->outbuf_completed,
169                           mask, e);
170     if(len < 0) {
171       if(errno == EAGAIN) return -1;
172       /* Do something else here? */
173       return -1;
174     }
175     ncct->outbuf_completed += len;
176   }
177   len = ncct->outbuf_len;
178   free(ncct->outbuf);
179   ncct->outbuf = NULL;
180   ncct->outbuf_allocd = ncct->outbuf_len =
181     ncct->outbuf_completed = ncct->outbuf_cooked = 0;
182   return len;
183 }
184
185 void
186 noit_console_init() {
187   el_multi_init();
188   signal(SIGTTOU, SIG_IGN);
189   eventer_name_callback("noit_console", noit_console_handler);
190 }
191
192 void
193 noit_console_dispatch(eventer_t e, const char *buffer,
194                       noit_console_closure_t ncct) {
195   char **cmds;
196   int i, cnt = 32;
197   cmds = alloca(32 * sizeof(*cmds));
198   nc_printf(ncct, "You said: %s\n", buffer);
199   i = noit_tokenize(buffer, cmds, &cnt);
200   if(i>cnt) nc_printf(ncct, "Command length too long.\n");
201   else if(i<0) nc_printf(ncct, "Error at offset: %d\n", 0-i);
202   else noit_console_state_do(ncct, cnt, cmds);
203   for(i=0;i<cnt;i++) {
204     nc_printf(ncct, "[%d] '%s'\n", i, cmds[i]);
205     free(cmds[i]);
206   }
207 }
208
209 int
210 noit_console_handler(eventer_t e, int mask, void *closure,
211                      struct timeval *now) {
212   int newmask = EVENTER_READ | EVENTER_EXCEPTION;
213   int keep_going;
214   noit_console_closure_t ncct = closure;
215
216   if(mask & EVENTER_EXCEPTION || (ncct && ncct->wants_shutdown)) {
217 socket_error:
218     /* Exceptions cause us to simply snip the connection */
219     eventer_remove_fd(e->fd);
220     e->opset->close(e->fd, &newmask, e);
221     if(ncct) noit_console_closure_free(ncct);
222     return 0;
223   }
224
225   if(!ncct) {
226     int on = 1;
227     ncct = closure = e->closure = noit_console_closure_alloc();
228     ncct->e = e;
229     if(openpty(&ncct->pty_master, &ncct->pty_slave, NULL, NULL, NULL) ||
230        ioctl(ncct->pty_master, FIONBIO, &on)) {
231       nc_printf(ncct, "Failed to open pty: %s\n", strerror(errno));
232       ncct->wants_shutdown = 1;
233     }
234     else {
235       HistEvent ev;
236       ncct->hist = history_init();
237       history(ncct->hist, &ev, H_SETSIZE, 500);
238       ncct->el = el_init("noitd", ncct->pty_master, e->fd, e->fd);
239       el_set(ncct->el, EL_EDITOR, "emacs");
240       el_set(ncct->el, EL_HIST, history, ncct->hist);
241       ncct->telnet = noit_console_telnet_alloc(ncct);
242       ncct->output_cooker = nc_telnet_cooker;
243       ncct->state = noit_console_state_initial();
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.