spc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /*
  24. * File: spc.h $XConsortium: spc.h /main/3 1995/10/26 15:48:38 rswiston $
  25. * Language: C
  26. *
  27. * (c) Copyright 1988, Hewlett-Packard Company, all rights reserved.
  28. *
  29. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  30. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  31. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  32. * (c) Copyright 1993, 1994 Novell, Inc. *
  33. */
  34. #ifndef _spc_h
  35. #define _spc_h
  36. /* -------------------------------------------- */
  37. /* Requires: */
  38. #include <stdio.h>
  39. #include <bms/XeUserMsg.h>
  40. #include <termios.h>
  41. /* -------------------------------------------- */
  42. typedef struct _SPC_Channel *SPC_Channel_Ptr;
  43. typedef struct _XeHostInfo {
  44. XeString os;
  45. XeString os_ver;
  46. XeString hw_arch;
  47. } *XeHostInfo;
  48. extern FILE *SPC_Print_Protocol;
  49. extern FILE *spc_logF;
  50. extern XeString spc_user_environment_file;
  51. /* Error returns for SPC routines */
  52. #define SPC_ERROR FALSE /* Use this value for error checking */
  53. /*
  54. * These are the channel connector definitions
  55. */
  56. #define STDIN 0
  57. #define STDOUT 1
  58. #define STDERR 2
  59. #define MASTER_SIDE 0
  60. #define SLAVE_SIDE 1
  61. /* These are the sub-process notification identifiers */
  62. #define SPC_PROCESS_STOPPED 1 /* Child process is in background */
  63. #define SPC_PROCESS_EXITED 2 /* Child process called exit(cause); */
  64. #define SPC_PROCESS_SIGNALLED 3 /* Child process received signal: cause */
  65. #define SPC_PROCESS_INTERRUPT 4 /* Child process WAIT was interrupted */
  66. #define SPC_PROCESS_DUMPED(a) ((a) & 0200) /* True when core dumped */
  67. /* This is the maximum size of an SPC I/O Buffer */
  68. #define SPC_BUFSIZ 4096
  69. /*
  70. * These macros define the bit field portion of an SPC_IOMode
  71. */
  72. #define SPCIO_ALL_MASK 0xffffffff
  73. /* The IO Modes that define the input and output sources */
  74. #define SPCIO_SOURCE_MASK 0xf
  75. #define SPCIO_NOIO 0x0 /* The default - no input/output */
  76. #define SPCIO_WRITEONLY 0x1 /* Only write app stdin */
  77. #define SPCIO_READONLY 0x2 /* Only read app stdout */
  78. #define SPCIO_READWRITE 0x3 /* Read stdout, write stdin */
  79. #define SPCIO_ERRORONLY 0x4 /* Only read stderr */
  80. #define SPCIO_WRITEERROR 0x5 /* Write stdin, read stderr */
  81. #define SPCIO_READERROR 0x6 /* Only read stdout/stderr */
  82. #define SPCIO_READWRITEERROR 0x7 /* Full std (in, out, err) */
  83. /* Use this bit with above IO Mode for splitting stdout and stderr data */
  84. #define SPCIO_SEPARATEREADERROR 0x8 /* Separate stdout & stderr */
  85. /* The IO Modes that deal with communication styles (features) */
  86. #define SPCIO_STYLE_MASK 0x70
  87. #define SPCIO_PTY 0x10 /* Use a PTY */
  88. #define SPCIO_PIPE 0x20 /* Use pipe() - no line editing */
  89. #define SPCIO_NOIOMODE 0x40 /* Use neither */
  90. #define SPCIO_LINEEDIT 0x80 /* Valid only with PTY */
  91. /* Other flags */
  92. #define SPCIO_SYSTEM 0x100 /* Use system() - Spawns a SHELL */
  93. #define SPCIO_LINEORIENTED 0x200 /* Invoke callback on line bounds */
  94. /* It is possible to break two byte
  95. characters. See note on XeSPCRead. */
  96. #define SPCIO_WAIT 0x400 /* Wait for process to finish */
  97. #define SPCIO_USE_XTOOLKIT 0x800 /* Use the X toolkit */
  98. #define SPCIO_SYNC_TERMINATOR 0x1000 /* Handle termination synchronously */
  99. #define SPCIO_USE_LOGFILE 0x2000 /* Use logfile for stderr -- only
  100. valid with SPCIO_NOIO */
  101. #define SPCIO_SIGNAL_PGRP 0x20000 /* Propagate signals to entire process
  102. group. */
  103. #define SPCIO_FORCE_CONTEXT 0x40000 /* Error on SPC Spawn if context dir
  104. is not present */
  105. /*
  106. * Routines
  107. */
  108. /* spc.c */
  109. #if defined(__STDC__) || defined(__cplusplus) || defined(c_plusplus)
  110. # define EXTERN_DECL(type, name, arglist) type name arglist
  111. # if defined(__cplusplus) && defined(__c_callable)
  112. # define EXTERN_C_CALLABLE(type, name, arglist) \
  113. extern "C" { type name arglist ; }
  114. # else
  115. # define EXTERN_C_CALLABLE(type, name, arglist) \
  116. EXTERN_DECL(type, name, arglist)
  117. # endif
  118. #else
  119. #ifdef _AIX
  120. # define EXTERN_C_CALLABLE(type, name, arglist) \
  121. extern type name arglist
  122. # define EXTERN_DECL(type, name, arglist) \
  123. type name arglist
  124. #else /* _AIX */
  125. # define EXTERN_DECL(type, name, arglist) name arglist
  126. # define EXTERN_C_DECL(type, name, arglist) EXTERN_DECL(type, name, arglist)
  127. #endif /* (_AIX) */
  128. #endif
  129. EXTERN_C_CALLABLE(SPC_Channel_Ptr, XeSPCOpen, (XeString hostname, int iomode));
  130. /*
  131. Open an SPC channel. Process will run on 'hostname' (or the local
  132. host if value is NULL), with the specified iomode.
  133. */
  134. EXTERN_C_CALLABLE(int, XeSPCClose, (SPC_Channel_Ptr channel));
  135. /*
  136. Close an SPC channel. Closing a channel will automatically deactivate it
  137. (meaning that any subprocess associated with the channel is terminated).
  138. */
  139. EXTERN_DECL(int, XeSPCReset, (SPC_Channel_Ptr channel));
  140. /*
  141. Reset an SPC channel. This will allow it to be used in a subsequent
  142. spawn or exec call.
  143. */
  144. EXTERN_DECL(int, XeSPCRead,
  145. (SPC_Channel_Ptr channel, int connector,
  146. XeString buffer, int length));
  147. /*
  148. Read length characters from an SPC channel into some preallocated buffer.
  149. Note that it is possible to split a two-byte character, if the first
  150. byte if the character is read in just at buffer[length]. However, the
  151. next read will return the second byte (just like Unix read). The
  152. 'connector' value is either STDOUT or STDERR.
  153. */
  154. EXTERN_DECL(int, XeSPCWrite,
  155. (SPC_Channel_Ptr channel, XeString buffer, int length));
  156. /*
  157. Write length characters from buffer to the standard input of a
  158. process on the other side of an SPC channel.
  159. */
  160. EXTERN_C_CALLABLE(int, XeSPCActive, (SPC_Channel_Ptr channel));
  161. /*
  162. Returns True when channel is active, False otherwise
  163. */
  164. EXTERN_DECL(int, XeSPCData, (SPC_Channel_Ptr channel));
  165. /*
  166. Returns True when channel be read from, False otherwise
  167. */
  168. EXTERN_C_CALLABLE(int, XeSPCSpawn,
  169. (XeString pathname, XeString context_dir, XeString *argv,
  170. XeString *envp, SPC_Channel_Ptr channel));
  171. /*
  172. Spawn an application under SPC
  173. */
  174. EXTERN_DECL(SPC_Channel_Ptr, XeSPCOpenAndSpawn,
  175. (XeString hostname, int iomode, XeString pathname,
  176. XeString context_dir, XeString *argv, XeString *envp));
  177. /*
  178. Combine the Open and Spawn channel operations
  179. */
  180. EXTERN_DECL(int, XeSPCExecuteProcess, (SPC_Channel_Ptr channel));
  181. /*
  182. Restart a new subprocess on a channel
  183. */
  184. EXTERN_C_CALLABLE(void, XeSPCKillProcesses, (int wait));
  185. /*
  186. Kill all known executing processes (useful for catching SIGTERM, etc)
  187. */
  188. EXTERN_DECL(int, XeSPCKillProcess, (SPC_Channel_Ptr channel, int wait));
  189. /*
  190. Kill executing process on an SPC channel. 'wait' TRUE means don't
  191. return from call until process is completely terminated (including
  192. after user specified callbacks are called).
  193. */
  194. EXTERN_DECL(int, XeSPCInterruptProcess, (SPC_Channel_Ptr channel));
  195. /*
  196. Interrupt executing process on an SPC channel (send SIGINT).
  197. */
  198. EXTERN_DECL(int, XeSPCSignalProcess, (SPC_Channel_Ptr channel, int sig));
  199. /*
  200. Send an arbitrary signal to executing process on an SPC channel.
  201. */
  202. typedef
  203. EXTERN_DECL(void, (*SbInputHandlerProc),
  204. (void *client_data, XeString buf, int nchars, int connector));
  205. EXTERN_C_CALLABLE(int, XeSPCAddInput,
  206. (SPC_Channel_Ptr channel,
  207. SbInputHandlerProc handler,
  208. void *client_data));
  209. /*
  210. Add an input channel handler
  211. */
  212. /*
  213. * The user input handler takes the following form:
  214. *
  215. * void UserInputHandler(client_data, text, size, connection)
  216. * void * client_data; *** Useful for passing widet destination ***
  217. * XeString text; *** The text coming from the SPC channel ***
  218. * int size; *** The number of character in passed text ***
  219. * int connection; *** The connection where data was received ***
  220. * *** (STDOUT or STDERR) ***
  221. */
  222. typedef
  223. EXTERN_DECL(void, (*SPC_TerminateHandlerType),
  224. (SPC_Channel_Ptr chan,
  225. int pid,
  226. int type,
  227. int cause,
  228. void *Terminate_Data));
  229. EXTERN_C_CALLABLE(int, XeSPCRegisterTerminator,
  230. (SPC_Channel_Ptr channel,
  231. SPC_TerminateHandlerType teminator,
  232. void * client_data));
  233. /*
  234. Add a termination handler to a channel (called when sub-process dies)
  235. */
  236. /*
  237. * The user termination handler takes the following form:
  238. *
  239. * void UserTerminator(channel, pid, type, cause, client_data)
  240. * SPC_CHannel_ptr channel;
  241. * int pid; *** The Process ID of the terminated appl. ***
  242. * int type; *** The type of termination (see above) ***
  243. * int cause; *** The number associated w/termination ***
  244. * void * client_data; *** User specified client data ***
  245. */
  246. /*
  247. * These are the channel access routines
  248. */
  249. EXTERN_DECL(XeString, XeSPCGetDevice,
  250. (SPC_Channel_Ptr channel, int connector, int side));
  251. /*
  252. Return the device name associated with a side of a channel device
  253. pair. 'connector' is either STDIN, STDOUT, or STDERR, and 'side' is
  254. either MASTER_SIDE or SLAVE_SIDE. This call is valid only in PTY
  255. channels.
  256. */
  257. EXTERN_DECL(int, XeSPCGetProcessStatus,
  258. (SPC_Channel_Ptr channel, int *type, int *cause));
  259. /*
  260. Fill in the type and cause of a process termination.
  261. */
  262. EXTERN_DECL(int, XeSPCAttach, (SPC_Channel_Ptr channel, int pid));
  263. /*
  264. Returns True if a process ID was associated with an SPC channel.
  265. */
  266. EXTERN_DECL(int, XeSPCDetach, (SPC_Channel_Ptr channel));
  267. EXTERN_DECL(int, XeSPCGetPID, (SPC_Channel_Ptr channel));
  268. /*
  269. Returns the Process ID of the channel or NULL if none
  270. */
  271. EXTERN_DECL(int, XeSPCGetLogfile,
  272. (SPC_Channel_Ptr channel, XeString *host, XeString *file));
  273. /*
  274. Return the logfile for the channel. If the channel was not opened
  275. with SPCIO_USE_LOGFILE specified, it will return NULL. Also note
  276. that it returns an XeString *, not an XeString.
  277. */
  278. EXTERN_DECL(int, XeSPCRemoveLogfile, (SPC_Channel_Ptr channel));
  279. /*
  280. Remove the logfile associated with the channel
  281. */
  282. /*
  283. *
  284. * Features currently not implemented:
  285. *
  286. * SPCIO_WAIT with ptys
  287. *
  288. * SEPARATEREADERROR with ptys
  289. *
  290. * Complete error checking. For example, there
  291. * are no checks for trying to write to a channel
  292. * opened W/O SPCIO_WRITE specified
  293. *
  294. */
  295. EXTERN_DECL(int, XeSPCGetChannelSyncFd, (SPC_Channel_Ptr channel));
  296. /*
  297. Get the file descriptor for checking synchronous termination. This
  298. is used for interfacing with event loops.
  299. */
  300. EXTERN_DECL(SPC_Channel_Ptr, XeSPCHandleTerminator, (int fd));
  301. /*
  302. Handle a synchronous termination condition
  303. This routine is to be used with the file descriptor returned by
  304. XeSPCGetChannelSyncFd. The idea is that one opens a channel using
  305. SPCIO_SYNC_TERMINATOR set in the iomode, and then at some point
  306. checks for input available on the returned file descriptor (possibly
  307. using a select(2) system call). If there is input, it means that
  308. some SYNC_TERMINATOR channel had a subprocess die. The program then
  309. calls XeSPCHandleTerminator to get the termination handler invoked.
  310. IT IS THE RESPONSIBILITY OF THE USER PROGRAM TO EVENTUALLY CALL
  311. XeSPCHandleTerminator. IF IT DOES NOT, THE PROGRAM MAY DEADLOCK
  312. ITSELF.
  313. */
  314. /* SPC Error handling */
  315. typedef struct _SPCError { /* An SPC Error message */
  316. XeString text; /* The text */
  317. XeString format; /* How to format args */
  318. XeSeverity severity; /* How bad is it, doc? */
  319. char use_errno; /* Whether to use the system errno */
  320. } SPCError;
  321. /* Use this to get the current error number */
  322. extern int XeSPCErrorNumber;
  323. EXTERN_DECL(SPCError *, XeSPCLookupError, (int errnum));
  324. /*
  325. Returns the SPCError structure associated with the passed error number or
  326. NULL if the passed error number is not a valid SPC error. The
  327. error structure returned will be overwritten by a subsequent
  328. XeSPCLookupError call.
  329. */
  330. EXTERN_DECL(void, XeSPCShutdownCallbacks, (void));
  331. EXTERN_DECL(void, XeSPCRestartCallbacks, (void));
  332. /* These two routines are used to temporarily suspend SPC callbacks */
  333. EXTERN_DECL(int, XeSetpgrp, (int read_current_termio));
  334. /*
  335. This routine will do the following:
  336. 1. open /dev/tty
  337. 2. get the termio information from the file descriptor just opened
  338. 3. close /dev/tty
  339. 3. allocate a master / slave pty pair, opening the master side
  340. 4. set the termio info of the master side to be the result of step 2
  341. 5. setpgrp
  342. 6. open the slave side.
  343. All of this has the effect of making the process which called this
  344. routine immune to interrupts, etc., but also passing on the termio
  345. characteristics of the original tty.
  346. If read_current_termio is non-zero, steps 1-3 will NOT be performed, but
  347. instead this routine will get the information from the following termio
  348. struct:
  349. */
  350. EXTERN_DECL(XeHostInfo, SPC_GetHostinfo, (SPC_Channel_Ptr channel));
  351. /*
  352. Return information about the host (os, os-ver, hw) to which "channel"
  353. is currently connected. The return is to a static structure of static
  354. strings. Do not modify or free and of them!
  355. */
  356. /*
  357. **
  358. ** New B.00 functions
  359. **
  360. */
  361. EXTERN_C_CALLABLE(int, XeSPCSendEOF, (SPC_Channel_Ptr channel));
  362. /*
  363. Close the standard input of the process on the other side of the channel
  364. */
  365. EXTERN_C_CALLABLE(int, XeSPCSetTermio,
  366. (SPC_Channel_Ptr channel,
  367. int connection,
  368. int side,
  369. struct termios *termio));
  370. /*
  371. Set the termio value of the PTY associated with 'connection' (STDIN,
  372. STDOUT, or STDERR), on 'side' (MASTER_SIDE or SLAVE_SIDE) to the
  373. value pointed to by termio. This call must be made before the
  374. subprocess is spawned.
  375. */
  376. #endif /* #ifdef _spc_h */