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