0
votes

I'm writing a program in C with a mind on cross-compatibility, but I don't think I'm going to be able to get away without at least some platform specific #ifdefs.

One of the required features is to be able to put the system into Suspend power mode. On Windows, with the appropriate OS config, this is accomplished by using the SetSystemPowerState() function (after an appropriate security dance prior to the call).

Is there a Linux function that will do the same thing? I suspect it's there but so far my searching is only picking up command line programs and not any headers or function signatures.

2
Which command line programs did you find? If you request the manpage for such a program, it might tell you which system calls or device files it uses.Fred Foo
There are no standard system calls or library functions. Different distributions use different methods. Generally you will have to execute external commands. Here's s something that should work for Ubuntu and probably other modern distros. Running external commands is the Unix way of doing things, nothing to be afraid of.n. 1.8e9-where's-my-share m.
@larsmans I found pm-suspend within the pm-utils package, and it sounds like calling those command line apps is the way to go!Applekid
@larsmans: reimplementing a Linux power management tool would be a useless waste of resources.n. 1.8e9-where's-my-share m.
Bear in mind that one usually needs root privileges to run pm-utils. The dbus interface does not require root.n. 1.8e9-where's-my-share m.

2 Answers

2
votes

Try to install pm-utils. look here

Depend on your Linux, you better use yum, apt-get, etc...

Then it would be something like: system("pm-hibernate") or system("pm-suspend")

Maybe you'll need to use a full path to the utility.

It is open source, so if you want to program it with c, open the sources and check how it done.

EDIT:

A c solution is to use d-bus look here

it is a c source code, check how the dbus-send.c (in the tools dir) is working (with the arguments below).

Build all, and all you need is to link with the libdbus.

cpmmand line to dbus-send tool:

dbus-send --system --print-reply --dest=org.freedesktop.Hal \
 /org/freedesktop/Hal/devices/computer \
 org.freedesktop.Hal.Device.SystemPowerManagement.Suspend \
 int32:0
1
votes

Make a file susp.sh and save

   dbus-send --system --print-reply --dest=org.freedesktop.Hal \/org/freedesktop/Hal/devices/computer \org.freedesktop.Hal.Device.SystemPowerManagement.Suspend \int32:0

Make a file resume.sh and save dbus-send --system --print-reply --dest=org.freedesktop.Hal /org/freedesktop/Hal/devices/computer \org.freedesktop.Hal.Device.SystemPowerManagement.Hibernate

Now add:

   system("sh susp.sh");

To suspend and

   system("sh resume.sh");

To resume