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