]>
Commit | Line | Data |
---|---|---|
44a47c6e | 1 | |
db40ae20 | 2 | /* $Id: tools.cc,v 1.9 1996/03/27 05:12:40 wessels Exp $ */ |
44a47c6e | 3 | |
4 | #include "squid.h" | |
5 | ||
090089c4 | 6 | int do_mallinfo = 0; /* don't do mallinfo() unless this gets set */ |
090089c4 | 7 | |
44a47c6e | 8 | static int PrintRusage _PARAMS((void (*)(), FILE *)); |
090089c4 | 9 | |
44a47c6e | 10 | extern int gethostname _PARAMS((char *name, int namelen)); |
090089c4 | 11 | |
12 | #define DEAD_MSG "\ | |
13 | The Harvest Cache (version %s) died.\n\ | |
14 | \n\ | |
15 | You've encountered a fatal error in the Harvest Cache version %s.\n\ | |
16 | If a core file was created (possibly in the swap directory),\n\ | |
17 | please execute 'gdb cached core' or 'dbx cached core', then type 'where',\n\ | |
18 | and report the trace back to harvest-dvl@cs.colorado.edu.\n\ | |
19 | \n\ | |
20 | Thanks!\n" | |
21 | ||
22 | static char *dead_msg() | |
23 | { | |
24 | static char msg[1024]; | |
73a5465f | 25 | sprintf(msg, DEAD_MSG, SQUID_VERSION, SQUID_VERSION); |
090089c4 | 26 | return msg; |
27 | } | |
28 | ||
29 | void mail_warranty() | |
30 | { | |
31 | FILE *fp; | |
32 | static char filename[256]; | |
33 | static char command[256]; | |
34 | ||
35 | sprintf(filename, "/tmp/mailin%d", (int) getpid()); | |
36 | fp = fopen(filename, "w"); | |
37 | if (fp != NULL) { | |
38 | fprintf(fp, "From: cached\n"); | |
39 | fprintf(fp, "To: %s\n", getAdminEmail()); | |
40 | fprintf(fp, "Subject: %s\n", dead_msg()); | |
41 | fclose(fp); | |
42 | ||
43 | sprintf(command, "mail %s < %s", getAdminEmail(), filename); | |
44 | ||
45 | system(command); | |
46 | unlink(filename); | |
47 | } | |
48 | } | |
49 | ||
50 | void print_warranty() | |
51 | { | |
52 | if (getAdminEmail()) | |
53 | mail_warranty(); | |
54 | else | |
55 | puts(dead_msg()); | |
56 | } | |
57 | ||
0e08ce4b | 58 | void death(sig) |
59 | int sig; | |
44a47c6e | 60 | { |
0e08ce4b | 61 | if (sig == SIGSEGV) |
62 | fprintf(stderr, "FATAL: Received Segment Violation...dying.\n"); | |
63 | else if (sig == SIGBUS) | |
64 | fprintf(stderr, "FATAL: Received bus error...dying.\n"); | |
65 | else | |
66 | fprintf(stderr, "FATAL: Received signal %d...dying.\n", sig); | |
44a47c6e | 67 | signal(SIGSEGV, SIG_DFL); |
68 | signal(SIGBUS, SIG_DFL); | |
0e08ce4b | 69 | signal(sig, SIG_DFL); |
44a47c6e | 70 | storeWriteCleanLog(); |
71 | PrintRusage(NULL, stderr); | |
72 | print_warranty(); | |
73 | abort(); | |
74 | } | |
75 | ||
76 | ||
090089c4 | 77 | void rotate_logs(sig) |
78 | int sig; | |
79 | { | |
80 | debug(1, "rotate_logs: SIGHUP received.\n"); | |
81 | ||
82 | storeWriteCleanLog(); | |
83 | neighbors_rotate_log(); | |
84 | stat_rotate_log(); | |
85 | _db_rotate_log(); | |
ed43818f | 86 | #if defined(_SQUID_SYSV_SIGNALS_) |
090089c4 | 87 | signal(sig, rotate_logs); |
88 | #endif | |
89 | } | |
90 | ||
91 | void shut_down(sig) | |
92 | int sig; | |
93 | { | |
94 | debug(1, "Shutting down...\n"); | |
95 | storeWriteCleanLog(); | |
96 | PrintRusage(NULL, stderr); | |
97 | debug(0, "Harvest Cache (Version %s): Exiting due to signal %d.\n", | |
73a5465f | 98 | SQUID_VERSION, sig); |
090089c4 | 99 | exit(1); |
100 | } | |
101 | ||
102 | void fatal_common(message) | |
103 | char *message; | |
104 | { | |
db40ae20 | 105 | #if HAVE_SYSLOG |
090089c4 | 106 | if (syslog_enable) |
107 | syslog(LOG_ALERT, message); | |
db40ae20 | 108 | #endif |
090089c4 | 109 | fprintf(stderr, "FATAL: %s\n", message); |
110 | fprintf(stderr, "Harvest Cache (Version %s): Terminated abnormally.\n", | |
73a5465f | 111 | SQUID_VERSION); |
090089c4 | 112 | fflush(stderr); |
113 | PrintRusage(NULL, stderr); | |
114 | if (debug_log != stderr) { | |
115 | debug(0, "FATAL: %s\n", message); | |
116 | debug(0, "Harvest Cache (Version %s): Terminated abnormally.\n", | |
73a5465f | 117 | SQUID_VERSION); |
090089c4 | 118 | } |
119 | } | |
120 | ||
121 | /* fatal */ | |
122 | void fatal(message) | |
123 | char *message; | |
124 | { | |
125 | fatal_common(message); | |
126 | exit(1); | |
127 | } | |
128 | ||
129 | /* fatal with dumping core */ | |
130 | void fatal_dump(message) | |
131 | char *message; | |
132 | { | |
133 | if (message) | |
134 | fatal_common(message); | |
135 | if (catch_signals) | |
136 | storeWriteCleanLog(); | |
137 | abort(); | |
138 | } | |
139 | ||
140 | ||
141 | void dumpMallocStats(f) | |
142 | FILE *f; | |
143 | { | |
144 | #if USE_MALLINFO | |
145 | struct mallinfo mp; | |
146 | ||
147 | if (!do_mallinfo) | |
148 | return; | |
149 | ||
150 | mp = mallinfo(); | |
151 | ||
152 | fprintf(f, "Malloc Instrumentation via mallinfo(): \n"); | |
153 | fprintf(f, " total space in arena %d\n", mp.arena); | |
154 | fprintf(f, " number of ordinary blocks %d\n", mp.ordblks); | |
155 | fprintf(f, " number of small blocks %d\n", mp.smblks); | |
156 | fprintf(f, " number of holding blocks %d\n", mp.hblks); | |
157 | fprintf(f, " space in holding block headers %d\n", mp.hblkhd); | |
158 | fprintf(f, " space in small blocks in use %d\n", mp.usmblks); | |
159 | fprintf(f, " space in free blocks %d\n", mp.fsmblks); | |
160 | fprintf(f, " space in ordinary blocks in use %d\n", mp.uordblks); | |
161 | fprintf(f, " space in free ordinary blocks %d\n", mp.fordblks); | |
162 | fprintf(f, " cost of enabling keep option %d\n", mp.keepcost); | |
163 | #if LNG_MALLINFO | |
164 | fprintf(f, " max size of small blocks %d\n", mp.mxfast); | |
165 | fprintf(f, " number of small blocks in a holding block %d\n", | |
166 | mp.nlblks); | |
167 | fprintf(f, " small block rounding factor %d\n", mp.grain); | |
168 | fprintf(f, " space (including overhead) allocated in ord. blks %d\n", | |
169 | mp.uordbytes); | |
170 | fprintf(f, " number of ordinary blocks allocated %d\n", | |
171 | mp.allocated); | |
172 | fprintf(f, " bytes used in maintaining the free tree %d\n", | |
173 | mp.treeoverhead); | |
174 | #endif /* LNG_MALLINFO */ | |
175 | ||
176 | #if PRINT_MMAP | |
177 | mallocmap(); | |
178 | #endif /* PRINT_MMAP */ | |
179 | #endif /* USE_MALLINFO */ | |
180 | } | |
181 | ||
182 | int PrintRusage(f, lf) | |
183 | void (*f) (); | |
184 | FILE *lf; | |
185 | { | |
186 | #if defined(HAVE_RUSAGE) && defined(RUSAGE_SELF) | |
187 | struct rusage rusage; | |
188 | ||
189 | getrusage(RUSAGE_SELF, &rusage); | |
190 | fprintf(lf, "CPU Usage: user %d sys %d\nMemory Usage: rss %d KB\n", | |
191 | rusage.ru_utime.tv_sec, rusage.ru_stime.tv_sec, | |
192 | rusage.ru_maxrss * getpagesize() / 1000); | |
193 | fprintf(lf, "Page faults with physical i/o: %d\n", | |
194 | rusage.ru_majflt); | |
195 | ||
196 | #endif | |
197 | dumpMallocStats(lf); | |
198 | if (f) | |
199 | f(0); | |
200 | return 0; | |
201 | } | |
202 | ||
203 | int getHeapSize() | |
204 | { | |
205 | #if USE_MALLINFO | |
206 | struct mallinfo mp; | |
207 | ||
208 | mp = mallinfo(); | |
209 | ||
210 | return (mp.arena); | |
211 | #else | |
212 | return (0); | |
213 | #endif | |
214 | } | |
215 | ||
216 | void sig_child(sig) | |
217 | int sig; | |
218 | { | |
219 | int status; | |
220 | int pid; | |
090089c4 | 221 | |
f31d5a1d | 222 | if ((pid = waitpid(-1, &status, WNOHANG)) > 0) |
090089c4 | 223 | debug(3, "sig_child: Ate pid %d\n", pid); |
f31d5a1d | 224 | |
ed43818f | 225 | #if defined(_SQUID_SYSV_SIGNALS_) |
090089c4 | 226 | signal(sig, sig_child); |
227 | #endif | |
228 | } | |
229 | ||
090089c4 | 230 | /* |
231 | * getMaxFD - returns the file descriptor table size | |
232 | */ | |
233 | int getMaxFD() | |
234 | { | |
235 | static int i = -1; | |
236 | ||
237 | if (i == -1) { | |
238 | #if defined(HAVE_SYSCONF) && defined(_SC_OPEN_MAX) | |
239 | i = sysconf(_SC_OPEN_MAX); /* prefered method */ | |
240 | #elif defined(HAVE_GETDTABLESIZE) | |
241 | i = getdtablesize(); /* the BSD way */ | |
242 | #elif defined(OPEN_MAX) | |
243 | i = OPEN_MAX; | |
244 | #elif defined(NOFILE) | |
245 | i = NOFILE; | |
246 | #elif defined(_NFILE) | |
247 | i = _NFILE; | |
248 | #else | |
249 | i = 64; /* 64 is a safe default */ | |
250 | #endif | |
251 | debug(10, "getMaxFD set MaxFD at %d\n", i); | |
252 | } | |
253 | return (i); | |
254 | } | |
44a47c6e | 255 | |
256 | char *getMyHostname() | |
257 | { | |
258 | static char host[SQUIDHOSTNAMELEN + 1]; | |
259 | static int present = 0; | |
260 | struct hostent *h = NULL; | |
261 | ||
262 | /* Get the host name and store it in host to return */ | |
263 | if (!present) { | |
264 | host[0] = '\0'; | |
265 | if (gethostname(host, SQUIDHOSTNAMELEN) == -1) { | |
266 | debug(1, "comm_hostname: gethostname failed: %s\n", | |
267 | xstrerror()); | |
268 | return NULL; | |
269 | } else { | |
270 | if ((h = ipcache_gethostbyname(host)) != NULL) { | |
271 | /* DNS lookup successful */ | |
272 | /* use the official name from DNS lookup */ | |
273 | strcpy(host, h->h_name); | |
274 | } | |
275 | present = 1; | |
276 | } | |
277 | } | |
278 | return host; | |
279 | } | |
280 | ||
281 | int safeunlink(s, quiet) | |
282 | char *s; | |
283 | int quiet; | |
284 | { | |
285 | int err; | |
286 | if ((err = unlink(s)) < 0) | |
287 | if (!quiet) | |
288 | debug(1, "safeunlink: Couldn't delete %s. %s\n", s, xstrerror()); | |
289 | return (err); | |
290 | } |