Skip to content

Commit 57b0864

Browse files
committed
refactor: break down long functions into focused helpers
Refactored five functions >50 lines by extracting focused helper functions, improving code readability and maintainability: 1. netlog-journal.c: journal_read_input() (97 → ~60 lines) - Extracted parse_journal_fields(), parse_syslog_severity(), parse_syslog_facility() 2. netlog-protocol.c: format_rfc5424() (103 → ~50 lines) - Extracted set_priority_version_field(), set_timestamp_field(), set_string_field_with_separator(), set_structured_data_field(), set_message_length_field() 3. netlog-protocol.c: format_rfc3339() (67 → ~52 lines) - Extracted set_priority_field() - Reused set_timestamp_field() 4. systemd-netlogd.c: main() (86 → ~53 lines) - Extracted initialize_logging(), resolve_user_credentials(), initialize_ssl_manager(), run_event_loop() 5. netlog-ssl.c: ssl_verify_certificate_validity() (66 → ~36 lines) - Extracted extract_certificate_names(), check_certificate_chain_depth(), determine_verification_log_level() All tests passing. No functional changes.
1 parent 5c620ec commit 57b0864

4 files changed

Lines changed: 300 additions & 220 deletions

File tree

src/netlog/netlog-journal.c

Lines changed: 101 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -88,28 +88,103 @@ static int parse_fieldv(
8888
return 0;
8989
}
9090

91+
static int parse_journal_fields(Manager *m,
92+
char **message,
93+
char **identifier,
94+
char **hostname,
95+
char **pid,
96+
char **facility,
97+
char **priority,
98+
char **structured_data,
99+
char **msgid) {
100+
const void *data;
101+
size_t length;
102+
int r;
103+
size_t hostname_len = 0, identifier_len = 0, message_len = 0, priority_len = 0, facility_len = 0,
104+
structured_data_len = 0, msgid_len = 0, pid_len = 0;
105+
const ParseFieldVec fields[] = {
106+
PARSE_FIELD_VEC_ENTRY("_PID=", pid, &pid_len ),
107+
PARSE_FIELD_VEC_ENTRY("MESSAGE=", message, &message_len ),
108+
PARSE_FIELD_VEC_ENTRY("PRIORITY=", priority, &priority_len ),
109+
PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", hostname, &hostname_len ),
110+
PARSE_FIELD_VEC_ENTRY("SYSLOG_FACILITY=", facility, &facility_len ),
111+
PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", identifier, &identifier_len ),
112+
PARSE_FIELD_VEC_ENTRY("SYSLOG_STRUCTURED_DATA=", structured_data, &structured_data_len ),
113+
PARSE_FIELD_VEC_ENTRY("SYSLOG_MSGID", msgid, &msgid_len ),
114+
};
115+
116+
JOURNAL_FOREACH_DATA_RETVAL(m->journal, data, length, r) {
117+
r = parse_fieldv(data, length, fields, ELEMENTSOF(fields));
118+
if (r < 0)
119+
return r;
120+
}
121+
122+
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
123+
log_debug_errno(r, "Skipping message we can't read: %m");
124+
return 0;
125+
}
126+
127+
return r;
128+
}
129+
130+
static int parse_syslog_severity(Manager *m, const char *priority, unsigned *sev) {
131+
int r;
132+
133+
assert(sev);
134+
135+
if (!priority)
136+
return 0;
137+
138+
r = safe_atou(priority, sev);
139+
if (r < 0) {
140+
log_debug("Failed to parse syslog priority: %s", priority);
141+
return r;
142+
}
143+
144+
if (*sev < _SYSLOG_LEVEL_MAX && ((UINT8_C(1) << *sev) & m->excluded_syslog_levels)) {
145+
log_debug("Skipping message with excluded syslog level %s.", syslog_level_to_string(*sev));
146+
return 1; /* filtered */
147+
}
148+
149+
if (*sev > LOG_DEBUG)
150+
*sev = JOURNAL_DEFAULT_SEVERITY;
151+
152+
return 0;
153+
}
154+
155+
static int parse_syslog_facility(Manager *m, const char *facility, unsigned *fac) {
156+
int r;
157+
158+
assert(fac);
159+
160+
if (!facility)
161+
return 0;
162+
163+
r = safe_atou(facility, fac);
164+
if (r < 0) {
165+
log_debug("Failed to parse syslog facility: %s", facility);
166+
return r;
167+
}
168+
169+
if (*fac < _SYSLOG_FACILITY_MAX && ((UINT32_C(1) << *fac) & m->excluded_syslog_facilities)) {
170+
log_debug("Skipping message with excluded syslog facility %s.", syslog_facility_to_string(*fac));
171+
return 1; /* filtered */
172+
}
173+
174+
if (*fac >= LOG_NFACILITIES)
175+
*fac = JOURNAL_DEFAULT_FACILITY;
176+
177+
return 0;
178+
}
179+
91180
static int journal_read_input(Manager *m) {
92181
_cleanup_free_ char *facility = NULL, *identifier = NULL, *priority = NULL, *message = NULL, *pid = NULL,
93182
*hostname = NULL, *structured_data = NULL, *msgid = NULL, *cursor = NULL;
94-
size_t hostname_len = 0, identifier_len = 0, message_len = 0, priority_len = 0, facility_len = 0,
95-
structured_data_len = 0, msgid_len = 0, pid_len = 0;
96183
unsigned sev = JOURNAL_DEFAULT_SEVERITY;
97184
unsigned fac = JOURNAL_DEFAULT_FACILITY;
98185
struct timeval tv, *tvp = NULL;
99-
const void *data;
100186
usec_t realtime;
101-
size_t length;
102187
int r;
103-
const ParseFieldVec fields[] = {
104-
PARSE_FIELD_VEC_ENTRY("_PID=", &pid, &pid_len ),
105-
PARSE_FIELD_VEC_ENTRY("MESSAGE=", &message, &message_len ),
106-
PARSE_FIELD_VEC_ENTRY("PRIORITY=", &priority, &priority_len ),
107-
PARSE_FIELD_VEC_ENTRY("_HOSTNAME=", &hostname, &hostname_len ),
108-
PARSE_FIELD_VEC_ENTRY("SYSLOG_FACILITY=", &facility, &facility_len ),
109-
PARSE_FIELD_VEC_ENTRY("SYSLOG_IDENTIFIER=", &identifier, &identifier_len ),
110-
PARSE_FIELD_VEC_ENTRY("SYSLOG_STRUCTURED_DATA=", &structured_data, &structured_data_len ),
111-
PARSE_FIELD_VEC_ENTRY("SYSLOG_MSGID", &msgid, &msgid_len ),
112-
};
113188

114189
assert(m);
115190
assert(m->journal);
@@ -120,24 +195,18 @@ static int journal_read_input(Manager *m) {
120195

121196
log_debug("Reading from journal cursor=%s", cursor);
122197

123-
JOURNAL_FOREACH_DATA_RETVAL(m->journal, data, length, r) {
124-
r = parse_fieldv(data, length, fields, ELEMENTSOF(fields));
125-
if (r < 0)
126-
return r;
127-
}
128-
129-
if (IN_SET(r, -EBADMSG, -EADDRNOTAVAIL)) {
130-
log_debug_errno(r, "Skipping message we can't read: %m");
131-
return 0;
132-
}
198+
r = parse_journal_fields(m, &message, &identifier, &hostname, &pid, &facility, &priority, &structured_data, &msgid);
133199
if (r < 0)
134200
return log_error_errno(r, "Failed to get journal fields: %m");
201+
if (r == 0)
202+
return 0;
135203

136204
if (!message) {
137205
log_debug("Skipping message without MESSAGE= field.");
138206
return 0;
139-
} else
140-
log_debug("Received from journal MESSAGE='%s'", message);
207+
}
208+
209+
log_debug("Received from journal MESSAGE='%s'", message);
141210

142211
r = sd_journal_get_realtime_usec(m->journal, &realtime);
143212
if (r < 0)
@@ -150,31 +219,13 @@ static int journal_read_input(Manager *m) {
150219
tvp = &tv;
151220
}
152221

153-
if (facility) {
154-
r = safe_atou(facility, &fac);
155-
if (r < 0)
156-
log_debug("Failed to parse syslog facility: %s", facility);
157-
else if (fac < _SYSLOG_FACILITY_MAX && ((UINT32_C(1) << fac) & m->excluded_syslog_facilities)) {
158-
log_debug("Skipping message with excluded syslog facility %s.", syslog_facility_to_string(fac));
159-
return 0;
160-
}
161-
162-
if (fac >= LOG_NFACILITIES)
163-
fac = JOURNAL_DEFAULT_FACILITY;
164-
}
165-
166-
if (priority) {
167-
r = safe_atou(priority, &sev);
168-
if (r < 0)
169-
log_debug("Failed to parse syslog priority: %s", priority);
170-
else if (sev < _SYSLOG_LEVEL_MAX && ((UINT8_C(1) << sev) & m->excluded_syslog_levels)) {
171-
log_debug("Skipping message with excluded syslog level %s.", syslog_level_to_string(sev));
172-
return 0;
173-
}
222+
r = parse_syslog_facility(m, facility, &fac);
223+
if (r > 0) /* filtered */
224+
return 0;
174225

175-
if (sev > LOG_DEBUG)
176-
sev = JOURNAL_DEFAULT_SEVERITY;
177-
}
226+
r = parse_syslog_severity(m, priority, &sev);
227+
if (r > 0) /* filtered */
228+
return 0;
178229

179230
return manager_push_to_network(m,
180231
sev,

0 commit comments

Comments
 (0)