VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/linux/HostPowerLinux.cpp@ 57415

Last change on this file since 57415 was 56845, checked in by vboxsync, 9 years ago

added test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.7 KB
Line 
1/** @file
2 *
3 * VirtualBox interface to host's power notification service
4 */
5
6/*
7 * Copyright (C) 2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include "HostPower.h"
19#include "Logging.h"
20
21#include <iprt/asm.h>
22#include <iprt/power.h>
23#include <iprt/time.h>
24
25static bool checkDBusError(DBusError *pError, DBusConnection *pConnection)
26{
27 if (dbus_error_is_set(pError))
28 {
29 LogRel(("HostPowerServiceLinux: DBus connection Error (%s)\n", pError->message));
30 dbus_error_free(pError);
31 /* Close the socket or whatever underlying the connection. */
32 dbus_connection_close(pConnection);
33 /* Free in-process resources used for the now-closed connection. */
34 dbus_connection_unref(pConnection);
35 return true;
36 }
37 return false;
38}
39
40HostPowerServiceLinux::HostPowerServiceLinux(VirtualBox *aVirtualBox)
41 : HostPowerService(aVirtualBox)
42 , mThread(NULL)
43 , mpConnection(NULL)
44{
45 DBusError error;
46 int rc;
47
48 rc = RTDBusLoadLib();
49 if (RT_FAILURE(rc))
50 {
51 LogRel(("HostPowerServiceLinux: DBus library not found. Service not available.\n"));
52 return;
53 }
54 dbus_error_init(&error);
55 /* Connect to the DBus. The connection will be not shared with any other
56 * in-process callers of dbus_bus_get(). This is considered wasteful (see
57 * API documentation) but simplifies our code, specifically shutting down.
58 * The session bus allows up to 100000 connections per user as it "is just
59 * running as the user anyway" (see session.conf.in in the DBus sources). */
60 mpConnection = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error);
61 if (checkDBusError(&error, mpConnection))
62 return;
63 /* We do not want to exit(1) if the connection is broken. */
64 dbus_connection_set_exit_on_disconnect(mpConnection, FALSE);
65 /* Tell the bus to wait for the sleep signal(s). */
66 /* The current systemd-logind interface. */
67 dbus_bus_add_match(mpConnection, "type='signal',interface='org.freedesktop.login1.Manager'", &error);
68 /* The previous UPower interfaces (2010 - ca 2013). */
69 dbus_bus_add_match(mpConnection, "type='signal',interface='org.freedesktop.UPower'", &error);
70 dbus_connection_flush(mpConnection);
71 if (checkDBusError(&error, mpConnection))
72 return;
73 /* Create the new worker thread. */
74 rc = RTThreadCreate(&mThread, HostPowerServiceLinux::powerChangeNotificationThread, this, 0 /* cbStack */,
75 RTTHREADTYPE_MSG_PUMP, RTTHREADFLAGS_WAITABLE, "MainPower");
76 if (RT_FAILURE(rc))
77 LogRel(("HostPowerServiceLinux: RTThreadCreate failed with %Rrc\n", rc));
78}
79
80
81HostPowerServiceLinux::~HostPowerServiceLinux()
82{
83 /* Closing the connection should cause the event loop to exit. */
84 LogFunc((": Stopping thread\n"));
85 if (mpConnection)
86 dbus_connection_close(mpConnection);
87
88 RTThreadWait(mThread, 5000, NULL);
89 mThread = NIL_RTTHREAD;
90}
91
92
93DECLCALLBACK(int) HostPowerServiceLinux::powerChangeNotificationThread(RTTHREAD hThreadSelf, void *pInstance)
94{
95 NOREF(hThreadSelf);
96 HostPowerServiceLinux *pPowerObj = static_cast<HostPowerServiceLinux *>(pInstance);
97
98 Log(("HostPowerServiceLinux: Thread started\n"));
99 while (dbus_connection_read_write(pPowerObj->mpConnection, -1))
100 {
101 DBusMessage *pMessage = NULL;
102
103 for (;;)
104 {
105 DBusMessageIter args;
106 dbus_bool_t fSuspend;
107
108 pMessage = dbus_connection_pop_message(pPowerObj->mpConnection);
109 if (pMessage == NULL)
110 break;
111 /* The systemd-logind interface notification. */
112 if ( dbus_message_is_signal(pMessage, "org.freedesktop.login1.Manager", "PrepareForSleep")
113 && dbus_message_iter_init(pMessage, &args)
114 && dbus_message_iter_get_arg_type(&args) == DBUS_TYPE_BOOLEAN)
115 {
116 dbus_message_iter_get_basic(&args, &fSuspend);
117 /* Trinary operator does not work here as Reason_... is an
118 * anonymous enum. */
119 if (fSuspend)
120 pPowerObj->notify(Reason_HostSuspend);
121 else
122 pPowerObj->notify(Reason_HostResume);
123 }
124 /* The UPowerd interface notifications. Sleeping is the older one,
125 * NotifySleep the newer. This gives us one second grace before the
126 * suspend triggers. */
127 if ( dbus_message_is_signal(pMessage, "org.freedesktop.UPower", "Sleeping")
128 || dbus_message_is_signal(pMessage, "org.freedesktop.UPower", "NotifySleep"))
129 pPowerObj->notify(Reason_HostSuspend);
130 if ( dbus_message_is_signal(pMessage, "org.freedesktop.UPower", "Resuming")
131 || dbus_message_is_signal(pMessage, "org.freedesktop.UPower", "NotifyResume"))
132 pPowerObj->notify(Reason_HostResume);
133 /* Free local resources held for the message. */
134 dbus_message_unref(pMessage);
135 }
136 }
137 /* Close the socket or whatever underlying the connection. */
138 dbus_connection_close(pPowerObj->mpConnection);
139 /* Free in-process resources used for the now-closed connection. */
140 dbus_connection_unref(pPowerObj->mpConnection);
141 pPowerObj->mpConnection = NULL;
142 Log(("HostPowerServiceLinux: Exiting thread\n"));
143 return VINF_SUCCESS;
144}
145
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette