#include <string.h>
#include "common.h"
#include <ctype.h>
+#include <inttypes.h>
// === CONSTANTS ===
#define DEFAULT_SHELL "/Acess/SBin/login"
int AddSerialTerminal(const char *DevPathSegment, const char *ModeStr, char **Command);
int AddDaemon(char *StdoutPath, char *StderrPath, char **Command);
- int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV);
+ int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV, const char **EnvP);
int SpawnKTerm(tInitProgram *Program);
int SpawnSTerm(tInitProgram *Program);
int SpawnDaemon(tInitProgram *Program);
#if 1
for( ;; )
{
- int pid = SpawnCommand(0, 1, 1, (char *[]){DEFAULT_SHELL, NULL});
+ char *args[] = {DEFAULT_SHELL, NULL};
+ const char *env[] = {"TERM=screen", NULL};
+ int pid = SpawnCommand(0, 1, 1, args, env);
// TODO: Detect errors
_SysWaitTID(pid, NULL);
}
goto lineError;
}
AddKTerminal(id, command);
- free(command);
}
else if(strcmp(cmdbuf, "stty") == 0 ) {
// stty <devpath> [78][NOE][012][bB]<baud> <command...>
char path_seg[32+1];
char modespec[4+6+1];
- if( fscanf(fp, "%32s %6s ", path_seg, modespec) != 2 ) {
+ if( fscanf(fp, "%32s %10s ", path_seg, modespec) != 2 ) {
goto lineError;
}
char **command = ReadCommand(fp);
// - Runs a daemon (respawning) that logs to the specified files
// - Will append a header whenever the daemon starts
char *stdout_path = ReadQuotedString(fp);
+ if( !stdout_path )
+ goto lineError;
char *stderr_path = ReadQuotedString(fp);
+ if( !stderr_path )
+ goto lineError;
char **command = ReadCommand(fp);
+ if( !command )
+ goto lineError;
AddDaemon(stdout_path, stderr_path, command);
}
if(!command)
goto lineError;
- int handles[] = {0, 1, 2};
+ int handles[] = {0, 1, 1};
int pid = _SysSpawn(command[0], (const char **)command, NULL, 3, handles, NULL);
int retstatus;
_SysWaitTID(pid, &retstatus);
int baud;
// Parse mode string
- if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 5 ) {
+ if( sscanf(ModeStr, "%1[78]%1[NOE]%1[012]%*1[bB]%d", &dbit, &parity, &sbit, &baud) != 4 ) {
// Oops?
+ _SysDebug("Serial mode string is invalid ('%s')", ModeStr);
return -1;
}
modeword |= (sbit - '0') << 3;
// Create info
- const char DEVPREFIX[] = "/Devices/";
+ const char DEVPREFIX[] = "/Devices/pts/serial";
int pathlen = sizeof(DEVPREFIX) + strlen(DevPathSegment);
tInitProgram *ent = AllocateProgram(Command, PT_STERM, sizeof(struct sSTerm)+pathlen);
ent->TypeInfo.STerm.FormatBits = modeword;
return 0;
}
-int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV)
+int SpawnCommand(int c_stdin, int c_stdout, int c_stderr, char **ArgV, const char **EnvP)
{
int handles[] = {c_stdin, c_stdout, c_stderr};
- int rv = _SysSpawn(ArgV[0], (const char **)ArgV, NULL, 3, handles, NULL);
+ _SysDebug("Spawning '%s'", ArgV[0]);
+
+ if( c_stdin < 0 ) {
+ _SysDebug("SpawnCommand: stdin is invalid");
+ return 1;
+ }
+ if( c_stdout < 0 ) {
+ _SysDebug("SpawnCommand: stdout is invalid");
+ return 1;
+ }
+ if( c_stderr < 0 ) {
+ _SysDebug("SpawnCommand: stderr is invalid");
+ return 1;
+ }
+
+ int rv = _SysSpawn(ArgV[0], (const char **)ArgV, EnvP, 3, handles, NULL);
_SysClose(c_stdin);
if( c_stdout != c_stdin )
int SpawnKTerm(tInitProgram *Program)
{
- const char fmt[] = "/Devices/VTerm/%i";
+ const char fmt[] = "/Devices/pts/vt%i";
+ const char *env[] = {"TERM=screen", NULL};
char path[sizeof(fmt)];
snprintf(path, sizeof(path), fmt, Program->TypeInfo.KTerm.ID);
int in = _SysOpen(path, OPENFLAG_READ);
int out = _SysOpen(path, OPENFLAG_WRITE);
- return SpawnCommand(in, out, out, Program->Command);
+ _SysDebug("Spawning virtual terminal '%s' with term '%s'",
+ path, Program->Command[0]);
+ return SpawnCommand(in, out, out, Program->Command, env);
}
int SpawnSTerm(tInitProgram *Program)
int in = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_READ);
int out = _SysOpen(Program->TypeInfo.STerm.Path, OPENFLAG_WRITE);
+ if(in == -1 || out == -1 ) {
+ _SysDebug("Unable to open serial port '%s'", Program->TypeInfo.STerm.Path);
+ return -1;
+ }
+
#if 0
if( _SysIOCtl(in, 0, NULL) != DRV_TYPE_SERIAL )
{
_SysIOCtl(in, SERIAL_IOCTL_GETSETFORMAT, &Program->TypeInfo.STerm.FormatBits);
#endif
- return SpawnCommand(in, out, out, Program->Command);
+ _SysDebug("Spawning serial terminal '%s' with term '%s'",
+ Program->TypeInfo.STerm.Path, Program->Command[0]);
+ return SpawnCommand(in, out, out, Program->Command, NULL);
}
int SpawnDaemon(tInitProgram *Program)
int out = _SysOpen(Program->TypeInfo.Daemon.StdoutPath, OPENFLAG_WRITE);
int err = _SysOpen(Program->TypeInfo.Daemon.StderrPath, OPENFLAG_WRITE);
- if( in == -1 || out == -1 || err == -1 ) {
+ if( in < 0 || out < 0 || err < 0 ) {
+ perror("SpawnDaemon");
_SysClose(in);
_SysClose(out);
_SysClose(err);
return -2;
}
- return SpawnCommand(in, out, err, Program->Command);
+ // Log spawn header
+ {
+ char buffer[101];
+ size_t len = snprintf(buffer, 100, "[%"PRIi64"] init spawning ", _SysTimestamp());
+ _SysWrite(out, buffer, len);
+ for( int i = 0; Program->Command[i]; i ++ )
+ {
+ _SysWrite(out, "'", 1);
+ _SysWrite(out, Program->Command[i], strlen(Program->Command[i]));
+ _SysWrite(out, "'", 1);
+ }
+ _SysWrite(out, "\n", 1);
+ }
+
+ return SpawnCommand(in, out, err, Program->Command, NULL);
}