root/src/noit_console.c

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

I don't care what you said... And treat D like exit

  • 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   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       noit_console_state_init(ncct);
244     }
245   }
246
247   /* If we still have data to send back to the client, this will take
248    * care of that
249    */
250   if(noit_console_continue_sending(ncct, &newmask) < 0) {
251     if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error;
252     return newmask | EVENTER_EXCEPTION;
253   }
254
255   for(keep_going=1 ; keep_going ; ) {
256     int len, plen;
257     char sbuf[4096];
258     const char *buffer;
259
260     keep_going = 0;
261
262     buffer = el_gets(ncct->el, &plen);
263     if(!el_eagain(ncct->el)) {
264       if(!buffer) {
265         buffer = "exit";
266         plen = 4;
267         nc_write(ncct, "\n", 1);
268       }
269       keep_going++;
270     }
271
272     len = e->opset->read(e->fd, sbuf, sizeof(sbuf)-1, &newmask, e);
273     if(len == 0 || (len < 0 && errno != EAGAIN)) {
274       eventer_remove_fd(e->fd);
275       close(e->fd);
276       return 0;
277     }
278     if(len > 0) {
279       keep_going++;
280       sbuf[len] = '\0';
281       if(ncct->telnet) {
282         noit_console_telnet_telrcv(ncct, sbuf, len);
283         ptyflush(ncct);
284       }
285       else {
286         write(ncct->pty_slave, sbuf, len);
287       }
288     }
289     if(buffer) {
290       char *cmd_buffer;
291       HistEvent ev;
292       cmd_buffer = malloc(plen+1);
293       memcpy(cmd_buffer, buffer, plen);
294       /* chomp */
295       cmd_buffer[plen] = '\0';
296       if(cmd_buffer[plen-1] == '\n') cmd_buffer[plen-1] = '\0';
297       noitL(noit_debug, "IN: '%s'\n", cmd_buffer);
298       history(ncct->hist, &ev, H_ENTER, cmd_buffer);
299       noit_console_dispatch(e, cmd_buffer, ncct);
300       free(cmd_buffer);
301     }
302     if(noit_console_continue_sending(ncct, &newmask) == -1) {
303       if(ncct->wants_shutdown || errno != EAGAIN) goto socket_error;
304       return newmask | EVENTER_EXCEPTION;
305     }
306     if(ncct->wants_shutdown) goto socket_error;
307   }
308   return newmask | EVENTER_EXCEPTION;
309 }
310
Note: See TracBrowser for help on using the browser.