1/* Copyright (C) 1997-2021 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 <https://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
99
100int
101fmtmsg (long int classification, const char *label, int severity,
102 const char *text, const char *action, const char *tag)
103{
104 __libc_once_define (static, once);
105 struct severity_info *severity_rec;
106
107 /* Make sure everything is initialized. */
108 __libc_once (once, init);
109
110 /* Start the real work. First check whether the input is ok. */
111 if (label != MM_NULLLBL)
112 {
113 /* Must be two fields, separated by a colon. */
114 const char *cp = strchr (label, ':');
115 if (cp == NULL)
116 return MM_NOTOK;
117
118 /* The first field must not contain more than 10 bytes. */
119 if (cp - label > 10
120 /* The second field must not have more than 14 bytes. */
121 || strlen (cp + 1) > 14)
122 return MM_NOTOK;
123 }
124
125 /* We do not want this call to be cut short by a thread
126 cancellation. Therefore disable cancellation for now. */
127 int state = PTHREAD_CANCEL_ENABLE;
128 __pthread_setcancelstate (PTHREAD_CANCEL_DISABLE, &state);
129
130 __libc_lock_lock (lock);
131
132 for (severity_rec = severity_list; severity_rec != NULL;
133 severity_rec = severity_rec->next)
134 if (severity == severity_rec->severity)
135 /* Bingo. */
136 break;
137
138 /* If we don't know anything about the severity level return an error. */
139 int result = MM_NOTOK;
140 if (severity_rec != NULL)
141 {
142 result = MM_OK;
143
144 /* Now we can print. */
145 if (classification & MM_PRINT)
146 {
147 int do_label = (print & label_mask) && label != MM_NULLLBL;
148 int do_severity = (print & severity_mask) && severity != MM_NULLSEV;
149 int do_text = (print & text_mask) && text != MM_NULLTXT;
150 int do_action = (print & action_mask) && action != MM_NULLACT;
151 int do_tag = (print & tag_mask) && tag != MM_NULLTAG;
152 int need_colon = (do_label
153 && (do_severity | do_text | do_action | do_tag));
154
155 if (__fxprintf (stderr, "%s%s%s%s%s%s%s%s%s%s\n",
156 do_label ? label : "",
157 need_colon ? ": " : "",
158 do_severity ? severity_rec->string : "",
159 do_severity && (do_text | do_action | do_tag)
160 ? ": " : "",
161 do_text ? text : "",
162 do_text && (do_action | do_tag) ? "\n" : "",
163 do_action ? "TO FIX: " : "",
164 do_action ? action : "",
165 do_action && do_tag ? " " : "",
166 do_tag ? tag : "") < 0)
167 /* Oh, oh. An error occurred during the output. */
168 result = MM_NOMSG;
169 }
170
171 if (classification & MM_CONSOLE)
172 {
173 int do_label = label != MM_NULLLBL;
174 int do_severity = severity != MM_NULLSEV;
175 int do_text = text != MM_NULLTXT;
176 int do_action = action != MM_NULLACT;
177 int do_tag = tag != MM_NULLTAG;
178 int need_colon = (do_label
179 && (do_severity | do_text | do_action | do_tag));
180
181 syslog (LOG_ERR, "%s%s%s%s%s%s%s%s%s%s\n",
182 do_label ? label : "",
183 need_colon ? ": " : "",
184 do_severity ? severity_rec->string : "",
185 do_severity && (do_text | do_action | do_tag) ? ": " : "",
186 do_text ? text : "",
187 do_text && (do_action | do_tag) ? "\n" : "",
188 do_action ? "TO FIX: " : "",
189 do_action ? action : "",
190 do_action && do_tag ? " " : "",
191 do_tag ? tag : "");
192 }
193 }
194
195 __libc_lock_unlock (lock);
196
197 __pthread_setcancelstate (state, NULL);
198
199 return result;
200}
201
202
203/* Initialize from environment variable content. */
204static void
205init (void)
206{
207 const char *msgverb_var = getenv ("MSGVERB");
208 const char *sevlevel_var = getenv ("SEV_LEVEL");
209
210 if (msgverb_var != NULL && msgverb_var[0] != '\0')
211 {
212 /* Using this extra variable allows us to work without locking. */
213 do
214 {
215 size_t cnt;
216
217 for (cnt = 0; cnt < NKEYWORDS; ++cnt)
218 if (memcmp (msgverb_var,
219 keywords[cnt].name, keywords[cnt].len) == 0
220 && (msgverb_var[keywords[cnt].len] == ':'
221 || msgverb_var[keywords[cnt].len] == '\0'))
222 break;
223
224 if (cnt < NKEYWORDS)
225 {
226 print |= 1 << cnt;
227
228 msgverb_var += keywords[cnt].len;
229 if (msgverb_var[0] == ':')
230 ++msgverb_var;
231 }
232 else
233 {
234 /* We found an illegal keyword in the environment
235 variable. The specifications say that we print all
236 fields. */
237 print = all_mask;
238 break;
239 }
240 }
241 while (msgverb_var[0] != '\0');
242 }
243 else
244 print = all_mask;
245
246
247 if (sevlevel_var != NULL)
248 {
249 __libc_lock_lock (lock);
250
251 while (sevlevel_var[0] != '\0')
252 {
253 const char *end = __strchrnul (sevlevel_var, ':');
254 int level;
255
256 /* First field: keyword. This is not used here but it must be
257 present. */
258 while (sevlevel_var < end)
259 if (*sevlevel_var++ == ',')
260 break;
261
262 if (sevlevel_var < end)
263 {
264 /* Second field: severity level, a number. */
265 char *cp;
266
267 level = strtol (sevlevel_var, &cp, 0);
268 if (cp != sevlevel_var && cp < end && *cp++ == ','
269 && level > MM_INFO)
270 {
271 const char *new_string;
272
273 new_string = __strndup (cp, end - cp);
274
275 if (new_string != NULL
276 && (internal_addseverity (level, new_string)
277 != MM_OK))
278 free ((char *) new_string);
279 }
280 }
281
282 sevlevel_var = end + (*end == ':' ? 1 : 0);
283 }
284
285 __libc_lock_unlock (lock);
286 }
287}
288
289
290/* Add the new entry to the list. */
291static int
292internal_addseverity (int severity, const char *string)
293{
294 struct severity_info *runp, *lastp;
295 int result = MM_OK;
296
297 /* First see if there is already a record for the severity level. */
298 for (runp = severity_list, lastp = NULL; runp != NULL; runp = runp->next)
299 if (runp->severity == severity)
300 break;
301 else
302 lastp = runp;
303
304 if (runp != NULL)
305 {
306 if (string != NULL)
307 /* Change the string. */
308 runp->string = string;
309 else
310 {
311 /* Remove the severity class. */
312 if (lastp == NULL)
313 severity_list = runp->next;
314 else
315 lastp->next = runp->next;
316
317 free (runp);
318 }
319 }
320 else if (string != NULL)
321 {
322 runp = malloc (sizeof (*runp));
323 if (runp == NULL)
324 result = MM_NOTOK;
325 else
326 {
327 runp->severity = severity;
328 runp->next = severity_list;
329 runp->string = string;
330 severity_list = runp;
331 }
332 }
333 else
334 /* We tried to remove a non-existing severity class. */
335 result = MM_NOTOK;
336
337 return result;
338}
339
340
341/* Add new severity level or remove old one. */
342int
343__addseverity (int severity, const char *string)
344{
345 int result;
346
347 /* Prevent illegal SEVERITY values. */
348 if (severity <= MM_INFO)
349 return MM_NOTOK;
350
351 /* Protect the global data. */
352 __libc_lock_lock (lock);
353
354 /* Do the real work. */
355 result = internal_addseverity (severity, string);
356
357 /* Release the lock. */
358 __libc_lock_unlock (lock);
359
360 return result;
361}
362weak_alias (__addseverity, addseverity)
363
364
365libc_freeres_fn (free_mem)
366{
367 struct severity_info *runp = severity_list;
368
369 while (runp != NULL)
370 if (runp->severity > MM_INFO)
371 {
372 /* This is data we have to release. */
373 struct severity_info *here = runp;
374 runp = runp->next;
375 free (here);
376 }
377 else
378 runp = runp->next;
379}
380