4
* (c) Copyright 1992-1994 Adobe Systems Incorporated.
7
* Permission to use, copy, modify, distribute, and sublicense this software
8
* and its documentation for any purpose and without fee is hereby granted,
9
* provided that the above copyright notices appear in all copies and that
10
* both those copyright notices and this permission notice appear in
11
* supporting documentation and that the name of Adobe Systems Incorporated
12
* not be used in advertising or publicity pertaining to distribution of the
13
* software without specific, written prior permission. No trademark license
14
* to use the Adobe trademarks is hereby granted. If the Adobe trademark
15
* "Display PostScript"(tm) is used to describe this software, its
16
* functionality or for any other purpose, such use shall be limited to a
17
* statement that this software works in conjunction with the Display
18
* PostScript system. Proper trademark attribution to reflect Adobe's
19
* ownership of the trademark shall be given whenever any such reference to
20
* the Display PostScript system is made.
22
* ADOBE MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THE SOFTWARE FOR
23
* ANY PURPOSE. IT IS PROVIDED "AS IS" WITHOUT EXPRESS OR IMPLIED WARRANTY.
24
* ADOBE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
25
* IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26
* NON- INFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL ADOBE BE LIABLE
27
* TO YOU OR ANY OTHER PARTY FOR ANY SPECIAL, INDIRECT, OR CONSEQUENTIAL
28
* DAMAGES OR ANY DAMAGES WHATSOEVER WHETHER IN AN ACTION OF CONTRACT,
29
* NEGLIGENCE, STRICT LIABILITY OR ANY OTHER ACTION ARISING OUT OF OR IN
30
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ADOBE WILL NOT
31
* PROVIDE ANY TRAINING OR OTHER SUPPORT FOR THE SOFTWARE.
33
* Adobe, PostScript, and Display PostScript are trademarks of Adobe Systems
34
* Incorporated which may be registered in certain jurisdictions
36
* Author: Adobe Systems Incorporated
38
/* $XFree86: xc/lib/dps/csstartNX.c,v 1.7 2002/05/31 18:45:48 dawes Exp $ */
40
#include <sys/param.h>
42
#include <X11/Xlibint.h>
44
#include <DPS/dpsNXargs.h>
45
#include <sys/socket.h>
49
#include "DPSCAPproto.h"
50
#include "Xlibnet.h" /* New for R5, delete for R4 */
51
#include "dpsassert.h"
53
#include "csstartNX.h"
59
#define DOZETIME 1 /* time to wait for agent to start up (sec) */
61
#define BASE_TCP_PORT CSDPSPORT
64
#define CSDPSMAXPORT 16
68
#define SO_REUSEADDR 1
73
pid_t gSecretAgentPID = 0; /* PID of launched agent *Shh!* Not public! */
75
/* ---Private Functions--- */
80
struct sockaddr_in insock;
82
unsigned short port, startPort = 0;
83
struct servent *serventInfo;
87
/* Ultrix has a nasty bug in getservbyname(). If the name passed
88
to it doesn't exist in the services list it will seg. fault...
90
if ((serventInfo = getservbyname(DPS_NX_SERV_NAME,
92
if (strcmp("tcp", serventInfo->s_proto) == 0) {
93
startPort = ntohs(serventInfo->s_port);
95
/* So, for Ultrix we just default to the default default port :-) */
97
if (startPort == 0) startPort = BASE_TCP_PORT;
98
if ((request = socket (AF_INET, SOCK_STREAM, 0)) < 0)
100
DPSWarnProc(NULL, "Creating TCP socket while recommending port\n");
104
/* Necesary to restart the server without a reboot */
107
setsockopt(request, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(int));
109
/* What the hell is all this? I'll tell you. We don't know
110
a prioi what port is free, so we try to bind to each
111
in sequence and return the one that works. */
114
struct linger lingere;
116
lingere.l_onoff = 0; /* off */
117
lingere.l_linger = 0; /* don't */
118
if(setsockopt(request, SOL_SOCKET, SO_LINGER, (char *)&lingere,
119
sizeof(struct linger)) != 0)
121
"Couldn't set TCP SO_DONTLINGER while recommending port.");
124
#endif /* SO_REUSEADDR */
125
bzero((char *)&insock, sizeof (insock));
126
insock.sin_family = AF_INET;
127
insock.sin_addr.s_addr = htonl(INADDR_ANY);
130
for (port = startPort; (int) port < (int) startPort + CSDPSMAXPORT; port++)
133
insock.sin_port = htons(port);
136
result = bind(request, (struct sockaddr *) &insock, sizeof (insock));
139
if (errno != EADDRINUSE)
141
DPSWarnProc(NULL, "Binding TCP socket while recommending port.\n");
149
/* We have a good port number */
155
return (okay) ? port : -1;
158
/* ---Functions--- */
161
XDPSNXRecommendPort(int transport)
167
case XDPSNX_TRANS_UNIX:
168
/* If the TCP socket exists, we just assume the UNIX one
169
is there too. FALL THRU! */
170
case XDPSNX_TRANS_TCP: /* TCP */
179
StartXDPSNX(char **additionalArgs)
183
int argc = 1; /* 1, args[0]:=path, args[1]:=null */
185
int status = Success; /* assume we'll succeed */
186
char *execObj, **execArgs;
188
(void) XDPSGetNXArg(XDPSNX_EXEC_FILE, (void **) &execObj);
189
if (execObj == 0) return (!Success);
191
/* Create the argv list for the execl() call */
192
(void) XDPSGetNXArg(XDPSNX_EXEC_ARGS, (void **) &execArgs);
194
for(cpp = execArgs; *cpp != 0; cpp++, argc++); /* count args. */
195
if (additionalArgs != 0) /* add on the add-on args. */
196
for(cpp = additionalArgs; *cpp != 0; cpp++, argc++);
198
args = (char **) Xmalloc(sizeof(char *) * (argc+1));
201
args[argc] = 0; /* cap end of args */
203
if (additionalArgs != 0)
204
for(cpp = additionalArgs; *cpp != 0; cpp++, i++) args[i] = *cpp;
206
for(cpp = execArgs; *cpp != 0; cpp++, i++) args[i] = *cpp;
208
/* now try to start up the agent... */
209
if ((childPid = fork()) != -1) {
210
if (childPid == 0) { /* Child process */
213
DPSWarnProc(NULL, "Agent unable to create session. Continuing...\n");
216
/* Try to start the agent */
217
if (execvp(args[0], args) < 0) { /* Error!! */
218
exit(1); /* This is OKAY, we're the child here */
220
/* SHOULD NEVER REACH HERE */
221
} else { /* Parent (NX Client) */
222
(void) sleep(DOZETIME);
223
/* if decmips, pray that we hesitate long enough for the child... */
224
/* Check on child (NX Agent) */
225
if (waitpid(childPid, NULL, WNOHANG) != 0) {
226
/* Server terminated or stopped; don't care, result is same... */
228
} else { /* we think the agent started okay */
229
gSecretAgentPID = childPid; /* set secret global */
232
} else { /* Error in fork */
235
if (args != 0) (void) XFree(args);