1/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ 2/* dbus-launch-win.c dbus-launch utility 3 * 4 * Copyright (C) 2007 Ralf Habacker <ralf.habacker@freenet.de> 5 * 6 * Licensed under the Academic Free License version 2.1 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24#include <config.h> 25#ifndef UNICODE 26#define UNICODE 1 27#endif 28#include <windows.h> 29#include <stdio.h> 30#include <string.h> 31#include <assert.h> 32 33/* Save string functions. Instead of figuring out the exact _MSC_VER 34 that work, override for everybody. */ 35 36#define errno_t int 37#define wcscat_s my_wcscat_s 38#define wcscpy_s my_wcscpy_s 39 40static errno_t 41wcscat_s (wchar_t *dest, size_t size, wchar_t *src) 42{ 43 assert (sizeof (wchar_t) * (wcslen (dest) + wcslen (src) + 1) <= size); 44 wcscat (dest, src); 45 return 0; 46} 47 48 49static errno_t 50wcscpy_s (wchar_t *dest, size_t size, wchar_t *src) 51{ 52 assert (sizeof (wchar_t) * (wcslen (src) + 1) <= size); 53 wcscpy (dest, src); 54 return 0; 55} 56 57/* TODO (tl): This Windows version of dbus-launch is curretly rather 58 * pointless as it doesn't take the same command-line options as the 59 * UNIX dbus-launch does. A main point of the dbus-launch command is 60 * to pass it for instance a --config-file option to make the started 61 * dbus-daemon use that config file. 62 * 63 * This version also doesn't print out any information, which is a 64 * main point of the UNIX one. It should at least support the 65 * --sh-syntax option, and maybe also a --cmd-syntax to print out the 66 * variable settings in cmd.exe syntax? 67 * 68 * NOTE (rh): The main task of dbus-launch is (from the man page) to start 69 * a session bus and this is archieved by the current implemention. 70 * 71 * Additional on windows the session bus starting in not integrated 72 * into the logon process, so there is no need for any --syntax option. 73 * In fact (at least for kde on windows) the session bus is autostarted 74 * with the first application requesting a session bus. 75 * 76 */ 77 78#define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1 79 80#define DIM(x) (sizeof(x) / sizeof(x[0])) 81#define WCSTRINGIFY_(x) L ## x 82#define WCSTRINGIFY(x) WCSTRINGIFY_(x) 83 84int 85main (int argc, char **argv) 86{ 87 wchar_t dbusDaemonPath[MAX_PATH * 2 + 1]; 88 wchar_t command[MAX_PATH * 2 + 1]; 89 wchar_t *p; 90 wchar_t *daemon_name; 91 int result; 92 int showConsole = 0; 93#ifdef DBUS_WINCE 94 char *s = NULL; 95#else 96 char *s = getenv("DBUS_VERBOSE"); 97#endif 98 int verbose = s && *s != '\0' ? 1 : 0; 99 100 PROCESS_INFORMATION pi; 101 STARTUPINFOW si; 102 BOOL inherit = TRUE; 103 DWORD flags = 0; 104 105#ifdef AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 106 if (verbose) 107 showConsole = 1; 108#endif 109 GetModuleFileNameW (NULL, dbusDaemonPath, DIM (dbusDaemonPath)); 110 111 daemon_name = WCSTRINGIFY(DBUS_DAEMON_NAME) L".exe"; 112 113 if ((p = wcsrchr (dbusDaemonPath, L'\\'))) 114 { 115 p[1] = L'\0'; 116 wcscat_s (dbusDaemonPath, sizeof (dbusDaemonPath), daemon_name); 117 } 118 else 119 { 120 if (verbose) 121 fprintf (stderr, "error: could not extract path from current " 122 "applications module filename\n"); 123 return 1; 124 } 125 126#ifdef DBUS_WINCE 127 /* Windows CE has a different interpretation of cmdline: Start with argv[1]. */ 128 wcscpy_s (command, sizeof (command), L"--session"); 129 if (verbose) 130 fprintf (stderr, "%ls %ls\n", dbusDaemonPath, command); 131#else 132 command[0] = L'\0'; 133 /* Windows CE has a different interpretation of cmdline: Start with argv[1]. */ 134 wcscpy_s (command, sizeof (command), dbusDaemonPath); 135 wcscat_s (command, sizeof (command), L" --session"); 136 if (verbose) 137 fprintf (stderr, "%ls\n", command); 138#endif 139 140 memset (&si, 0, sizeof (si)); 141 memset (&pi, 0, sizeof (pi)); 142 si.cb = sizeof (si); 143 144 if (verbose) 145 flags |= CREATE_NEW_CONSOLE; 146 147#ifdef DBUS_WINCE 148 inherit = FALSE; 149#else 150 flags |= NORMAL_PRIORITY_CLASS; 151 if (!verbose) 152 flags |= DETACHED_PROCESS; 153#endif 154 155 result = CreateProcessW (dbusDaemonPath, command, 0, 0, 156 inherit, flags, 0, 0, &si, &pi); 157 158 if (result == 0) 159 { 160 if (verbose) 161 fprintf (stderr, "Could not start " DBUS_DAEMON_NAME ". error=%d\n", 162 GetLastError ()); 163 return 4; 164 } 165 166 CloseHandle (pi.hProcess); 167 CloseHandle (pi.hThread); 168 169 return 0; 170} 171