1 //============================================================================
5 // The eCos synthetic target I/O auxiliary
7 //============================================================================
8 //####COPYRIGHTBEGIN####
10 // ----------------------------------------------------------------------------
11 // Copyright (C) 2002 Bart Veer
13 // This file is part of the eCos host tools.
15 // This program is free software; you can redistribute it and/or modify it
16 // under the terms of the GNU General Public License as published by the Free
17 // Software Foundation; either version 2 of the License, or (at your option)
20 // This program is distributed in the hope that it will be useful, but WITHOUT
21 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
25 // You should have received a copy of the GNU General Public License along with
26 // this program; if not, write to the Free Software Foundation, Inc.,
27 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
29 // ----------------------------------------------------------------------------
31 //####COPYRIGHTEND####
32 //============================================================================
33 //#####DESCRIPTIONBEGIN####
41 // The main module for the eCos synthetic target auxiliary. This
42 // program is fork'ed and execve'd during the initialization phase of
43 // any synthetic target application, and is primarily responsible for
44 // I/O operations. This program should never be run directly by a
47 //####DESCRIPTIONEND####
48 //============================================================================
58 #include <sys/param.h>
59 #include <sys/types.h>
60 // Avoid compatibility problems with Tcl 8.4 vs. earlier
65 // The protocol between host and target is defined by a private
66 // target-side header.
67 #include "../src/synth_protocol.h"
69 // ----------------------------------------------------------------------------
71 #define PROGNAME "Synthetic target auxiliary"
73 static int no_windows = 0; // -nw arg
75 static pid_t parent_pid;
77 // ----------------------------------------------------------------------------
80 // When a synthetic target application is executed, whether directly
81 // or from inside gdb, it can fork() and execve() the synthetic
82 // target auxiliary: this program, ecosynth. For now this is disabled
83 // by default but can be enabled using a --io option on the command line.
84 // In future it may be enabled by default, but can be suppressed using
87 // stdin, stdout, and stderr will be inherited from the eCos application, so
88 // that attempts to use printf() or fprintf(stderr, ) from inside the
89 // auxiliary or its helper applications work as expected. In addition file
90 // descriptor 3 will be a pipe from the eCos application, and file descriptor
91 // 4 will be a pipe to the application. The protocol defined in
92 // ../src/synth_protocol.h runs over these pip
94 // argv[] and environ are also as per the application, with the exception
95 // of argv[0] which is the full path to this application.
97 // The bulk of the hard work is done inside Tcl. The C++ code is
98 // responsible only for initialization and for implementing some
99 // additional commands, for example to send a SIGIO signal to the
100 // parent when there is a new pending interrupt. The primary script is
101 // ecosynth.tcl which should be installed alongside the executable.
103 // This code makes use of the standard Tcl initialization facilities:
105 // 1) main() calls Tcl_Main() with the command-line arguments and an
106 // application-specific initialization routine ecosynth_appinit().
108 // 2) Tcl_Main() goes through the initialization sequence in generic/tclMain.c.
109 // There is one slight complication: Tcl_main() interprets arguments in
110 // a way that makes sense for tclsh, but not for ecosynth. Specially if
111 // argv[1] exists and does not begin with a - then it will be interpreted
112 // as a script to be executed. Hence argv[] is re-allocated and the name
113 // of a suitable script is inserted.
115 // 3) ecosynth_appinit() initializes the Tcl interpreter, and optionally
116 // the Tk interpreter as well. This is controlled by the presence of
117 // a -nw argument on the command line. This initialization routine
118 // also takes care of adding some commands to the Tcl interpreter.
120 // 4) Tcl_Main() will now proceed to execute the startup script, which
121 // is eccentric.tcl installed in the libexec directory.
123 static int ecosynth_appinit(Tcl_Interp*);
126 main(int argc, char** argv)
128 char ecosynth_tcl_path[_POSIX_PATH_MAX];
132 parent_pid = getppid();
134 // The various core Tcl scripts are installed in the same
135 // directory as ecosynth. The Tcl script itself will check whether
136 // there is a newer version of itself in the source tree and
137 // switch to that instead.
138 assert((strlen(LIBEXECDIR) + strlen(PACKAGE_INSTALL) + 20) < _POSIX_PATH_MAX);
139 strcpy(ecosynth_tcl_path, LIBEXECDIR);
140 strcat(ecosynth_tcl_path, "/ecos/");
141 strcat(ecosynth_tcl_path, PACKAGE_INSTALL);
142 strcat(ecosynth_tcl_path, "/ecosynth.tcl");
144 // Installation sanity checks.
145 if (0 != access(ecosynth_tcl_path, F_OK)) {
146 fprintf(stderr, PROGNAME ": error, a required Tcl script has not been installed.\n");
147 fprintf(stderr, " The script is \"%s\"\n", ecosynth_tcl_path);
150 if (0 != access(ecosynth_tcl_path, R_OK)) {
151 fprintf(stderr, PROGNAME ": error, no read access to a required Tcl script.\n");
152 fprintf(stderr, " The script is \"%s\"\n", ecosynth_tcl_path);
156 // Look for options -nw and -w. This information is needed by the appinit() routine.
158 for (i = 1; i < argc; i++) {
159 if ((0 == strcmp("-nw", argv[i])) || (0 == strcmp("--nw", argv[i])) ||
160 (0 == strcmp("-no-windows", argv[i])) || (0 == strcmp("--no-windows", argv[i]))) {
162 } else if ((0 == strcmp("-w", argv[i])) || (0 == strcmp("--w", argv[i])) ||
163 (0 == strcmp("-windows", argv[i])) || (0 == strcmp("--windows", argv[i]))) {
168 new_argv = malloc((argc+2) * sizeof(char*));
169 if (NULL == new_argv) {
170 fprintf(stderr, PROGNAME ": internal error, out of memory.\n");
173 new_argv[0] = argv[0];
174 new_argv[1] = ecosynth_tcl_path;
175 for (i = 1; i < argc; i++) {
176 new_argv[i+1] = argv[i];
178 new_argv[i+1] = NULL;
180 // Ignore SIGINT requests. Those can happen if e.g. the application is
181 // ctrl-C'd or if a gdb session is interrupted because this process is
182 // a child of the eCos application. Instead the normal code for handling
183 // application termination needs to run.
184 signal(SIGINT, SIG_IGN);
186 // Similarly ignore SIGTSTP if running in graphical mode, it would
187 // be inappropriate for the GUI to freeze if the eCos application is
188 // suspended. If running in text mode then it is better for both
189 // the application and the I/O auxiliary to freeze, halting any further
192 signal(SIGTSTP, SIG_IGN);
195 Tcl_Main(argc+1, new_argv, &ecosynth_appinit);
200 // ----------------------------------------------------------------------------
201 // Commands for the Tcl interpreter. These are few and far between because
202 // as much work as possible is done in Tcl.
204 // Send a SIGIO signal to the parent process. This is done whenever a new
205 // interrupt is pending. There is a possibility of strange behaviour if
206 // the synthetic target application is exiting at just the wrong moment
207 // and this process has become a zombie. An alternative approach would
208 // involve loading the Extended Tcl extension.
210 ecosynth_send_SIGIO(ClientData clientData __attribute__ ((unused)),
213 char** argv __attribute__ ((unused)))
216 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_send_SIGIO\"" , TCL_STATIC);
220 (void) kill(parent_pid, SIGIO);
224 // Similarly send a SIGKILL (-9) to the parent process. This allows the GUI
225 // code to kill of the eCos application
227 ecosynth_send_SIGKILL(ClientData clientData __attribute__ ((unused)),
230 char** argv __attribute__ ((unused)))
233 Tcl_SetResult(interp, "wrong # args: should be \"usbtest::_send_SIGIO\"" , TCL_STATIC);
237 (void) kill(parent_pid, SIGKILL);
242 // ----------------------------------------------------------------------------
243 // Application-specific initialization.
246 ecosynth_appinit(Tcl_Interp* interp)
248 Tcl_Channel from_app;
251 // Tcl library initialization. This has the effect of executing init.tcl,
252 // thus setting up package load paths etc. Not all of that initialization
253 // is necessarily appropriate for ecosynth, but some devices may well want
254 // to load in additional packages or whatever.
255 if (Tcl_Init(interp) == TCL_ERROR) {
258 // Optionally initialize Tk as well. This can be suppressed by an
259 // argument -nw. Possibly this could be done by the Tcl script
260 // instead using dynamic loading.
262 // There is a problem with the way that Tk does its argument processing.
263 // By default it will accept abbreviations for the standard wish arguments,
264 // so if the user specifies e.g. -v then the Tk code will interpret this
265 // as an abbreviation for -visual, and will probably complain because
266 // -visual takes an argument whereas ecosynth's -v is just a flag.
268 // The Tk argument processing can be suppressed simply by temporarily
269 // getting rid of the argv variable. The disadvantage is that some
270 // standard arguments now have to be processed explicitly:
272 // -colormap map ignored, of little interest these days
273 // -display display currently ignored. This would have to be
274 // implemented at the C level, not the Tcl level,
275 // since Tk_Init() will start interacting with the
276 // X server. Its implementation would involve a
278 // -geometry geom implemented in the Tcl code using "wm geometry"
279 // -name name ignored for now.
280 // -sync ignored, of little interest to typical users
281 // -use id ignored, probably of little interest for now
282 // -visual visual ignored, of little interest these days
284 // so actually the only extra work that is required is for the Tcl code
285 // to process -geometry.
287 Tcl_Obj* argv = Tcl_GetVar2Ex(interp, "argv", NULL, TCL_GLOBAL_ONLY);
288 Tcl_IncrRefCount(argv);
289 Tcl_UnsetVar(interp, "argv", TCL_GLOBAL_ONLY);
290 if (Tk_Init(interp) == TCL_ERROR) {
293 Tcl_SetVar2Ex(interp, "argv", NULL, argv, TCL_GLOBAL_ONLY);
294 Tcl_DecrRefCount(argv);
297 // Create the synth:: namespace. Currently this does not seem to be possible from
299 if (TCL_OK != Tcl_Eval(interp,
300 "namespace eval synth {\n"
301 " variable channel_from_app 0\n"
302 " variable channel_to_app 0\n"
304 fprintf(stderr, PROGNAME ": internal error, failed to create Tcl synth:: namespace\n");
305 fprintf(stderr, " Please check Tcl version (8.3 or later required).\n");
309 // The pipe to/from the application is exposed to Tcl, allowing
310 // the main communication to be handled at the Tcl level and
311 // specifically via the event loop. This requires two additional
312 // Tcl channels to be created for file descriptors 3 and 4.
313 from_app = Tcl_MakeFileChannel((ClientData) 3, TCL_READABLE);
314 if (NULL == from_app) {
315 fprintf(stderr, PROGNAME ": internal error, failed to create Tcl channel for pipe from application.\n");
318 Tcl_RegisterChannel(interp, from_app);
320 // The channel name will be something like file0. Add a variable to the
321 // interpreter to store this name.
322 if (NULL == Tcl_SetVar(interp, "synth::_channel_from_app", Tcl_GetChannelName(from_app), TCL_GLOBAL_ONLY)) {
323 fprintf(stderr, PROGNAME ": internal error, failed to create Tcl variable from application channel 0\n");
327 // Repeat for the channel to the application.
328 to_app = Tcl_MakeFileChannel((ClientData) 4, TCL_WRITABLE);
329 if (NULL == to_app) {
330 fprintf(stderr, PROGNAME ": internal error, failed to create Tcl channel for pipe to application.\n");
333 Tcl_RegisterChannel(interp, to_app);
334 if (NULL == Tcl_SetVar(interp, "synth::_channel_to_app", Tcl_GetChannelName(to_app), TCL_GLOBAL_ONLY)) {
335 fprintf(stderr, PROGNAME ": internal error, failed to create Tcl variable from application channel 1\n");
339 // The auxiliary may spawn additional programs, via
340 // device-specific scripts. Those programs should not have
341 // direct access to the pipe - that would just lead to
343 fcntl(3, F_SETFD, FD_CLOEXEC);
344 fcntl(4, F_SETFD, FD_CLOEXEC);
346 // Add synthetic target-specific commands to the Tcl interpreter.
347 Tcl_CreateCommand(interp, "synth::_send_SIGIO", &ecosynth_send_SIGIO, (ClientData)NULL, (Tcl_CmdDeleteProc*) NULL);
348 Tcl_CreateCommand(interp, "synth::_send_SIGKILL", &ecosynth_send_SIGKILL, (ClientData)NULL, (Tcl_CmdDeleteProc*) NULL);
350 // Define additional variables.
352 // The version, from the AM_INIT_AUTOMAKE() macro in configure.in
353 Tcl_SetVar(interp, "synth::_ecosynth_version", ECOSYNTH_VERSION, TCL_GLOBAL_ONLY);
355 // The parent process id, i.e. that for the eCos application itself.
356 Tcl_SetVar2Ex(interp, "synth::_ppid", NULL, Tcl_NewIntObj(getppid()), TCL_GLOBAL_ONLY);
358 // The various directories
359 Tcl_SetVar(interp, "synth::_ecosynth_repository", ECOS_REPOSITORY, TCL_GLOBAL_ONLY);
360 Tcl_SetVar(interp, "synth::_ecosynth_libexecdir", LIBEXECDIR, TCL_GLOBAL_ONLY);
361 Tcl_SetVar(interp, "synth::_ecosynth_package_dir", PACKAGE_DIR, TCL_GLOBAL_ONLY);
362 Tcl_SetVar(interp, "synth::_ecosynth_package_version", PACKAGE_VERSION, TCL_GLOBAL_ONLY);
363 Tcl_SetVar(interp, "synth::_ecosynth_package_install", PACKAGE_INSTALL, TCL_GLOBAL_ONLY);