Skip to content

Commit a98f757

Browse files
xginn8poettering
authored andcommitted
Add counter for socket unit refuse events (systemd#9217)
core: add counter for socket unit rejection events
1 parent c602fd0 commit a98f757

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

src/core/dbus-socket.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ const sd_bus_vtable bus_socket_vtable[] = {
125125
SD_BUS_PROPERTY("Result", "s", property_get_result, offsetof(Socket, result), SD_BUS_VTABLE_PROPERTY_EMITS_CHANGE),
126126
SD_BUS_PROPERTY("NConnections", "u", bus_property_get_unsigned, offsetof(Socket, n_connections), 0),
127127
SD_BUS_PROPERTY("NAccepted", "u", bus_property_get_unsigned, offsetof(Socket, n_accepted), 0),
128+
SD_BUS_PROPERTY("NRefused", "u", bus_property_get_unsigned, offsetof(Socket, n_refused), 0),
128129
SD_BUS_PROPERTY("FileDescriptorName", "s", property_get_fdname, 0, 0),
129130
SD_BUS_PROPERTY("SocketProtocol", "i", bus_property_get_int, offsetof(Socket, socket_protocol), SD_BUS_VTABLE_PROPERTY_CONST),
130131
SD_BUS_PROPERTY("TriggerLimitIntervalUSec", "t", bus_property_get_usec, offsetof(Socket, trigger_limit.interval), SD_BUS_VTABLE_PROPERTY_CONST),

src/core/socket.c

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,18 +2243,17 @@ static void socket_enter_running(Socket *s, int cfd) {
22432243
log_unit_debug(UNIT(s), "Suppressing connection request since unit stop is scheduled.");
22442244

22452245
if (cfd >= 0)
2246-
cfd = safe_close(cfd);
2246+
goto refuse;
22472247
else
22482248
flush_ports(s);
22492249

22502250
return;
22512251
}
22522252

22532253
if (!ratelimit_below(&s->trigger_limit)) {
2254-
safe_close(cfd);
22552254
log_unit_warning(UNIT(s), "Trigger limit hit, refusing further activation.");
22562255
socket_enter_stop_pre(s, SOCKET_FAILURE_TRIGGER_LIMIT_HIT);
2257-
return;
2256+
goto refuse;
22582257
}
22592258

22602259
if (cfd < 0) {
@@ -2292,15 +2291,13 @@ static void socket_enter_running(Socket *s, int cfd) {
22922291
if (s->n_connections >= s->max_connections) {
22932292
log_unit_warning(UNIT(s), "Too many incoming connections (%u), dropping connection.",
22942293
s->n_connections);
2295-
safe_close(cfd);
2296-
return;
2294+
goto refuse;
22972295
}
22982296

22992297
if (s->max_connections_per_source > 0) {
23002298
r = socket_acquire_peer(s, cfd, &p);
23012299
if (r < 0) {
2302-
safe_close(cfd);
2303-
return;
2300+
goto refuse;
23042301
} else if (r > 0 && p->n_ref > s->max_connections_per_source) {
23052302
_cleanup_free_ char *t = NULL;
23062303

@@ -2309,8 +2306,7 @@ static void socket_enter_running(Socket *s, int cfd) {
23092306
log_unit_warning(UNIT(s),
23102307
"Too many incoming connections (%u) from source %s, dropping connection.",
23112308
p->n_ref, strnull(t));
2312-
safe_close(cfd);
2313-
return;
2309+
goto refuse;
23142310
}
23152311
}
23162312

@@ -2326,8 +2322,7 @@ static void socket_enter_running(Socket *s, int cfd) {
23262322
/* ENOTCONN is legitimate if TCP RST was received.
23272323
* This connection is over, but the socket unit lives on. */
23282324
log_unit_debug(UNIT(s), "Got ENOTCONN on incoming socket, assuming aborted connection attempt, ignoring.");
2329-
safe_close(cfd);
2330-
return;
2325+
goto refuse;
23312326
}
23322327

23332328
r = unit_name_to_prefix(UNIT(s)->id, &prefix);
@@ -2371,6 +2366,11 @@ static void socket_enter_running(Socket *s, int cfd) {
23712366

23722367
return;
23732368

2369+
refuse:
2370+
s->n_refused++;
2371+
safe_close(cfd);
2372+
return;
2373+
23742374
fail:
23752375
log_unit_warning(UNIT(s), "Failed to queue service startup job (Maybe the service file is missing or not a %s unit?): %s",
23762376
cfd >= 0 ? "template" : "non-template",
@@ -2514,6 +2514,7 @@ static int socket_serialize(Unit *u, FILE *f, FDSet *fds) {
25142514
unit_serialize_item(u, f, "state", socket_state_to_string(s->state));
25152515
unit_serialize_item(u, f, "result", socket_result_to_string(s->result));
25162516
unit_serialize_item_format(u, f, "n-accepted", "%u", s->n_accepted);
2517+
unit_serialize_item_format(u, f, "n-refused", "%u", s->n_refused);
25172518

25182519
if (s->control_pid > 0)
25192520
unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
@@ -2594,6 +2595,13 @@ static int socket_deserialize_item(Unit *u, const char *key, const char *value,
25942595
log_unit_debug(u, "Failed to parse n-accepted value: %s", value);
25952596
else
25962597
s->n_accepted += k;
2598+
} else if (streq(key, "n-refused")) {
2599+
unsigned k;
2600+
2601+
if (safe_atou(value, &k) < 0)
2602+
log_unit_debug(u, "Failed to parse n-refused value: %s", value);
2603+
else
2604+
s->n_refused += k;
25972605
} else if (streq(key, "control-pid")) {
25982606
pid_t pid;
25992607

src/core/socket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ struct Socket {
7373

7474
unsigned n_accepted;
7575
unsigned n_connections;
76+
unsigned n_refused;
7677
unsigned max_connections;
7778
unsigned max_connections_per_source;
7879

src/systemctl/systemctl.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3890,6 +3890,7 @@ typedef struct UnitStatusInfo {
38903890
/* Socket */
38913891
unsigned n_accepted;
38923892
unsigned n_connections;
3893+
unsigned n_refused;
38933894
bool accept;
38943895

38953896
/* Pairs of type, path */
@@ -4155,8 +4156,13 @@ static void print_status_info(
41554156
STRV_FOREACH_PAIR(t, t2, i->listen)
41564157
printf(" %*s %s (%s)\n", 9, t == i->listen ? "Listen:" : "", *t2, *t);
41574158

4158-
if (i->accept)
4159-
printf(" Accepted: %u; Connected: %u\n", i->n_accepted, i->n_connections);
4159+
if (i->accept) {
4160+
printf(" Accepted: %u; Connected: %u;", i->n_accepted, i->n_connections);
4161+
if (i->n_refused)
4162+
printf(" Refused: %u", i->n_refused);
4163+
printf("\n");
4164+
}
4165+
41604166

41614167
LIST_FOREACH(exec, p, i->exec) {
41624168
_cleanup_free_ char *argv = NULL;
@@ -4956,6 +4962,7 @@ static int show_one(
49564962
{ "NextElapseUSecMonotonic", "t", NULL, offsetof(UnitStatusInfo, next_elapse_monotonic) },
49574963
{ "NAccepted", "u", NULL, offsetof(UnitStatusInfo, n_accepted) },
49584964
{ "NConnections", "u", NULL, offsetof(UnitStatusInfo, n_connections) },
4965+
{ "NRefused", "u", NULL, offsetof(UnitStatusInfo, n_refused) },
49594966
{ "Accept", "b", NULL, offsetof(UnitStatusInfo, accept) },
49604967
{ "Listen", "a(ss)", map_listen, offsetof(UnitStatusInfo, listen) },
49614968
{ "SysFSPath", "s", NULL, offsetof(UnitStatusInfo, sysfs_path) },

0 commit comments

Comments
 (0)