Changeset 37cf7d8f9e336376dc0ecdb1b80e4776721a950b
- Timestamp:
- 02/06/08 04:47:35 (5 years ago)
- git-parent:
- Files:
-
- src/Makefile.in (modified) (2 diffs)
- src/modules/http.c (modified) (1 diff)
- src/noit_console.c (modified) (4 diffs)
- src/noit_console.h (modified) (2 diffs)
- src/noit_tokenizer.c (added)
- src/noit_tokenizer.h (added)
- src/noit_tokenizer.re (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/Makefile.in
r57a3273 r37cf7d8 26 26 27 27 OBJS=noitd.o noit_listener.o noit_console.o noit_check.o \ 28 noit_module.o noit_conf.o 28 noit_module.o noit_conf.o noit_tokenizer.o 29 29 30 30 all: noitd … … 49 49 $(CC) $(CPPFLAGS) $(CFLAGS) -c $< 50 50 51 noit_tokenizer.c: noit_tokenizer.re 52 re2c -o $@ noit_tokenizer.re 53 51 54 clean-subdirs: 52 55 for dir in $(SUBS) ; do \ src/modules/http.c
r5229ef1 r37cf7d8 257 257 services = xmlXPathNodeSetGetLength(pobj->nodesetval); 258 258 } else { 259 noitL(nlerr, "Error in resmon doc: %s\n", ci->body.b); 259 if(ci->body.l) 260 noitL(nlerr, "Error in resmon doc: %s\n", ci->body.b); 260 261 } 261 262 src/noit_console.c
r57a3273 r37cf7d8 8 8 #include <stdio.h> 9 9 #include <unistd.h> 10 #include <errno.h> 10 11 11 12 #include "eventer/eventer.h" … … 13 14 #include "noit_listener.h" 14 15 #include "noit_console.h" 16 #include "noit_tokenizer.h" 17 18 struct __noit_console_closure { 19 char *outbuf; 20 int outbuf_allocd; 21 int outbuf_len; 22 int outbuf_completed; 23 }; 24 25 int 26 nc_printf(noit_console_closure_t ncct, const char *fmt, ...) { 27 int len; 28 va_list arg; 29 va_start(arg, fmt); 30 len = nc_vprintf(ncct, fmt, arg); 31 va_end(arg); 32 return len; 33 } 34 int 35 nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg) { 36 #ifdef va_copy 37 va_list copy; 38 #endif 39 int lenwanted; 40 41 if(!ncct->outbuf_allocd) { 42 ncct->outbuf = malloc(4096); 43 if(!ncct->outbuf) return 0; 44 ncct->outbuf_allocd = 4096; 45 } 46 while(1) { 47 char *newbuf; 48 #ifdef va_copy 49 va_copy(copy, arg); 50 lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len, 51 ncct->outbuf_allocd - ncct->outbuf_len, 52 fmt, copy); 53 va_end(copy); 54 #else 55 lenwanted = vsnprintf(ncct->outbuf + ncct->outbuf_len, 56 ncct->outbuf_allocd - ncct->outbuf_len, 57 fmt, arg); 58 #endif 59 if(ncct->outbuf_len + lenwanted < ncct->outbuf_allocd) { 60 /* All went well, things are as we want them. */ 61 ncct->outbuf_len += lenwanted; 62 return lenwanted; 63 } 64 65 /* We need to enlarge the buffer */ 66 lenwanted += ncct->outbuf_len; 67 lenwanted /= 4096; 68 lenwanted += 1; 69 lenwanted *= 4096; 70 newbuf = realloc(ncct->outbuf, lenwanted); 71 if(!newbuf) { 72 return 0; 73 } 74 ncct->outbuf = newbuf; 75 ncct->outbuf_allocd = lenwanted; 76 } 77 return -1; 78 } 79 void 80 noit_console_closure_free(noit_console_closure_t ncct) { 81 if(ncct->outbuf) free(ncct->outbuf); 82 free(ncct); 83 } 84 85 noit_console_closure_t 86 noit_console_closure_alloc() { 87 noit_console_closure_t new_ncct; 88 new_ncct = calloc(1, sizeof(*new_ncct)); 89 return new_ncct; 90 } 91 92 int 93 noit_console_continue_sending(eventer_t e, noit_console_closure_t ncct, 94 int *mask) { 95 int len; 96 if(!ncct->outbuf_len) return 0; 97 while(ncct->outbuf_len > ncct->outbuf_completed) { 98 len = e->opset->write(e->fd, ncct->outbuf + ncct->outbuf_completed, 99 ncct->outbuf_len - ncct->outbuf_completed, 100 mask, e); 101 if(len < 0) { 102 if(errno == EAGAIN) return -1; 103 /* Do something else here? */ 104 return -1; 105 } 106 ncct->outbuf_completed += len; 107 } 108 len = ncct->outbuf_len; 109 free(ncct->outbuf); 110 ncct->outbuf = NULL; 111 ncct->outbuf_allocd = 0; 112 ncct->outbuf_len = ncct->outbuf_completed = 0; 113 return len; 114 } 15 115 16 116 void … … 19 119 } 20 120 121 void 122 noit_console_dispatch(eventer_t e, const char *buffer, 123 noit_console_closure_t ncct) { 124 char *cmds[32]; 125 int i, cnt = 32; 126 nc_printf(ncct, "You said: %s", buffer); 127 i = noit_tokenize(buffer, cmds, &cnt); 128 if(i>cnt) nc_printf(ncct, "Command length too long.\n"); 129 if(i<0) nc_printf(ncct, "Error at offset: %d\n", 0-i); 130 for(i=0;i<cnt;i++) { 131 nc_printf(ncct, "[%d] '%s'\n", i, cmds[i]); 132 free(cmds[i]); 133 } 134 } 135 21 136 int 22 137 noit_console_handler(eventer_t e, int mask, void *closure, 23 138 struct timeval *now) { 24 139 int newmask = EVENTER_READ; 140 noit_console_closure_t ncct = closure; 141 25 142 if(mask & EVENTER_EXCEPTION) { 143 /* Exceptions cause us to simply snip the connection */ 26 144 eventer_remove_fd(e->fd); 27 close(e->fd); 145 e->opset->close(e->fd, &newmask, e); 146 if(ncct) noit_console_closure_free(ncct); 28 147 return 0; 29 148 } 149 150 if(!ncct) ncct = closure = e->closure = noit_console_closure_alloc(); 151 152 /* If we still have data to send back to the client, this will take 153 * care of that 154 */ 155 if(noit_console_continue_sending(e, ncct, &newmask) == -1) 156 return newmask; 157 30 158 if(mask & EVENTER_READ) { 31 159 int len; … … 37 165 return 0; 38 166 } 39 printf("IN: %.*s", len, buffer); 167 buffer[len] = '\0'; 168 printf("IN: %s", buffer); 169 noit_console_dispatch(e, buffer, ncct); 170 if(noit_console_continue_sending(e, ncct, &newmask) == -1) 171 return newmask; 40 172 } 41 173 return newmask | EVENTER_EXCEPTION; src/noit_console.h
rcaa7b86 r37cf7d8 10 10 #include "eventer/eventer.h" 11 11 12 13 struct __noit_console_closure; 14 typedef struct __noit_console_closure * noit_console_closure_t; 15 12 16 API_EXPORT(void) noit_console_init(); 13 17 … … 16 20 struct timeval *now); 17 21 22 23 API_EXPORT(int) 24 nc_printf(noit_console_closure_t ncct, const char *fmt, ...); 25 26 API_EXPORT(int) 27 nc_vprintf(noit_console_closure_t ncct, const char *fmt, va_list arg); 28 29 18 30 #endif
