Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow checking/depending on system services from user init script #770

Open
sevmonster opened this issue Jan 22, 2025 · 5 comments
Open

Comments

@sevmonster
Copy link

sevmonster commented Jan 22, 2025

With the merge of user-mode OpenRC, one thing that becomes evident is that there is no built-in way to check system state from within a user init script. So, for example, there's no way to depend on net, since it and everything it interacts with exists entirely outside of the current OpenRC process and environment.

At least, I haven't seen a way to do it. But I also haven't looked too hard, so if this already exists, I apologize.

I imagine this would be possible to implement manually, by changing/clearing the relevant env vars and running the needed OpenRC commands yourself—but it would be nice if there was a native way to do it.

Maybe this could be implemented with user-mode only depends verbs like needs_system? This would be the most cross-platform but would mean new functions. Could also approach it with prefixes or something, like needs system.net, but this could cause conflict if a service with that prefix already exists.

@navi-desu
Copy link
Member

i've considered this, but it's an issue because user scripts can't start system services, so all we achieve from this is stopping the service from starting all together, which isn't really useful

implementing this would also involve a lot of internal refactoring, so if we decide it's necessary, i'd like to do so in a release after user-script proper

@sevmonster
Copy link
Author

sevmonster commented Feb 22, 2025

i've considered this, but it's an issue because user scripts can't start system services, so all we achieve from this is stopping the service from starting all together, which isn't really useful

You're right in hindsight that a full implementation of depends, needs etc is not super useful. But I do actually think it would be useful to die if certain services aren't running, or at least wait some amount of time for certain system services to start. This makes sense if you launch your user OpenRC as part of your init, as I do. Some interface to do that would be great, otherwise it is still technically possible to poll system OpenRC at fixed intervals in your user script.

It could be implemented in a single interface: wait for service to start for a specified timeout (in rc.conf?) in non-interactive sessions, die immediately with a friendly error message for interactive sessions.

@navi-desu
Copy link
Member

This makes sense if you launch your user OpenRC as part of your init, as I do. Some interface to do that would be great, otherwise it is still technically possible to poll system OpenRC at fixed intervals in your user script.

#777 introduces a user system-wide init script which is meant to address this. what you'd do is multiplex it for your user, user -> user.sevmonster, then add that to a runlevel.

it might be prudent to add after * to it though so we're sure it starts after everything that is supposed to.

@sevmonster
Copy link
Author

sevmonster commented Feb 23, 2025

That's a solution. It would be nice to have the user be able to specify in their scripts what services they want to start after, but simply waiting until the system is fully up is fine too. I actually took a similar approach to that by adding this script to local.d:

#!/bin/sh
# run OpenRC user mode for all users
getent passwd | while read -r x; do
    dir=$(printf %s "$x" | cut -d: -f6)
    rc="$dir/.config/rc"
    # start up OpenRC if special file or rc conf dir is present
    # the file is required if your rc conf dir isn't at the default location
    # TODO: handle this check in the user's shell? if they set XDG_CONFIG_HOME
    [ -d "$rc" -o -e "$dir/.start_user_rc" ] || continue

    # check for valid shell
    shell=$(printf %s "$x" | cut -d: -f7)
    getent shells "$shell" >/dev/null ||
      getent shells $(readlink "$shell") >/dev/null ||
      continue

    # mimic alpine/busybox inittab for OpenRC user mode
    # see also: https://github.com/OpenRC/openrc/blob/master/etc/rc.in
    # XXX: assuming posix-compatible shell
    sudo -niu "$(printf %s "$x" | cut -d: -f1)" "$shell" <<EOF
: \${HOME:=$dir}
export HOME
: \${XDG_RUNTIME_DIR:=~/.local/run}
export XDG_RUNTIME_DIR
[ ! -e "\$XDG_RUNTIME_DIR" ] && /bin/mkdir -p "\$XDG_RUNTIME_DIR"
/sbin/openrc -U sysinit || exit 1
/sbin/openrc -U boot || exit 1
/sbin/openrc -U default
EOF
done
# vim: sts=4 sw=4 et

@navi-desu
Copy link
Member

that works

    # TODO: handle this check in the user's shell? if they set XDG_CONFIG_HOME

#777 addresses this by starting a helper script with pw->pw_shell - -c helper_script

on your script, just be careful about running openrc manually from a logged in session, as elogind or similar might set XDG_RUNTIME_DIR to a different location, and that (having the rundir change during runtime) we don't support, also i'd recommend putting rundir in /tmp and/or a tmpfs so there's no junk in there after a reboot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants