swaywm / swaylock

Screen locker for Wayland
MIT License
853 stars 202 forks source link

Bug/Feature Request: Exit Code documentation/implementation #284

Open cyntheticfox opened 1 year ago

cyntheticfox commented 1 year ago

As written, swaylock will exit one of four different values: 0, 1, 2, EXIT_SUCCESS, and EXIT_FAILURE, where the last two are undefined POSIX values that I believe GNU/Linux, BSD, and LLVM map to 0 and 1 respectively (supposedly VMS does something else, and EXIT_FAILURE could be -1 according to the FreeBSD manpage).

So far as I can tell, 2 is used when unable to lock with the legacy wayland API due to that lock being held (likely by another lockscreen), the display dispatch failing, and readiness notification failures.

0 being used as "success" in the sense of daemonization (hopefully) checking, no caught errors (excepting config reads... which always succeed) and PAM/shadow success.

Everything else (all errors) maps to 1.

Can we possibly get this documented in the manpage?

emersion commented 1 year ago

Ideally we'd pick more sensible/consistent exit codes while at it…

cyntheticfox commented 1 year ago

Looking through a few wikis, manpages, and POSIX specs, besides C only allowing an 8-bit integer return and POSIX only looking at 8 bits in most cases anyways, there's only really two common semantics for use beyond 0 and 1:

I personally like the set of error codes that BSD defines, but it really just needs to be documented, whatever is used -- likely defined as macros in some header file. The main point of exit codes is for a calling program to know the exit status anyways, not really humans (that's what we have logs and stderr for).

For what's already used for errors, I can see classifying them (and so giving different error codes) in at least these ways:

Broadly, they're probably just:

Also is that a stray semicolon in pam.c? I'm not super familiar with the undefined C behavior stuff.

EDIT: Had to fix the file links. Works differently than I expected

emersion commented 1 year ago

I'd just use 1 everywhere tbh. No good reason why users would want to tell apart these.

cyntheticfox commented 1 year ago

Most of the time, yes, the specific exit code isn't important. The main time you really need it though is when you fork/manage it via another process.

My particular need is for running the application as a Systemd (oneshot) service, where I need to differentiate why a program exited -- so that for certain types of errors, the program is restarted, but isn't for other, more critical ones.

If nothing else, it could be set to EXIT_FAILURE, with some sort of advanced expectation of the executor to read the stderr to determine it's behavior. As far as I know, there's currently not any one approach taken (varying exit codes, fail-safe behavior in some cases, no error logged in others)

cyntheticfox commented 1 year ago

Whichever strategy is preferable, I can probably implement it in a PR myself if you'd like