@@ -407,7 +407,8 @@ static int parse_options (pam_handle_t *pamh,
407407 * The service is started by the Flux prolog when a job begins and stopped
408408 * by housekeeping when the last job ends, so inactive means no active job.
409409 *
410- * Returns 0 if service is running (ActiveState=active, SubState=running).
410+ * Returns 0 if service is active or activating (mirrors systemd's
411+ * UNIT_IS_ACTIVE_OR_ACTIVATING macro).
411412 * Returns -1 if service is not running or an error occurred with specific
412413 * reason set in errmsg
413414 */
@@ -423,7 +424,6 @@ static int check_user_service_active (pam_handle_t *pamh,
423424 const char * unit_path_raw = NULL ;
424425 char * unit_path = NULL ;
425426 char * active_state = NULL ;
426- char * sub_state = NULL ;
427427 int rc = -1 ;
428428
429429 * errmsg = "Unable to determine unit state" ;
@@ -507,29 +507,17 @@ static int check_user_service_active (pam_handle_t *pamh,
507507 }
508508 sd_bus_error_free (& error );
509509
510- /* Get SubState property.
510+ /* Mirror systemd's own UNIT_IS_ACTIVE_OR_ACTIVATING macro when
511+ * checking for an active or activating user@UID service:
511512 */
512- if (sd_bus_get_property_string (bus ,
513- "org.freedesktop.systemd1" ,
514- unit_path ,
515- "org.freedesktop.systemd1.Unit" ,
516- "SubState" ,
517- & error ,
518- & sub_state ) < 0 ) {
519- pam_syslog (pamh ,
520- LOG_ERR ,
521- "failed to get SubState for %s: %s" ,
522- unit_name ,
523- error .message ? error .message : "unknown error" );
524- goto out ;
525- }
526-
527513 if (strcmp (active_state , "active" ) == 0
528- && strcmp (sub_state , "running" ) == 0 ) {
514+ || strcmp (active_state , "activating" ) == 0
515+ || strcmp (active_state , "reloading" ) == 0
516+ || strcmp (active_state , "refreshing" ) == 0 ) {
529517 if (debug )
530518 pam_syslog (pamh ,
531519 LOG_INFO ,
532- "%s is active" ,
520+ "%s is active or activating " ,
533521 unit_name );
534522 rc = 0 ;
535523 }
@@ -540,18 +528,16 @@ static int check_user_service_active (pam_handle_t *pamh,
540528 */
541529 pam_syslog (pamh ,
542530 LOG_INFO ,
543- "%s not running : ActiveState=%s SubState =%s" ,
531+ "%s not active or activating : ActiveState=%s" ,
544532 unit_name ,
545- active_state ,
546- sub_state );
547- * errmsg = "unit not running" ;
533+ active_state );
534+ * errmsg = "unit not active or activating" ;
548535 rc = -1 ;
549536 }
550537
551538out :
552539 free (unit_path );
553540 free (active_state );
554- free (sub_state );
555541 sd_bus_message_unref (reply );
556542 sd_bus_error_free (& error );
557543 sd_bus_unref (bus );
@@ -768,6 +754,7 @@ PAM_EXTERN int
768754pam_sm_acct_mgmt (pam_handle_t * pamh , int flags , int argc , const char * * argv )
769755{
770756 const char * user ;
757+ const char * service = NULL ;
771758 uid_t uid ;
772759 int auth = PAM_PERM_DENIED ;
773760 flux_auth_t result ;
@@ -779,6 +766,17 @@ pam_sm_acct_mgmt (pam_handle_t *pamh, int flags, int argc, const char **argv)
779766 if (parse_options (pamh , & opts , argc , argv ) < 0 )
780767 return PAM_SYSTEM_ERR ;
781768
769+ /* Skip systemd-user service - it's starting user@UID.service itself.
770+ * Checking the slice from within that service's own startup is circular
771+ * and not meaningful.
772+ */
773+ pam_get_item (pamh , PAM_SERVICE , (const void * * ) & service );
774+ if (service && strcmp (service , "systemd-user" ) == 0 ) {
775+ if (opts .debug )
776+ pam_syslog (pamh , LOG_INFO , "skipping for systemd-user service" );
777+ return PAM_IGNORE ;
778+ }
779+
782780 result = flux_check_user (pamh , & opts , uid );
783781 if (result != FLUX_AUTH_DENIED ) {
784782 /* User has a local job or allow-guest-user is true. In either case
@@ -824,6 +822,7 @@ pam_sm_open_session (pam_handle_t *pamh,
824822{
825823 uid_t uid ;
826824 const char * user ;
825+ const char * service = NULL ;
827826 const void * pam_flux_authorized = NULL ;
828827 int manage_slice ;
829828 struct options opts = {
@@ -836,6 +835,17 @@ pam_sm_open_session (pam_handle_t *pamh,
836835 if (parse_options (pamh , & opts , argc , argv ) < 0 )
837836 return PAM_SESSION_ERR ;
838837
838+ /* Skip systemd-user service - it's starting user@UID.service itself.
839+ * Creating a scope under user-UID.slice from within that service's own
840+ * startup is circular and not meaningful.
841+ */
842+ pam_get_item (pamh , PAM_SERVICE , (const void * * ) & service );
843+ if (service && strcmp (service , "systemd-user" ) == 0 ) {
844+ if (opts .debug )
845+ pam_syslog (pamh , LOG_INFO , "skipping for systemd-user service" );
846+ return PAM_IGNORE ;
847+ }
848+
839849 /* Session management decision table:
840850 *
841851 * pam_flux_authorized is a sentinel set by pam_sm_acct_mgmt when it
0 commit comments