Damien Miller | d4a8b7e | 1999-10-27 13:42:43 +1000 | [diff] [blame] | 1 | /* |
| 2 | |
| 3 | log-client.c |
| 4 | |
| 5 | Author: Tatu Ylonen <ylo@cs.hut.fi> |
| 6 | |
| 7 | Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland |
| 8 | All rights reserved |
| 9 | |
| 10 | Created: Mon Mar 20 21:13:40 1995 ylo |
| 11 | |
| 12 | Client-side versions of debug(), log(), etc. These print to stderr. |
| 13 | |
| 14 | */ |
| 15 | |
| 16 | #include "includes.h" |
| 17 | RCSID("$Id: log-client.c,v 1.1 1999/10/27 03:42:44 damien Exp $"); |
| 18 | |
| 19 | #include "xmalloc.h" |
| 20 | #include "ssh.h" |
| 21 | |
| 22 | static int log_debug = 0; |
| 23 | static int log_quiet = 0; |
| 24 | |
| 25 | void log_init(char *av0, int on_stderr, int debug, int quiet, |
| 26 | SyslogFacility facility) |
| 27 | { |
| 28 | log_debug = debug; |
| 29 | log_quiet = quiet; |
| 30 | } |
| 31 | |
| 32 | void log(const char *fmt, ...) |
| 33 | { |
| 34 | va_list args; |
| 35 | |
| 36 | if (log_quiet) |
| 37 | return; |
| 38 | va_start(args, fmt); |
| 39 | vfprintf(stderr, fmt, args); |
| 40 | fprintf(stderr, "\r\n"); |
| 41 | va_end(args); |
| 42 | } |
| 43 | |
| 44 | void debug(const char *fmt, ...) |
| 45 | { |
| 46 | va_list args; |
| 47 | if (log_quiet || !log_debug) |
| 48 | return; |
| 49 | va_start(args, fmt); |
| 50 | fprintf(stderr, "debug: "); |
| 51 | vfprintf(stderr, fmt, args); |
| 52 | fprintf(stderr, "\r\n"); |
| 53 | va_end(args); |
| 54 | } |
| 55 | |
| 56 | void error(const char *fmt, ...) |
| 57 | { |
| 58 | va_list args; |
| 59 | if (log_quiet) |
| 60 | return; |
| 61 | va_start(args, fmt); |
| 62 | vfprintf(stderr, fmt, args); |
| 63 | fprintf(stderr, "\r\n"); |
| 64 | va_end(args); |
| 65 | } |
| 66 | |
| 67 | struct fatal_cleanup |
| 68 | { |
| 69 | struct fatal_cleanup *next; |
| 70 | void (*proc)(void *); |
| 71 | void *context; |
| 72 | }; |
| 73 | |
| 74 | static struct fatal_cleanup *fatal_cleanups = NULL; |
| 75 | |
| 76 | /* Registers a cleanup function to be called by fatal() before exiting. */ |
| 77 | |
| 78 | void fatal_add_cleanup(void (*proc)(void *), void *context) |
| 79 | { |
| 80 | struct fatal_cleanup *cu; |
| 81 | |
| 82 | cu = xmalloc(sizeof(*cu)); |
| 83 | cu->proc = proc; |
| 84 | cu->context = context; |
| 85 | cu->next = fatal_cleanups; |
| 86 | fatal_cleanups = cu; |
| 87 | } |
| 88 | |
| 89 | /* Removes a cleanup frunction to be called at fatal(). */ |
| 90 | |
| 91 | void fatal_remove_cleanup(void (*proc)(void *context), void *context) |
| 92 | { |
| 93 | struct fatal_cleanup **cup, *cu; |
| 94 | |
| 95 | for (cup = &fatal_cleanups; *cup; cup = &cu->next) |
| 96 | { |
| 97 | cu = *cup; |
| 98 | if (cu->proc == proc && cu->context == context) |
| 99 | { |
| 100 | *cup = cu->next; |
| 101 | xfree(cu); |
| 102 | return; |
| 103 | } |
| 104 | } |
| 105 | fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n", |
| 106 | (unsigned long)proc, (unsigned long)context); |
| 107 | } |
| 108 | |
| 109 | /* Function to display an error message and exit. This is in this file because |
| 110 | this needs to restore terminal modes before exiting. See log-client.c |
| 111 | for other related functions. */ |
| 112 | |
| 113 | void fatal(const char *fmt, ...) |
| 114 | { |
| 115 | va_list args; |
| 116 | struct fatal_cleanup *cu, *next_cu; |
| 117 | static int fatal_called = 0; |
| 118 | |
| 119 | if (!fatal_called) |
| 120 | { |
| 121 | fatal_called = 1; |
| 122 | |
| 123 | /* Call cleanup functions. */ |
| 124 | for (cu = fatal_cleanups; cu; cu = next_cu) |
| 125 | { |
| 126 | next_cu = cu->next; |
| 127 | (*cu->proc)(cu->context); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | va_start(args, fmt); |
| 132 | vfprintf(stderr, fmt, args); |
| 133 | fprintf(stderr, "\r\n"); |
| 134 | va_end(args); |
| 135 | exit(255); |
| 136 | } |
| 137 | |
| 138 | /* fatal() is in ssh.c so that it can properly reset terminal modes. */ |