1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
|
Index: src/child.c
===================================================================
RCS file: /cvsroot/tinyproxy/tinyproxy-devel/src/child.c,v
retrieving revision 1.5
diff -u -u -r1.5 child.c
--- src/child.c 27 Jun 2002 16:29:21 -0000 1.5
+++ src/child.c 4 Sep 2002 21:54:24 -0000
@@ -164,8 +164,11 @@
socklen_t clilen;
cliaddr = safemalloc(addrlen);
- if (!cliaddr)
+ if (!cliaddr) {
+ log_message(LOG_CRIT,
+ "Could not allocate memory for child address.");
exit(0);
+ }
ptr->connects = 0;
@@ -382,6 +385,8 @@
/* Handle log rotation if it was requested */
if (received_sighup) {
+ truncate_log_file();
+
#ifdef FILTER_ENABLE
if (config.filter) {
filter_destroy();
Index: src/daemon.c
===================================================================
RCS file: /cvsroot/tinyproxy/tinyproxy-devel/src/daemon.c,v
retrieving revision 1.1
diff -u -u -r1.1 daemon.c
--- src/daemon.c 23 May 2002 04:39:32 -0000 1.1
+++ src/daemon.c 4 Sep 2002 21:54:24 -0000
@@ -40,9 +40,11 @@
chdir("/");
umask(077);
+#if 0
close(0);
close(1);
close(2);
+#endif
}
/*
Index: src/log.c
===================================================================
RCS file: /cvsroot/tinyproxy/tinyproxy-devel/src/log.c,v
retrieving revision 1.22
diff -u -u -r1.22 log.c
--- src/log.c 15 Jun 2002 17:37:11 -0000 1.22
+++ src/log.c 4 Sep 2002 21:54:24 -0000
@@ -19,10 +19,10 @@
#include "tinyproxy.h"
-#include "hashmap.h"
#include "heap.h"
#include "log.h"
#include "utils.h"
+#include "vector.h"
static char *syslog_level[] = {
NULL,
@@ -40,6 +40,11 @@
#define STRING_LENGTH 800
/*
+ * Global file descriptor for the log file
+ */
+int log_file_fd = -1;
+
+/*
* Store the log level setting.
*/
static int log_level = LOG_INFO;
@@ -50,7 +55,36 @@
* The key is the actual messages (already filled in full), and the value
* is the log level.
*/
-static hashmap_t log_message_storage;
+static vector_t log_message_storage;
+
+/*
+ * Open the log file and store the file descriptor in a global location.
+ */
+int
+open_log_file(const char* log_file_name)
+{
+ log_file_fd = create_file_safely(log_file_name, FALSE);
+ return log_file_fd;
+}
+
+/*
+ * Close the log file
+ */
+void
+close_log_file(void)
+{
+ close(log_file_fd);
+}
+
+/*
+ * Truncate log file to a zero length.
+ */
+void
+truncate_log_file(void)
+{
+ lseek(log_file_fd, 0, SEEK_SET);
+ ftruncate(log_file_fd, 0);
+}
/*
* Set the log level for writing to the log file.
@@ -71,9 +105,7 @@
time_t nowtime;
char time_string[TIME_LENGTH];
-#if defined(HAVE_SYSLOG_H) && !defined(HAVE_VSYSLOG_H)
char str[STRING_LENGTH];
-#endif
#ifdef NDEBUG
/*
@@ -101,19 +133,23 @@
* the messages for later processing.
*/
if (!processed_config_file) {
- char string_level[4];
+ char* entry_buffer;
if (!log_message_storage) {
- log_message_storage = hashmap_create(1);
+ log_message_storage = vector_create();
if (!log_message_storage)
return;
}
vsnprintf(str, STRING_LENGTH, fmt, args);
- snprintf(string_level, 4, "%d", level);
- hashmap_insert(log_message_storage, str,
- string_level, strlen(string_level) + 1);
+ entry_buffer = safemalloc(strlen(str) + 6);
+ if (!entry_buffer)
+ return;
+
+ sprintf(entry_buffer, "%d %s", level, str);
+ vector_insert(log_message_storage, entry_buffer,
+ strlen(entry_buffer) + 1);
va_end(args);
@@ -130,23 +166,21 @@
# endif
} else {
#endif
- FILE* log_file;
- int fd;
-
- fd = create_file_safely(config.logf_name, FALSE);
- log_file = fdopen(fd, "w+");
-
nowtime = time(NULL);
/* Format is month day hour:minute:second (24 time) */
strftime(time_string, TIME_LENGTH, "%b %d %H:%M:%S",
localtime(&nowtime));
- fprintf(log_file, "%-9s %s [%ld]: ", syslog_level[level],
- time_string, (long int) getpid());
- vfprintf(log_file, fmt, args);
- fprintf(log_file, "\n");
+ snprintf(str, STRING_LENGTH, "%-9s %s [%ld]: ", syslog_level[level],
+ time_string, (long int) getpid());
+
+ assert(log_file_fd >= 0);
+
+ write(log_file_fd, str, strlen(str));
+ vsnprintf(str, STRING_LENGTH, fmt, args);
+ write(log_file_fd, str, strlen(str));
+ write(log_file_fd, "\n", 1);
- fclose(log_file);
#ifdef HAVE_SYSLOG_H
}
#endif
@@ -160,35 +194,32 @@
void
send_stored_logs(void)
{
- hashmap_iter iter;
- char *level_string;
char *string;
+ char *ptr;
+
int level;
- iter = hashmap_first(log_message_storage);
- if (iter >= 0) {
- for ( ; !hashmap_is_end(log_message_storage, iter); ++iter) {
- hashmap_return_entry(log_message_storage,
- iter,
- &string,
- (void **)&level_string);
+ int i;
- level = atoi(level_string);
+ for (i = 0; i < vector_length(log_message_storage); ++i) {
+ vector_getentry(log_message_storage, i, (void **)&string);
+
+ ptr = strchr(string, ' ') + 1;
+ level = atoi(string);
#if NDEBUG
- if (log_level == LOG_CONN && level == LOG_INFO)
- continue;
- else if (log_level == LOG_INFO) {
- if (level > LOG_INFO && level != LOG_CONN)
- continue;
- } else if (level > log_level)
+ if (log_level == LOG_CONN && level == LOG_INFO)
+ continue;
+ else if (log_level == LOG_INFO) {
+ if (level > LOG_INFO && level != LOG_CONN)
continue;
+ } else if (level > log_level)
+ continue;
#endif
- log_message(level, string);
- }
+ log_message(level, ptr);
}
- hashmap_delete(log_message_storage);
+ vector_delete(log_message_storage);
log_message_storage = NULL;
}
Index: src/log.h
===================================================================
RCS file: /cvsroot/tinyproxy/tinyproxy-devel/src/log.h,v
retrieving revision 1.10
diff -u -u -r1.10 log.h
--- src/log.h 26 May 2002 18:55:19 -0000 1.10
+++ src/log.h 4 Sep 2002 21:54:24 -0000
@@ -99,6 +99,10 @@
# define DEBUG2(x, y...) do { } while(0)
#endif
+extern int open_log_file(const char* file);
+extern void close_log_file(void);
+extern void truncate_log_file(void);
+
extern void log_message(int level, char *fmt, ...);
extern void set_log_level(int level);
extern void send_stored_logs(void);
Index: src/sock.c
===================================================================
RCS file: /cvsroot/tinyproxy/tinyproxy-devel/src/sock.c,v
retrieving revision 1.38
diff -u -u -r1.38 sock.c
--- src/sock.c 31 May 2002 18:08:01 -0000 1.38
+++ src/sock.c 4 Sep 2002 21:54:24 -0000
@@ -152,7 +152,7 @@
* - rjkaes
*/
int
-listen_sock(uint16_t port, socklen_t * addrlen)
+listen_sock(uint16_t port, socklen_t* addrlen)
{
int listenfd;
const int on = 1;
Index: src/tinyproxy.c
===================================================================
RCS file: /cvsroot/tinyproxy/tinyproxy-devel/src/tinyproxy.c,v
retrieving revision 1.38
diff -u -u -r1.38 tinyproxy.c
--- src/tinyproxy.c 12 Jul 2002 17:02:02 -0000 1.38
+++ src/tinyproxy.c 4 Sep 2002 21:54:24 -0000
@@ -230,6 +230,13 @@
"%s: You MUST set a LogFile in the configuration file.\n",
argv[0]);
exit(EX_SOFTWARE);
+ } else {
+ if (open_log_file(config.logf_name) < 0) {
+ fprintf(stderr,
+ "%s: Could not create log file.\n",
+ argv[0]);
+ exit(EX_SOFTWARE);
+ }
}
} else {
if (godaemon == TRUE)
@@ -238,6 +245,9 @@
openlog("tinyproxy", LOG_PID, LOG_USER);
}
+ processed_config_file = TRUE;
+ send_stored_logs();
+
/*
* Set the default values if they were not set in the config file.
*/
@@ -352,9 +362,6 @@
exit(EX_SOFTWARE);
}
- processed_config_file = TRUE;
- send_stored_logs();
-
/*
* These signals are only for the parent process.
*/
@@ -407,6 +414,8 @@
if (config.syslog)
closelog();
+ else
+ close_log_file();
exit(EX_OK);
}
|