@@ -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+
23742374fail :
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
0 commit comments