author | viric@mandarina |
Sun, 15 Feb 2009 19:12:08 +0100 | |
changeset 261 | dc8f1609bad8 |
parent 254 | 41252d5e85f5 |
child 267 | 11631dd11ff8 |
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 |
231
558b281b88f5
Updating the Copyright years from 2007 to 2007-2008.
llbatlle@taga
parents:
146
diff
changeset
|
3 |
Copyright (C) 2007-2008 LluĂs Batlle i Rossell |
102
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; |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
63 |
char *path; |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
64 |
int new_size; |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
65 |
char *new_path; |
92 | 66 |
|
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
67 |
create_socket_path(&path); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
68 |
new_size = strlen(path)+10; |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
69 |
new_path = malloc(new_size); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
70 |
|
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
71 |
strncpy(new_path, path, new_size); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
72 |
strncat(new_path, ".error", (new_size - strlen(path)) - 1); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
73 |
free(path); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
74 |
|
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
75 |
fd = open(new_path, O_CREAT | O_APPEND | O_WRONLY, 0600); |
92 | 76 |
if (fd == -1) |
77 |
return 0; |
|
78 |
||
79 |
out = fdopen(fd, "a"); |
|
80 |
if (out == NULL) |
|
81 |
{ |
|
82 |
close(fd); |
|
83 |
return 0; |
|
84 |
} |
|
85 |
||
251
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
86 |
free(new_path); |
5e0802df5788
Making a ts.error file for each socket ($TS_SOCKET.error)
viric@mandarina
parents:
231
diff
changeset
|
87 |
|
92 | 88 |
return out; |
89 |
} |
|
90 |
||
91 |
static void print_error(FILE *out, enum Etype type, const char *str, va_list ap) |
|
92 |
{ |
|
93 |
if (type == ERROR) |
|
94 |
fprintf(out, "Error\n"); |
|
95 |
else if (type == WARNING) |
|
96 |
fprintf(out, "Warning\n"); |
|
97 |
else |
|
98 |
fprintf(out, "Unknown kind of error\n"); |
|
99 |
||
100 |
fprintf(out, " Msg: "); |
|
101 |
||
102 |
vfprintf(out, str, ap); |
|
103 |
||
104 |
fprintf(out, "\n"); |
|
104 | 105 |
fprintf(out, " errno %i, \"%s\"\n", real_errno, strerror(real_errno)); |
92 | 106 |
} |
107 |
||
108 |
static void problem(enum Etype type, const char *str, va_list ap) |
|
109 |
{ |
|
110 |
FILE *out; |
|
111 |
||
112 |
out = open_error_file(); |
|
113 |
if (out == 0) |
|
114 |
return; |
|
115 |
||
116 |
/* out is ready */ |
|
117 |
print_error(out, type, str, ap); |
|
118 |
dump_structs(out); |
|
119 |
||
120 |
/* this will close the fd also */ |
|
121 |
fclose(out); |
|
122 |
} |
|
123 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
124 |
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
|
125 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
126 |
FILE *out; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
127 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
128 |
out = open_error_file(); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
129 |
if (out == 0) |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
130 |
return; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
131 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
132 |
/* out is ready */ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
133 |
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
|
134 |
msgdump(out, m); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
135 |
dump_structs(out); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
136 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
137 |
/* 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
|
138 |
fclose(out); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
139 |
} |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
140 |
|
92 | 141 |
void error(const char *str, ...) |
142 |
{ |
|
143 |
va_list ap; |
|
144 |
||
145 |
va_start(ap, str); |
|
146 |
||
147 |
real_errno = errno; |
|
148 |
||
149 |
problem(ERROR, str, ap); |
|
104 | 150 |
exit(-1); |
92 | 151 |
} |
152 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
153 |
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
|
154 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
155 |
va_list ap; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
156 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
157 |
va_start(ap, str); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
158 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
159 |
real_errno = errno; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
160 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
161 |
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
|
162 |
exit(-1); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
163 |
} |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
164 |
|
92 | 165 |
void warning(const char *str, ...) |
166 |
{ |
|
167 |
va_list ap; |
|
168 |
||
169 |
va_start(ap, str); |
|
170 |
||
171 |
real_errno = errno; |
|
172 |
||
173 |
problem(WARNING, str, ap); |
|
174 |
} |
|
175 |
||
111
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
176 |
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
|
177 |
{ |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
178 |
va_list ap; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
179 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
180 |
va_start(ap, str); |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
181 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
182 |
real_errno = errno; |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
183 |
|
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
184 |
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
|
185 |
} |
d6bc62904b5a
Fixed a buffer overflow in jobs.c, when sending the lines for LISTJOBS.
viric@mandarina
parents:
105
diff
changeset
|
186 |
|
92 | 187 |
static void dump_structs(FILE *out) |
188 |
{ |
|
189 |
dump_proc_info(out); |
|
190 |
if (process_type == SERVER) |
|
191 |
{ |
|
192 |
dump_jobs_struct(out); |
|
254
41252d5e85f5
Fixed a bug in the notify list, reported by mark meissonnier.
viric@mandarina
parents:
251
diff
changeset
|
193 |
dump_notifies_struct(out); |
92 | 194 |
dump_conns_struct(out); |
195 |
} |
|
196 |
} |