summaryrefslogtreecommitdiffstats
path: root/kdejava/koala/org/kde/koala/KProcess.java
blob: 641a1ff845207ee1b9428ca4233ebdf9eab676f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
//Auto-generated by kalyptus. DO NOT EDIT.
package org.kde.koala;

import org.kde.qt.Qt;
import org.kde.qt.TQMetaObject;
import org.kde.qt.QtSupport;
import org.kde.qt.TQObject;
import org.kde.qt.TQObject;

/**

 Child process invocation, monitoring and control.
 <li><b>General usage and features:</b></li>
 This class allows a KDE application to start child processes without having
 to worry about UNX signal handling issues and zombie process reaping.
 Basically, this class distinguishes three different ways of running
 child processes:

	<li>
	DontCare -- The child process is invoked and both the child
	 process and the parent process continue concurrently.
	</li>
	 The process is started in an own session (see setsid(2)).

	<li>
	NotifyOnExit -- The child process is invoked and both the
	 child and the parent process run concurrently.
	</li>
	 When the child process exits, the KProcess instance
 corresponding to it emits the Qt signal processExited().
 Since this signal is <b>not</b> emitted from within a UNX
 signal handler, arbitrary function calls can be made.
 Be aware: When the KProcess object gets destructed, the child
 process will be killed if it is still running!
 This means in particular, that it usually makes no sense to use
 a KProcess on the stack with NotifyOnExit.

	<li>
	OwnGroup -- like NotifyOnExit, but the child process is started
	 in an own process group (and an own session, FWIW). The behavior of
	 kill() changes to killing the whole process group - this makes
	 this mode useful for implementing primitive job management. It can be
	 used to work around broken wrapper scripts that don't propagate signals
	 to the "real" program. However, use this with care, as you disturb the
	 shell's job management if your program is started from the command line.
	</li>
	
	<li>
	Block -- The child process starts and the parent process
	 is suspended until the child process exits. (<b>Really</b> not recommended
	 for programs with a GUI.)
	 In this mode the parent can read the child's output, but can't send it any
	 input.
	</li>
	 KProcess also provides several functions for determining the exit status
 and the pid of the child process it represents.
 Furthermore it is possible to supply command-line arguments to the process
 in a clean fashion (no null-terminated stringlists and such...)
 A small usage example:
 <pre>
   KProcess proc = new KProcess;
   proc << "my_executable";
   proc << "These" << "are" << "the" << "command" << "line" << "args";
   TQApplication.connect(proc, SIGNAL("processExited(KProcess )"),
                         pointer_to_my_object, SLOT("my_objects_slot(KProcess )"));
   proc.start();
 </pre>
 This will start "my_executable" with the commandline arguments "These"...
 When the child process exits, the slot will be invoked.
 <li><b>Communication with the child process:</b></li>
 KProcess supports communication with the child process through
 stdin/stdout/stderr.
 The following functions are provided for getting data from the child
 process or sending data to the child's stdin (For more information,
 have a look at the documentation of each function):

	<li>
	writeStdin()
	  -- Transmit data to the child process' stdin. When all data was sent, the
	 signal wroteStdin() is emitted.
	</li>
	
	<li>
	When data arrives at stdout or stderr, the signal receivedStdout()
	 resp. receivedStderr() is emitted.
	</li>
	
	<li>
	You can shut down individual communication channels with
	 closeStdin(), closeStdout(), and closeStderr(), resp.
	</li>
	 See {@link KProcessSignals} for signals emitted by KProcess
		@author Christian Czezatke e9025461@student.tuwien.ac.at

		@short    Child process invocation, monitoring and control.
		@see KProcIO

*/
public class KProcess extends TQObject  {
	protected KProcess(Class dummy){super((Class) null);}
	/**	
		 More or less intuitive constants for use with setPriority().
		   		@short    More or less intuitive constants for use with setPriority().
	*/
	public static final int PrioLowest = 20;
	public static final int PrioLow = 10;
	public static final int PrioLower = 5;
	public static final int PrioNormal = 0;
	public static final int PrioHigher = -5;
	public static final int PrioHigh = -10;
	public static final int PrioHighest = -19;

	/**	
		 Modes in which the communication channel can be opened.
			 If communication for more than one channel is required,
		 the values have to be or'ed together, for example to get
		 communication with stdout as well as with stdin, you would
		 specify <code>Stdin</code> | <code>Stdout</code>
			 If <code>NoRead</code> is specified in conjunction with <code>Stdout</code>,
		 no data is actually read from <code>Stdout</code> but only
		 the signal receivedStdout(int fd, int &len) is emitted.
		   		@short    Modes in which the communication channel can be opened.
	*/
	public static final int NoCommunication = 0;
	public static final int Stdin = 1;
	public static final int Stdout = 2;
	public static final int Stderr = 4;
	public static final int AllOutput = 6;
	public static final int All = 7;
	public static final int NoRead = 8;

	/**	
		 Run-modes for a child process.
		   		@short    Run-modes for a child process.
	*/
	public static final int DontCare = 0;
	public static final int NotifyOnExit = 1;
	public static final int Block = 2;
	public static final int OwnGroup = 3;

	public native TQMetaObject metaObject();
	public native String className();
	/**	
		 Constructor
				@short    Constructor
	*/
	public KProcess(TQObject parent, String name) {
		super((Class) null);
		newKProcess(parent,name);
	}
	private native void newKProcess(TQObject parent, String name);
	public KProcess(TQObject parent) {
		super((Class) null);
		newKProcess(parent);
	}
	private native void newKProcess(TQObject parent);
	/**	
		 Constructor
		   		@short    Constructor
	*/
	public KProcess() {
		super((Class) null);
		newKProcess();
	}
	private native void newKProcess();
	/**	
		 Sets the executable and the command line argument list for this process.
			 For example, doing an "ls -l /usr/local/bin" can be achieved by:
		  <pre>
		  KProcess p;
		  ...
		  p << "ls" << "-l" << "/usr/local/bin"
		  </pre>
			@param arg the argument to add
				@return a reference to this KProcess

		@short    Sets the executable and the command line argument list for this process.
	*/
	public native KProcess op_write(String arg);
	/**	
		 Sets the executable and the command line argument list for this process,
		 in a single method call, or add a list of arguments.
			@param args the arguments to add
				@return a reference to this KProcess

		@short    Sets the executable and the command line argument list for this process,  in a single method call, or add a list of arguments.
	*/
	public native KProcess op_write(String[] args);
	/**	
		 Clear a command line argument list that has been set by using
		 operator<<.
		  		@short    Clear a command line argument list that has been set by using  operator<<.
	*/
	public native void clearArguments();
	/**	
		  Starts the process.
		  For a detailed description of the
		  various run modes and communication semantics, have a look at the
		  general description of the KProcess class. Note that if you use
		 setUsePty( Stdout | Stderr, \<boolean\> ), you cannot use Stdout | Stderr
		  here - instead, use Stdout only to receive the mixed output.
			  The following problems could cause this function to
		    return false:
		
			<li>
			The process is already running.
			</li>
			
			<li>
			The command line argument list is empty.
			</li>
			
			<li>
			The the <code>comm</code> parameter is incompatible with the selected pty usage.
			</li>
			
			<li>
			The starting of the process failed (could not fork).
			</li>
			
			<li>
			The executable was not found.
			</li>
				@param runmode The Run-mode for the process.
			@param comm Specifies which communication links should be
		  established to the child process (stdin/stdout/stderr). By default,
		  no communication takes place and the respective communication
		  signals will never get emitted.
				@return true on success, false on error
  (see above for error conditions)

		@short     Starts the process.
	*/
	public native boolean start(int runmode, int comm);
	public native boolean start(int runmode);
	public native boolean start();
	/**	
		 Stop the process (by sending it a signal).
			@param signo The signal to send. The default is SIGTERM.
				@return true if the signal was delivered successfully.
  
		@short    Stop the process (by sending it a signal).
	*/
	public native boolean kill(int signo);
	public native boolean kill();
	/**	
		 Checks whether the process is running.
				@return true if the process is (still) considered to be running
  
		@short    Checks whether the process is running.
	*/
	public native boolean isRunning();
	/**	 Returns the process id of the process.
			 If it is called after
		 the process has exited, it returns the process id of the last
		  child process that was created by this instance of KProcess.
			  Calling it before any child process has been started by this
		  KProcess instance causes pid() to return 0.
				@return the pid of the process or 0 if no process has been started yet.

		@short   Returns the process id of the process.
	*/
	public native int pid();
	/**	
		 Suspend processing of data from stdout of the child process.
		   		@short    Suspend processing of data from stdout of the child process.
	*/
	public native void suspend();
	/**	
		 Resume processing of data from stdout of the child process.
		   		@short    Resume processing of data from stdout of the child process.
	*/
	public native void resume();
	/**	
		 Suspend execution of the current thread until the child process dies
		 or the timeout hits. This function is not recommended for programs
		 with a GUI.
			@param timeout timeout in seconds. -1 means wait indefinitely.
				@return true if the process exited, false if the timeout hit.

		@short    Suspend execution of the current thread until the child process dies  or the timeout hits.
	*/
	public native boolean waitThread(int timeout);
	public native boolean waitThread();
	/**	
		 Checks whether the process exited cleanly.
				@return true if the process has already finished and has exited
  "voluntarily", ie: it has not been killed by a signal.
   
		@short    Checks whether the process exited cleanly.
	*/
	public native boolean normalExit();
	/**	
		 Checks whether the process was killed by a signal.
				@return true if the process has already finished and has not exited
 "voluntarily", ie: it has been killed by a signal.

		@short    Checks whether the process was killed by a signal.
	*/
	public native boolean signalled();
	/**	
		 Checks whether a killed process dumped core.
				@return true if signalled() returns true and the process
 dumped core. Note that on systems that don't define the
 WCOREDUMP macro, the return value is always false.

		@short    Checks whether a killed process dumped core.
	*/
	public native boolean coreDumped();
	/**	
		 Returns the exit status of the process.
				@return the exit status of the process. Note that this value
 is not valid if normalExit() returns false.
   
		@short    Returns the exit status of the process.
	*/
	public native int exitStatus();
	/**	
		 Returns the signal the process was killed by.
				@return the signal number that caused the process to exit.
 Note that this value is not valid if signalled() returns false.

		@short    Returns the signal the process was killed by.
	*/
	public native int exitSignal();
	/**	
			 Transmit data to the child process' stdin.
			 This function may return false in the following cases:
		
			<li>
			The process is not currently running.
			 This implies that you cannot use this function in Block mode.
			</li>
			
			<li>
			Communication to stdin has not been requested in the start() call.
			</li>
			
			<li>
			Transmission of data to the child process by a previous call to
			 writeStdin() is still in progress.
			</li>
				 Please note that the data is sent to the client asynchronously,
		 so when this function returns, the data might not have been
		 processed by the child process.
		 That means that you must not free <code>buffer</code> or call writeStdin()
		 again until either a wroteStdin() signal indicates that the
		 data has been sent or a processExited() signal shows that
		 the child process is no longer alive.
			 If all the data has been sent to the client, the signal
		 wroteStdin() will be emitted.
			@param buffer the buffer to write
			@param buflen the length of the buffer
				@return false if an error has occurred

		@short   	 Transmit data to the child process' stdin.
	*/
	public native boolean writeStdin(String buffer, int buflen);
	/**	
		 Shuts down the Stdin communication link. If no pty is used, this
		 causes "EOF" to be indicated on the child's stdin file descriptor.
				@return false if no Stdin communication link exists (any more).
   
		@short    Shuts down the Stdin communication link.
	*/
	public native boolean closeStdin();
	/**	
		 Shuts down the Stdout communication link. If no pty is used, any further
		 attempts by the child to write to its stdout file descriptor will cause
		 it to receive a SIGPIPE.
				@return false if no Stdout communication link exists (any more).
   
		@short    Shuts down the Stdout communication link.
	*/
	public native boolean closeStdout();
	/**	
		 Shuts down the Stderr communication link. If no pty is used, any further
		 attempts by the child to write to its stderr file descriptor will cause
		 it to receive a SIGPIPE.
				@return false if no Stderr communication link exists (any more).
   
		@short    Shuts down the Stderr communication link.
	*/
	public native boolean closeStderr();
	/**	
		 Deletes the optional utmp entry and closes the pty.
			 Make sure to shut down any communication links that are using the pty
		 before calling this function.
				@return false if the pty is not open (any more).
   
		@short    Deletes the optional utmp entry and closes the pty.
	*/
	public native boolean closePty();
	/**	
		 @brief Close stdin, stdout, stderr and the pty
			 This is the same that calling all close functions in a row:
				@short    @brief Close stdin, stdout, stderr and the pty 
		@see #closeStdin
		@see @see
		@see #closeStdout
		@see @see
		@see #closeStderr
		@see @see
		@see #closePty
	*/
	public native void closeAll();
	/**	
		 Lets you see what your arguments are for debugging.
				@return the list of arguments
   
		@short    Lets you see what your arguments are for debugging.
	*/
	// const TQValueList<TQCString>& args(); >>>> NOT CONVERTED
	/**	
		 Controls whether the started process should drop any
		 setuid/setgid privileges or whether it should keep them.
		 Note that this function is mostly a dummy, as the KDE libraries
		 currently refuse to run with setuid/setgid privileges.
			 The default is false: drop privileges
			@param keepPrivileges true to keep the privileges
		   		@short    Controls whether the started process should drop any  setuid/setgid privileges or whether it should keep them.
	*/
	public native void setRunPrivileged(boolean keepPrivileges);
	/**	
		 Returns whether the started process will drop any
		 setuid/setgid privileges or whether it will keep them.
				@return true if the process runs privileged
   
		@short    Returns whether the started process will drop any  setuid/setgid privileges or whether it will keep them.
	*/
	public native boolean runPrivileged();
	/**	
		 Adds the variable <code>name</code> to the process' environment.
		 This function must be called before starting the process.
			@param name the name of the environment variable
			@param value the new value for the environment variable
		   		@short    Adds the variable <code>name</code> to the process' environment.
	*/
	public native void setEnvironment(String name, String value);
	/**	
		 Changes the current working directory (CWD) of the process
		 to be started.
		 This function must be called before starting the process.
			@param dir the new directory
		   		@short    Changes the current working directory (CWD) of the process  to be started.
	*/
	public native void setWorkingDirectory(String dir);
	/**	
		 Specify whether to start the command via a shell or directly.
		 The default is to start the command directly.
		 If <code>useShell</code> is true <code>shell</code> will be used as shell, or
		 if shell is empty, /bin/sh will be used.
			 When using a shell, the caller should make sure that all filenames etc.
		 are properly quoted when passed as argument.
			@param useShell true if the command should be started via a shell
			@param shell the path to the shell that will execute the process, or
		              0 to use /bin/sh. Use getenv("SHELL") to use the user's
		              default shell, but note that doing so is usually a bad idea
		              for shell compatibility reasons.
				@short    Specify whether to start the command via a shell or directly.
		@see #quote
	*/
	public native void setUseShell(boolean useShell, String shell);
	public native void setUseShell(boolean useShell);
	/**	
		 Detaches KProcess from child process. All communication is closed.
		 No exit notification is emitted any more for the child process.
		 Deleting the KProcess will no longer kill the child process.
		 Note that the current process remains the parent process of the
		 child process.
		   		@short    Detaches KProcess from child process.
	*/
	public native void detach();
	/**	
		 Sets the scheduling priority of the process.
			@param prio the new priority in the range -20 (high) to 19 (low).
				@return false on error; see setpriority(2) for possible reasons.

		@short    Sets the scheduling priority of the process.
	*/
	public native boolean setPriority(int prio);
	/**	
		 This function can be used to quote an argument string such that
		 the shell processes it properly. This is e. g. necessary for
		 user-provided file names which may contain spaces or quotes.
		 It also prevents expansion of wild cards and environment variables.
			@param arg the argument to quote
				@return the quoted argument

		@short    This function can be used to quote an argument string such that  the shell processes it properly.
	*/
	public static native String quote(String arg);
	/**	
		 Sets up the environment according to the data passed via
		 setEnvironment()
		   		@short    Sets up the environment according to the data passed via  setEnvironment()
	*/
	protected native void setupEnvironment();
	/**	
		 This function is called from start() right before a fork() takes
		 place. According to the <code>comm</code> parameter this function has to initialize
		 the in, out and err data members of KProcess.
			 This function should return 1 if setting the needed communication channels
		 was successful.
			 The default implementation is to create UNIX STREAM sockets for the
		 communication, but you could reimplement this function to establish a
		 TCP/IP communication for network communication, for example.
		   		@short    This function is called from start() right before a fork() takes  place.
	*/
	protected native int setupCommunication(int comm);
	/**	
		 Called right after a (successful) fork() on the parent side. This function
		 will usually do some communications cleanup, like closing in[0],
		 out[1] and out[1].
			 Furthermore, it must also create the TQSocketNotifiers innot,
		 outnot and errnot and connect their Qt signals to the respective
		 KProcess slots.
			 For a more detailed explanation, it is best to have a look at the default
		 implementation in kprocess.cpp.
		   		@short    Called right after a (successful) fork() on the parent side.
	*/
	protected native int commSetupDoneP();
	/**	
		 Called right after a (successful) fork(), but before an exec() on the child
		 process' side. It usually duplicates the in[0], out[1] and
		 err[1] file handles to the respective standard I/O handles.
		   		@short    Called right after a (successful) fork(), but before an exec() on the child  process' side.
	*/
	protected native int commSetupDoneC();
	/**	
		 Immediately called after a successfully started process in NotifyOnExit
		 mode has exited. This function normally calls commClose()
		 and emits the processExited() signal.
			@param state the exit code of the process as returned by waitpid()
		   		@short    Immediately called after a successfully started process in NotifyOnExit  mode has exited.
	*/
	protected native void processHasExited(int state);
	/**	
		 Cleans up the communication links to the child after it has exited.
		 This function should act upon the values of pid() and runs.
		 See the kprocess.cpp source for details.
		
			<li>
			If pid() returns zero, the communication links should be closed
			  only.
			</li>
			
			<li>
			if pid() returns non-zero and runs is false, all data
			  immediately available from the communication links should be processed
			  before closing them.
			</li>
			
			<li>
			if pid() returns non-zero and runs is true, the communication
			  links should be monitored for data until the file handle returned by
			  KProcessController.theKProcessController.notifierFd() becomes ready
			  for reading - when it triggers, runs should be reset to false, and
			  the function should be immediately left without closing anything.
			</li>
				 The previous semantics of this function are forward-compatible, but should
		 be avoided, as they are prone to race conditions and can cause KProcess
		 (and thus the whole program) to lock up under certain circumstances. At the
		 end the function closes the communication links in any case. Additionally
		
			<li>
			if runs is true, the communication links are monitored for data
			  until all of them have returned EOF. Note that if any system function is
			  interrupted (errno == EINTR) the polling loop should be aborted.
			</li>
			
			<li>
			if runs is false, all data immediately available from the
			  communication links is processed.
			   
			</li>		@short    Cleans up the communication links to the child after it has exited.
	*/
	protected native void commClose();
	/**	
		 Specify the actual executable that should be started (first argument to execve)
		 Normally the the first argument is the executable but you can
		 override that with this function.
		   		@short    Specify the actual executable that should be started (first argument to execve)  Normally the the first argument is the executable but you can  override that with this function.
	*/
	protected native void setBinaryExecutable(String filename);
	/**	
		 Called by slotChildOutput() this function copies data arriving from
		 the child process' stdout to the respective buffer and emits the signal
		 receivedStdout().
		   		@short    Called by slotChildOutput() this function copies data arriving from  the child process' stdout to the respective buffer and emits the signal  receivedStdout().
	*/
	protected native int childOutput(int fdno);
	/**	
		 Called by slotChildError() this function copies data arriving from
		 the child process' stderr to the respective buffer and emits the signal
		 receivedStderr().
		   		@short    Called by slotChildError() this function copies data arriving from  the child process' stderr to the respective buffer and emits the signal  receivedStderr().
	*/
	protected native int childError(int fdno);
	/**	
		 This slot gets activated when data from the child's stdout arrives.
		 It usually calls childOutput().
			@param fdno the file descriptor for the output
		  		@short    This slot gets activated when data from the child's stdout arrives.
	*/
	protected native void slotChildOutput(int fdno);
	/**	
		 This slot gets activated when data from the child's stderr arrives.
		 It usually calls childError().
			@param fdno the file descriptor for the output
		  		@short    This slot gets activated when data from the child's stderr arrives.
	*/
	protected native void slotChildError(int fdno);
	/**	
		 Called when another bulk of data can be sent to the child's
		 stdin. If there is no more data to be sent to stdin currently
		 available, this function must disable the TQSocketNotifier innot.
			@param dummy ignore this argument
		   		@short    Called when another bulk of data can be sent to the child's  stdin.
	*/
	protected native void slotSendData(int dummy);
	/** Deletes the wrapped C++ instance */
	protected native void finalize() throws InternalError;
	/** Delete the wrapped C++ instance ahead of finalize() */
	public native void dispose();
	/** Has the wrapped C++ instance been deleted? */
	public native boolean isDisposed();
}