Line data Source code
1 : // Copyright (c) 2013-2024 Made to Order Software Corp. All Rights Reserved 2 : // 3 : // https://snapwebsites.org/project/eventdispatcher 4 : // contact@m2osw.com 5 : // 6 : // This program is free software; you can redistribute it and/or modify 7 : // it under the terms of the GNU General Public License as published by 8 : // the Free Software Foundation; either version 2 of the License, or 9 : // (at your option) any later version. 10 : // 11 : // This program is distributed in the hope that it will be useful, 12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of 13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 : // GNU General Public License for more details. 15 : // 16 : // You should have received a copy of the GNU General Public License along 17 : // with this program; if not, write to the Free Software Foundation, Inc., 18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 19 : 20 : // self 21 : // 22 : #include <cppprocess/io_pipe.h> 23 : 24 : #include <cppprocess/exception.h> 25 : 26 : 27 : // eventdispatcher 28 : // 29 : #include <eventdispatcher/communicator.h> 30 : 31 : 32 : // snapdev 33 : // 34 : #include <snapdev/not_reached.h> 35 : 36 : 37 : // C 38 : // 39 : #include <unistd.h> 40 : 41 : 42 : // last include 43 : // 44 : #include <snapdev/poison.h> 45 : 46 : 47 : 48 : namespace cppprocess 49 : { 50 : 51 : 52 : 53 7 : io_pipe::io_pipe(io_flags_t flags) 54 : : io(flags) 55 7 : , pipe_connection(flags_to_pipe_mode(flags)) 56 : { 57 7 : } 58 : 59 : 60 0 : void io_pipe::process_error() 61 : { 62 0 : process_done(done_reason_t::DONE_REASON_ERROR); 63 0 : } 64 : 65 : 66 0 : void io_pipe::process_invalid() 67 : { 68 0 : process_done(done_reason_t::DONE_REASON_INVALID); 69 0 : } 70 : 71 : 72 4 : void io_pipe::process_hup() 73 : { 74 4 : process_done(done_reason_t::DONE_REASON_HUP); 75 4 : } 76 : 77 : 78 0 : int io_pipe::get_fd() 79 : { 80 0 : return get_socket(); 81 : } 82 : 83 : 84 7 : int io_pipe::get_other_fd() 85 : { 86 7 : return get_other_socket(); 87 : } 88 : 89 : 90 7 : void io_pipe::close_both() 91 : { 92 7 : close(); 93 7 : } 94 : 95 : 96 7 : void io_pipe::close_other() 97 : { 98 7 : forked(); 99 7 : } 100 : 101 : 102 7 : void io_pipe::process_starting() 103 : { 104 7 : ed::communicator::instance()->add_connection(shared_from_this()); 105 7 : } 106 : 107 : 108 7 : bool io_pipe::process_done(done_reason_t reason) 109 : { 110 7 : close_both(); 111 7 : ed::communicator::instance()->remove_connection(shared_from_this()); 112 7 : return io::process_done(reason); 113 : } 114 : 115 : 116 7 : ed::pipe_t io_pipe::flags_to_pipe_mode(io_flags_t flags) 117 : { 118 7 : switch(flags & (IO_FLAG_INPUT | IO_FLAG_OUTPUT)) 119 : { 120 0 : case IO_FLAG_INPUT | IO_FLAG_OUTPUT: 121 0 : return ed::pipe_t::PIPE_BIDIRECTIONAL; 122 : 123 3 : case IO_FLAG_INPUT: 124 3 : return ed::pipe_t::PIPE_CHILD_INPUT; 125 : 126 4 : case IO_FLAG_OUTPUT: 127 4 : return ed::pipe_t::PIPE_CHILD_OUTPUT; 128 : 129 0 : default: 130 0 : throw cppprocess_invalid_parameters("io_pipe flags must be set to INPUT, OUTPUT, or both; neither is not an available option"); 131 : 132 : } 133 : snapdev::NOT_REACHED(); 134 : return ed::pipe_t::PIPE_BIDIRECTIONAL; 135 : } 136 : 137 : 138 : 139 : } // namespace cppprocess 140 : // vim: ts=4 sw=4 et