author | viric@mandarina |
Mon, 05 May 2008 23:14:22 +0200 | |
changeset 216 | b4476e12a717 |
parent 146 | 5e689cb593aa |
child 231 | 558b281b88f5 |
permissions | -rw-r--r-- |
102
27b0f0e44de8
Added copyright headers to the new files. I always forget that.
viric@llimona
parents:
92
diff
changeset
|
1 |
/* |
27b0f0e44de8
Added copyright headers to the new files. I always forget that.
viric@llimona
parents:
92
diff
changeset
|
2 |
Task Spooler - a task queue system for the unix user |
27b0f0e44de8
Added copyright headers to the new files. I always forget that.
viric@llimona
parents:
92
diff
changeset
|
3 |
Copyright (C) 2007 LluĂs Batlle i Rossell |
27b0f0e44de8
Added copyright headers to the new files. I always forget that.
viric@llimona
parents:
92
diff
changeset
|
4 |
|
27b0f0e44de8
Added copyright headers to the new files. I always forget that.
viric@llimona
parents:
92
diff
changeset
|
5 |
Please find the license in the provided COPYING file. |
27b0f0e44de8
Added copyright headers to the new files. I always forget that.
viric@llimona
parents:
92
diff
changeset
|
6 |
*/ |
92 | 7 |
#include <unistd.h> |
8 |
#include <stdio.h> |
|
9 |
#include <time.h> |
|
10 |
#include <sys/types.h> |
|
11 |
#include <errno.h> |
|
12 |
#include <string.h> |
|
13 |
#include <fcntl.h> |
|
14 |
#include <sys/stat.h> |
|
15 |
#include <stdarg.h> |
|
105 | 16 |
#include <stdlib.h> |
146 | 17 |
#include <sys/time.h> |
92 | 18 |
|
19 |
#include "main.h" |
|
20 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
21 |
enum Etype |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
22 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
23 |
WARNING, |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
24 |
ERROR |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
25 |
}; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
26 |
|
92 | 27 |
/* Declared in main.h as extern */ |
28 |
enum Process_type process_type; |
|
29 |
||
30 |
static int real_errno; |
|
31 |
||
32 |
/* Local protos */ |
|
33 |
static void dump_structs(FILE *out); |
|
34 |
||
35 |
||
36 |
static void print_date(FILE *out) |
|
37 |
{ |
|
38 |
time_t t; |
|
39 |
const char *tstr; |
|
40 |
||
41 |
t = time(NULL); |
|
42 |
tstr = ctime(&t); |
|
43 |
||
104 | 44 |
fprintf(out, "date %s", tstr); |
92 | 45 |
} |
46 |
||
47 |
static void dump_proc_info(FILE *out) |
|
48 |
{ |
|
49 |
print_date(out); |
|
50 |
fprintf(out, "pid %i\n", getpid()); |
|
51 |
if (process_type == SERVER) |
|
52 |
fprintf(out, "type SERVER\n"); |
|
53 |
else if (process_type == CLIENT) |
|
54 |
fprintf(out, "type CLIENT\n"); |
|
55 |
else |
|
56 |
fprintf(out, "type UNKNOWN\n"); |
|
57 |
} |
|
58 |
||
59 |
static FILE * open_error_file() |
|
60 |
{ |
|
61 |
int fd; |
|
62 |
FILE* out; |
|
63 |
||
104 | 64 |
fd = open("/tmp/ts.error", O_CREAT | O_APPEND | O_WRONLY, 0600); |
92 | 65 |
if (fd == -1) |
66 |
return 0; |
|
67 |
||
68 |
out = fdopen(fd, "a"); |
|
69 |
if (out == NULL) |
|
70 |
{ |
|
71 |
close(fd); |
|
72 |
return 0; |
|
73 |
} |
|
74 |
||
75 |
return out; |
|
76 |
} |
|
77 |
||
78 |
static void print_error(FILE *out, enum Etype type, const char *str, va_list ap) |
|
79 |
{ |
|
80 |
if (type == ERROR) |
|
81 |
fprintf(out, "Error\n"); |
|
82 |
else if (type == WARNING) |
|
83 |
fprintf(out, "Warning\n"); |
|
84 |
else |
|
85 |
fprintf(out, "Unknown kind of error\n"); |
|
86 |
||
87 |
fprintf(out, " Msg: "); |
|
88 |
||
89 |
vfprintf(out, str, ap); |
|
90 |
||
91 |
fprintf(out, "\n"); |
|
104 | 92 |
fprintf(out, " errno %i, \"%s\"\n", real_errno, strerror(real_errno)); |
92 | 93 |
} |
94 |
||
95 |
static void problem(enum Etype type, const char *str, va_list ap) |
|
96 |
{ |
|
97 |
FILE *out; |
|
98 |
||
99 |
out = open_error_file(); |
|
100 |
if (out == 0) |
|
101 |
return; |
|
102 |
||
103 |
/* out is ready */ |
|
104 |
print_error(out, type, str, ap); |
|
105 |
dump_structs(out); |
|
106 |
||
107 |
/* this will close the fd also */ |
|
108 |
fclose(out); |
|
109 |
} |
|
110 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
111 |
static void problem_msg(enum Etype type, const struct msg *m, const char *str, va_list ap) |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
112 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
113 |
FILE *out; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
114 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
115 |
out = open_error_file(); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
116 |
if (out == 0) |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
117 |
return; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
118 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
119 |
/* out is ready */ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
120 |
print_error(out, type, str, ap); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
121 |
msgdump(out, m); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
122 |
dump_structs(out); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
123 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
124 |
/* this will close the fd also */ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
125 |
fclose(out); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
126 |
} |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
127 |
|
92 | 128 |
void error(const char *str, ...) |
129 |
{ |
|
130 |
va_list ap; |
|
131 |
||
132 |
va_start(ap, str); |
|
133 |
||
134 |
real_errno = errno; |
|
135 |
||
136 |
problem(ERROR, str, ap); |
|
104 | 137 |
exit(-1); |
92 | 138 |
} |
139 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
140 |
void error_msg(const struct msg *m, const char *str, ...) |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
141 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
142 |
va_list ap; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
143 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
144 |
va_start(ap, str); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
145 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
146 |
real_errno = errno; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
147 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
148 |
problem_msg(ERROR, m, str, ap); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
149 |
exit(-1); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
150 |
} |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
151 |
|
92 | 152 |
void warning(const char *str, ...) |
153 |
{ |
|
154 |
va_list ap; |
|
155 |
||
156 |
va_start(ap, str); |
|
157 |
||
158 |
real_errno = errno; |
|
159 |
||
160 |
problem(WARNING, str, ap); |
|
161 |
} |
|
162 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
163 |
void warning_msg(const struct msg *m, const char *str, ...) |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
164 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
165 |
va_list ap; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
166 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
167 |
va_start(ap, str); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
168 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
169 |
real_errno = errno; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
170 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
171 |
problem_msg(WARNING, m, str, ap); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
172 |
} |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
173 |
|
92 | 174 |
static void dump_structs(FILE *out) |
175 |
{ |
|
176 |
dump_proc_info(out); |
|
177 |
if (process_type == SERVER) |
|
178 |
{ |
|
179 |
dump_jobs_struct(out); |
|
180 |
dump_conns_struct(out); |
|
181 |
} |
|
182 |
} |