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 (at) 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 40 static errno_t 41 wcscat_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 49 static errno_t 50 wcscpy_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 84 int 85 main (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 cmdline starts with path, which can contain spaces. */ 134 wcscpy_s (command, sizeof (command), L"\""); 135 wcscat_s (command, sizeof (command), dbusDaemonPath); 136 wcscat_s (command, sizeof (command), L"\" --session"); 137 if (verbose) 138 fprintf (stderr, "%ls\n", command); 139 #endif 140 141 memset (&si, 0, sizeof (si)); 142 memset (&pi, 0, sizeof (pi)); 143 si.cb = sizeof (si); 144 145 if (verbose) 146 flags |= CREATE_NEW_CONSOLE; 147 148 #ifdef DBUS_WINCE 149 inherit = FALSE; 150 #else 151 flags |= NORMAL_PRIORITY_CLASS; 152 if (!verbose) 153 flags |= DETACHED_PROCESS; 154 #endif 155 156 result = CreateProcessW (dbusDaemonPath, command, 0, 0, 157 inherit, flags, 0, 0, &si, &pi); 158 159 if (result == 0) 160 { 161 if (verbose) 162 fprintf (stderr, "Could not start " DBUS_DAEMON_NAME ". error=%d\n", 163 GetLastError ()); 164 return 4; 165 } 166 167 CloseHandle (pi.hProcess); 168 CloseHandle (pi.hThread); 169 170 return 0; 171 } 172