3

My laptop has suspend capability:

# cat /sys/power/state
freeze mem

and indeed I can suspend as root:

echo -n mem > /sys/power/state

This works fine. But what would be a minimalist way to enable user (non-root) to suspend ?

I am using Debian Buster, but I don't have systemd installed (I am using old style sysvinit). And I don't have udev daemon running either (I am using the "kernel space" udev).

I would like to achieve user suspend capability with minimal additional packages required.

On earlier Debian version (Wheezy), I could install:

  • pm-utils
  • upower

and then issue a command via dbus. I think it was something like this:

dbus-send --system --print-reply --dest="org.freedesktop.UPower" /org/freedesktop/UPower org.freedesktop.UPower.Suspend

this no longer works. Now I have read that upower has changed and the curent verison in Debian 0.99.10 is not supposed to work. but I installed upower from wheezy (0.9.17), just to test it, but that does not work either. The dbus-send command gives me this error:

Error org.freedesktop.DBus.Error.Spawn.ChildExited: Launch helper exited with unknown return code 127

But using pm-utils, upower and dbus might not be the simplest setup anyway. What is actually the purpose of pm-utils of upower? Those utilities only work for root. But as root, I already can suspend with echo -n mem > /sys/power/state. What do those utilities bring extra?

I see pm-utils installs lots of various scripts. What else do I need to handle besides issuing echo -n mem > /sys/power/state ?

And how could I achieve all that as user ?

Perhaps writing a simple C program that writes mem to /sys/power/state, and set the s bit same as for instance /usr/bin/passwd has.

Would this be a workable and clean solution? Or would this be considered a "dirty" solution? Are there better solutions?

What else do I need to handle, besides writing mem to /sys/power/state?

Do I need to ifdown and ifup the network ?

400 the Cat
  • 819
  • 4
  • 37
  • 85

2 Answers2

2

You can write a simple program in c:

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {

    FILE *f;

        f = fopen("/sys/power/state", "w");

    if(f == NULL) {
        printf("Error opening file: /sys/power/state\n");   
        exit(1);             
    }

    fprintf(f,"%s","mem");

    fclose(f);

}

Compile it, and copy the binary to /usr/local/bin/pm-suspend. Set the setuid bit:

chmod u+s pm-suspend
Martin Vegter
  • 69
  • 66
  • 195
  • 326
0

You can allow specific set of suspend commands to be used without a password using sudo:

Create an /etc/sudoers.d/suspend file containing:

Cmnd_Alias     SUSPEND = /usr/bin/systemctl suspend, /usr/sbin/rtcwake ^-m mem --date [0-9+][^ ]{0,30}$, /usr/sbin/rtcwake ^-m mem -[st] [0-9]{1,11}$
yourusername   ALL=NOPASSWD: SUSPEND
gps
  • 103
  • 3