Properly fix the license for C++ template usage. This means we needed to change from...
[libasyncio] / asyncio / async_process.cpp
CommitLineData
8c15b8c7
TJ
1/*
2The software in this package is distributed under the GNU General
3Public License version 2 (with a special exception described below).
4
5A copy of GNU General Public License (GPL) is included in this distribution,
6in the file COPYING.GPL.
7
8As a special exception, if other files instantiate templates or use macros
9or inline functions from this file, or you compile this file and link it
10with other works to produce a work based on this file, this file
11does not by itself cause the resulting work to be covered
12by the GNU General Public License.
13
14However the source code for this file must still be made available
15in accordance with section (3) of the GNU General Public License.
16
17This exception does not invalidate any other reasons why a work based
18on this file might be covered by the GNU General Public License.
19*/
5c8a3d40
RP
20/** @file
21 *
5c8a3d40 22 * (c) Copyright 2007-2008 by Intra2net AG
5c8a3d40
RP
23 */
24
25//#define NOISEDEBUG
26
42b7c46d 27#include "async_process.hpp"
5c8a3d40
RP
28
29#include <iterator>
30#include <algorithm>
31
32#include <unistd.h>
33#include <fcntl.h>
34#include <sys/socket.h>
35#include <sys/types.h>
36#include <errno.h>
37#include <signal.h>
38#include <sys/wait.h>
39
40#include <filefunc.hxx>
41
42
43#ifdef NOISEDEBUG
44#include <iostream>
45#include <iomanip>
46#define DOUT(msg) std::cout << msg << std::endl
47#define FODOUT(obj,msg) std::cout << typeid(*obj).name() << "[" << obj << "]:" << msg << std::endl
48#define ODOUT(msg) std::cout << typeid(*this).name() << "[" << this << "]:" << msg << std::endl
49#else
50#define DOUT(msg) do {} while (0)
51#define FODOUT(obj,msg) do {} while (0)
52#define ODOUT(msg) do {} while (0)
53#endif
54
55
56namespace
57{
58
42b7c46d 59using namespace AsyncIo;
5c8a3d40
RP
60
61/**
62 * local configuration values
63 */
64namespace config
65{
66
67 /// the capacity of the child status list (/ vector)
68 const unsigned int pid_pool_capacity= 512;
69
70} // eo namespace config
71
72
73
74/// the previous handler for the child signal (SIGCHLD)
75void (*oldChildHandler)(int) = NULL;
76
77/// method pointer for activating process manager
78void (ProcessManager::*_activate_manager)();
79
80PidStateList pending_pid_states;
81
82
83/**
84 * signal handler for child signal (SIGCHLD)
85 * @param sig the signal number as provided by the OS
86 */
87void handleSigChild(int sig)
88{
89 int status;
90 pid_t pid;
91 while ( (pid = waitpid(-1,&status,WNOHANG)) > 0)
92 {
93 pending_pid_states.push_back( PidStatePair(pid,status) );
94 }
95 if (_activate_manager)
96 {
97 // tricky way to access a protected method without being a (official) friend:
98 ( ProcessManager::getInstance()->*_activate_manager)();
99 }
100 //TODO: ?
101 signal(sig,handleSigChild);
102} // eo handleSigChild
103
104
105namespace process
106{
107
108typedef std::pair<pid_t, ProcessImplementation*> PidProcPair;
109typedef std::list< PidProcPair > PidProcList;
110
111
112template< typename F, typename S >
113struct CmpFirst
114{
115 F _f;
116 CmpFirst ( F f ) : _f(f) {}
117 bool operator () ( const std::pair<F,S>& v ) const { return v.first == _f; }
118}; // eo struct CmpFirst
119
120
121std::list<ProcessImplementation*> g_process_list;
122PidProcList g_pid_list;
123
124
125void addProcessInstance( ProcessImplementation* obj )
126{
127 g_process_list.push_back(obj);
128} // eo addProcessInstance(ProcessImplementation*)
129
130
131void removeProcessInstance( ProcessImplementation* obj )
132{
133 // remove obj from list
134 g_process_list.remove(obj);
135 // clear pointers in pid list
136 for(PidProcList::iterator it= g_pid_list.begin();
137 it != g_pid_list.end();
138 ++it)
139 {
140 if (it->second == obj)
141 {
142 it->second= NULL;
143 }
144 }
145} // eo removeProcessInstance(ProcessImplementation*)
146
147
148void addChildProcess( pid_t pid, ProcessImplementation* obj)
149{
150 g_pid_list.push_back ( PidProcPair(pid,obj) );
151} // eo addChildProcess(pid_t,ProcessImplementation*)
152
153
154void removeChildProcess ( pid_t pid, ProcessImplementation* obj)
155{
156 PidProcList::iterator it= std::find(
157 g_pid_list.begin(), g_pid_list.end(),
158 PidProcPair(pid,obj));
159 if (it != g_pid_list.end())
160 {
161 g_pid_list.erase(it);
162 }
163} // eo removeChildProcess(pid_t,ProcessImplementation*)
164
165
166bool findChildProcess ( pid_t pid, ProcessImplementation* & obj )
167{
168 PidProcList::iterator it = std::find_if(
169 g_pid_list.begin(), g_pid_list.end(),
170 CmpFirst<pid_t,ProcessImplementation*>(pid) );
171 if (it == g_pid_list.end())
172 {
173 return false;
174 }
175 obj = it->second;
176 return true;
177} // eo findChildProcess(pid_t,ProcessImplementation*&)
178
179
180} // eo namespace process
181
182
183
184
185
186/*
187** misc tools
188*/
189
190
191/**
192 * convenience tool for closing file descriptors...
193 */
194struct FdCloser
195{
196 int m_fd;
197
198 FdCloser(int fd=-1) : m_fd(fd) {}
199
200 ~FdCloser()
201 {
202 if (m_fd >= 0) ::close(m_fd);
203 }
204
205 void release() { m_fd= -1; }
206
207}; // eo struct FdCloser
208
209
210
211} // eo namespace <anonymous>
212
213
42b7c46d 214namespace AsyncIo
5c8a3d40
RP
215{
216
217
218/*
219 * global functions
220 */
221
222/**
223 * installs the handler for the child signal (SIGCHLD).
224 * Installing this handler is mandatory for the process subsystem to work correctly.
225 * @return @a true iff the child handler is successfully installed.
226 */
227bool installChildHandler()
228{
229 if (oldChildHandler)
230 {
231 // already installed
232 return true;
233 }
234 if (! ProcessManager::getInstance() )
235 {
236 // we need an instance of the process manager
237 return false;
238 }
239 pending_pid_states.reserve( config::pid_pool_capacity );
240 oldChildHandler = signal( Signal::CHLD, handleSigChild );
241 if (oldChildHandler == SIG_ERR)
242 {
243 oldChildHandler= NULL;
244 return false;
245 }
246 return true;
247} // eo installChildHandler
248
249
250/**
251 * uninstalls the child handler.
252 * @return @a true iff the old child handler is reestablished.
253 */
254bool restoreChildHandler()
255{
256 if (!oldChildHandler)
257 {
258 return false;
259 }
260 void(*res)(int) = signal( Signal::CHLD, oldChildHandler);
261
262 if (res == SIG_ERR)
263 {
264 return false;
265 }
266 oldChildHandler= NULL;
267 return true;
268} // eo restoreChildHandler
269
270
271
272
273/*
274 * Implementation of ProcessImplementation
275 */
276
277IOImplementation2* ProcessImplementation::_StderrOnStdout = ((IOImplementation2*) 1);
278IOImplementation2* ProcessImplementation::_UseParentsStderr = ((IOImplementation2*) 0);
279
280
281/**
282 * @brief constructor for the process implementation.
283 *
284 * the constructor takes the path to the executable and (initial) cli arguments.
285 *
286 * @param path path to the executable.
287 * @param args initial command line arguments.
288 */
289ProcessImplementation::ProcessImplementation(
290 const std::string& path,
291 const std::vector<std::string>& args
292 )
293: IOImplementation(-1,-1)
294, m_path(path)
295, m_nice_inc(0)
296, m_create_new_session(false)
297, m_pid(0)
298, m_state(ProcessState::stopped)
299, m_exit_code(0)
300{
301 m_args.push_back(path);
302 std::copy( args.begin(), args.end(), std::back_inserter(m_args) );
303 process::addProcessInstance(this);
304} // eo ProcessImplementation::ProcessImplementation(const std::string&)
305
306
307ProcessImplementation::~ProcessImplementation()
308{
309 if (m_pid > 0 && m_state!=ProcessState::stopped)
310 {
311 stopProcess(true);
312 }
313 process::removeProcessInstance(this);
314} // eo ProcessImplementation::~ProcessImplementation()
315
316
317void ProcessImplementation::close(Direction direction)
318{
319 inherited::close(direction);
320 if (!inherited::opened() && (m_state != ProcessState::stopped) )
321 {
322 stopProcess(false);
323 }
324} // eo ProcessImplementation::close(Direction)
325
326
327/**
328 * returns an object for adding new arguments to the argument list.
329 * @return the adder object.
330 */
331PushBackFiller<std::string, std::vector > ProcessImplementation::getArgAdder()
332{
333 return PushBackFiller<std::string, std::vector >(m_args);
334} // eo ProcessImplementation::getArgAdder()
335
336
337/**
338 * @brief set if the process should create a new session when started.
339 * @param enable determine if the process should start a new session.
340 * @return @a true iff the value of enable was accepted.
341 *
342 * If the process is already running, a new value is not accepted.
343 */
344bool ProcessImplementation::setCreateNewSession( bool enable )
345{
346 if (m_state != ProcessState::stopped and enable != m_create_new_session)
347 {
348 return false;
349 }
350 m_create_new_session= enable;
351 return true;
352} // eo ProcessImplementation::setCreateNewSession(bool);
353
354
355/**
356 * @brief sets a new nice increment.
357 * @param nice the desired nice increment.
358 * @return @a true if the value was accepted and - in case the process was already started -
359 * the nice value was successfully changed.
360 */
361bool ProcessImplementation::setNice(int nice)
362{
363 errno= 0;
364 if (m_state != ProcessState::stopped)
365 {
366 int delta= m_nice_inc + nice;
367 m_nice_inc= nice;
368 int res= ::nice(delta);
369 if (res == -1 and errno !=0 )
370 {
371 return false;
372 }
373 }
374 else
375 {
376 m_nice_inc = nice;
377 }
378 return true;
379} // eo ProcessImplementation::setNice(int)
380
381
382/**
383 * @brief sets the work dir the process should be started with.
384 * @param workdir the workdir
385 * @return @a true if the new workdir was accepted.
386 *
387 * The method will return @a false if the process is already started.
388 * The workdir can only be set before the process is started.
389 */
390bool ProcessImplementation::setWorkDir(const std::string& workdir)
391{
392 if ( m_state != ProcessState::stopped and workdir != m_workdir)
393 {
394 return false;
395 }
396 if (not workdir.empty())
397 {
398 I2n::Stat stat(workdir);
399 if (not stat or not stat.is_directory())
400 {
401 return false;
402 }
403 }
404 m_workdir= workdir;
405 return true;
406} // eo ProcessImplementation::setWorkDir(const std::string&)
407
408
409/**
410 * @brief sets new arguments for the process (the path to the binary is kept).
411 *
412 * @param args the new cli arguments for the subprocess (replacing the old ones).
413 */
414void ProcessImplementation::resetArgs( const std::vector< std::string >& args )
415{
416 if (m_args.size() > 1)
417 {
418 m_args.erase( ++m_args.begin(), m_args.end());
419 }
420 std::copy( args.begin(), args.end(), std::back_inserter(m_args) );
421} // eo ProcessImplementation::resetArgs(const std::vectors< std::string >&)
422
423
424/**
425 * starts the new process.
426 * provides pipes for sending data to/ receiving data from the new process.
427 * Basically forks and execs the new process.
428 *
429 * @param stderr if not NULL the given object will be connected to stderr of the new process.
430 * The object can then be used for reading the data from the process' stderr; but cannot be written to.
431 * (The object will be closed if it was open).
432 * If the constant @a ProcessImplementation::StderrOnStdout is passed then stderr of the new process will
433 * be written to the same channel as stdout (i.e. can be read from the process class instance like the
434 * normal output).
435 * If NULL then the stderr channel from the parent process will also be used by the child.
436 * @return @a true iff the new subprocess started.
437 */
438bool ProcessImplementation::startProcess( IOImplementation2 *stderr )
439{
440 bool stderr2stdout= false;
441 m_errno = 0;
442 m_input_buffer.clear();
443 if (m_pid > 0 && m_state != ProcessState::stopped)
444 {
445 // process still/already running...
446 return false;
447 }
448 m_exit_code= 0;
449
450 if (stderr == _StderrOnStdout)
451 {
452 stderr2stdout= true;
453 stderr= NULL;
454 }
455
456 int to_process_pipe[2];
457 int from_process_pipe[2];
458 int from_process_stderr_pipe[2]= { -1, -1 };
459
460 if ( ::pipe(to_process_pipe) )
461 {
462 m_errno= errno;
463 return false;
464 }
465 FdCloser closeTo0( to_process_pipe[0] );
466 FdCloser closeTo1( to_process_pipe[1] );
467 if ( ::pipe (from_process_pipe) )
468 {
469 m_errno= errno;
470 return false;
471 }
472 FdCloser closeFrom0( from_process_pipe[0] );
473 FdCloser closeFrom1( from_process_pipe[1] );
474 if (stderr)
475 {
476 if (stderr->opened()) stderr->close();
477 if ( ::pipe (from_process_stderr_pipe) )
478 {
479 m_errno= errno;
480 return false;
481 }
482 }
483 FdCloser closeFromErr0( from_process_stderr_pipe[0] );
484 FdCloser closeFromErr1( from_process_stderr_pipe[1] );
485
486 m_pid = ::fork();
487
488 if ( m_pid == (pid_t)-1 )
489 {
490 m_errno= errno;
491 m_pid= 0;
492 // error; something went wrong
493 return false;
494 }
495 else if (m_pid > 0)
496 {
497 // we are in the parent part
498
499 // keep the fd's we need and (later) close the other ones:
500 closeTo1.release(); // don't close this fd!
501 setWriteFd(to_process_pipe[1]);
502 closeFrom0.release(); // don't close this fd!
503 setReadFd(from_process_pipe[0]);
504
505 if (stderr)
506 {
507 closeFromErr0.release(); // don't close this fd!
508 stderr->setReadFd(from_process_stderr_pipe[0]);
509 }
510
511 m_state= ProcessState::running;
512 process::addChildProcess(m_pid,this);
513 DOUT(" started child with pid " << m_pid);
514 return true;
515 }
516 else // pid > 0
517 {
518 // we are in the child part
519
520 // dup the fd's for stdin/-out/-err into place:
521 ::dup2(to_process_pipe[0],0);
522 ::dup2(from_process_pipe[1],1);
523 if (stderr)
524 {
525 ::dup2(from_process_stderr_pipe[1],2);
526 ::close(from_process_stderr_pipe[0]); ::close(from_process_stderr_pipe[1]);
527 }
528 else if (stderr2stdout)
529 {
530 ::dup2(from_process_pipe[1],2);
531 }
532 // close what we don't need:
533 ::close(to_process_pipe[0]); ::close(to_process_pipe[1]);
534 ::close(from_process_pipe[0]); ::close(from_process_pipe[1]);
535
536 // set workdir if requested:
537 if (not m_workdir.empty())
538 {
539 int r= ::chdir( m_workdir.c_str() );
540 if (r !=0 )
541 {
542 //TODO?
543 exit(255);
544 }
545 }
546
547 //
548 // collect args:
549 char **argv= new char*[m_args.size()+1];
550 int i=0;
551 for(std::vector<std::string>::iterator it= m_args.begin();
552 it != m_args.end();
553 ++it,++i)
554 {
555 argv[i]= strdup( it->c_str() );
556 }
557 argv[i]= NULL;
558 // update nice level:
559 if (m_nice_inc)
560 {
561 nice(m_nice_inc);
562 }
563 // create a new session id if requested:
564 if (m_create_new_session)
565 {
566 setsid();
567 }
568 // execute:
569 execv(m_path.c_str(), argv);
570 // exit if exec failed
571 exit(255);
572 //cleanup! ... just joking; we exec or we exit, in either case the system cleans
573 // everything which needs to be cleaned up.
574 }
575 return false; // keep the compiler happy...
576} // eo ProcessImplementation::startProcess()
577
578
579/**
580 * convenience method for starting the child process.
581 * This method uses predefined enum values for the stderr handling mode.
582 *
583 * @param stderr_mode the desired stderr mode.
584 * @return @a true iff the child process was created.
585 */
586bool ProcessImplementation::startProcess( ProcessImplementation::StderrMode stderr_mode )
587{
588 switch (stderr_mode)
589 {
590 case UseParentsStderr:
591 return startProcess( _UseParentsStderr );
592
593 case StderrOnStdout:
594 return startProcess( _StderrOnStdout );
595 }
596 return false;
597}; // eo ProcessImplementation::startProcess(ProcessImplementation::StderrMode)
598
599
600/**
601 * stops the process.
602 *
603 * @todo think about a more intelligent handling...
604 */
605void ProcessImplementation::stopProcess(bool force)
606{
607 // TODO: do it somewhat more intelligent?!
608 if (force)
609 {
610 kill(Signal::KILL);
611 //TODO: set running state?
612 }
613 else
614 {
615 kill(Signal::TERM);
616 }
617} // eo ProcessImplementation::stop(bool)
618
619
620
621/**
622 * sends a signal to the child process.
623 * @param signal the Signal which should be send.
624 * @return @a true if the signal was sent; @a false if an error occured.
625 */
626bool ProcessImplementation::kill(Signal signal)
627{
628 m_errno = 0;
629 if (m_pid == 0 || m_pid == (pid_t)-1)
630 {
631 m_errno= ESRCH;
632 return false;
633 }
634 int res = ::kill(m_pid, signal);
635 if (res < 0)
636 {
637 m_errno= errno;
638 return false;
639 }
640 if (signal == Signal::CONT && m_state == ProcessState::suspended)
641 {
642 m_state = ProcessState::running;
643 }
644 return true;
645} // eo ProcessImplementation::kill(Signal)
646
647
648
649/**
650 * set a new child state with information gobbled by the child signal handler.
651 *
652 * @note This method should only be called by the process manager!
653 *
654 * @param pid the pid of the child process.
655 * @param status the new status value (as delivered by waitpid())
656 */
657void ProcessImplementation::setChildState(pid_t pid, int status)
658{
659 DOUT("setChildState("<<pid<<","<<status<<") pid="<<m_pid);
660 if (pid != m_pid)
661 {
662 // old child... ignore!
663 return;
664 }
665 if (WIFSTOPPED(status))
666 {
667 DOUT("stopped");
668 // stopped:
669 int stopsignal = WSTOPSIG(status);
670 // make stop signal available in exit_code:
671 m_exit_code= (stopsignal << 8);
672 m_state= ProcessState::suspended;
673 return;
674 }
675#ifdef WIFCONTINUED
676 if (WIFCONTINUED(status))
677 {
678 DOUT("continued");
679 // continued after a stop:
680 m_state= ProcessState::running;
681 return;
682 }
683#endif
684 if (WIFEXITED(status))
685 {
686 DOUT("normal exit");
687 //normal exit:
688 m_exit_code= (0xff & WEXITSTATUS(status));
689 m_pid= 0;
690 close(Direction::out);
691 m_state= ProcessState::stopped;
692 m_signal_terminated();
693 return;
694 }
695 if (WIFSIGNALED(status))
696 {
697 DOUT("signaled stop");
698 // exit by signal:
699 int termsignal = WTERMSIG(status);
700 // make term signal available in exit code (normal exit codes are only 8 bit)
701 m_exit_code = (termsignal << 8);
702 m_pid= 0;
703 close(Direction::out);
704 m_state= ProcessState::stopped;
705 m_signal_terminated();
706 return;
707 }
708 // this point should never be reached...!!
709} // eo ProcessImplementation::setChildState(pid_t,int)
710
711
712/*
713 * implementation of ProcessManager
714 */
715
716/// the instance of the process manager (highlander; there can be only one!)
717ProcessManager* ProcessManager::the_instance= NULL;
718
719
720ProcessManager::ProcessManager()
721{
722 setWhenTime(0);
723} // eo ProcessManager::ProcessManager
724
725
726/**
727 * delivers the process manager instance (generate if it doesn't exist)
728 * @return the process manager instance
729 */
730ProcessManager* ProcessManager::getInstance()
731{
732 if (! the_instance)
733 {
734 the_instance = new ProcessManager();
735 _activate_manager = &ProcessManager::activateMe;
736 }
737 return the_instance;
738} // eo ProcessManager::getInstance
739
740
741/**
742 * activate the timer so it's handled by the next backend cycle
743 */
744void ProcessManager::activateMe()
745{
746 setWhenTime(0);
747 activate();
748} // eo ProcessManager::activateMe
749
750
751/**
752 * real work is done here.
753 * Processes the information collected by the child signal handler.
754 */
755void ProcessManager::execute()
756{
757 PidStateList pid_state_list;
758 {
759 // block child signals (within this scope)
760 ScopedSignalBlocker blocker( Signal::CHLD );
761 // and now fetch the list of pending information
762 // (simply swap with our local empty list)
763 std::swap(pid_state_list, pending_pid_states);
764 // reserve the desired (minimum) capacity
765 pending_pid_states.reserve( config::pid_pool_capacity );
766 }
767 ODOUT("exec, " << pid_state_list.size() << " entries");
768
769 // interpret states:
770 for(PidStateList::iterator it = pid_state_list.begin();
771 it != pid_state_list.end();
772 ++it)
773 {
774 pid_t pid = it->first;
775 int status = it->second;
776 ODOUT(" pid=" << pid << ", status=" << status);
777 ProcessImplementation *process_obj;
778 if (process::findChildProcess(pid,process_obj))
779 {
780 ODOUT(" local managed child, process_obj="<< process_obj);
781 // pid found in list:
782 if (!WIFSTOPPED(status)
783#ifdef WIFCONTINUED
784 && !WIFCONTINUED(status)
785#endif
786 )
787 {
788 // take it from list if the child exited:
789 process::removeChildProcess(pid,process_obj);
790 }
791 if (process_obj)
792 {
793 // give the process object a chance to handle the state change:
794 process_obj->setChildState(pid, status);
795 }
796 }
797 else
798 {
799 ODOUT("foreign child");
800 // pid not found in list:
801 /* NOTE: in a non threaded environment this pid must be from a child process which is not
802 managed by this process classes; since this method is called after all setup of a child process
803 is done (; especially entering the new child pid into our internal lists).
804 */
805 m_foreign_pid_states.push_back(*it);
806 }
807 }
808
809 // handle the foreign childs:
810 {
811 /* idea:
812 * fetch a (pid,status) from the list, erase it (to avoid reentrance problems)
813 * and fire the signal. If someone forks childs outside this module then he can
814 * connect to the signal and receive all necessary status information gobbled by
815 * our child handler.
816 */
817 while (! m_foreign_pid_states.empty())
818 {
819 PidStateList::iterator it= m_foreign_pid_states.begin();
820 pid_t pid = it->first;
821 int status = it->second;
822 m_foreign_pid_states.erase(it);
823 m_foreign_child_state_changed_signal(pid,status);
824 }
825 }
826
827} // eo ProcessManager::execute
828
829
42b7c46d 830} // eo namespace AsyncIo