1
/* $XConsortium: remote.c,v 1.15 95/01/03 17:26:52 mor Exp $ */
2
/******************************************************************************
4
Copyright (c) 1993, 1999 X Consortium
6
Permission is hereby granted, free of charge, to any person obtaining a copy
7
of this software and associated documentation files (the "Software"), to deal
8
in the Software without restriction, including without limitation the rights
9
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
copies of the Software, and to permit persons to whom the Software is
11
furnished to do so, subject to the following conditions:
13
The above copyright notice and this permission notice shall be included in
14
all copies or substantial portions of the Software.
16
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
Except as contained in this notice, the name of the X Consortium shall not be
24
used in advertising or otherwise to promote the sale, use or other dealings
25
in this Software without prior written authorization from the X Consortium.
26
******************************************************************************/
29
* We use the rstart protocol to restart clients on remote machines.
38
#include <X11/ICE/ICElib.h>
39
#include <X11/ICE/ICEutil.h>
44
static char *format_rstart_env (char *);
45
static void close_child (GPid pid, gint status, gpointer ignore);
48
close_child (GPid pid, gint status, gpointer ignore)
50
g_spawn_close_pid (pid);
54
remote_start (char *restart_info, char **argv,
55
char *cwd, char **envp,
56
GPid *child_pid, GError **error)
67
return gsm_exec_async (cwd, argv, envp, child_pid, error);
70
if (strncmp (restart_info, RSTART_RSH, sizeof (RSTART_RSH))
71
|| restart_info[sizeof (RSTART_RSH)] != '/')
73
return gsm_exec_async (cwd, argv, envp, child_pid, error);
76
machine = restart_info + sizeof (RSTART_RSH) + 1;
79
return gsm_exec_async (cwd, argv, envp, child_pid, error);
82
g_message ("Attempting to restart remote client on %s\n",
85
rargv[0] = RSH_COMMAND;
90
/* TODO: we would like to report whether rstartd failed to a log
91
window in the parent. We could do this by checking all the
92
fprintf()s for error return. */
93
if (! g_spawn_async_with_pipes (NULL, rargv, NULL,
94
G_SPAWN_DO_NOT_REAP_CHILD,
95
NULL, NULL, child_pid,
96
&pipefd, NULL, NULL, error))
98
g_child_watch_add (*child_pid, close_child, NULL);
100
fp = (FILE *) fdopen (pipefd, "w");
102
fprintf (fp, "CONTEXT X\n");
103
fprintf (fp, "DIR %s\n", cwd);
104
fprintf (fp, "DETACH\n");
109
* The application saved its environment.
112
for (i = 0; envp[i]; i++)
115
* rstart requires that any spaces, backslashes, or
116
* non-printable characters inside of a string be
117
* represented by octal escape sequences.
120
char *temp = format_rstart_env (envp[i]);
121
fprintf (fp, "MISC X %s\n", temp);
129
* The application did not save its environment.
130
* The default PATH set up by rstart may not contain
131
* the program we want to restart. We play it safe
132
* and pass xsm's PATH. This will most likely contain
136
const char *path = g_getenv ("PATH");
139
fprintf (fp, "MISC X PATH=%s\n", path);
143
/* FIXME: implement */
144
fprintf (fp, "MISC X %s\n", non_local_display_env);
145
fprintf (fp, "MISC X %s\n", non_local_session_env);
149
* Pass the authentication data.
150
* Each transport has auth data for ICE and XSMP.
151
* Don't pass local auth data.
153
for (list = auth_entries; list != NULL; list = list->next)
155
IceAuthFileEntry *entry = (IceAuthFileEntry *) list->data;
156
if (strstr (entry->network_id, "local/"))
159
fprintf (fp, "AUTH ICE %s \"\" %s %s ",
160
entry->protocol_name,
164
for (i = 0; i < entry->auth_data_length; ++i)
165
fprintf (fp, "%02x", entry->auth_data[i]);
171
* And execute the program
174
fprintf (fp, "EXEC %s %s", argv[0], argv[0]);
175
for (i = 1; argv[i]; i++)
176
fprintf (fp, " %s", argv[i]);
177
fprintf (fp, "\n\n");
185
* rstart requires that any spaces/backslashes/non-printable characters
186
* inside of a string be represented by octal escape sequences.
190
format_rstart_env (char *str)
192
int escape_count = 0, i;
193
unsigned char *temp = (unsigned char *) str;
195
while (*temp != '\0')
197
if (!isgraph (*temp) || *temp == '\\')
202
if (escape_count == 0)
206
int len = strlen (str) + 1 + (escape_count * 3);
207
char *ret = (char *) g_malloc (len);
210
temp = (unsigned char *) str;
211
while (*temp != '\0')
213
if (!isgraph (*temp) || *temp == '\\')
216
g_snprintf (octal, sizeof (octal), "%o", *temp);
218
for (i = 0; i < (3 - (int) strlen (octal)); i++)
221
ptr += strlen (octal);