Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27// Has to be defined before including due to legacy Unices
28#define SYSLOG_NAMES 1
29
30#include <AK/String.h>
31#include <AK/StringBuilder.h>
32#include <stdio.h>
33#include <string.h>
34#include <syslog.h>
35#include <unistd.h>
36
37// This implementation doesn't talk to a syslog server. Any options related to
38// that are no-ops.
39
40extern "C" {
41
42// For implementation simplicity, we actually only use the re-entrant version
43// of each function, and the version that isn't just redirects with a static
44// struct to share.
45static struct syslog_data global_log_data = {
46 .ident = nullptr,
47 .logopt = 0,
48 .facility = LOG_USER,
49 .maskpri = LOG_UPTO(LOG_DEBUG)
50};
51
52// Used when ident is null, since syslog traditionally prints the program's
53// own name; the process name will always be the same unless we exec.
54static char program_name_buffer[256];
55static bool program_name_set = false;
56
57// Convenience function for initialization and checking what string to use
58// for the program name.
59static const char* get_syslog_ident(struct syslog_data* data)
60{
61 if (!program_name_set && data->ident == nullptr)
62 program_name_set = get_process_name(program_name_buffer, sizeof(program_name_buffer)) >= 0;
63
64 if (data->ident != nullptr)
65 return data->ident;
66 else if (program_name_set)
67 return program_name_buffer;
68
69 ASSERT_NOT_REACHED();
70}
71
72void openlog_r(const char* ident, int logopt, int facility, struct syslog_data* data)
73{
74 data->ident = ident;
75 data->logopt = logopt;
76 data->facility = facility;
77 // default value
78 data->maskpri = LOG_UPTO(LOG_DEBUG);
79 // would be where we connect to a daemon
80}
81
82void openlog(const char* ident, int logopt, int facility)
83{
84 openlog_r(ident, logopt, facility, &global_log_data);
85}
86
87void closelog_r(struct syslog_data* data)
88{
89 // would be where we disconnect from a daemon
90 // restore defaults
91 data->ident = nullptr;
92 data->logopt = 0;
93 data->facility = LOG_USER;
94 data->maskpri = LOG_UPTO(LOG_DEBUG);
95}
96
97void closelog(void)
98{
99 closelog_r(&global_log_data);
100}
101
102int setlogmask_r(int maskpri, struct syslog_data* data)
103{
104 // Remember, this takes the input of LOG_MASK/LOG_UPTO
105 int old_maskpri = data->maskpri;
106 data->maskpri = maskpri;
107 return old_maskpri;
108}
109
110int setlogmask(int maskpri)
111{
112 return setlogmask_r(maskpri, &global_log_data);
113}
114
115void syslog_r(int priority, struct syslog_data* data, const char* message, ...)
116{
117 va_list ap;
118 va_start(ap, message);
119 vsyslog_r(priority, data, message, ap);
120 va_end(ap);
121}
122
123void syslog(int priority, const char* message, ...)
124{
125 va_list ap;
126 va_start(ap, message);
127 vsyslog_r(priority, &global_log_data, message, ap);
128 va_end(ap);
129}
130
131void vsyslog_r(int priority, struct syslog_data* data, const char* message, va_list args)
132{
133 StringBuilder combined;
134
135 int real_priority = LOG_PRI(priority);
136 // Lots of parens, but it just extracts the priority from combo and masks.
137 if (!(data->maskpri & LOG_MASK(real_priority)))
138 return;
139
140 // Some metadata would be consumed by a syslog daemon, if we had one.
141 if (data->logopt & LOG_PID)
142 combined.appendf("%s[%d]: ", get_syslog_ident(data), getpid());
143 else
144 combined.appendf("%s: ", get_syslog_ident(data));
145
146 combined.appendvf(message, args);
147 String combined_string = combined.build();
148
149 if (data->logopt & LOG_CONS)
150 dbgputstr(combined_string.characters(), combined_string.length());
151 if (data->logopt & LOG_PERROR)
152 fputs(combined_string.characters(), stderr);
153}
154
155void vsyslog(int priority, const char* message, va_list args)
156{
157 vsyslog_r(priority, &global_log_data, message, args);
158}
159}