2007-07-14 02:44:01 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
2010-03-16 15:54:00 +01:00
|
|
|
/* dbus-launch-win.c dbus-launch utility
|
2007-06-01 22:24:57 +00:00
|
|
|
*
|
|
|
|
|
* Copyright (C) 2007 Ralf Habacker <ralf.habacker@freenet.de>
|
|
|
|
|
*
|
2023-01-02 15:31:43 +01:00
|
|
|
* SPDX-License-Identifier: AFL-2.1 OR GPL-2.0-or-later
|
|
|
|
|
*
|
2007-06-01 22:24:57 +00:00
|
|
|
* Licensed under the Academic Free License version 2.1
|
|
|
|
|
*
|
|
|
|
|
* 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; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* 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, write to the Free Software
|
2009-07-10 19:32:38 -04:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2007-06-01 22:24:57 +00:00
|
|
|
*
|
|
|
|
|
*/
|
2010-03-16 15:54:00 +01:00
|
|
|
|
2010-02-03 12:27:47 -08:00
|
|
|
#include <config.h>
|
2010-03-16 15:54:00 +01:00
|
|
|
#ifndef UNICODE
|
|
|
|
|
#define UNICODE 1
|
|
|
|
|
#endif
|
2007-06-01 22:24:57 +00:00
|
|
|
#include <windows.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
/* Save string functions. Instead of figuring out the exact _MSC_VER
|
|
|
|
|
that work, override for everybody. */
|
|
|
|
|
|
2007-06-01 22:24:57 +00:00
|
|
|
#define errno_t int
|
2010-03-16 15:54:00 +01:00
|
|
|
#define wcscat_s my_wcscat_s
|
|
|
|
|
#define wcscpy_s my_wcscpy_s
|
2007-06-01 22:24:57 +00:00
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
static errno_t
|
Be more const-correct
As a general design principle, strings that we aren't going to modify
should usually be const. When compiling with -Wwrite-strings, quoted
string constants are of type "const char *", causing compiler warnings
when they are assigned to char * variables.
Unfortunately, we need to add casts in a few places:
* _dbus_list_append(), _dbus_test_oom_handling() and similar generic
"user-data" APIs take a void *, not a const void *, so we have
to cast
* For historical reasons the execve() family of functions take a
(char * const *), i.e. a constant pointer to an array of mutable
strings, so again we have to cast
* _dbus_spawn_async_with_babysitter similarly takes a char **,
although we can make it a little more const-correct by making it
take (char * const *) like execve() does
This also incorporates a subsequent patch by Thomas Zimmermann to
put various string constants in static storage, which is a little
more efficient.
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
2016-10-07 17:41:01 +01:00
|
|
|
wcscat_s (wchar_t *dest, size_t size, const wchar_t *src)
|
2007-06-01 22:24:57 +00:00
|
|
|
{
|
2010-03-16 15:54:00 +01:00
|
|
|
assert (sizeof (wchar_t) * (wcslen (dest) + wcslen (src) + 1) <= size);
|
|
|
|
|
wcscat (dest, src);
|
2007-06-01 22:24:57 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
|
|
|
|
|
static errno_t
|
Be more const-correct
As a general design principle, strings that we aren't going to modify
should usually be const. When compiling with -Wwrite-strings, quoted
string constants are of type "const char *", causing compiler warnings
when they are assigned to char * variables.
Unfortunately, we need to add casts in a few places:
* _dbus_list_append(), _dbus_test_oom_handling() and similar generic
"user-data" APIs take a void *, not a const void *, so we have
to cast
* For historical reasons the execve() family of functions take a
(char * const *), i.e. a constant pointer to an array of mutable
strings, so again we have to cast
* _dbus_spawn_async_with_babysitter similarly takes a char **,
although we can make it a little more const-correct by making it
take (char * const *) like execve() does
This also incorporates a subsequent patch by Thomas Zimmermann to
put various string constants in static storage, which is a little
more efficient.
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
2016-10-07 17:41:01 +01:00
|
|
|
wcscpy_s (wchar_t *dest, size_t size, const wchar_t *src)
|
2007-06-01 22:24:57 +00:00
|
|
|
{
|
2010-03-16 15:54:00 +01:00
|
|
|
assert (sizeof (wchar_t) * (wcslen (src) + 1) <= size);
|
|
|
|
|
wcscpy (dest, src);
|
2007-06-01 22:24:57 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
2009-04-22 09:30:23 +02:00
|
|
|
|
2009-11-30 12:14:24 +01:00
|
|
|
/* TODO (tl): This Windows version of dbus-launch is curretly rather
|
2009-04-22 09:30:23 +02:00
|
|
|
* pointless as it doesn't take the same command-line options as the
|
|
|
|
|
* UNIX dbus-launch does. A main point of the dbus-launch command is
|
|
|
|
|
* to pass it for instance a --config-file option to make the started
|
|
|
|
|
* dbus-daemon use that config file.
|
2009-11-30 12:14:24 +01:00
|
|
|
*
|
2009-04-22 09:30:23 +02:00
|
|
|
* This version also doesn't print out any information, which is a
|
|
|
|
|
* main point of the UNIX one. It should at least support the
|
|
|
|
|
* --sh-syntax option, and maybe also a --cmd-syntax to print out the
|
|
|
|
|
* variable settings in cmd.exe syntax?
|
2009-11-30 12:14:24 +01:00
|
|
|
*
|
|
|
|
|
* NOTE (rh): The main task of dbus-launch is (from the man page) to start
|
|
|
|
|
* a session bus and this is archieved by the current implemention.
|
|
|
|
|
*
|
|
|
|
|
* Additional on windows the session bus starting in not integrated
|
|
|
|
|
* into the logon process, so there is no need for any --syntax option.
|
|
|
|
|
* In fact (at least for kde on windows) the session bus is autostarted
|
|
|
|
|
* with the first application requesting a session bus.
|
|
|
|
|
*
|
2009-04-22 09:30:23 +02:00
|
|
|
*/
|
2007-06-01 22:24:57 +00:00
|
|
|
|
|
|
|
|
#define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1
|
|
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
#define DIM(x) (sizeof(x) / sizeof(x[0]))
|
|
|
|
|
#define WCSTRINGIFY_(x) L ## x
|
|
|
|
|
#define WCSTRINGIFY(x) WCSTRINGIFY_(x)
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
main (int argc, char **argv)
|
2007-06-01 22:24:57 +00:00
|
|
|
{
|
2010-03-16 15:54:00 +01:00
|
|
|
wchar_t dbusDaemonPath[MAX_PATH * 2 + 1];
|
|
|
|
|
wchar_t command[MAX_PATH * 2 + 1];
|
|
|
|
|
wchar_t *p;
|
Be more const-correct
As a general design principle, strings that we aren't going to modify
should usually be const. When compiling with -Wwrite-strings, quoted
string constants are of type "const char *", causing compiler warnings
when they are assigned to char * variables.
Unfortunately, we need to add casts in a few places:
* _dbus_list_append(), _dbus_test_oom_handling() and similar generic
"user-data" APIs take a void *, not a const void *, so we have
to cast
* For historical reasons the execve() family of functions take a
(char * const *), i.e. a constant pointer to an array of mutable
strings, so again we have to cast
* _dbus_spawn_async_with_babysitter similarly takes a char **,
although we can make it a little more const-correct by making it
take (char * const *) like execve() does
This also incorporates a subsequent patch by Thomas Zimmermann to
put various string constants in static storage, which is a little
more efficient.
Signed-off-by: Simon McVittie <smcv@debian.org>
Reviewed-by: Thomas Zimmermann <tdz@users.sourceforge.net>
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=97357
2016-10-07 17:41:01 +01:00
|
|
|
const wchar_t *daemon_name;
|
2007-06-01 22:24:57 +00:00
|
|
|
int result;
|
2015-03-04 08:19:59 +01:00
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
#ifdef DBUS_WINCE
|
|
|
|
|
char *s = NULL;
|
|
|
|
|
#else
|
2007-06-01 22:24:57 +00:00
|
|
|
char *s = getenv("DBUS_VERBOSE");
|
2010-03-16 15:54:00 +01:00
|
|
|
#endif
|
2007-06-01 22:24:57 +00:00
|
|
|
int verbose = s && *s != '\0' ? 1 : 0;
|
2010-03-16 15:54:00 +01:00
|
|
|
|
2007-06-01 22:24:57 +00:00
|
|
|
PROCESS_INFORMATION pi;
|
2010-03-16 15:54:00 +01:00
|
|
|
STARTUPINFOW si;
|
|
|
|
|
BOOL inherit = TRUE;
|
|
|
|
|
DWORD flags = 0;
|
|
|
|
|
|
|
|
|
|
GetModuleFileNameW (NULL, dbusDaemonPath, DIM (dbusDaemonPath));
|
2010-02-03 12:27:47 -08:00
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
daemon_name = WCSTRINGIFY(DBUS_DAEMON_NAME) L".exe";
|
2010-02-03 12:27:47 -08:00
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
if ((p = wcsrchr (dbusDaemonPath, L'\\')))
|
2007-06-01 22:24:57 +00:00
|
|
|
{
|
2010-03-16 15:54:00 +01:00
|
|
|
p[1] = L'\0';
|
|
|
|
|
wcscat_s (dbusDaemonPath, sizeof (dbusDaemonPath), daemon_name);
|
2007-06-01 22:24:57 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
2010-03-16 15:54:00 +01:00
|
|
|
fprintf (stderr, "error: could not extract path from current "
|
|
|
|
|
"applications module filename\n");
|
2007-06-01 22:24:57 +00:00
|
|
|
return 1;
|
|
|
|
|
}
|
2010-03-16 15:54:00 +01:00
|
|
|
|
|
|
|
|
#ifdef DBUS_WINCE
|
|
|
|
|
/* Windows CE has a different interpretation of cmdline: Start with argv[1]. */
|
|
|
|
|
wcscpy_s (command, sizeof (command), L"--session");
|
|
|
|
|
if (verbose)
|
|
|
|
|
fprintf (stderr, "%ls %ls\n", dbusDaemonPath, command);
|
|
|
|
|
#else
|
|
|
|
|
command[0] = L'\0';
|
2012-07-18 18:09:44 +01:00
|
|
|
/* Windows cmdline starts with path, which can contain spaces. */
|
|
|
|
|
wcscpy_s (command, sizeof (command), L"\"");
|
|
|
|
|
wcscat_s (command, sizeof (command), dbusDaemonPath);
|
|
|
|
|
wcscat_s (command, sizeof (command), L"\" --session");
|
2010-03-16 15:54:00 +01:00
|
|
|
if (verbose)
|
|
|
|
|
fprintf (stderr, "%ls\n", command);
|
|
|
|
|
#endif
|
2007-06-01 22:24:57 +00:00
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
memset (&si, 0, sizeof (si));
|
|
|
|
|
memset (&pi, 0, sizeof (pi));
|
|
|
|
|
si.cb = sizeof (si);
|
2007-06-01 22:24:57 +00:00
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
if (verbose)
|
|
|
|
|
flags |= CREATE_NEW_CONSOLE;
|
|
|
|
|
|
|
|
|
|
#ifdef DBUS_WINCE
|
|
|
|
|
inherit = FALSE;
|
|
|
|
|
#else
|
|
|
|
|
flags |= NORMAL_PRIORITY_CLASS;
|
|
|
|
|
if (!verbose)
|
|
|
|
|
flags |= DETACHED_PROCESS;
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
result = CreateProcessW (dbusDaemonPath, command, 0, 0,
|
|
|
|
|
inherit, flags, 0, 0, &si, &pi);
|
2007-06-01 22:24:57 +00:00
|
|
|
|
|
|
|
|
if (result == 0)
|
|
|
|
|
{
|
|
|
|
|
if (verbose)
|
2013-08-19 21:29:18 +02:00
|
|
|
fprintf (stderr, "Could not start " DBUS_DAEMON_NAME ". error=%u\n",
|
|
|
|
|
(unsigned)GetLastError ());
|
2007-06-01 22:24:57 +00:00
|
|
|
return 4;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 15:54:00 +01:00
|
|
|
CloseHandle (pi.hProcess);
|
|
|
|
|
CloseHandle (pi.hThread);
|
|
|
|
|
|
2007-06-01 22:24:57 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|