dbus-marshal-validate: Validate length of arrays of fixed-length items
[platform/upstream/dbus.git] / tools / dbus-launch-win.c
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
40 static errno_t
41 wcscat_s (wchar_t *dest, size_t size, const 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, const 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   const wchar_t *daemon_name;
91   int result;
92
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   GetModuleFileNameW (NULL, dbusDaemonPath, DIM (dbusDaemonPath));
106   
107   daemon_name = WCSTRINGIFY(DBUS_DAEMON_NAME) L".exe";
108   
109   if ((p = wcsrchr (dbusDaemonPath, L'\\'))) 
110     {
111       p[1] = L'\0';
112       wcscat_s (dbusDaemonPath, sizeof (dbusDaemonPath), daemon_name);
113     }
114   else 
115     {
116       if (verbose)
117           fprintf (stderr, "error: could not extract path from current "
118                    "applications module filename\n");
119       return 1;
120     } 
121
122 #ifdef DBUS_WINCE
123    /* Windows CE has a different interpretation of cmdline: Start with argv[1].  */
124    wcscpy_s (command, sizeof (command), L"--session");
125    if (verbose)
126      fprintf (stderr, "%ls %ls\n", dbusDaemonPath, command);
127 #else
128    command[0] = L'\0';
129    /* Windows cmdline starts with path, which can contain spaces.  */
130    wcscpy_s (command, sizeof (command), L"\"");
131    wcscat_s (command, sizeof (command), dbusDaemonPath);
132    wcscat_s (command, sizeof (command), L"\" --session");
133    if (verbose)
134      fprintf (stderr, "%ls\n", command);
135 #endif
136   
137   memset (&si, 0, sizeof (si));
138   memset (&pi, 0, sizeof (pi));
139   si.cb = sizeof (si);
140   
141   if (verbose)
142     flags |= CREATE_NEW_CONSOLE;
143
144 #ifdef DBUS_WINCE
145   inherit = FALSE;
146 #else
147   flags |= NORMAL_PRIORITY_CLASS;
148   if (!verbose)
149     flags |= DETACHED_PROCESS;
150 #endif
151
152   result = CreateProcessW (dbusDaemonPath, command, 0, 0,
153                            inherit, flags, 0, 0, &si, &pi);
154
155   if (result == 0) 
156     {
157       if (verbose)
158         fprintf (stderr, "Could not start " DBUS_DAEMON_NAME ". error=%u\n",
159                  (unsigned)GetLastError ());
160       return 4;
161     }
162    
163   CloseHandle (pi.hProcess);
164   CloseHandle (pi.hThread);
165
166   return 0;
167 }