VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/hostversion.cpp@ 86873

Last change on this file since 86873 was 86873, checked in by vboxsync, 4 years ago

Additions/VBoxClient: Big revamp of the internal service handling and termination fixes. A service now runs as part of a worker thread, while the main thread is used for initialization / shutdown and signal handling [build fix].

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/* $Id: hostversion.cpp 86873 2020-11-12 10:30:55Z vboxsync $ */
2/** @file
3 * X11 guest client - Host version check.
4 */
5
6/*
7 * Copyright (C) 2011-2020 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#include <stdio.h>
18#include <iprt/assert.h>
19#include <iprt/errcore.h>
20#include <iprt/mem.h>
21#include <iprt/ldr.h>
22#include <iprt/string.h>
23#include <iprt/thread.h>
24
25#ifdef VBOX_WITH_DBUS
26# include <VBox/dbus.h>
27#endif
28#include <VBox/log.h>
29#include <VBox/VBoxGuestLib.h>
30#ifdef VBOX_OSE
31# include <VBox/version.h>
32#endif
33
34#include "VBoxClient.h"
35
36static int showNotify(const char *pszHeader, const char *pszBody)
37{
38 int rc;
39# ifdef VBOX_WITH_DBUS
40 DBusConnection *conn;
41 DBusMessage* msg = NULL;
42 conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
43 if (conn == NULL)
44 {
45 VBClLogError("Could not retrieve D-BUS session bus\n");
46 rc = VERR_INVALID_HANDLE;
47 }
48 else
49 {
50 msg = dbus_message_new_method_call("org.freedesktop.Notifications",
51 "/org/freedesktop/Notifications",
52 "org.freedesktop.Notifications",
53 "Notify");
54 if (msg == NULL)
55 {
56 VBClLogError("Could not create D-BUS message!\n");
57 rc = VERR_INVALID_HANDLE;
58 }
59 else
60 rc = VINF_SUCCESS;
61 }
62 if (RT_SUCCESS(rc))
63 {
64 uint32_t msg_replace_id = 0;
65 const char *msg_app = "VBoxClient";
66 const char *msg_icon = "";
67 const char *msg_summary = pszHeader;
68 const char *msg_body = pszBody;
69 int32_t msg_timeout = -1; /* Let the notification server decide */
70
71 DBusMessageIter iter;
72 DBusMessageIter array;
73 /*DBusMessageIter dict; - unused */
74 /*DBusMessageIter value; - unused */
75 /*DBusMessageIter variant; - unused */
76 /*DBusMessageIter data; - unused */
77
78 /* Format: UINT32 org.freedesktop.Notifications.Notify
79 * (STRING app_name, UINT32 replaces_id, STRING app_icon, STRING summary, STRING body,
80 * ARRAY actions, DICT hints, INT32 expire_timeout)
81 */
82 dbus_message_iter_init_append(msg,&iter);
83 dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_app);
84 dbus_message_iter_append_basic(&iter,DBUS_TYPE_UINT32,&msg_replace_id);
85 dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_icon);
86 dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_summary);
87 dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_body);
88 dbus_message_iter_open_container(&iter,DBUS_TYPE_ARRAY,DBUS_TYPE_STRING_AS_STRING,&array);
89 dbus_message_iter_close_container(&iter,&array);
90 dbus_message_iter_open_container(&iter,DBUS_TYPE_ARRAY,"{sv}",&array);
91 dbus_message_iter_close_container(&iter,&array);
92 dbus_message_iter_append_basic(&iter,DBUS_TYPE_INT32,&msg_timeout);
93
94 DBusError err;
95 dbus_error_init(&err);
96
97 DBusMessage *reply;
98 reply = dbus_connection_send_with_reply_and_block(conn, msg, 30 * 1000 /* 30 seconds timeout */, &err);
99 if (dbus_error_is_set(&err))
100 VBClLogError("D-BUS returned an error while sending the notification: %s", err.message);
101 else if (reply)
102 {
103 dbus_connection_flush(conn);
104 dbus_message_unref(reply);
105 }
106 if (dbus_error_is_set(&err))
107 dbus_error_free(&err);
108 }
109 if (msg != NULL)
110 dbus_message_unref(msg);
111# else
112 /** @todo Implement me */
113 RT_NOREF(pszHeader, pszBody);
114 rc = VINF_SUCCESS;
115# endif /* VBOX_WITH_DBUS */
116 return rc;
117}
118
119/**
120 * @interface_method_impl{VBCLSERVICE,pfnWorker}
121 */
122/** @todo Move this part in VbglR3 and just provide a callback for the platform-specific
123 notification stuff, since this is very similar to the VBoxTray code. */
124static DECLCALLBACK(int) vbclHostVerWorker(bool volatile *pfShutdown)
125{
126 RT_NOREF(pfShutdown);
127
128 LogFlowFuncEnter();
129
130 int rc;
131# ifdef VBOX_WITH_DBUS
132 rc = RTDBusLoadLib();
133 if (RT_FAILURE(rc))
134 VBClLogError("D-Bus seems not to be installed; no host version check/notification done\n");
135# else
136 rc = VERR_NOT_IMPLEMENTED;
137# endif /* VBOX_WITH_DBUS */
138
139# ifdef VBOX_WITH_GUEST_PROPS
140 uint32_t uGuestPropSvcClientID;
141 if (RT_SUCCESS(rc))
142 {
143 rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
144 if (RT_FAILURE(rc))
145 VBClLogError("Cannot connect to guest property service while chcking for host version, rc = %Rrc\n", rc);
146 }
147
148 if (RT_SUCCESS(rc))
149 {
150 /* Let the main thread know that it can continue spawning services. */
151 RTThreadUserSignal(RTThreadSelf());
152
153 /* Because we need desktop notifications to be displayed, wait
154 * some time to make the desktop environment load (as a work around). */
155 if (g_fDaemonized)
156 RTThreadSleep(RT_MS_30SEC);
157
158 char *pszHostVersion;
159 char *pszGuestVersion;
160 bool fUpdate;
161
162 rc = VbglR3HostVersionCheckForUpdate(uGuestPropSvcClientID, &fUpdate, &pszHostVersion, &pszGuestVersion);
163 if (RT_SUCCESS(rc))
164 {
165 if (fUpdate)
166 {
167 char szMsg[1024];
168 char szTitle[64];
169
170 /** @todo add some translation macros here */
171 RTStrPrintf(szTitle, sizeof(szTitle), "VirtualBox Guest Additions update available!");
172#ifndef VBOX_OSE
173 RTStrPrintf(szMsg, sizeof(szMsg), "Your guest is currently running the Guest Additions version %s. "
174 "We recommend updating to the latest version (%s) by choosing the "
175 "install option from the Devices menu.", pszGuestVersion, pszHostVersion);
176#else
177/* This is the message which appears for non-Oracle builds of the
178* Guest Additions. Distributors are encouraged to customise this. */
179 RTStrPrintf(szMsg, sizeof(szMsg), "Your virtual machine is currently running the Guest Additions version %s. Since you are running a version of the Guest Additions provided by the operating system you installed in the virtual machine we recommend that you update it to at least version %s using that system's update features, or alternatively that you remove this version and then install the " VBOX_VENDOR_SHORT " Guest Additions package using the install option from the Devices menu. Please consult the documentation for the operating system you are running to find out how to update or remove the current Guest Additions package.", pszGuestVersion, pszHostVersion);
180#endif
181 rc = showNotify(szTitle, szMsg);
182 VBClLogInfo("VirtualBox Guest Additions update available!\n");
183 if (RT_FAILURE(rc))
184 VBClLogError("Could not show version notifier tooltip! rc = %d\n", rc);
185 }
186
187 /* Store host version to not notify again */
188 rc = VbglR3HostVersionLastCheckedStore(uGuestPropSvcClientID, pszHostVersion);
189
190 VbglR3GuestPropReadValueFree(pszHostVersion);
191 VbglR3GuestPropReadValueFree(pszGuestVersion);
192 }
193 VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
194 }
195# endif /* VBOX_WITH_GUEST_PROPS */
196
197 return rc;
198}
199
200VBCLSERVICE g_SvcHostVersion =
201{
202 "hostversion", /* szName */
203 "VirtualBox host version check", /* pszDescription */
204 ".vboxclient-hostversion.pid", /* pszPidFilePath */
205 NULL, /* pszUsage */
206 NULL, /* pszOptions */
207 NULL, /* pfnOption */
208 NULL, /* pfnInit */
209 vbclHostVerWorker, /* pfnWorker */
210 NULL, /* pfnStop*/
211 NULL /* pfnTerm */
212};
213
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