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