]>
Commit | Line | Data |
---|---|---|
3ce1f295 | 1 | /* Copyright (C) 1997,1999-2003,2005,2006,2011 Free Software Foundation, Inc. |
0501d603 UD |
2 | This file is part of the GNU C Library. |
3 | Contributed by Ulrich Drepper <drepper@cygnus.com>, 1997. | |
4 | ||
5 | The GNU C Library is free software; you can redistribute it and/or | |
41bdb6e2 AJ |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either | |
8 | version 2.1 of the License, or (at your option) any later version. | |
0501d603 UD |
9 | |
10 | The GNU C Library is distributed in the hope that it will be useful, | |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
41bdb6e2 | 13 | Lesser General Public License for more details. |
0501d603 | 14 | |
41bdb6e2 AJ |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with the GNU C Library; if not, write to the Free | |
17 | Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA | |
18 | 02111-1307 USA. */ | |
0501d603 UD |
19 | |
20 | #include <fmtmsg.h> | |
5107cf1d | 21 | #include <bits/libc-lock.h> |
0501d603 UD |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <sys/syslog.h> | |
3ce1f295 | 26 | #include <wchar.h> |
0501d603 UD |
27 | |
28 | ||
29 | /* We have global data, protect the modification. */ | |
30 | __libc_lock_define_initialized (static, lock) | |
31 | ||
32 | ||
33 | enum | |
34 | { | |
35 | label_mask = 0x01, | |
36 | severity_mask = 0x02, | |
37 | text_mask = 0x04, | |
38 | action_mask = 0x08, | |
39 | tag_mask = 0x10, | |
40 | all_mask = label_mask | severity_mask | text_mask | action_mask | tag_mask | |
41 | }; | |
42 | ||
c4563d2d | 43 | static const struct |
0501d603 | 44 | { |
fd5ea238 | 45 | uint32_t len; |
c4563d2d | 46 | /* Adjust the size if new elements are added. */ |
23f5f62d | 47 | const char name[12]; |
0501d603 UD |
48 | } keywords[] = |
49 | { | |
c4563d2d UD |
50 | { 5, "label" }, |
51 | { 8, "severity" }, | |
52 | { 4, "text" }, | |
53 | { 6, "action"}, | |
54 | { 3, "tag" } | |
0501d603 UD |
55 | }; |
56 | #define NKEYWORDS (sizeof( keywords) / sizeof (keywords[0])) | |
57 | ||
58 | ||
59 | struct severity_info | |
60 | { | |
61 | int severity; | |
62 | const char *string; | |
63 | struct severity_info *next; | |
64 | }; | |
65 | ||
66 | ||
67 | /* List of known severities. */ | |
68 | static const struct severity_info nosev = | |
69 | { | |
70 | MM_NOSEV, "", NULL | |
71 | }; | |
72 | static const struct severity_info haltsev = | |
73 | { | |
74 | MM_HALT, "HALT", (struct severity_info *) &nosev | |
75 | }; | |
76 | static const struct severity_info errorsev = | |
77 | { | |
78 | MM_ERROR, "ERROR", (struct severity_info *) &haltsev | |
79 | }; | |
80 | static const struct severity_info warningsev = | |
81 | { | |
82 | MM_WARNING, "WARNING", (struct severity_info *) &errorsev | |
83 | }; | |
84 | static const struct severity_info infosev = | |
85 | { | |
86 | MM_INFO, "INFO", (struct severity_info *) &warningsev | |
87 | }; | |
88 | ||
89 | /* Start of the list. */ | |
90 | static struct severity_info *severity_list = (struct severity_info *) &infosev; | |
91 | ||
8f2ece69 UD |
92 | /* Mask of values we will print. */ |
93 | static int print; | |
0501d603 UD |
94 | |
95 | /* Prototypes for local functions. */ | |
8f2ece69 | 96 | static void init (void); |
dfd2257a UD |
97 | static int internal_addseverity (int severity, const char *string) |
98 | internal_function; | |
0501d603 UD |
99 | |
100 | ||
101 | int | |
102 | fmtmsg (long int classification, const char *label, int severity, | |
103 | const char *text, const char *action, const char *tag) | |
104 | { | |
ca34d7a7 | 105 | __libc_once_define (static, once); |
0501d603 UD |
106 | int result = MM_OK; |
107 | struct severity_info *severity_rec; | |
108 | ||
f1c30c98 | 109 | /* Make sure everything is initialized. */ |
8f2ece69 | 110 | __libc_once (once, init); |
0501d603 UD |
111 | |
112 | /* Start the real work. First check whether the input is ok. */ | |
113 | if (label != MM_NULLLBL) | |
114 | { | |
115 | /* Must be two fields, separated by a colon. */ | |
116 | const char *cp = strchr (label, ':'); | |
117 | if (cp == NULL) | |
118 | return MM_NOTOK; | |
119 | ||
120 | /* The first field must not contain more then 10 bytes. */ | |
121 | if (cp - label > 10 | |
122 | /* The second field must not have more then 14 bytes. */ | |
123 | || strlen (cp + 1) > 14) | |
124 | return MM_NOTOK; | |
125 | } | |
126 | ||
127 | for (severity_rec = severity_list; severity_rec != NULL; | |
128 | severity_rec = severity_rec->next) | |
129 | if (severity == severity_rec->severity) | |
130 | /* Bingo. */ | |
131 | break; | |
132 | ||
133 | /* If we don't know anything about the severity level return an error. */ | |
134 | if (severity_rec == NULL) | |
135 | return MM_NOTOK; | |
136 | ||
137 | ||
f1c30c98 UD |
138 | #ifdef __libc_ptf_call |
139 | /* We do not want this call to be cut short by a thread | |
140 | cancellation. Therefore disable cancellation for now. */ | |
141 | int state = PTHREAD_CANCEL_ENABLE; | |
142 | __libc_ptf_call (pthread_setcancelstate, (PTHREAD_CANCEL_DISABLE, &state), | |
143 | 0); | |
144 | #endif | |
145 | ||
0501d603 UD |
146 | /* Now we can print. */ |
147 | if (classification & MM_PRINT) | |
148 | { | |
149 | int do_label = (print & label_mask) && label != MM_NULLLBL; | |
150 | int do_severity = (print & severity_mask) && severity != MM_NULLSEV; | |
151 | int do_text = (print & text_mask) && text != MM_NULLTXT; | |
152 | int do_action = (print & action_mask) && action != MM_NULLACT; | |
153 | int do_tag = (print & tag_mask) && tag != MM_NULLTAG; | |
154 | ||
df6f8969 | 155 | if (__fxprintf (stderr, "%s%s%s%s%s%s%s%s%s%s\n", |
df6f8969 UD |
156 | do_label ? label : "", |
157 | do_label && (do_severity | do_text | do_action | do_tag) | |
158 | ? ": " : "", | |
159 | do_severity ? severity_rec->string : "", | |
160 | do_severity && (do_text | do_action | do_tag) | |
161 | ? ": " : "", | |
162 | do_text ? text : "", | |
163 | do_text && (do_action | do_tag) ? "\n" : "", | |
164 | do_action ? "TO FIX: " : "", | |
165 | do_action ? action : "", | |
166 | do_action && do_tag ? " " : "", | |
167 | do_tag ? tag : "") < 0) | |
168 | /* Oh, oh. An error occurred during the output. */ | |
169 | result = MM_NOMSG; | |
0501d603 UD |
170 | } |
171 | ||
172 | if (classification & MM_CONSOLE) | |
173 | { | |
174 | int do_label = label != MM_NULLLBL; | |
175 | int do_severity = severity != MM_NULLSEV; | |
176 | int do_text = text != MM_NULLTXT; | |
177 | int do_action = action != MM_NULLACT; | |
178 | int do_tag = tag != MM_NULLTAG; | |
179 | ||
180 | syslog (LOG_ERR, "%s%s%s%s%s%s%s%s%s%s\n", | |
181 | do_label ? label : "", | |
9f6c1fc4 UD |
182 | do_label && (do_severity | do_text | do_action | do_tag) |
183 | ? ": " : "", | |
0501d603 | 184 | do_severity ? severity_rec->string : "", |
9f6c1fc4 | 185 | do_severity && (do_text | do_action | do_tag) ? ": " : "", |
0501d603 | 186 | do_text ? text : "", |
9f6c1fc4 | 187 | do_text && (do_action | do_tag) ? "\n" : "", |
0501d603 UD |
188 | do_action ? "TO FIX: " : "", |
189 | do_action ? action : "", | |
9f6c1fc4 | 190 | do_action && do_tag ? " " : "", |
0501d603 UD |
191 | do_tag ? tag : ""); |
192 | } | |
193 | ||
f1c30c98 UD |
194 | #ifdef __libc_ptf_call |
195 | __libc_ptf_call (pthread_setcancelstate, (state, NULL), 0); | |
196 | #endif | |
197 | ||
0501d603 UD |
198 | return result; |
199 | } | |
200 | ||
201 | ||
8f2ece69 UD |
202 | /* Initialize from environment variable content. */ |
203 | static void | |
204 | init (void) | |
205 | { | |
206 | const char *msgverb_var = getenv ("MSGVERB"); | |
207 | const char *sevlevel_var = getenv ("SEV_LEVEL"); | |
208 | ||
209 | if (msgverb_var != NULL && msgverb_var[0] != '\0') | |
210 | { | |
211 | /* Using this extra variable allows us to work without locking. */ | |
212 | do | |
213 | { | |
214 | size_t cnt; | |
215 | ||
216 | for (cnt = 0; cnt < NKEYWORDS; ++cnt) | |
217 | if (memcmp (msgverb_var, | |
218 | keywords[cnt].name, keywords[cnt].len) == 0 | |
219 | && (msgverb_var[keywords[cnt].len] == ':' | |
220 | || msgverb_var[keywords[cnt].len] == '\0')) | |
221 | break; | |
222 | ||
223 | if (cnt < NKEYWORDS) | |
224 | { | |
225 | print |= 1 << cnt; | |
226 | ||
227 | msgverb_var += keywords[cnt].len; | |
228 | if (msgverb_var[0] == ':') | |
229 | ++msgverb_var; | |
230 | } | |
231 | else | |
232 | { | |
233 | /* We found an illegal keyword in the environment | |
234 | variable. The specifications say that we print all | |
235 | fields. */ | |
236 | print = all_mask; | |
237 | break; | |
238 | } | |
239 | } | |
240 | while (msgverb_var[0] != '\0'); | |
241 | } | |
242 | else | |
243 | print = all_mask; | |
244 | ||
245 | ||
246 | if (sevlevel_var != NULL) | |
247 | { | |
248 | __libc_lock_lock (lock); | |
249 | ||
250 | while (sevlevel_var[0] != '\0') | |
251 | { | |
c4563d2d | 252 | const char *end = __strchrnul (sevlevel_var, ':'); |
8f2ece69 UD |
253 | int level; |
254 | ||
8f2ece69 UD |
255 | /* First field: keyword. This is not used here but it must be |
256 | present. */ | |
257 | while (sevlevel_var < end) | |
258 | if (*sevlevel_var++ == ',') | |
259 | break; | |
260 | ||
261 | if (sevlevel_var < end) | |
262 | { | |
263 | /* Second field: severity level, a number. */ | |
264 | char *cp; | |
265 | ||
266 | level = strtol (sevlevel_var, &cp, 0); | |
267 | if (cp != sevlevel_var && cp < end && *cp++ == ',' | |
268 | && level > MM_INFO) | |
269 | { | |
270 | const char *new_string; | |
271 | ||
272 | new_string = __strndup (cp, end - cp); | |
273 | ||
274 | if (new_string != NULL | |
275 | && (internal_addseverity (level, new_string) | |
276 | != MM_OK)) | |
277 | free ((char *) new_string); | |
278 | } | |
279 | } | |
280 | ||
281 | sevlevel_var = end + (*end == ':' ? 1 : 0); | |
282 | } | |
283 | } | |
284 | } | |
285 | ||
286 | ||
0501d603 UD |
287 | /* Add the new entry to the list. */ |
288 | static int | |
dfd2257a | 289 | internal_function |
0501d603 UD |
290 | internal_addseverity (int severity, const char *string) |
291 | { | |
292 | struct severity_info *runp, *lastp; | |
293 | int result = MM_OK; | |
294 | ||
295 | /* First see if there is already a record for the severity level. */ | |
792dcd77 | 296 | for (runp = severity_list, lastp = NULL; runp != NULL; runp = runp->next) |
0501d603 UD |
297 | if (runp->severity == severity) |
298 | break; | |
299 | else | |
300 | lastp = runp; | |
301 | ||
302 | if (runp != NULL) | |
303 | { | |
0501d603 UD |
304 | if (string != NULL) |
305 | /* Change the string. */ | |
306 | runp->string = string; | |
307 | else | |
308 | { | |
309 | /* Remove the severity class. */ | |
310 | if (lastp == NULL) | |
311 | severity_list = runp->next; | |
312 | else | |
313 | lastp->next = runp->next; | |
314 | ||
315 | free (runp); | |
316 | } | |
317 | } | |
318 | else if (string != NULL) | |
319 | { | |
320 | runp = malloc (sizeof (*runp)); | |
321 | if (runp == NULL) | |
322 | result = MM_NOTOK; | |
323 | else | |
324 | { | |
325 | runp->severity = severity; | |
326 | runp->next = severity_list; | |
327 | runp->string = string; | |
328 | severity_list = runp; | |
329 | } | |
330 | } | |
331 | else | |
332 | /* We tried to remove a non-existing severity class. */ | |
333 | result = MM_NOTOK; | |
334 | ||
335 | return result; | |
336 | } | |
337 | ||
338 | ||
339 | /* Add new severity level or remove old one. */ | |
340 | int | |
341 | addseverity (int severity, const char *string) | |
342 | { | |
343 | int result; | |
0501d603 | 344 | |
a5a0310d UD |
345 | /* Prevent illegal SEVERITY values. */ |
346 | if (severity <= MM_INFO) | |
347 | return MM_NOTOK; | |
348 | ||
0501d603 UD |
349 | /* Protect the global data. */ |
350 | __libc_lock_lock (lock); | |
351 | ||
352 | /* Do the real work. */ | |
353 | result = internal_addseverity (severity, string); | |
354 | ||
0501d603 UD |
355 | /* Release the lock. */ |
356 | __libc_lock_unlock (lock); | |
357 | ||
358 | return result; | |
359 | } | |
a5a0310d UD |
360 | |
361 | ||
c877418f | 362 | libc_freeres_fn (free_mem) |
a5a0310d UD |
363 | { |
364 | struct severity_info *runp = severity_list; | |
365 | ||
366 | while (runp != NULL) | |
367 | if (runp->severity > MM_INFO) | |
368 | { | |
369 | /* This is data we have to release. */ | |
370 | struct severity_info *here = runp; | |
a5a0310d UD |
371 | runp = runp->next; |
372 | free (here); | |
373 | } | |
374 | else | |
375 | runp = runp->next; | |
376 | } |