Get a trace file descriptor from "key" env variable. */
| 36 | |
| 37 | /* Get a trace file descriptor from "key" env variable. */ |
| 38 | static int get_trace_fd(struct trace_key *key, const char *override_envvar) |
| 39 | { |
| 40 | const char *trace; |
| 41 | |
| 42 | /* don't open twice */ |
| 43 | if (key->initialized) |
| 44 | return key->fd; |
| 45 | |
| 46 | trace = override_envvar ? override_envvar : getenv(key->key); |
| 47 | |
| 48 | if (!trace || !strcmp(trace, "") || |
| 49 | !strcmp(trace, "0") || !strcasecmp(trace, "false")) |
| 50 | key->fd = 0; |
| 51 | else if (!strcmp(trace, "1") || !strcasecmp(trace, "true")) |
| 52 | key->fd = STDERR_FILENO; |
| 53 | else if (strlen(trace) == 1 && isdigit(*trace)) |
| 54 | key->fd = atoi(trace); |
| 55 | else if (is_absolute_path(trace)) { |
| 56 | int fd = open(trace, O_WRONLY | O_APPEND | O_CREAT, 0666); |
| 57 | if (fd == -1) { |
| 58 | warning("could not open '%s' for tracing: %s", |
| 59 | trace, strerror(errno)); |
| 60 | trace_disable(key); |
| 61 | } else { |
| 62 | key->fd = fd; |
| 63 | key->need_close = 1; |
| 64 | } |
| 65 | } else { |
| 66 | warning("unknown trace value for '%s': %s\n" |
| 67 | " If you want to trace into a file, then please set %s\n" |
| 68 | " to an absolute pathname (starting with /)", |
| 69 | key->key, trace, key->key); |
| 70 | trace_disable(key); |
| 71 | } |
| 72 | |
| 73 | key->initialized = 1; |
| 74 | return key->fd; |
| 75 | } |
| 76 | |
| 77 | void trace_override_envvar(struct trace_key *key, const char *value) |
| 78 | { |
no test coverage detected