diff options
author | Michele Calgaro <michele.calgaro@yahoo.it> | 2019-01-28 15:49:56 +0900 |
---|---|---|
committer | Michele Calgaro <michele.calgaro@yahoo.it> | 2019-02-06 23:37:13 +0900 |
commit | 5931eecd26b26e4af8197ddae60be1625bf9815c (patch) | |
tree | 163bc614507ea9acf00a75e2791ccce9f81959f5 | |
parent | b11c80958878278a87e6b75ed8d47aff6ac1219c (diff) | |
download | tdelibs-5931eecd26b26e4af8197ddae60be1625bf9815c.tar.gz tdelibs-5931eecd26b26e4af8197ddae60be1625bf9815c.zip |
tdehw dbus daemon: improved response for can* functions and added
polkit support to cpugovernor and brightness interfaces.
Signed-off-by: Michele Calgaro <michele.calgaro@yahoo.it>
-rw-r--r-- | tdecore/tdehw/hwlibdaemons/dbus/org.trinitydesktop.hardwarecontrol.policy | 30 | ||||
-rw-r--r-- | tdecore/tdehw/hwlibdaemons/dbus/tde_dbus_hardwarecontrol.cpp | 319 |
2 files changed, 165 insertions, 184 deletions
diff --git a/tdecore/tdehw/hwlibdaemons/dbus/org.trinitydesktop.hardwarecontrol.policy b/tdecore/tdehw/hwlibdaemons/dbus/org.trinitydesktop.hardwarecontrol.policy index 5183b47cb..f176359e9 100644 --- a/tdecore/tdehw/hwlibdaemons/dbus/org.trinitydesktop.hardwarecontrol.policy +++ b/tdecore/tdehw/hwlibdaemons/dbus/org.trinitydesktop.hardwarecontrol.policy @@ -7,6 +7,26 @@ <vendor>The Trinity Desktop Environment Project</vendor> <vendor_url>http://www.trinitydesktop.org</vendor_url> + <action id="org.trinitydesktop.hardwarecontrol.cpugovernor.setcpugovernor"> + <description>Set CPU governor.</description> + <message>Authentication is required to set the CPU governor.</message> + <defaults> + <allow_any>no</allow_any> + <allow_inactive>no</allow_inactive> + <allow_active>yes</allow_active> + </defaults> + </action> + + <action id="org.trinitydesktop.hardwarecontrol.brightness.setbrightness"> + <description>Set display brightness.</description> + <message>Authentication is required to set the display brightness.</message> + <defaults> + <allow_any>no</allow_any> + <allow_inactive>no</allow_inactive> + <allow_active>yes</allow_active> + </defaults> + </action> + <action id="org.trinitydesktop.hardwarecontrol.power.freeze"> <description>Freeze the system.</description> <message>Authentication is required to freeze the system.</message> @@ -57,4 +77,14 @@ </defaults> </action> + <action id="org.trinitydesktop.hardwarecontrol.power.sethibernationmethod"> + <description>Set method used to hibernate the system.</description> + <message>Authentication is required to set hibernation method.</message> + <defaults> + <allow_any>no</allow_any> + <allow_inactive>no</allow_inactive> + <allow_active>yes</allow_active> + </defaults> + </action> + </policyconfig> diff --git a/tdecore/tdehw/hwlibdaemons/dbus/tde_dbus_hardwarecontrol.cpp b/tdecore/tdehw/hwlibdaemons/dbus/tde_dbus_hardwarecontrol.cpp index 8ba765c34..3ef9196ee 100644 --- a/tdecore/tdehw/hwlibdaemons/dbus/tde_dbus_hardwarecontrol.cpp +++ b/tdecore/tdehw/hwlibdaemons/dbus/tde_dbus_hardwarecontrol.cpp @@ -21,8 +21,7 @@ #define BITS_PER_LONG (sizeof(long) * 8) #define NUM_BITS(x) ((((x) - 1) / BITS_PER_LONG) + 1) -bool checkPolKitAuthorization(DBusMessage* msg, const TQString &action_id) -{ +bool checkPolKitAuthorization(DBusMessage* msg, const TQString &action_id) { if (!msg) { return false; } @@ -64,7 +63,7 @@ bool checkPolKitAuthorization(DBusMessage* msg, const TQString &action_id) return false; } -void reply_Bool(DBusMessage* msg, DBusConnection* conn, int value) { +void replyBool(DBusMessage* msg, DBusConnection* conn, int value) { DBusMessage* reply; DBusMessageIter args; const char* member = dbus_message_get_member(msg); @@ -91,218 +90,141 @@ void reply_Bool(DBusMessage* msg, DBusConnection* conn, int value) { dbus_message_unref(reply); } -void reply_CanSetGivenPath(DBusMessage* msg, DBusConnection* conn, const char* param) { - DBusMessage* reply; - DBusMessageIter args; - const char* member = dbus_message_get_member(msg); - dbus_uint32_t serial = 0; - int writable = false; - - // check if path is writable - int rval = access (param, W_OK); - if (rval == 0) { - writable = true; - } - - // create a reply from the message - reply = dbus_message_new_method_return(msg); - - // add the arguments to the reply - dbus_message_iter_init_append(reply, &args); - if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &writable)) { - fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_append_basic failed\n", member); - return; - } - - // send the reply && flush the connection - if (!dbus_connection_send(conn, reply, &serial)) { - fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_connection_send failed\n", member); - return; - } - dbus_connection_flush(conn); - - // free the reply - dbus_message_unref(reply); -} - -void reply_SetGivenPath(DBusMessage* msg, DBusConnection* conn, const char* param, const char* contents) { - DBusMessage* reply; - DBusMessageIter args; - const char* member = dbus_message_get_member(msg); - dbus_uint32_t serial = 0; - int writable = false; - int written = false; - - // check if path is writable - int rval = access (param, W_OK); - if (rval == 0) { - writable = true; - } - +bool setGivenPath(const char *path, const char *contents) { + int writable = (access(path, W_OK) == 0); + bool result = false; if (writable) { - FILE *node = fopen(param, "w"); + FILE *node = fopen(path, "w"); if (node != NULL) { if (fputs(contents, node) != EOF) { - written = true; - } - if (fclose(node) == EOF) { - // Error! + result = true; } + fclose(node); } } - - // create a reply from the message - reply = dbus_message_new_method_return(msg); - - // add the arguments to the reply - dbus_message_iter_init_append(reply, &args); - if (!dbus_message_iter_append_basic(&args, DBUS_TYPE_BOOLEAN, &written)) { - fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_message_iter_append_basic failed\n", member); - return; - } - - // send the reply && flush the connection - if (!dbus_connection_send(conn, reply, &serial)) { - fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: dbus_connection_send failed\n", member); - return; - } - dbus_connection_flush(conn); - - // free the reply - dbus_message_unref(reply); + return result; } -void reply_CanSetCPUGovernor(DBusMessage* msg, DBusConnection* conn) { +bool canSetCPUGovernor(DBusMessage *msg, DBusConnection *conn) { DBusMessageIter args; - const char* member = dbus_message_get_member(msg); - dbus_int32_t cpunum; - char path[256]; + const char *member = dbus_message_get_member(msg); // read the arguments if (!dbus_message_iter_init(msg, &args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: no argument supplied\n", member); + return false; } else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: argument not 32-bit integer\n", member); + return false; } - else { - dbus_message_iter_get_basic(&args, &cpunum); - } + dbus_int32_t cpunum; + dbus_message_iter_get_basic(&args, &cpunum); + char path[256]; snprintf(path, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpunum); - reply_CanSetGivenPath(msg, conn, path); + return (access(path, W_OK) == 0); } -void reply_SetCPUGovernor(DBusMessage* msg, DBusConnection* conn) { +bool setCPUGovernor(DBusMessage *msg, DBusConnection *conn) { DBusMessageIter args; - const char* member = dbus_message_get_member(msg); - dbus_int32_t cpunum = -1; - char* governor = NULL; - char path[256]; + const char *member = dbus_message_get_member(msg); // read the arguments if (!dbus_message_iter_init(msg, &args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: no arguments supplied\n", member); + return false; } else if (DBUS_TYPE_INT32 != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: first argument not 32-bit integer\n", member); + return false; + } - else { - dbus_message_iter_get_basic(&args, &cpunum); - } + dbus_int32_t cpunum = -1; + dbus_message_iter_get_basic(&args, &cpunum); if (!dbus_message_iter_next(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: second argument not supplied\n", member); + return false; } else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: second argument not string\n", member); + return false; } - else { - dbus_message_iter_get_basic(&args, &governor); - } + char *governor = NULL; + dbus_message_iter_get_basic(&args, &governor); + bool result = false; + char path[256]; snprintf(path, 256, "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_governor", cpunum); - if ((cpunum>-1) && governor) { - reply_SetGivenPath(msg, conn, path, governor); - } - else { - reply_Bool(msg, conn, false); + if ((cpunum > -1) && governor) { + result = setGivenPath(path, governor); } + return result; } -void reply_CanSetBrightness(DBusMessage* msg, DBusConnection* conn) { +bool CanSetBrightness(DBusMessage* msg, DBusConnection* conn) { DBusMessageIter args; const char* member = dbus_message_get_member(msg); - char* rawpath; - char* safepath; // read the arguments if (!dbus_message_iter_init(msg, &args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: no argument supplied\n", member); + return false; } else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: argument not string\n", member); + return false; } - else { - dbus_message_iter_get_basic(&args, &rawpath); - } - - safepath = realpath(rawpath, NULL); + char *rawpath; + dbus_message_iter_get_basic(&args, &rawpath); - if (safepath && - (strstr(safepath, "/sys/devices") == safepath) && - (strstr(safepath, "/brightness") == (safepath+strlen(safepath)-strlen("/brightness"))) - ) { - reply_CanSetGivenPath(msg, conn, safepath); - } - else { - reply_Bool(msg, conn, false); + bool result = false; + char *safepath = realpath(rawpath, NULL); + if (safepath && (strstr(safepath, "/sys/devices") == safepath) && + (strstr(safepath, "/brightness") == (safepath+strlen(safepath)-strlen("/brightness")))) { + result = (access(safepath, W_OK) == 0); } - free(safepath); + return result; } -void reply_SetBrightness(DBusMessage* msg, DBusConnection* conn) { +bool SetBrightness(DBusMessage* msg, DBusConnection* conn) { DBusMessageIter args; const char* member = dbus_message_get_member(msg); - char* rawpath; - char* safepath; - char* brightness; // read the arguments if (!dbus_message_iter_init(msg, &args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: no arguments supplied\n", member); + return false; } else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: first argument not string\n", member); + return false; } - else { - dbus_message_iter_get_basic(&args, &rawpath); - } + char* rawpath; + dbus_message_iter_get_basic(&args, &rawpath); if (!dbus_message_iter_next(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: second argument not supplied\n", member); + return false; } else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: second argument not string\n", member); + return false; } - else { - dbus_message_iter_get_basic(&args, &brightness); - } + char* brightness; + dbus_message_iter_get_basic(&args, &brightness); + bool result = false; + char* safepath; safepath = realpath(rawpath, NULL); - - if (safepath && brightness && - (strstr(safepath, "/sys/devices") == safepath) && - (strstr(safepath, "/brightness") == (safepath+strlen(safepath)-strlen("/brightness"))) - ) { - reply_SetGivenPath(msg, conn, safepath, brightness); - } - else { - reply_Bool(msg, conn, false); + if (safepath && brightness && (strstr(safepath, "/sys/devices") == safepath) && + (strstr(safepath, "/brightness") == (safepath+strlen(safepath)-strlen("/brightness")))) { + result = setGivenPath(safepath, brightness); } - free(safepath); + return result; } bool CanSetPowerState(const char* state, const char* disk, const char* mem) { @@ -424,33 +346,32 @@ bool SetPowerState(const char* state, const char* disk, const char* mem) { return result; } -void reply_CanSetHibernationMethod(DBusMessage* msg, DBusConnection* conn) { - // check if path is writable - reply_CanSetGivenPath(msg, conn, "/sys/power/disk"); +bool CanSetHibernationMethod(DBusMessage* msg, DBusConnection* conn) { + return (access("/sys/power/disk", W_OK) == 0); } -void reply_SetHibernationMethod(DBusMessage* msg, DBusConnection* conn) { +bool SetHibernationMethod(DBusMessage* msg, DBusConnection* conn) { DBusMessageIter args; const char* member = dbus_message_get_member(msg); - char* method = NULL; // read the arguments if (!dbus_message_iter_init(msg, &args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: no arguments supplied\n", member); + return false; } else if (DBUS_TYPE_STRING != dbus_message_iter_get_arg_type(&args)) { fprintf(stderr, "[tde_dbus_hardwarecontrol] %s: argument not string\n", member); + return false; } - else { - dbus_message_iter_get_basic(&args, &method); - } + char* method = NULL; + dbus_message_iter_get_basic(&args, &method); // set hibernation method if (method) { - reply_SetGivenPath(msg, conn, "/sys/power/disk", method); + return setGivenPath("/sys/power/disk", method); } else { - reply_Bool(msg, conn, false); + return false; } } @@ -807,26 +728,51 @@ void listen() { // check this is a method call for the right interface & method if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.CPUGovernor", "CanSetCPUGovernor")) { - reply_CanSetCPUGovernor(msg, conn); + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.cpugovernor.setcpugovernor"); + if (result) { + result = canSetCPUGovernor(msg, conn); + } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.CPUGovernor", "SetCPUGovernor")) { - reply_SetCPUGovernor(msg, conn); + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.cpugovernor.setcpugovernor"); + if (!result) { + error_PolkitAccessDenied(msg, conn); + } + else { + result = false; + if (canSetCPUGovernor(msg, conn)) { + result = setCPUGovernor(msg, conn); + } + replyBool(msg, conn, result); + } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Brightness", "CanSetBrightness")) { - reply_CanSetBrightness(msg, conn); + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.brightness.setbrightness"); + if (result) { + result = CanSetBrightness(msg, conn); + } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Brightness", "SetBrightness")) { - reply_SetBrightness(msg, conn); - } - else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanFreeze")) { - bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.freeze"); + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.brightness.setbrightness"); if (!result) { error_PolkitAccessDenied(msg, conn); } else { + result = false; + if (CanSetBrightness(msg, conn)) { + result = SetBrightness(msg, conn); + } + replyBool(msg, conn, result); + } + } + else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanFreeze")) { + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.freeze"); + if (result) { result = CanSetPowerState("freeze", NULL, NULL) || CanSetPowerState("mem", NULL, "s2idle"); - reply_Bool(msg, conn, result); } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Freeze")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.freeze"); @@ -841,18 +787,15 @@ void listen() { else if (CanSetPowerState("mem", NULL, "s2idle")) { result = SetPowerState("mem", NULL, "s2idle"); } - reply_Bool(msg, conn, result); + replyBool(msg, conn, result); } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanStandby")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.standby"); - if (!result) { - error_PolkitAccessDenied(msg, conn); - } - else { + if (result) { result = CanSetPowerState("standby", NULL, NULL) || CanSetPowerState("mem", NULL, "shallow"); - reply_Bool(msg, conn, result); } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Standby")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.standby"); @@ -867,19 +810,16 @@ void listen() { else if (CanSetPowerState("mem", NULL, "shallow")) { result = SetPowerState("mem", NULL, "shallow"); } - reply_Bool(msg, conn, result); + replyBool(msg, conn, result); } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanSuspend")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.suspend"); - if (!result) { - error_PolkitAccessDenied(msg, conn); - } - else { + if (result) { result = (CanSetPowerState("mem", NULL, NULL) && access("/sys/power/mem_sleep", R_OK) != 0) || - CanSetPowerState("mem", NULL, "deep"); - reply_Bool(msg, conn, result); + CanSetPowerState("mem", NULL, "deep"); } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Suspend")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.suspend"); @@ -894,18 +834,15 @@ void listen() { else if (CanSetPowerState("mem", NULL, "deep")) { result = SetPowerState("mem", NULL, "deep"); } - reply_Bool(msg, conn, result); + replyBool(msg, conn, result); } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanHybridSuspend")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.hybridsuspend"); - if (!result) { - error_PolkitAccessDenied(msg, conn); - } - else { + if (result) { result = CanSetPowerState("disk", "suspend", NULL); - reply_Bool(msg, conn, result); } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "HybridSuspend")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.hybridsuspend"); @@ -913,19 +850,19 @@ void listen() { error_PolkitAccessDenied(msg, conn); } else { - result = SetPowerState("disk", "suspend", NULL); - reply_Bool(msg, conn, result); + result = false; + if (CanSetPowerState("disk", "suspend", NULL)) { + result = SetPowerState("disk", "suspend", NULL); + } + replyBool(msg, conn, result); } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanHibernate")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.hibernate"); - if (!result) { - error_PolkitAccessDenied(msg, conn); - } - else { + if (result) { result = CanSetPowerState("disk", "shutdown", NULL) || CanSetPowerState("disk", "platform", NULL); - reply_Bool(msg, conn, result); } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "Hibernate")) { bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.hibernate"); @@ -940,14 +877,28 @@ void listen() { else if (CanSetPowerState("disk", "platform", NULL)) { result = SetPowerState("disk", "platform", NULL); } - reply_Bool(msg, conn, result); + replyBool(msg, conn, result); } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "CanSetHibernationMethod")) { - reply_CanSetHibernationMethod(msg, conn); + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.sethibernationmethod"); + if (result) { + result = CanSetHibernationMethod(msg, conn); + } + replyBool(msg, conn, result); } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.Power", "SetHibernationMethod")) { - reply_SetHibernationMethod(msg, conn); + bool result = checkPolKitAuthorization(msg, "org.trinitydesktop.hardwarecontrol.power.sethibernationmethod"); + if (!result) { + error_PolkitAccessDenied(msg, conn); + } + else { + result = false; + if (CanSetHibernationMethod(msg, conn)) { + result = SetHibernationMethod(msg, conn); + } + replyBool(msg, conn, result); + } } else if (dbus_message_is_method_call(msg, "org.trinitydesktop.hardwarecontrol.InputEvents", "GetProvidedSwitches")) { reply_InputEventsGetSwitches(msg, conn, false); |