Bug 14240 - `samba --foreground --no-process-group` exits with 127 on SIGTERM
Summary: `samba --foreground --no-process-group` exits with 127 on SIGTERM
Status: NEW
Alias: None
Product: Samba 4.1 and newer
Classification: Unclassified
Component: AD: LDB/DSDB/SAMDB (show other bugs)
Version: 4.11.4
Hardware: All All
: P5 normal (vote)
Target Milestone: ---
Assignee: Andrew Bartlett
QA Contact: Samba QA Contact
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2020-01-17 07:17 UTC by SATOH Fumiyasu
Modified: 2020-03-04 17:56 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description SATOH Fumiyasu 2020-01-17 07:17:05 UTC
On systemd:

```
# systemctl start samba
# systemctl stop samba
# systemctl status --no-pager samba
● samba.service - Samba AD Daemon
   Loaded: loaded (/usr/lib/systemd/system/samba.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Fri 2020-01-17 15:58:29 JST; 16min ago
     Docs: man:samba(8)
           man:samba(7)
           man:smb.conf(5)
  Process: 4535 ExecStart=/opt/samba/sbin/samba --foreground --no-process-group $SAMBAOPTIONS (code=exited, status=127)
 Main PID: 4535 (code=exited, status=127)
   Status: "smbd: ready to serve connections..."

Jan 10 16:15:31 dc1.example.jp systemd[1]: Starting Samba AD Daemon...
Jan 10 16:15:34 dc1.example.jp systemd[1]: Started Samba AD Daemon.
Jan 17 15:58:29 dc1.example.jp systemd[1]: Stopping Samba AD Daemon...
Jan 17 15:58:29 dc1.example.jp systemd[1]: samba.service: Main process exited, code=e…27/n/a
Jan 17 15:58:29 dc1.example.jp systemd[1]: samba.service: Failed with result 'exit-code'.
Jan 17 15:58:29 dc1.example.jp systemd[1]: Stopped Samba AD Daemon.
Hint: Some lines were ellipsized, use -l to show in full.
```

On command-line:

```
# zsh -l
# setopt notify
# /opt/samba/sbin/samba --foreground --no-process-group &
[1] 30827
# kill `cat /opt/samba/var/run/samba/samba.pid`
#
[1]  + exit 127   /opt/samba/sbin/samba --foreground --no-process-group
```

Stopping with smbcontrol is no problem:

```
# zsh -l
# setopt notify
# /opt/samba/sbin/samba --foreground --no-process-group &
[1] 30924
# /opt/samba/bin/smbcontrol `cat /opt/samba/var/run/samba/samba.pid` shutdown
[1]  + done       /opt/samba/sbin/samba --foreground --no-process-group
```
Comment 1 Andrew Bartlett 2020-01-17 20:03:31 UTC
This seems to be what we expect to do, see source4/smbd/server.c:

static void sig_term(int sig)
{
#ifdef HAVE_GETPGRP
	if (getpgrp() == getpid()) {
		/*
		 * We're the process group leader, send
		 * SIGTERM to our process group.
		 */
		kill(-getpgrp(), SIGTERM);
	}
#endif
	_exit(127);
}

static void sigterm_signal_handler(struct tevent_context *ev,
				struct tevent_signal *se,
				int signum, int count, void *siginfo,
				void *private_data)
{
	struct server_state *state = talloc_get_type_abort(
                private_data, struct server_state);

	DBG_DEBUG("Process %s got SIGTERM\n", state->binary_name);
	TALLOC_FREE(state);
	sig_term(SIGTERM);
}

What is the problem with this behaviour?
Comment 2 Andrew Bartlett 2020-01-17 20:05:04 UTC
Is the concern that this is not consistent with exit_server_cleanly() in source3, which does an exit(0)?
Comment 3 SATOH Fumiyasu 2020-01-18 06:17:53 UTC
No problem :-), but we shoud add `SuccessExitStatus=127‘ (or remove `--no-process-group` option?) to samba.service for systemd?
Comment 4 Björn Baumbach 2020-03-04 16:50:54 UTC
(In reply to Andrew Bartlett from comment #2)

The problem is, that systemd typically stops a service with a SIGTERM signal.
When samba exists with 127, then the service results in a failed state:

   Active: failed (Result: exit-code) since Wed 2020-03-04 16:09:06 UTC; 2min 18s ago
   Process: 3500 ExecStart=/usr/sbin/samba --foreground --no-process-group (code=exited, status=127)
 Main PID: 3500 (code=exited, status=127)

This leads to a degraded system state:

# systemctl status | head -4
● bionic-amd64
    State: degraded
     Jobs: 0 queued
   Failed: 1 units

The expected behaviour is exit with 0, which makes the systemd happy:

   Active: inactive (dead) ...

# systemctl status | head -4
● bionic-amd64
    State: running
     Jobs: 0 queued
   Failed: 0 units


It's correct, that we could add the SuccessExitStatus=127 option to sambas systemd service file in order to satisfy systemd, but I would expect the exit code 0, not 127.
Comment 5 Andrew Bartlett 2020-03-04 17:56:29 UTC
(In reply to Björn Baumbach from comment #4)
I agree.  Consistency is really important, and the smbd behaviour has been around longer and is more broadly expected.

Sorry for not clarifying that 'yes, I agree that this is what the code does' does not mean 'I think this should stay that way forever'.