home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / os / vxworks / 1253 < prev    next >
Encoding:
Text File  |  1993-01-28  |  8.9 KB  |  414 lines

  1. Path: sparky!uunet!scifi!acheron!philabs!linus!agate!dog.ee.lbl.gov!lbl.gov!vxwexplo
  2. From: zander@luke.atdiv.lanl.gov (Mark Zander)
  3. Newsgroups: comp.os.vxworks
  4. Subject: re: Executing shell scripts
  5. Date: Thu, 28 Jan 93 12:20:31 MST
  6. Organization: Lawrence Berkeley Laboratory, Berkeley CA
  7. Lines: 401
  8. Sender: vxwexplo@lbl.gov
  9. Message-ID: <9301281920.AA13272@luke.atdiv.lanl.gov>
  10. NNTP-Posting-Host: 128.3.112.16
  11. Originator: daemon@vxw.ee.lbl.gov
  12.  
  13. Sorry it took so long, but I wanted to clear this before sending it.
  14.  
  15. Brian Quigley writes:
  16.  
  17. > I've got a need to execute a shell script from within a program I'm
  18. > working on.  I figured I'd copy the startup shell script code since it
  19. > does what I want.  Unfortunately, the code that I took from usrConfig.c
  20. > does not behave the same when I put it my code.  The script does not execute.  
  21. > I've tried killing any existing shell before I start the new one. This
  22. > doesn't seem to help.  Various messing with ioTaskSetStd, 
  23. > shellOrigStdSet, and ioGlobalStdSet produce undesirable results.
  24. > Any hints would be appreciated.
  25. > Thanks.
  26. > (P.S. Hints for a Unix like "system()" call would also be appreciated.)
  27.  
  28. I've had a similar need to execute shell scripts from a unix shell.
  29. The following code worked for that purpose.  I hope it can help you
  30. as well.  Good luck!
  31.  
  32. /*    r s h d . c
  33. *
  34. *************************************************************************
  35. *
  36. *                L O S   A L A M O S
  37. *            Los Alamos National Laboratory
  38. *             Los Alamos, New Mexico 87545
  39. *
  40. *    File Name:            rshd.c
  41. *    Environment:        VxWorks V4.0
  42. *    Developement:        Sun C, Sun OS 4.0, Sun 3/60
  43. *    Make Description:
  44. *
  45. *
  46. *    Modification History
  47. *    ------------ -------
  48. *
  49. *    Date        Programmer        Comments
  50. *    ----        ----------        --------
  51. *
  52. *    1-Jun-90    Mark Zander        Borrowed from telnetLib
  53. *
  54. *************************************************************************
  55. */
  56.  
  57. /*
  58. *       Source Code Control System Identifier
  59. */
  60. static char SccsId[ ] = "@(#)rshd.c    1.1    11/20/91";
  61. /*
  62. *************************************************************************
  63. *++
  64. *    Name
  65. *    ----
  66. *
  67. *>>    rshd    - remote shell daemon for vxWorks
  68. *
  69. *    Purpose
  70. *    -------
  71. *
  72. *
  73. *    Subroutine List
  74. *    ---------- ----
  75. *
  76. *    rshInit        - initialize remote shell daemon
  77. *    rshd        - remote shell daemon
  78. *
  79. *    Include Files
  80. *    ------- -----
  81. *
  82. *
  83. *
  84. *    See Also
  85. *    --- ----
  86. *
  87. *
  88. *--
  89. *************************************************************************
  90. */
  91. #include    "vxWorks.h"
  92. #include    "types.h"
  93. #include    "socket.h"
  94. #include    "in.h"
  95. #include    "ioLib.h"
  96. #include    "taskLib.h"
  97.  
  98. /* login       513/tcp */
  99. /* shell       514/tcp     cmd     # no passwords used */
  100.  
  101. #define        RSH_SERVICE            514                /* shell port number */
  102.  
  103. #define        STDIN_BUF_SIZE        512
  104.  
  105. static int    rshTaskOptions        = VX_SUPERVISOR_MODE | VX_UNBREAKABLE;
  106. static int    rshTaskStackSize    = 1500;
  107. static int    rshTaskPriority        = 2;
  108.  
  109. int            rshdId;                                /* task id of rshd task */
  110.  
  111. static char    *rshShellName        = "shell";
  112.  
  113. static char    *ptyRshName            = "/pty/rsh.";
  114. static char    *ptyRshNameM        = "/pty/rsh.M";    /* master side */
  115. static char    *ptyRshNameS        = "/pty/rsh.S";    /* slave side */
  116.  
  117. VOID rshd( );
  118.  
  119. /*
  120. *************************************************************************
  121. *+
  122. *    Name
  123. *    ----
  124. *
  125. *>    rshInit        - initialize remote shell daemon
  126. *
  127. *    Purpose
  128. *    -------
  129. *
  130. *    Remote shell initialize.
  131. *
  132. *    Calling Sequence
  133. *    ------- --------
  134. *
  135. *    rshInit( );
  136. *
  137. *-
  138. *************************************************************************
  139. */
  140. VOID rshInit( )
  141.  
  142. {
  143.     static BOOL done = FALSE;    /* FALSE = not done */
  144.  
  145.     if ( done ) {
  146.         printErr( "rshInit: already initialized.\n" );
  147.         return;
  148.     }
  149.  
  150.     if ( ptyDrv( ) == ERROR ||
  151.             ptyDevCreate( ptyRshName, 1024, 1024 ) == ERROR ) {
  152.         printErr ("rshInit: unable to create pty device.\n");
  153.         return;
  154.     }
  155.  
  156.     rshdId = taskSpawn( "rshd", rshTaskPriority,
  157.                         rshTaskOptions, rshTaskStackSize,
  158.                         rshd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 );  
  159.  
  160.     if ( rshdId == ERROR ) {
  161.         printErr( "rshInit: unable to spawn rshd.\n" );
  162.     } else {
  163.         done = TRUE;
  164.     }
  165. }
  166.  
  167. /*
  168. *************************************************************************
  169. *+
  170. *    Name
  171. *    ----
  172. *
  173. *>    rshd        - remote shell daemon
  174. *
  175. *    Purpose
  176. *    -------
  177. *
  178. *    Remote shell daemon.  Similar to telnetd.  Executes a shell command sent
  179. *    via the shell socket from another machine.  Does not redirect input or
  180. *    output of the given command and for this reason requires a special version
  181. *    of the unix rshell command.  Started from routine "rshInit".
  182. *
  183. *    Calling Sequence
  184. *    ------- --------
  185. *
  186. *    rshd( );
  187. *-
  188. *************************************************************************
  189. */
  190. VOID rshd( )
  191.  
  192. {
  193.     int                    optval;
  194.     struct sockaddr_in    myAddress;
  195.     struct sockaddr_in    clientAddress;
  196.     int                    clientAddrLen;
  197.     int                    client;
  198.     int                    masterFd;
  199.     int                    slaveFd;
  200.     int                    sd;
  201.     char                buf[ STDIN_BUF_SIZE ];
  202.     int                    n;
  203.     int                    shellInFd;
  204. /*
  205. *    Open a socket and wait for a client
  206. */
  207.     sd = socket( AF_INET, SOCK_STREAM, 0 );
  208.    
  209.     bzero( (char *) &myAddress, sizeof( myAddress ) );
  210.     myAddress.sin_family = AF_INET;
  211.     myAddress.sin_port   = RSH_SERVICE;
  212.  
  213.     if ( bind( sd, (struct sockaddr *) &myAddress, sizeof( myAddress ) )
  214.             == ERROR ) {
  215.         printErr( "rshd: bind failed.\n" );
  216.         return;
  217.     }
  218.  
  219.     listen( sd, 5 );
  220.  
  221.     FOREVER {
  222. /*
  223. *    Wait for shell to exist
  224. */
  225.         while ( taskNameToId( rshShellName ) == ERROR ) {
  226.             taskDelay( sysClkRateGet( ) );
  227.         }
  228.         errnoSet( OK );        /* clear errno for pretty i() display */
  229. /*
  230. *    Now accept connection
  231. */
  232.         clientAddrLen = sizeof( clientAddress );
  233.         client = accept( sd, (struct sockaddr *) &clientAddress, &clientAddrLen );
  234.  
  235.         if ( client == ERROR ) {
  236.             printErr( "rshd: accept failed - status = 0x%x\n", errnoGet( ) );
  237.             continue;
  238.         }
  239. /*
  240. *    create the pseudo terminal:
  241. *        the master side is connected to the socket to the remote machine
  242. */
  243.  
  244.         if ( ( masterFd = open( ptyRshNameM, WRITE ) ) == ERROR ) {
  245.             char *msg = "\r\nSorry, trouble with pty.\r\n";
  246.  
  247.             printErr( "rshd: error opening %s\n", ptyRshNameM );
  248.             write( client, msg, strlen( msg ) );
  249.             close( client );
  250.             continue;
  251.         }
  252.  
  253.         if ( ( slaveFd = open( ptyRshNameS, READ ) ) == ERROR ) {
  254.             char *msg = "\r\nSorry, trouble with pty.\r\n";
  255.  
  256.             printErr( "rshd: error opening %s\n", ptyRshNameS );
  257.             write( client, msg, strlen( msg ) );
  258.             close( client );
  259.             close( masterFd );
  260.             continue;
  261.         }
  262. /*
  263. *    Set up slave
  264. */
  265.         ioctl( slaveFd, FIOOPTIONS, OPT_RAW );
  266.         ioctl( slaveFd, FIOFLUSH );
  267. /*
  268. *    Save original standard in of shell
  269. */
  270.         shellInFd  = ioGlobalStdGet( STD_IN );
  271. /*
  272. *    Redirect standard in of shell
  273. */
  274.         shellOrigStdSet( STD_IN, slaveFd );
  275. /*
  276. *    Restart shell so it'll accept this command
  277. */
  278.         taskRestart( taskNameToId( rshShellName ) );
  279. /*
  280. *    Get command form remote host and send it to shell
  281. */
  282.         if ( ( n = read( client, buf, sizeof( buf ) ) ) > 0 ) {
  283.             buf[n] = 0;
  284.             write( masterFd, buf, n );
  285.         }
  286. /*
  287. *    Set shell back to orginal standard in
  288. */
  289.         shellOrigStdSet( STD_IN, shellInFd );
  290. /*
  291. *    Close connections to shell and remote host
  292. */
  293.         close( masterFd );
  294.         close( slaveFd );
  295.         close( client );
  296.     }
  297. }
  298.  
  299.  
  300. /*    v s h . c
  301. *
  302. *************************************************************************
  303. *
  304. *                L O S   A L A M O S
  305. *            Los Alamos National Laboratory
  306. *             Los Alamos, New Mexico 87545
  307. *
  308. *    File Name:            vsh.c
  309. *    Environment:        VxWorks V4.0
  310. *    Developement:        Sun C, Sun OS 4.0, Sun 3/60
  311. *    Make Description:
  312. *
  313. *
  314. *    Modification History
  315. *    ------------ -------
  316. *
  317. *    Date        Programmer        Comments
  318. *    ----        ----------        --------
  319. *
  320. *    4-Jun-90    Mark Zander        Original
  321. *    21-Nov-91    me                change name from rsh to vsh to avoid name
  322. *                                clashes
  323. *
  324. *************************************************************************
  325. */
  326.  
  327. /*
  328. *************************************************************************
  329. *+
  330. *    Name
  331. *    ----
  332. *
  333. *>    vsh        - execute remote shell command on a vxWorks system
  334. *
  335. *    Purpose
  336. *    -------
  337. *
  338. *    Executes a remote shell command for a vxWorks system.  Sends one command
  339. *    over the shell socket.  Is similar to the unix rsh command but does not
  340. *    redirect stdin and stdout.
  341. *
  342. *    Command
  343. *    -------
  344. *
  345. *    % vsh hostname cmd
  346. *
  347. *    or
  348. *
  349. *    % vsh
  350. *    Host name? hostname
  351. *    Command? cmd
  352. *
  353. *    Arguments
  354. *    ---------
  355. *
  356. *    hostname    name of host listed in yp directory
  357. *    cmd            remote command to be executed
  358. *
  359. *    Example
  360. *    -------
  361. *
  362. *
  363. *    Special Comments
  364. *    ------- --------
  365. *
  366. *
  367. *    See Also
  368. *    --- ----
  369. *
  370. *
  371. *-
  372. *************************************************************************
  373. */
  374. #include <string.h>
  375.  
  376. #define        RSH_SERVICE            514                /* shell port number */
  377. /*
  378. *    Source Code Control System Identifier
  379. */
  380. static char SccsId[ ] = "@(#)vsh.c    1.3    11/21/91";
  381.  
  382. void main(argc, argv)
  383. /*
  384. *    Arguments
  385. */
  386.     int            argc;                /* command line argument count */
  387.     char        **argv;                /* command line arguments */
  388. {
  389. /*
  390. *    Variables
  391. */
  392.     int            argin;                /* argument index */
  393.     char        hostname[100];        /* host name */
  394.     char        cmd[300];            /* remote command */
  395.     int            sock;                /* socket */
  396.     int            i;                    /* iteration counter */
  397.  
  398.     argin = 1;
  399.     getarg("Host name? ", argc, argv, &argin, hostname);
  400.     getarg("Command? ", argc, argv, &argin, cmd);
  401.     for (i = argin; i < argc; i++) {
  402.         strcat(cmd, " ");
  403.         strcat(cmd, argv[i]);
  404.     }
  405.     strcat(cmd, "\n");
  406.     sock = socClient(hostname, RSH_SERVICE);
  407.     write(sock, cmd, strlen(cmd));
  408.     close(sock);
  409. }
  410.