VirtualBox

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

Last change on this file since 78273 was 76592, checked in by vboxsync, 6 years ago

Main: Don't use Logging.h.

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