Merge branch 'my-dbus-1.2'
[platform/upstream/dbus.git] / tools / dbus-launch-win.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-launch.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 #include <windows.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <mbstring.h>
27 #include <assert.h>
28
29 #if defined __MINGW32__ || (defined _MSC_VER && _MSC_VER <= 1310)
30 /* save string functions version
31 */ 
32 #define errno_t int
33
34 errno_t strcat_s(char *dest, size_t size, char *src) 
35 {
36   assert(strlen(dest) + strlen(src) +1 <= size);
37   strcat(dest,src);
38   return 0;
39 }
40
41 errno_t strcpy_s(char *dest, size_t size, char *src)
42 {
43   assert(strlen(src) +1 <= size);
44   strcpy(dest,src);  
45   return 0;
46 }
47 #endif
48
49 /* TODO: Use Unicode APIs */
50
51 /* TODO (tl): This Windows version of dbus-launch is curretly rather
52  * pointless as it doesn't take the same command-line options as the
53  * UNIX dbus-launch does. A main point of the dbus-launch command is
54  * to pass it for instance a --config-file option to make the started
55  * dbus-daemon use that config file.
56  * 
57  * This version also doesn't print out any information, which is a
58  * main point of the UNIX one. It should at least support the
59  * --sh-syntax option, and maybe also a --cmd-syntax to print out the
60  * variable settings in cmd.exe syntax?
61  * 
62  * NOTE (rh): The main task of dbus-launch is (from the man page) to start 
63  * a session bus and this is archieved by the current implemention. 
64  * 
65  * Additional on windows the session bus starting in not integrated 
66  * into the logon process, so there is no need for any --syntax option. 
67  * In fact (at least for kde on windows) the session bus is autostarted 
68  * with the first application requesting a session bus. 
69  *
70  */
71
72 #define AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE 1
73
74 int main(int argc,char **argv)
75 {
76   char dbusDaemonPath[MAX_PATH*2+1];
77   char command[MAX_PATH*2+1];
78   char *p;
79   char *daemon_name;
80   int result;
81   int showConsole = 0;
82   char *s = getenv("DBUS_VERBOSE");
83   int verbose = s && *s != '\0' ? 1 : 0;
84   PROCESS_INFORMATION pi;
85   STARTUPINFO si;
86   HANDLE h; 
87   
88 #ifdef AUTO_ACTIVATE_CONSOLE_WHEN_VERBOSE_MODE
89   if (verbose)
90       showConsole = 1; 
91 #endif
92   GetModuleFileName(NULL,dbusDaemonPath,sizeof(dbusDaemonPath));
93
94 #ifdef _DEBUG
95       daemon_name = "dbus-daemond.exe";
96 #else
97       daemon_name = "dbus-daemon.exe";
98 #endif
99   if ((p = _mbsrchr (dbusDaemonPath, '\\'))) 
100     {
101       *(p+1)= '\0';
102       strcat_s(dbusDaemonPath,sizeof(dbusDaemonPath),daemon_name);
103     }
104   else 
105     {
106       if (verbose)
107           fprintf(stderr,"error: could not extract path from current applications module filename\n");
108       return 1;
109     } 
110   
111   strcpy_s(command,sizeof(command),dbusDaemonPath);
112   strcat_s(command,sizeof(command)," --session");
113   if (verbose)
114       fprintf(stderr,"%s\n",command);
115   
116   memset(&si, 0, sizeof(si));
117   memset(&pi, 0, sizeof(pi));
118   si.cb = sizeof(si);
119
120   result = CreateProcess(NULL, 
121                   command,
122                   0,
123                   0,
124                   TRUE,
125                   (showConsole ? CREATE_NEW_CONSOLE : 0) | NORMAL_PRIORITY_CLASS,
126                   0,
127                   0,
128                   &si,
129                   &pi);
130
131   CloseHandle(pi.hProcess);
132   CloseHandle(pi.hThread);
133
134   if (result == 0) 
135     {
136       if (verbose)
137           fprintf(stderr, "Could not start dbus-daemon error=%d",GetLastError());
138       return 4;
139     }
140    
141   return 0;
142 }