1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
|
/* $Id: logging.cpp $ */
/** @file
* VirtualBox Guest Additions - X11 Client.
*/
/*
* Copyright (C) 2006-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
#include <sys/wait.h>
#include <stdlib.h>
#include <iprt/buildconfig.h>
#include <iprt/file.h>
#include <iprt/process.h>
#include <iprt/stream.h>
#include <iprt/system.h>
#ifdef VBOX_WITH_DBUS
# include <VBox/dbus.h>
#endif
#include <VBox/VBoxGuestLib.h>
#include <package-generated.h>
#include "VBoxClient.h"
/** Logging parameters. */
/** @todo Make this configurable later. */
static PRTLOGGER g_pLoggerRelease = NULL;
static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
/** Custom log prefix (to be set externally). */
static char *g_pszCustomLogPrefix;
extern unsigned g_cRespawn;
/**
* Fallback notification helper using 'notify-send'.
*
* @returns VBox status code.
* @returns VERR_NOT_SUPPORTED if 'notify-send' is not available, or there was an error while running 'notify-send'.
* @param pszMessage Message to notify desktop environment with.
*/
int vbclNotifyFallbackNotifySend(const char *pszMessage)
{
AssertPtrReturn(pszMessage, VERR_INVALID_POINTER);
int rc = VINF_SUCCESS;
if (g_cRespawn == 0)
{
char *pszCommand = RTStrAPrintf2("notify-send \"VBoxClient: %s\"", pszMessage);
if (pszCommand)
{
int status = system(pszCommand);
RTStrFree(pszCommand);
if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
{
pszCommand = RTStrAPrintf2("xmessage -buttons OK:0 -center \"VBoxClient: %s\"",
pszMessage);
if (pszCommand)
{
status = system(pszCommand);
if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
rc = VERR_NOT_SUPPORTED;
RTStrFree(pszCommand);
}
else
rc = VERR_NO_MEMORY;
}
}
else
rc = VERR_NO_MEMORY;
}
return rc;
}
/**
* Shows a notification on the desktop.
*
* @returns VBox status code.
* @returns VERR_NOT_SUPPORTED if the current desktop environment is not supported.
* @param pszHeader Header text to show.
* @param pszBody Body text to show.
*
* @note How this notification will look like depends on the actual desktop environment implementing
* the actual notification service. Currently only D-BUS-compatible environments are supported.
*
* Most notification implementations have length limits on their header / body texts, so keep
* the text(s) short.
*/
int VBClShowNotify(const char *pszHeader, const char *pszBody)
{
AssertPtrReturn(pszHeader, VERR_INVALID_POINTER);
AssertPtrReturn(pszBody, VERR_INVALID_POINTER);
int rc;
# ifdef VBOX_WITH_DBUS
rc = RTDBusLoadLib(); /** @todo Does this init / load the lib only once? Need to check this. */
if (RT_FAILURE(rc))
{
VBClLogError("D-Bus seems not to be installed; no desktop notifications available\n");
return rc;
}
DBusConnection *conn;
DBusMessage* msg = NULL;
conn = dbus_bus_get(DBUS_BUS_SESSION, NULL);
if (conn == NULL)
{
VBClLogError("Could not retrieve D-BUS session bus\n");
rc = VERR_INVALID_HANDLE;
}
else
{
msg = dbus_message_new_method_call("org.freedesktop.Notifications",
"/org/freedesktop/Notifications",
"org.freedesktop.Notifications",
"Notify");
if (msg == NULL)
{
VBClLogError("Could not create D-BUS message!\n");
rc = VERR_INVALID_HANDLE;
}
else
rc = VINF_SUCCESS;
}
if (RT_SUCCESS(rc))
{
uint32_t msg_replace_id = 0;
const char *msg_app = "VBoxClient";
const char *msg_icon = "";
const char *msg_summary = pszHeader;
const char *msg_body = pszBody;
int32_t msg_timeout = -1; /* Let the notification server decide */
DBusMessageIter iter;
DBusMessageIter array;
/*DBusMessageIter dict; - unused */
/*DBusMessageIter value; - unused */
/*DBusMessageIter variant; - unused */
/*DBusMessageIter data; - unused */
/* Format: UINT32 org.freedesktop.Notifications.Notify
* (STRING app_name, UINT32 replaces_id, STRING app_icon, STRING summary, STRING body,
* ARRAY actions, DICT hints, INT32 expire_timeout)
*/
dbus_message_iter_init_append(msg,&iter);
dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_app);
dbus_message_iter_append_basic(&iter,DBUS_TYPE_UINT32,&msg_replace_id);
dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_icon);
dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_summary);
dbus_message_iter_append_basic(&iter,DBUS_TYPE_STRING,&msg_body);
dbus_message_iter_open_container(&iter,DBUS_TYPE_ARRAY,DBUS_TYPE_STRING_AS_STRING,&array);
dbus_message_iter_close_container(&iter,&array);
dbus_message_iter_open_container(&iter,DBUS_TYPE_ARRAY,"{sv}",&array);
dbus_message_iter_close_container(&iter,&array);
dbus_message_iter_append_basic(&iter,DBUS_TYPE_INT32,&msg_timeout);
DBusError err;
dbus_error_init(&err);
DBusMessage *reply;
reply = dbus_connection_send_with_reply_and_block(conn, msg, 30 * 1000 /* 30 seconds timeout */, &err);
if (dbus_error_is_set(&err))
VBClLogError("D-BUS returned an error while sending the notification: %s", err.message);
else if (reply)
{
dbus_connection_flush(conn);
dbus_message_unref(reply);
}
if (dbus_error_is_set(&err))
dbus_error_free(&err);
}
if (msg != NULL)
dbus_message_unref(msg);
# else
/** @todo Implement me */
RT_NOREF(pszHeader, pszBody);
rc = VERR_NOT_SUPPORTED;
# endif /* VBOX_WITH_DBUS */
/* Try to use a fallback if the stuff above fails or is not available. */
if (RT_FAILURE(rc))
rc = vbclNotifyFallbackNotifySend(pszBody);
/* If everything fails, still print out our notification to stdout, in the hope
* someone still gets aware of it. */
if (RT_FAILURE(rc))
VBClLogInfo("*** Notification: %s - %s ***\n", pszHeader, pszBody);
return rc;
}
/**
* Logs a verbose message.
*
* @param pszFormat The message text.
* @param va Format arguments.
*/
static void vbClLogV(const char *pszFormat, va_list va)
{
char *psz = NULL;
RTStrAPrintfV(&psz, pszFormat, va);
AssertPtrReturnVoid(psz);
LogRel(("%s", psz));
RTStrFree(psz);
}
/**
* Logs a fatal error, notifies the desktop environment via a message and
* exits the application immediately.
*
* @param pszFormat Format string to log.
* @param ... Variable arguments for format string. Optional.
*/
void VBClLogFatalError(const char *pszFormat, ...)
{
va_list args;
va_start(args, pszFormat);
char *psz = NULL;
RTStrAPrintfV(&psz, pszFormat, args);
va_end(args);
AssertPtrReturnVoid(psz);
LogFunc(("Fatal Error: %s", psz));
LogRel(("Fatal Error: %s", psz));
VBClShowNotify("VBoxClient - Fatal Error", psz);
RTStrFree(psz);
}
/**
* Logs an error message to the (release) logging instance.
*
* @param pszFormat Format string to log.
*/
void VBClLogError(const char *pszFormat, ...)
{
va_list args;
va_start(args, pszFormat);
char *psz = NULL;
RTStrAPrintfV(&psz, pszFormat, args);
va_end(args);
AssertPtrReturnVoid(psz);
LogFunc(("Error: %s", psz));
LogRel(("Error: %s", psz));
RTStrFree(psz);
}
/**
* Logs an info message to the (release) logging instance.
*
* @param pszFormat Format string to log.
*/
void VBClLogInfo(const char *pszFormat, ...)
{
va_list args;
va_start(args, pszFormat);
vbClLogV(pszFormat, args);
va_end(args);
}
/**
* Displays a verbose message based on the currently
* set global verbosity level.
*
* @param iLevel Minimum log level required to display this message.
* @param pszFormat The message text.
* @param ... Format arguments.
*/
void VBClLogVerbose(unsigned iLevel, const char *pszFormat, ...)
{
if (iLevel <= g_cVerbosity)
{
va_list va;
va_start(va, pszFormat);
vbClLogV(pszFormat, va);
va_end(va);
}
}
/**
* @callback_method_impl{FNRTLOGPHASE, Release logger callback}
*/
static DECLCALLBACK(void) vbClLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
{
/* Some introductory information. */
static RTTIMESPEC s_TimeSpec;
char szTmp[256];
if (enmPhase == RTLOGPHASE_BEGIN)
RTTimeNow(&s_TimeSpec);
RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
switch (enmPhase)
{
case RTLOGPHASE_BEGIN:
{
pfnLog(pLoggerRelease,
"VBoxClient %s r%s (verbosity: %u) %s (%s %s) release log\n"
"Log opened %s\n",
RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity, VBOX_BUILD_TARGET,
__DATE__, __TIME__, szTmp);
int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
/* the package type is interesting for Linux distributions */
char szExecName[RTPATH_MAX];
char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
pfnLog(pLoggerRelease,
"Executable: %s\n"
"Process ID: %u\n"
"Package type: %s"
#ifdef VBOX_OSE
" (OSE)"
#endif
"\n",
pszExecName ? pszExecName : "unknown",
RTProcSelf(),
VBOX_PACKAGE_STRING);
break;
}
case RTLOGPHASE_PREROTATE:
pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
break;
case RTLOGPHASE_POSTROTATE:
pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
break;
case RTLOGPHASE_END:
pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
break;
default:
/* nothing */
break;
}
}
static DECLCALLBACK(size_t) vbClLogPrefixCb(PRTLOGGER pLogger, char *pchBuf, size_t cchBuf, void *pvUser)
{
size_t cbPrefix = 0;
RT_NOREF(pLogger);
RT_NOREF(pvUser);
if (g_pszCustomLogPrefix)
{
cbPrefix = RT_MIN(strlen(g_pszCustomLogPrefix), cchBuf);
memcpy(pchBuf, g_pszCustomLogPrefix, cbPrefix);
}
return cbPrefix;
}
/**
* Creates the default release logger outputting to the specified file.
*
* Pass NULL to disabled logging.
*
* @return IPRT status code.
* @param pszLogFile Filename for log output. NULL disables custom handling.
*/
int VBClLogCreate(const char *pszLogFile)
{
if (!pszLogFile)
return VINF_SUCCESS;
/* Create release logger (stdout + file). */
static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME | RTLOGFLAGS_PREFIX_CUSTOM;
#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
fFlags |= RTLOGFLAGS_USECRLF;
#endif
int rc = RTLogCreateEx(&g_pLoggerRelease, "VBOXCLIENT_RELEASE_LOG", fFlags, "all",
RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX /*cMaxEntriesPerGroup*/,
0 /*cBufDescs*/, NULL /*paBufDescs*/, RTLOGDEST_STDOUT | RTLOGDEST_USER,
vbClLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
NULL /*pOutputIf*/, NULL /*pvOutputIfUser*/,
NULL /*pErrInfo*/, "%s", pszLogFile ? pszLogFile : "");
if (RT_SUCCESS(rc))
{
/* register this logger as the release logger */
RTLogRelSetDefaultInstance(g_pLoggerRelease);
rc = RTLogSetCustomPrefixCallback(g_pLoggerRelease, vbClLogPrefixCb, NULL);
if (RT_FAILURE(rc))
VBClLogError("unable to register custom log prefix callback\n");
/* Explicitly flush the log in case of VBOXSERVICE_RELEASE_LOG=buffered. */
RTLogFlush(g_pLoggerRelease);
}
return rc;
}
/**
* Set custom log prefix.
*
* @param pszPrefix Custom log prefix string.
*/
void VBClLogSetLogPrefix(const char *pszPrefix)
{
g_pszCustomLogPrefix = (char *)pszPrefix;
}
/**
* Destroys the currently active logging instance.
*/
void VBClLogDestroy(void)
{
RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
}
|