ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/root-tail/root-tail.c
Revision: 1.7
Committed: Thu Mar 25 21:07:26 2004 UTC (22 years, 5 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.6: +91 -17 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 pcg 1.1 /*
2     * Copyright 2001 by Marco d'Itri <md@linux.it>
3     *
4     * Original version by Mike Baker, then maintained by pcg@goof.com.
5     *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
19     */
20    
21     #include "config.h"
22     #include <stdlib.h>
23     #include <stdio.h>
24     #include <unistd.h>
25     #include <string.h>
26     #include <signal.h>
27     #include <time.h>
28     #include <fcntl.h>
29     #include <errno.h>
30     #include <sys/time.h>
31     #include <sys/stat.h>
32     #include <sys/types.h>
33 pcg 1.7 #include <locale.h>
34 pcg 1.1 #if HAS_REGEX
35     #include <regex.h>
36     #endif
37     #include <X11/Xlib.h>
38 pcg 1.7 #include <X11/Xatom.h>
39 pcg 1.1 #include <X11/Xutil.h>
40    
41     /* data structures */
42     struct logfile_entry {
43     char *fname; /* name of file */
44     char *desc; /* alternative description */
45     FILE *fp; /* FILE struct associated with file */
46     ino_t inode; /* inode of the file opened */
47     off_t last_size; /* file size at the last check */
48     unsigned long color; /* color to be used for printing */
49     struct logfile_entry *next;
50     };
51    
52     struct linematrix {
53     char *line;
54     unsigned long color;
55     };
56    
57    
58     /* global variables */
59 pcg 1.6 int width = STD_WIDTH, listlen = STD_HEIGHT;
60 pcg 1.1 int win_x = LOC_X, win_y = LOC_Y;
61     int w = -1, h = -1, font_width, font_height, font_descent;
62     int do_reopen;
63     struct timeval interval = { 2, 400000 }; /* see Knuth */
64 pcg 1.7 XFontSet fontset;
65 pcg 1.1
66     /* command line options */
67 pcg 1.5 int opt_noinitial, opt_shade, opt_frame, opt_reverse, opt_nofilename,
68 pcg 1.1 geom_mask, reload = 3600;
69     const char *command = NULL,
70     *fontname = USE_FONT, *dispname = NULL, *def_color = DEF_COLOR;
71    
72     struct logfile_entry *loglist = NULL, *loglist_tail = NULL;
73    
74     Display *disp;
75     Window root;
76     GC WinGC;
77    
78     #if HAS_REGEX
79     struct re_list {
80     regex_t from;
81     const char *to;
82     struct re_list *next;
83     };
84     struct re_list *re_head, *re_tail;
85     #endif
86    
87    
88     /* prototypes */
89     void list_files(int);
90     void force_reopen(int);
91     void force_refresh(int);
92     void blank_window(int);
93    
94     void InitWindow(void);
95     unsigned long GetColor(const char *);
96     void redraw(void);
97     void refresh(struct linematrix *, int, int);
98    
99     void transform_line(char *s);
100     int lineinput(char *, int, FILE *);
101     void reopen(void);
102     void check_open_files(void);
103     FILE *openlog(struct logfile_entry *);
104     void main_loop(void);
105    
106     void display_version(void);
107     void display_help(char *);
108     void install_signal(int, void (*)(int));
109     void *xstrdup(const char *);
110     void *xmalloc(size_t);
111     int daemonize(void);
112    
113     /* signal handlers */
114     void list_files(int dummy)
115     {
116     struct logfile_entry *e;
117    
118     fprintf(stderr, "Files opened:\n");
119     for (e = loglist; e; e = e->next)
120     fprintf(stderr, "\t%s (%s)\n", e->fname, e->desc);
121     }
122    
123     void force_reopen(int dummy)
124     {
125     do_reopen = 1;
126     }
127    
128     void force_refresh(int dummy)
129     {
130 pcg 1.7 XClearArea(disp, root, win_x, win_y, w, h + font_descent + 2, False);
131 pcg 1.1 redraw();
132     }
133    
134     void blank_window(int dummy)
135     {
136 pcg 1.7 XClearArea(disp, root, win_x, win_y, w, h + font_descent + 2, False);
137 pcg 1.1 XFlush(disp);
138     exit(0);
139     }
140    
141     /* X related functions */
142     unsigned long GetColor(const char *ColorName)
143     {
144     XColor Color;
145     XWindowAttributes Attributes;
146    
147     XGetWindowAttributes(disp, root, &Attributes);
148     Color.pixel = 0;
149     if (!XParseColor(disp, Attributes.colormap, ColorName, &Color))
150     fprintf(stderr, "can't parse %s\n", ColorName);
151     else if (!XAllocColor(disp, Attributes.colormap, &Color))
152     fprintf(stderr, "can't allocate %s\n", ColorName);
153     return Color.pixel;
154     }
155    
156 pcg 1.7 static Window root_window (Display *display, int screen_number)
157     {
158     Atom __SWM_VROOT = XInternAtom (display, "__SWM_VROOT", False);
159     Window real_root_window = RootWindow (display, screen_number);
160    
161     if (__SWM_VROOT != None)
162     {
163     Window unused, *windows;
164     unsigned int count;
165    
166     if (XQueryTree (display, real_root_window, &unused, &unused, &windows,
167     &count))
168     {
169     int i;
170    
171     for (i = 0; i < count; i++)
172     {
173     Atom type;
174     int format;
175     unsigned long nitems, bytes_after_return;
176     unsigned char *virtual_root_window;
177    
178     if (XGetWindowProperty (display, windows[i], __SWM_VROOT,
179     0, 1, False, XA_WINDOW, &type, &format,
180     &nitems, &bytes_after_return,
181     &virtual_root_window)
182     == Success)
183     {
184     if (type != None)
185     {
186     if (type == XA_WINDOW)
187     {
188     XFree (windows);
189     return (Window)virtual_root_window;
190     }
191     else
192     fprintf (stderr, "__SWM_VROOT property type mismatch");
193     }
194     }
195     else
196     fprintf (stderr,
197     "failed to get __SWM_VROOT property on window 0x%lx",
198     windows[i]);
199     }
200    
201     if (count)
202     XFree (windows);
203     }
204     else
205     fprintf (stderr, "Can't query tree on root window 0x%lx",
206     real_root_window);
207     }
208     else
209     /* This shouldn't happen. The Xlib documentation is wrong BTW. */
210     fprintf (stderr, "Can't intern atom __SWM_VROOT");
211    
212     return real_root_window;
213     }
214    
215 pcg 1.1 void InitWindow(void)
216     {
217     XGCValues gcv;
218     Font font;
219     unsigned long gcm;
220     XFontStruct *info;
221     int screen, ScreenWidth, ScreenHeight;
222    
223     if (!(disp = XOpenDisplay(dispname))) {
224     fprintf(stderr, "Can't open display %s.\n", dispname);
225     exit(1);
226     }
227     screen = DefaultScreen(disp);
228     ScreenHeight = DisplayHeight(disp, screen);
229     ScreenWidth = DisplayWidth(disp, screen);
230 pcg 1.7 root = root_window (disp, screen);
231 pcg 1.1 gcm = GCBackground;
232     gcv.graphics_exposures = True;
233     WinGC = XCreateGC(disp, root, gcm, &gcv);
234     XMapWindow(disp, root);
235     XSetForeground(disp, WinGC, GetColor(DEF_COLOR));
236    
237     font = XLoadFont(disp, fontname);
238     XSetFont(disp, WinGC, font);
239     info = XQueryFont(disp, font);
240     font_width = info->max_bounds.width;
241     font_descent = info->max_bounds.descent;
242     font_height = info->max_bounds.ascent + font_descent;
243    
244     w = width * font_width;
245     h = listlen * font_height;
246     if (geom_mask & XNegative)
247     win_x = win_x + ScreenWidth - w;
248     if (geom_mask & YNegative)
249     win_y = win_y + ScreenHeight - h;
250    
251     XSelectInput(disp, root, ExposureMask|FocusChangeMask);
252     }
253    
254 pcg 1.7 char *
255 pcg 1.2 detabificate (char *s)
256     {
257     char * out;
258     int i, j;
259    
260     out = malloc (8 * strlen (s) + 1);
261    
262 pcg 1.7 for(i = 0, j = 0; s[i]; i++)
263 pcg 1.2 {
264 pcg 1.7 if (s[i] == '\t')
265     do
266 pcg 1.2 out[j++] = ' ';
267 pcg 1.7 while (j % 8);
268 pcg 1.2 else
269     out[j++] = s[i];
270     }
271 pcg 1.7
272 pcg 1.2 out[j] = '\0';
273     return out;
274     }
275    
276 pcg 1.1 /*
277     * redraw does a complete redraw, rather than an update (i.e. the area
278     * gets cleared first)
279     * the rest is handled by regular refresh()'es
280     */
281     void redraw(void)
282     {
283     XClearArea(disp, root, win_x, win_y, w, h + font_descent + 2, True);
284     }
285    
286     /* Just redraw everything without clearing (i.e. after an EXPOSE event) */
287     void refresh(struct linematrix *lines, int miny, int maxy)
288     {
289     int lin;
290     int offset = (listlen + 1) * font_height;
291     unsigned long black_color = GetColor("black");
292    
293     miny -= win_y + font_height;
294     maxy -= win_y - font_height;
295    
296 pcg 1.2 for (lin = listlen; lin--;)
297     {
298     char *temp;
299 pcg 1.7
300 pcg 1.2 offset -= font_height;
301 pcg 1.7
302 pcg 1.2 if (offset < miny || offset > maxy)
303     continue;
304 jebusbfb 1.4
305     #define LIN ((opt_reverse)?(listlen-lin-1):(lin))
306     temp = detabificate (lines[LIN].line);
307 pcg 1.7
308 pcg 1.2 if (opt_shade)
309     {
310     XSetForeground (disp, WinGC, black_color);
311 pcg 1.7 #if 0
312     XmbDrawString (disp, root, fontset, WinGC, win_x + 2, win_y + offset + 2,
313     temp, strlen (temp));
314     #endif
315 pcg 1.2 XDrawString (disp, root, WinGC, win_x + 2, win_y + offset + 2,
316     temp, strlen (temp));
317     }
318 pcg 1.7
319 jebusbfb 1.4 XSetForeground (disp, WinGC, lines[LIN].color);
320 pcg 1.7 #if 0
321     XmbDrawString (disp, root, fontset, WinGC, win_x, win_y + offset,
322     temp, strlen (temp));
323     #endif
324 pcg 1.2 XDrawString (disp, root, WinGC, win_x, win_y + offset,
325 pcg 1.7 temp, strlen (temp));
326 pcg 1.2 free (temp);
327 pcg 1.1 }
328    
329     if (opt_frame) {
330     int bot_y = win_y + h + font_descent + 2;
331    
332     XDrawLine(disp, root, WinGC, win_x, win_y, win_x + w, win_y);
333     XDrawLine(disp, root, WinGC, win_x + w, win_y, win_x + w, bot_y);
334     XDrawLine(disp, root, WinGC, win_x + w, bot_y, win_x, bot_y);
335     XDrawLine(disp, root, WinGC, win_x, bot_y, win_x, win_y);
336     }
337     }
338    
339     #if HAS_REGEX
340     void transform_line(char *s)
341     {
342     #ifdef I_AM_Md
343     int i;
344     if (1) {
345     for (i = 16; s[i]; i++)
346     s[i] = s[i + 11];
347     }
348     s[i + 1] = '\0';
349     #endif
350    
351     if (transformre) {
352     int i;
353     regmatch_t matched[16];
354    
355     i = regexec(&transformre, string, 16, matched, 0);
356     if (i == 0) { /* matched */
357     }
358     }
359     }
360     #endif
361    
362    
363     /*
364     * This routine should read 'width' characters and not more. However,
365 pcg 1.7 * we really want to read width + 1 characters if the last char is a '\n',
366 pcg 1.1 * which we should remove afterwards. So, read width+1 chars and ungetc
367     * the last character if it's not a newline. This means 'string' must be
368     * width + 2 wide!
369     */
370     int lineinput(char *string, int slen, FILE *f)
371     {
372     int len;
373    
374     do {
375     if (fgets(string, slen, f) == NULL) /* EOF or Error */
376     return 0;
377    
378     len = strlen(string);
379     } while (len == 0);
380    
381     if (string[len - 1] == '\n')
382     string[len - 1] = '\0'; /* erase newline */
383     else if (len >= slen - 1) {
384     ungetc(string[len - 1], f);
385     string[len - 1] = '\0';
386     }
387    
388     #if HAS_REGEX
389     transform_line(string);
390     #endif
391     return len;
392     }
393    
394     /* input: reads file->fname
395     * output: fills file->fp, file->inode
396     * returns file->fp
397     * in case of error, file->fp is NULL
398     */
399     FILE *openlog(struct logfile_entry *file)
400     {
401     struct stat stats;
402    
403     if ((file->fp = fopen(file->fname, "r")) == NULL) {
404     file->fp = NULL;
405     return NULL;
406     }
407    
408     fstat(fileno(file->fp), &stats);
409     if (S_ISFIFO(stats.st_mode)) {
410     if (fcntl(fileno(file->fp), F_SETFL, O_NONBLOCK) < 0)
411     perror("fcntl"), exit(1);
412     file->inode = 0;
413     } else
414     file->inode = stats.st_ino;
415    
416 pcg 1.2 if (opt_noinitial)
417     fseek (file->fp, 0, SEEK_END);
418     else if (stats.st_size > (listlen + 1) * width)
419     {
420     char dummy[255];
421    
422     fseek(file->fp, -((listlen + 2) * width), SEEK_END);
423     /* the pointer might point halfway some line. Let's
424     be nice and skip this damaged line */
425     lineinput(dummy, sizeof(dummy), file->fp);
426     }
427 pcg 1.1
428     file->last_size = stats.st_size;
429     return file->fp;
430     }
431    
432     void reopen(void)
433     {
434     struct logfile_entry *e;
435    
436     for (e = loglist; e; e = e->next) {
437     if (!e->inode)
438     continue; /* skip stdin */
439    
440     if (e->fp)
441     fclose(e->fp);
442     /* if fp is NULL we will try again later */
443     openlog(e);
444     }
445    
446     do_reopen = 0;
447     }
448    
449     void check_open_files(void)
450     {
451     struct logfile_entry *e;
452     struct stat stats;
453    
454     for (e = loglist; e; e = e->next) {
455     if (!e->inode)
456     continue; /* skip stdin */
457    
458     if (stat(e->fname, &stats) < 0) { /* file missing? */
459     sleep(1);
460     if (e->fp)
461     fclose(e->fp);
462     if (openlog(e) == NULL)
463     break;
464     }
465    
466     if (stats.st_ino != e->inode) { /* file renamed? */
467     if (e->fp)
468     fclose(e->fp);
469     if (openlog(e) == NULL)
470     break;
471     }
472    
473     if (stats.st_size < e->last_size) { /* file truncated? */
474     fseek(e->fp, 0, SEEK_SET);
475     e->last_size = stats.st_size;
476     }
477     }
478     }
479    
480     #define SCROLL_UP(lines, listlen) \
481     { \
482     int cur_line; \
483     for (cur_line = 0; cur_line < (listlen - 1); cur_line++) { \
484     strcpy(lines[cur_line].line, lines[cur_line + 1].line); \
485     lines[cur_line].color = lines[cur_line + 1].color; \
486     } \
487     }
488    
489     void main_loop(void)
490     {
491     struct linematrix *lines = xmalloc(sizeof(struct linematrix) * listlen);
492     int lin, miny, maxy, buflen;
493     char *buf;
494     time_t lastreload;
495     Region region = XCreateRegion();
496     XEvent xev;
497    
498     maxy = 0;
499     miny = win_y + h;
500     buflen = width + 2;
501     buf = xmalloc(buflen);
502     lastreload = time(NULL);
503    
504     /* Initialize linematrix */
505     for (lin = 0; lin < listlen; lin++) {
506     lines[lin].line = xmalloc(buflen);
507     strcpy(lines[lin].line, "~");
508     lines[lin].color = GetColor(def_color);
509     }
510    
511     if (!opt_noinitial)
512 pcg 1.7 {
513 pcg 1.1 while (lineinput(buf, buflen, loglist->fp) != 0) {
514     SCROLL_UP(lines, listlen);
515     /* print the next line */
516     strcpy(lines[listlen - 1].line, buf);
517     }
518    
519 pcg 1.7 redraw ();
520     }
521    
522 pcg 1.1 for (;;) {
523     int need_update = 0;
524     struct logfile_entry *current;
525     static struct logfile_entry *lastprinted = NULL;
526    
527     /* read logs */
528     for (current = loglist; current; current = current->next) {
529     if (!current->fp)
530     continue; /* skip missing files */
531    
532     clearerr(current->fp);
533    
534     while (lineinput(buf, buflen, current->fp) != 0) {
535     /* print filename if any, and if last line was from
536     different file */
537     if (!opt_nofilename &&
538     !(lastprinted && lastprinted == current) &&
539     current->desc[0]) {
540     SCROLL_UP(lines, listlen);
541     sprintf(lines[listlen - 1].line, "[%s]", current->desc);
542     lines[listlen - 1].color = current->color;
543     }
544    
545     SCROLL_UP(lines, listlen);
546     strcpy(lines[listlen - 1].line, buf);
547     lines[listlen - 1].color = current->color;
548    
549     lastprinted = current;
550     need_update = 1;
551     }
552     }
553    
554     if (need_update)
555     redraw();
556     else {
557     XFlush(disp);
558     if (!XPending(disp)) {
559     fd_set fdr;
560     struct timeval to = interval;
561    
562     FD_ZERO(&fdr);
563     FD_SET(ConnectionNumber(disp), &fdr);
564     select(ConnectionNumber(disp) + 1, &fdr, 0, 0, &to);
565     }
566     }
567    
568     check_open_files();
569    
570     if (do_reopen)
571     reopen();
572    
573     /* we ignore possible errors due to window resizing &c */
574     while (XPending(disp)) {
575     XNextEvent(disp, &xev);
576     switch (xev.type) {
577     case Expose:
578     {
579     XRectangle r;
580    
581     r.x = xev.xexpose.x;
582     r.y = xev.xexpose.y;
583     r.width = xev.xexpose.width;
584     r.height = xev.xexpose.height;
585     XUnionRectWithRegion(&r, region, region);
586     if (miny > r.y)
587     miny = r.y;
588     if (maxy < r.y + r.height)
589     maxy = r.y + r.height;
590     }
591     break;
592     default:
593     #ifdef DEBUGMODE
594     fprintf(stderr, "PANIC! Unknown event %d\n", xev.type);
595     #endif
596     break;
597     }
598     }
599    
600     /* reload if requested */
601     if (reload && lastreload + reload < time(NULL)) {
602     if (command)
603     system(command);
604    
605     reopen();
606     lastreload = time(NULL);
607     }
608    
609     if (!XEmptyRegion(region)) {
610     XSetRegion(disp, WinGC, region);
611     refresh(lines, miny, maxy);
612     XDestroyRegion(region);
613     region = XCreateRegion();
614     maxy = 0;
615     miny = win_y + h;
616     }
617     }
618     }
619    
620    
621     int main(int argc, char *argv[])
622     {
623     int i;
624     int opt_daemonize = 0;
625     #if HAS_REGEX
626     char *transform = NULL;
627     #endif
628    
629 pcg 1.7 setlocale (LC_CTYPE, ""); /* try to initialize the locale. */
630    
631 pcg 1.1 /* window needs to be initialized before colorlookups can be done */
632     /* just a dummy to get the color lookups right */
633     geom_mask = NoValue;
634     InitWindow();
635    
636     for (i = 1; i < argc; i++) {
637     if (argv[i][0] == '-' && argv[i][1] != '\0' && argv[i][1] != ',') {
638     if (!strcmp(argv[i], "--?") ||
639     !strcmp(argv[i], "--help") || !strcmp(argv[i], "-h"))
640     display_help(argv[0]);
641     else if (!strcmp(argv[i], "-V"))
642     display_version();
643     else if (!strcmp(argv[i], "-g") || !strcmp(argv[i], "-geometry"))
644     geom_mask = XParseGeometry(argv[++i],
645     &win_x, &win_y, &width, &listlen);
646     else if (!strcmp(argv[i], "-display"))
647     dispname = argv[++i];
648     else if (!strcmp(argv[i], "-font") || !strcmp(argv[i], "-fn"))
649     fontname = argv[++i];
650     #if HAS_REGEX
651     else if (!strcmp(argv[i], "-t"))
652     transform = argv[++i];
653     #endif
654     else if (!strcmp(argv[i], "-fork") || !strcmp(argv[i], "-f"))
655     opt_daemonize = 1;
656     else if (!strcmp(argv[i], "-reload")) {
657     reload = atoi(argv[++i]);
658     command = argv[++i];
659     }
660     else if (!strcmp(argv[i], "-shade"))
661     opt_shade = 1;
662     else if (!strcmp(argv[i], "-frame"))
663     opt_frame = 1;
664     else if (!strcmp(argv[i], "-no-filename"))
665     opt_nofilename = 1;
666     else if (!strcmp(argv[i], "-reverse"))
667     opt_reverse = 1;
668     else if (!strcmp(argv[i], "-color"))
669     def_color = argv[++i];
670     else if (!strcmp(argv[i], "-noinitial"))
671     opt_noinitial = 1;
672     else if (!strcmp(argv[i], "-interval") || !strcmp(argv[i], "-i")) {
673     double iv = atof(argv[++i]);
674    
675     interval.tv_sec = (int) iv;
676     interval.tv_usec = (iv - interval.tv_sec) * 1e6;
677     } else {
678     fprintf(stderr, "Unknown option '%s'.\n"
679     "Try --help for more information.\n", argv[i]);
680     exit(1);
681     }
682     } else { /* it must be a filename */
683     struct logfile_entry *e;
684     const char *fname, *desc, *fcolor = def_color;
685     char *p;
686    
687     /* this is not foolproof yet (',' in filenames are not allowed) */
688     fname = desc = argv[i];
689     if ((p = strchr(argv[i], ','))) {
690     *p = '\0';
691     fcolor = p + 1;
692    
693     if ((p = strchr(fcolor, ','))) {
694     *p = '\0';
695     desc = p + 1;
696     }
697     }
698    
699     e = xmalloc(sizeof(struct logfile_entry));
700     if (argv[i][0] == '-' && argv[i][1] == '\0') {
701     if ((e->fp = fdopen(0, "r")) == NULL)
702     perror("fdopen"), exit(1);
703     if (fcntl(0, F_SETFL, O_NONBLOCK) < 0)
704     perror("fcntl"), exit(1);
705     e->fname = NULL;
706     e->inode = 0;
707     e->desc = xstrdup("stdin");
708     } else {
709     int l;
710    
711     e->fname = xstrdup(fname);
712     if (openlog(e) == NULL)
713     perror(fname), exit(1);
714    
715     l = strlen(desc);
716     if (l > width - 2) /* must account for [ ] */
717     l = width - 2;
718     e->desc = xmalloc(l + 1);
719     memcpy(e->desc, desc, l);
720     *(e->desc + l) = '\0';
721     }
722    
723     e->color = GetColor(fcolor);
724     e->next = NULL;
725    
726     if (!loglist)
727     loglist = e;
728     if (loglist_tail)
729     loglist_tail->next = e;
730     loglist_tail = e;
731     }
732     }
733    
734     if (!loglist) {
735     fprintf(stderr, "You did not specify any files to tail\n"
736     "use %s --help for help\n", argv[0]);
737     exit(1);
738     }
739    
740     #if HAS_REGEX
741     if (transform) {
742     int i;
743    
744     transformre = xmalloc(sizeof(transformre));
745     i = regcomp(&transformre, transform, REG_EXTENDED);
746     if (i != 0) {
747     char buf[512];
748    
749     regerror(i, &transformre, buf, sizeof(buf));
750     fprintf(stderr, "Cannot compile regular expression: %s\n", buf);
751     }
752     }
753     #endif
754    
755     InitWindow();
756    
757     install_signal(SIGINT, blank_window);
758     install_signal(SIGQUIT, blank_window);
759     install_signal(SIGTERM, blank_window);
760     install_signal(SIGHUP, force_reopen);
761     install_signal(SIGUSR1, list_files);
762     install_signal(SIGUSR2, force_refresh);
763    
764     if (opt_daemonize)
765     daemonize();
766    
767     main_loop();
768    
769     exit(1); /* to make gcc -Wall stop complaining */
770     }
771    
772     void install_signal(int sig, void (*handler)(int))
773     {
774     struct sigaction action;
775    
776     action.sa_handler = handler;
777     sigemptyset(&action.sa_mask);
778     action.sa_flags = SA_RESTART;
779     if (sigaction(sig, &action, NULL) < 0)
780     fprintf(stderr, "sigaction(%d): %s\n", sig, strerror(errno)), exit(1);
781     }
782    
783     void *xstrdup(const char *string)
784     {
785     void *p;
786    
787     while ((p = strdup(string)) == NULL) {
788     fprintf(stderr, "Memory exausted.");
789     sleep(10);
790     }
791     return p;
792     }
793    
794     void *xmalloc(size_t size)
795     {
796     void *p;
797    
798     while ((p = malloc(size)) == NULL) {
799     fprintf(stderr, "Memory exausted.");
800     sleep(10);
801     }
802     return p;
803     }
804    
805     void display_help(char *myname)
806     {
807     printf("Usage: %s [options] file1[,color[,desc]] "
808     "[file2[,color[,desc]] ...]\n", myname);
809     printf(" -g | -geometry geometry -g WIDTHxHEIGHT+X+Y\n"
810     " -color color use color $color as default\n"
811     " -reload sec command reload after $sec and run command\n"
812     " by default -- 3 mins\n"
813     " -font FONTSPEC (-fn) font to use\n"
814     " -f | -fork fork into background\n"
815     " -reverse print new lines at the top\n"
816     " -shade add shading to font\n"
817     " -noinitial don't display the last file lines on\n"
818     " startup\n"
819     " -i | -interval seconds interval between checks (fractional\n"
820     " values o.k.). Default 3\n"
821     " -V display version information and exit\n"
822     "\n");
823     printf("Example:\n%s -g 80x25+100+50 -font fixed /var/log/messages,green "
824     "/var/log/secure,red,'ALERT'\n", myname);
825     exit(0);
826     }
827    
828     void display_version(void) {
829     printf("root-tail version " VERSION "\n");
830     exit(0);
831     }
832    
833     int daemonize(void) {
834     switch (fork()) {
835     case -1:
836     return -1;
837     case 0:
838     break;
839     default:
840     _exit(0);
841     }
842    
843     if (setsid() == -1)
844     return -1;
845    
846     return 0;
847     }