├── .clang-format ├── .gitignore ├── COPYING ├── LICENSE ├── Makefile ├── README.md ├── configure.h ├── configure.h.in ├── dpipe.c ├── tty_attach.c ├── tty_bus.c ├── tty_fake.c └── tty_plug.c /.clang-format: -------------------------------------------------------------------------------- 1 | --- 2 | BasedOnStyle: Google 3 | IndentWidth: '2' 4 | TabWidth: '2' 5 | UseTab: Never 6 | AlignAfterOpenBracket: Align 7 | AlignConsecutiveMacros: 'true' 8 | AlignEscapedNewlines: Left 9 | AlignOperands: 'true' 10 | AlignTrailingComments: 'true' 11 | AllowShortBlocksOnASingleLine: 'false' 12 | AllowShortCaseLabelsOnASingleLine: 'false' 13 | AllowShortFunctionsOnASingleLine: None 14 | AllowShortIfStatementsOnASingleLine: Never 15 | AllowShortLoopsOnASingleLine: 'false' 16 | BreakBeforeBraces: Attach 17 | ColumnLimit: '132' 18 | IncludeBlocks: Regroup 19 | IndentCaseLabels: 'true' 20 | IndentPPDirectives: BeforeHash 21 | KeepEmptyLinesAtTheStartOfBlocks: 'false' 22 | MaxEmptyLinesToKeep: '2' 23 | PointerAlignment: Right 24 | ReflowComments: 'true' 25 | SortIncludes: 'true' 26 | SortUsingDeclarations: 'true' 27 | SpaceAfterCStyleCast: 'true' 28 | SpaceAfterLogicalNot: 'false' 29 | SpaceBeforeAssignmentOperators: 'true' 30 | SpaceBeforeCpp11BracedList: 'true' 31 | SpaceBeforeCtorInitializerColon: 'true' 32 | SpaceBeforeInheritanceColon: 'false' 33 | SpaceBeforeParens: ControlStatements 34 | SpaceBeforeRangeBasedForLoopColon: 'true' 35 | SpaceInEmptyParentheses: 'false' 36 | SpacesBeforeTrailingComments: '2' 37 | SpacesInCStyleCastParentheses: 'false' 38 | SpacesInContainerLiterals: 'false' 39 | SpacesInParentheses: 'false' 40 | SpacesInSquareBrackets: 'false' 41 | 42 | ... 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Object files 2 | *.o 3 | *.ko 4 | *.obj 5 | *.elf 6 | 7 | # Precompiled Headers 8 | *.gch 9 | *.pch 10 | 11 | # Libraries 12 | *.lib 13 | *.a 14 | *.la 15 | *.lo 16 | 17 | # Shared objects (inc. Windows DLLs) 18 | *.dll 19 | *.so 20 | *.so.* 21 | *.dylib 22 | 23 | # Executables 24 | *.exe 25 | *.out 26 | *.app 27 | *.i*86 28 | *.x86_64 29 | *.hex 30 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CFLAGS=-Wall 2 | LDFLAGS=-lm 3 | CC=gcc 4 | BINARIES=tty_bus tty_fake tty_plug tty_attach dpipe 5 | 6 | PREFIX?=/usr/local 7 | 8 | all: configure.h $(BINARIES) 9 | 10 | install: all 11 | echo Installing binaries in $(PREFIX)/bin 12 | cp $(BINARIES) $(PREFIX)/bin 13 | 14 | configure.h: configure.h.in 15 | cat configure.h.in | sed -e "s/___SVNVERSION___/`svnversion`/g" > configure.h 16 | 17 | tty_bus: tty_bus.o 18 | 19 | 20 | 21 | 22 | # gcc -o tty_bus tty_bus.o 23 | tty_bus.o: tty_bus.c 24 | gcc -c tty_bus.c $(CFLAGS) 25 | 26 | tty_plug: tty_plug.o 27 | gcc -o tty_plug tty_plug.o 28 | tty_plug.o: tty_plug.c 29 | gcc -c tty_plug.c $(CFLAGS) 30 | 31 | 32 | 33 | tty_fake: tty_fake.o 34 | gcc -o tty_fake tty_fake.o 35 | tty_fake.o: tty_fake.c 36 | gcc -c tty_fake.c $(CFLAGS) 37 | 38 | 39 | tty_attach: tty_attach.o 40 | gcc -o tty_attach tty_attach.o 41 | tty_attach.o: tty_attach.c 42 | gcc -c tty_attach.c $(CFLAGS) 43 | 44 | dpipe: dpipe.o 45 | gcc -o dpipe dpipe.o 46 | dpipe.o: dpipe.c 47 | gcc -c dpipe.c $(CFLAGS) 48 | 49 | clean: 50 | rm -f *.o $(BINARIES) 51 | 52 | distclean: clean 53 | rm -f configure.h 54 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TTYBUS project 2 | **Copyright (c) 2010-2021 Daniele Lacamera and other contributors (see `git log` for details)** 3 | 4 | `dpipe` command - copyright 2003 Renzo Davoli 5 | 6 | Daemonization option - copyright 2020 Frank Sautter 7 | 8 | 9 | ## OVERVIEW 10 | The **TTYBUS** project is a toolkit that provides tty device virtualization, multiplexing, spoofing and sharing among unix processes. 11 | It allows users to connect their real and virtual terminals (including real serial devices) to a shared, *non-reliable* bus, and 12 | then to virtually any input/output stream. 13 | 14 | 15 | ## LICENSE 16 | **TTYBUS** comes without any warranty and it is released under the terms of the GNU/GPL v.2 17 | 18 | 19 | ## IDEA 20 | **TTYBUS** steals the architecture concept from VDE (Virtual Distributed Ethernet) design. 21 | The working scheme is quite simple: Users can connect their ttys together in a virtual bus, then all data sent and received 22 | from any of the devices connected to the virtual bus is echoed to all other devices. 23 | 24 | This includes real and virtual tty devices, with the possibility to create fake devices using posix pseudo-terminals. 25 | 26 | 27 | ## COMPONENTS 28 | 29 | ### `tty_bus` 30 | Creates a new tty_bus running on the system, at a given bus path specified with the `-s` option. The command creates the bus 31 | and exposes a unix socket at the given path. Once the path has been created, any device can be plugged in using the other 32 | toolkit's commands. The `-d` option deamonizes the process and detaches it from the terminal. 33 | 34 | ### `tty_plug` 35 | Connects `STDIN/STDOUT` of the current terminal to the tty_bus specified with the `-s` option. 36 | Eventually the `-i` option can be specified to add an init string to be passed to process stdout before it's connected 37 | to the tty_bus. The `-d` option deamonizes the process and detaches it from the terminal. 38 | 39 | ### `tty_fake` 40 | Creates a new pseudo-terminal devices connected to the tty_bus specified with the `-s` option. If the given path for the fake 41 | device already exists, `tty_fake` can be forced to replace it with the `-o` option. The `-d` option deamonizes the process and 42 | detaches it from the terminal. 43 | 44 | ### `tty_attach` 45 | Open a real (existing) tty and connects it to the tty_bus specified with the -s option. 46 | Eventually the `-i` option can be specified to add an init string to be passed to the real tty device before it's connected 47 | to the tty_bus. The `-d` option deamonizes the process and detaches it from the terminal. 48 | 49 | ### `dpipe` 50 | Taken from the VDE project, allows two unix processes to communicate each-other by attaching each process' `STDOUT` stream to 51 | the other one's `STDIN`. 52 | 53 | Please refer to each command's help for usage notes, using the `-h` option . 54 | 55 | 56 | ## EXAMPLES 57 | 58 | ### Use case 1 59 | Multiplexing serial input only or output only device attached to `/dev/ttyS0`, for use with multiple applications. 60 | 61 | 1. Create a new tty_bus called `/tmp/ttyS0mux`: 62 | 63 | `tty_bus -d -s /tmp/ttyS0mux` 64 | 65 | 1. Connect the real device to the bus using `tty_attach`: 66 | 67 | `tty_attach -d -s /tmp/ttyS0mux /dev/ttyS0` 68 | 69 | 1. Create two fake `/dev/ttyS0` devices, attached to the bus: 70 | 71 | `tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0fake0` 72 | 73 | `tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0fake1` 74 | 75 | 1. Start your application and force it to use the new serial device for input or output 76 | 77 | `/bin/foo /dev/ttyS0fake0 &` 78 | 79 | `/bin/bar /dev/ttyS0fake1 &` 80 | 81 | Both application will read (or write) from the same serial device. 82 | 83 | **CAUTION:** All data written on each of the two fake devices will be echoed on the other one too. 84 | 85 | 86 | ### Use case 2 87 | Create fake NMEA devices from your running gpsd daemon. 88 | 89 | 1. Create the bus: 90 | 91 | `tty_bus -s /tmp/gpsmux` 92 | 93 | 1. Use `dpipe`, `tty_plug` and `netcat` to connect tcp socket to gpsd daemon. Send 'r' as init string to get NMEA flow from the socket: 94 | 95 | `dpipe tty_plug -s /tmp/gpsmux -i "r" = nc -c ip.address.of.gpsd 2947` 96 | 97 | 1. Create a fake gps device 98 | 99 | `tty_fake -s /tmp/gpsmux /dev/gps0` 100 | 101 | 1. Connect your legacy serial, non gpsd-compatible, nmea application to the fake device `/dev/gps0` 102 | 103 | `/usr/local/games/stupid_nmea_app /dev/gps0` 104 | 105 | 106 | ### Use case 3: 107 | Remote serial device via SSH tunnel. Host *mars* has a serial device connected on `/dev/ttyUSB0`, which must be accessed and used from host *venus*. 108 | 109 | 1. Create two `tty_bus`ses, one per machine. 110 | 111 | `mars:$ tty_bus -s /tmp/exported_ttybus` 112 | 113 | `venus:$ tty_bus -s /tmp/remote_ttybus` 114 | 115 | 1. On *mars*, attach the device to the local bus: 116 | 117 | `mars:$ tty_attach -s /tmp/exported_ttybus /dev/ttyUSB0` 118 | 119 | 1. On *venus*, prepare the fake tty device, on the same path, temporarly overriding the local `/dev/ttyUSB0` device: 120 | 121 | `venus:$ tty_fake -s /tmp/remote_ttybus -o /dev/ttyUSB0` 122 | 123 | 1. Connect the two buses on the two hosts, using remote ssh command and `tty_plug`: 124 | 125 | `venus:$ dpipe tty_plug -s /tmp/remote_ttybus = ssh mars tty_plug -s /tmp/exported_ttybus` 126 | 127 | From now on, the `/dev/ttyUSB0` on *venus* is actually the device connected on *mars*. If it existed before being overridden, 128 | the original `/dev/ttyUSB0` is restored once the `tty_fake` process on venus gets killed. 129 | -------------------------------------------------------------------------------- /configure.h: -------------------------------------------------------------------------------- 1 | #define MAJORV "0" 2 | #define MINORV "2" 3 | #define SVNVERSION "0" -------------------------------------------------------------------------------- /configure.h.in: -------------------------------------------------------------------------------- 1 | #define MAJORV "0" 2 | #define MINORV "1" 3 | #define SVNVERSION "___SVNVERSION___" 4 | 5 | 6 | -------------------------------------------------------------------------------- /dpipe.c: -------------------------------------------------------------------------------- 1 | /* Copyright 2003 Renzo Davoli 2 | * Licensed under the GPL 3 | */ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | 13 | static char *progname; 14 | static int alternate_stdin; 15 | static int alternate_stdout; 16 | 17 | 18 | void usage() { 19 | fprintf(stderr, "Usage:\n\t%s cmd1 [arg1...] = cmd2 [arg2...]\n\n", progname); 20 | kill(getpgrp(), SIGTERM); 21 | exit(-1); 22 | } 23 | 24 | 25 | int splitindex(int argc, char *argv[], int *dirchar) { 26 | register int i; 27 | 28 | for (i = 0; i < argc; i++) { 29 | if (*dirchar == argv[i][0] && argv[i][1] == '=') { 30 | (argv[i])++; 31 | *dirchar = 0; 32 | } 33 | if (argv[i][0] == '=') { 34 | if (argv[i][1] == '}' || argv[i][1] == '{') 35 | *dirchar = argv[i][1]; 36 | break; 37 | } 38 | } 39 | return i; 40 | } 41 | 42 | 43 | static void alternate_fd() { 44 | char numstr[10]; 45 | alternate_stdin = open("/dev/null", O_RDONLY); 46 | alternate_stdout = open("/dev/null", O_RDONLY); 47 | close(alternate_stdin); 48 | close(alternate_stdout); 49 | snprintf(numstr, 10, "%d", alternate_stdin); 50 | setenv("ALTERNATE_STDIN", numstr, 1); 51 | snprintf(numstr, 10, "%d", alternate_stdout); 52 | setenv("ALTERNATE_STDOUT", numstr, 1); 53 | } 54 | 55 | 56 | int recmain(int argc, char *argv[], int olddirchar) { 57 | int split; 58 | int newdirchar = olddirchar; 59 | 60 | split = splitindex(argc, argv, &newdirchar); 61 | if (split >= argc) { 62 | if (newdirchar != 0) 63 | usage(); 64 | execvp(argv[0], argv); 65 | } else { 66 | char **argv1, **argv2; 67 | int p1[2], p2[2]; 68 | 69 | if (argc < 3 || split == 0 || split == argc - 1) 70 | usage(); 71 | 72 | pipe(p1); 73 | if (olddirchar == 0) 74 | pipe(p2); 75 | argv[split] = NULL; 76 | argv1 = argv; 77 | argv2 = argv + (split + 1); 78 | 79 | if (fork()) { 80 | switch (olddirchar) { 81 | case 0: 82 | close(p1[1]); 83 | close(p2[0]); 84 | if (p1[0] != alternate_stdin) { 85 | dup2(p1[0], alternate_stdin); 86 | close(p1[0]); 87 | } 88 | if (p1[0] != alternate_stdout) { 89 | dup2(p2[1], alternate_stdout); 90 | close(p2[1]); 91 | } 92 | break; 93 | case '}': 94 | close(p1[0]); 95 | dup2(p1[1], STDOUT_FILENO); 96 | close(p1[1]); 97 | break; 98 | case '{': 99 | close(p1[1]); 100 | dup2(p1[0], STDIN_FILENO); 101 | close(p1[0]); 102 | break; 103 | default: 104 | fprintf(stderr, "CHECA\n"); 105 | } 106 | execvp(argv1[0], argv1); 107 | } else { 108 | switch (olddirchar) { 109 | case 0: 110 | close(p2[1]); 111 | close(p1[0]); 112 | dup2(p2[0], STDIN_FILENO); 113 | dup2(p1[1], STDOUT_FILENO); 114 | close(p2[0]); 115 | close(p1[1]); 116 | break; 117 | case '}': 118 | close(p1[1]); 119 | dup2(p1[0], STDIN_FILENO); 120 | close(p1[0]); 121 | break; 122 | case '{': 123 | close(p1[0]); 124 | dup2(p1[1], STDOUT_FILENO); 125 | close(p1[1]); 126 | break; 127 | default: 128 | fprintf(stderr, "CHECA\n"); 129 | } 130 | recmain(argc - split - 1, argv2, newdirchar); 131 | } 132 | } 133 | return 0; 134 | } 135 | 136 | 137 | int main(int argc, char *argv[]) { 138 | int split; 139 | char **argv1, **argv2; 140 | int p1[2], p2[2]; 141 | int dirchar = 0; 142 | 143 | progname = argv[0]; 144 | argv++; 145 | argc--; 146 | 147 | alternate_fd(); 148 | split = splitindex(argc, argv, &dirchar); 149 | 150 | if (argc < 3 || split == 0 || split >= argc - 1) 151 | usage(); 152 | 153 | pipe(p1); 154 | pipe(p2); 155 | argv[split] = NULL; 156 | argv1 = argv; 157 | argv2 = argv + (split + 1); 158 | 159 | if (fork()) { 160 | close(p1[1]); 161 | close(p2[0]); 162 | dup2(p1[0], STDIN_FILENO); 163 | dup2(p2[1], STDOUT_FILENO); 164 | close(p1[0]); 165 | close(p2[1]); 166 | execvp(argv1[0], argv1); 167 | } else { 168 | close(p2[1]); 169 | close(p1[0]); 170 | dup2(p2[0], STDIN_FILENO); 171 | dup2(p1[1], STDOUT_FILENO); 172 | close(p1[1]); 173 | close(p2[0]); 174 | recmain(argc - split - 1, argv2, dirchar); 175 | } 176 | return (0); 177 | } -------------------------------------------------------------------------------- /tty_attach.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | 20 | #include "configure.h" 21 | 22 | #define MAX_TTY 256 23 | #define BUFFER_SIZE 4096 24 | #define POLL_R_TIMEOUT 100 25 | #define POLL_W_TIMEOUT 50 26 | 27 | static char *tty_bus_path; 28 | static char *devname; 29 | static char *init_string; 30 | 31 | 32 | static void usage(char *app) { 33 | fprintf(stderr, "%s, Ver %s.%s.%s\n", basename(app), MAJORV, MINORV, SVNVERSION); 34 | fprintf(stderr, "Usage: %s [-h] [-s bus_path] tty_device\n", app); 35 | fprintf(stderr, "-h: shows this help\n"); 36 | fprintf(stderr, "-d: detach from terminal and run as daemon\n"); 37 | fprintf(stderr, "-s bus_path: uses bus_path as bus path name (default: /tmp/ttybus)\n"); 38 | fprintf(stderr, "-i init_string: send init string to device\n\n"); 39 | fprintf(stderr, "Please also see: tty_bus, tty_fake, tty_plug, dpipe\n"); 40 | fprintf(stderr, "Example of usage:\n"); 41 | fprintf(stderr, " Create a new bus called /tmp/ttyS0mux\n"); 42 | fprintf(stderr, " tty_bus -d -s /tmp/ttyS0mux\n"); 43 | fprintf(stderr, " Connect a real device to the bus /tmp/ttyS0mux\n"); 44 | fprintf(stderr, " tty_attach -d -s /tmp/ttyS0mux /dev/ttyS0\n"); 45 | fprintf(stderr, " Create two fake ttyS0 devices, attached to the bus /tmp/ttyS0mux\n"); 46 | fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.0\n"); 47 | fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.1\n"); 48 | exit(2); 49 | } 50 | 51 | 52 | int tty_connect(char *path) { 53 | struct sockaddr_un sun; 54 | int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0); 55 | memset(&sun, 0, sizeof(struct sockaddr_un)); 56 | sun.sun_family = AF_UNIX; 57 | strncpy(sun.sun_path, path, strlen(path)); 58 | if (connect(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { 59 | perror("Cannot connect to socket"); 60 | syslog(LOG_ERR, "Cannot connect to socket"); 61 | exit(-1); 62 | } 63 | return connect_fd; 64 | } 65 | 66 | 67 | int main(int argc, char *argv[]) { 68 | int fd; 69 | struct pollfd pfd[2]; 70 | int pollret, r; 71 | char buffer[BUFFER_SIZE]; 72 | int realdev; 73 | int daemonize = 0; 74 | 75 | while (1) { 76 | int c; 77 | c = getopt(argc, argv, "dhi:s:"); 78 | if (c == -1) 79 | break; 80 | 81 | switch (c) { 82 | case 'd': 83 | daemonize = 1; 84 | break; 85 | case 'h': 86 | usage(argv[0]); // implies exit 87 | break; 88 | case 's': 89 | tty_bus_path = strdup(optarg); 90 | break; 91 | case 'i': 92 | init_string = strdup(optarg); 93 | break; 94 | default: 95 | usage(argv[0]); // implies exit 96 | } 97 | } 98 | if (optind != (argc - 1)) 99 | usage(argv[0]); // implies exit 100 | 101 | if (daemonize) 102 | daemon(0, 0); 103 | 104 | devname = strdup(argv[optind]); 105 | 106 | if (!tty_bus_path) 107 | tty_bus_path = strdup("/tmp/ttybus"); 108 | 109 | fprintf(stderr, "Connecting to bus: %s\n", tty_bus_path); 110 | syslog(LOG_INFO, "Connecting to bus: %s\n", tty_bus_path); 111 | fd = tty_connect(tty_bus_path); 112 | 113 | realdev = open(devname, O_RDWR); 114 | if (realdev < 0) { 115 | perror("opening device"); 116 | exit(3); 117 | } 118 | 119 | if (init_string) { 120 | pfd[0].fd = realdev; 121 | pfd[0].events = POLLOUT; 122 | pollret = poll(&pfd[0], 1, 50); 123 | if (pollret < 0) { 124 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 125 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 126 | exit(1); 127 | } 128 | if (pfd[0].revents & POLLOUT) { 129 | write(realdev, init_string, strlen(init_string)); 130 | write(realdev, "\n", 1); 131 | } else { 132 | fprintf(stderr, "Device is busy, cannot send init string.\n"); 133 | syslog(LOG_WARNING, "Device is busy, cannot send init string.\n"); 134 | } 135 | } 136 | for (;;) { 137 | pfd[0].fd = realdev; 138 | pfd[0].events = POLLIN; 139 | pfd[1].fd = fd; 140 | pfd[1].events = POLLIN; 141 | pollret = poll(pfd, 2, 1000); 142 | if (pollret < 0) { 143 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 144 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 145 | exit(1); 146 | } 147 | if (pollret == 0) 148 | continue; 149 | 150 | if ((pfd[0].revents & POLLHUP || pfd[0].revents & POLLERR || pfd[0].revents & POLLNVAL) || 151 | (pfd[1].revents & POLLHUP || pfd[1].revents & POLLERR || pfd[1].revents & POLLNVAL)) 152 | exit(1); 153 | if (pfd[0].revents & POLLIN) { 154 | r = read(realdev, buffer, BUFFER_SIZE); 155 | pfd[1].events = POLLOUT; 156 | pollret = poll(&pfd[1], 1, 50); 157 | if (pollret < 0) { 158 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 159 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 160 | exit(1); 161 | } 162 | if (pfd[1].revents & POLLOUT) 163 | write(fd, buffer, r); 164 | } 165 | if (pfd[1].revents & POLLIN) { 166 | r = read(fd, buffer, BUFFER_SIZE); 167 | pfd[0].fd = realdev; 168 | pfd[0].events = POLLOUT; 169 | pollret = poll(&pfd[0], 1, 50); 170 | if (pollret < 0) { 171 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 172 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 173 | exit(1); 174 | } 175 | if (pfd[0].revents & POLLOUT) 176 | write(realdev, buffer, r); 177 | } 178 | } 179 | } -------------------------------------------------------------------------------- /tty_bus.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | 22 | #include "configure.h" 23 | 24 | #define MAX_TTY 256 25 | #define BUFFER_SIZE 4096 26 | #define POLL_R_TIMEOUT 100 27 | #define POLL_W_TIMEOUT 50 28 | static char *tty_bus_path = NULL; 29 | 30 | 31 | static void usage(char *app) { 32 | fprintf(stderr, "%s, Ver %s.%s.%s\n", basename(app), MAJORV, MINORV, SVNVERSION); 33 | fprintf(stderr, "Usage: %s [-h] [-s bus_path]\n", app); 34 | fprintf(stderr, "-h: shows this help\n"); 35 | fprintf(stderr, "-d: detach from terminal and run as daemon\n"); 36 | fprintf(stderr, "-s bus_path: uses bus_path as bus path name (default: /tmp/ttybus)\n\n"); 37 | fprintf(stderr, "Please also see: tty_attach, tty_fake, tty_plug, dpipe\n"); 38 | fprintf(stderr, "Example of usage:\n"); 39 | fprintf(stderr, " Create a new bus called /tmp/ttyS0mux\n"); 40 | fprintf(stderr, " tty_bus -d -s /tmp/ttyS0mux\n"); 41 | fprintf(stderr, " Connect a real device to the bus /tmp/ttyS0mux\n"); 42 | fprintf(stderr, " tty_attach -d -s /tmp/ttyS0mux /dev/ttyS0\n"); 43 | fprintf(stderr, " Create two fake ttyS0 devices, attached to the bus /tmp/ttyS0mux\n"); 44 | fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.0\n"); 45 | fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.1\n"); 46 | exit(2); 47 | } 48 | 49 | 50 | void exiting(void) { 51 | unlink(tty_bus_path); 52 | } 53 | 54 | 55 | void signaled(int signo) { 56 | exit(0); 57 | } 58 | 59 | 60 | int bus_init(char *path) { 61 | struct sockaddr_un sun; 62 | int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0); 63 | memset(&sun, 0, sizeof(struct sockaddr_un)); 64 | sun.sun_family = AF_UNIX; 65 | strncpy(sun.sun_path, path, strlen(path)); 66 | if (bind(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { 67 | if ((errno == EADDRINUSE)) { 68 | printf("Could not bind to socket '%s': %s\n", path, strerror(errno)); 69 | syslog(LOG_ERR, "Could not bind to socket '%s': %s\n", path, strerror(errno)); 70 | exit(-1); 71 | } else if (bind(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { 72 | printf("Could not bind to socket '%s' (second attempt): %s", path, strerror(errno)); 73 | syslog(LOG_ERR, "Could not bind to socket '%s' (second attempt): %s", path, strerror(errno)); 74 | exit(-1); 75 | } 76 | } 77 | chmod(sun.sun_path, 0777); 78 | if (listen(connect_fd, 1) < 0) { 79 | printf("Could not listen on fd %d: %s", connect_fd, strerror(errno)); 80 | syslog(LOG_ERR, "Could not listen on fd %d: %s", connect_fd, strerror(errno)); 81 | exit(-1); 82 | } 83 | return connect_fd; 84 | } 85 | 86 | 87 | void bus_destroy(int fd, char *path) { 88 | close(fd); 89 | unlink(path); 90 | } 91 | 92 | 93 | int prepare_poll(int *tty, struct pollfd **ppfd, int lfd, int flags) { 94 | struct pollfd *pfd = (struct pollfd *) *ppfd; 95 | int i, fdcount = 0; 96 | if (lfd != -1) 97 | pfd[fdcount++].fd = lfd; 98 | 99 | for (i = 0; i < MAX_TTY; i++) { 100 | if (tty[i] != -1) 101 | pfd[fdcount++].fd = tty[i]; 102 | } 103 | 104 | for (i = 0; i < fdcount; i++) 105 | pfd[i].events = flags; 106 | 107 | return fdcount; 108 | } 109 | 110 | 111 | void init_dev_array(int **ptty) { 112 | int i; 113 | int *tty = *ptty; 114 | for (i = 0; i < MAX_TTY; i++) 115 | tty[i] = -1; 116 | } 117 | 118 | 119 | int check_poll_errors(struct pollfd *pfd, int n, int *tty) { 120 | int i, j; 121 | int err = 0; 122 | for (i = 0; i < n; i++) { 123 | if (pfd[i].revents & POLLHUP || pfd[i].revents & POLLERR || pfd[i].revents & POLLNVAL) { 124 | for (j = 0; j < MAX_TTY; j++) { 125 | if (tty[j] == pfd[i].fd) { 126 | close(tty[j]); 127 | tty[j] = -1; 128 | ++err; 129 | break; 130 | } 131 | } 132 | } 133 | } 134 | return err; 135 | } 136 | 137 | 138 | void recvbuff(int src, char *buf, int size, int *tty) { 139 | struct pollfd *wpfd; 140 | int n, i; 141 | int pollret; 142 | 143 | wpfd = (struct pollfd *) malloc(sizeof(struct pollfd) * MAX_TTY); 144 | if (!wpfd) { 145 | fprintf(stderr, "alloc error: %s\n", strerror(errno)); 146 | syslog(LOG_INFO, "alloc error: %s\n", strerror(errno)); 147 | } 148 | 149 | n = prepare_poll(tty, (struct pollfd **) &wpfd, -1, POLLOUT | POLLHUP); 150 | printf("Writing to %d clients: %d bytes\n", n, size); 151 | if (n > 1) { 152 | pollret = poll(wpfd, n, POLL_W_TIMEOUT); 153 | if (pollret < 0) { 154 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 155 | syslog(LOG_WARNING, "Poll error: %s\n", strerror(errno)); 156 | sleep(1); 157 | return; 158 | } 159 | (void) check_poll_errors(wpfd, n, tty); 160 | 161 | for (i = 0; i < n; i++) { 162 | if (wpfd[i].revents & POLLOUT && wpfd[i].fd != src) 163 | write(wpfd[i].fd, buf, size); 164 | } 165 | } 166 | free(wpfd); 167 | } 168 | 169 | 170 | int main(int argc, char *argv[]) { 171 | int n = 0; 172 | int listenfd = -1; 173 | int i, r = 0; 174 | int pollret; 175 | struct pollfd *pfd; 176 | int *tty; 177 | char buffer[BUFFER_SIZE]; 178 | int daemonize = 0; 179 | 180 | pfd = (struct pollfd *) malloc(sizeof(struct pollfd) * (1 + MAX_TTY)); 181 | tty = (int *) malloc(sizeof(int) * MAX_TTY); 182 | if (!pfd || !tty) { 183 | fprintf(stderr, "alloc error: %s\n", strerror(errno)); 184 | syslog(LOG_ERR, "alloc error: %s\n", strerror(errno)); 185 | exit(4); 186 | } 187 | while (1) { 188 | int c; 189 | c = getopt(argc, argv, "dhs:"); 190 | if (c == -1) 191 | break; 192 | 193 | switch (c) { 194 | case 'd': 195 | daemonize = 1; 196 | break; 197 | case 'h': 198 | usage(argv[0]); // implies exit 199 | break; 200 | case 's': 201 | tty_bus_path = strdup(optarg); 202 | break; 203 | default: 204 | usage(argv[0]); // implies exit 205 | } 206 | } 207 | if (optind < argc) 208 | usage(argv[0]); // implies exit 209 | 210 | if (daemonize) 211 | daemon(0, 0); 212 | 213 | if (!tty_bus_path) 214 | tty_bus_path = strdup("/tmp/ttybus"); 215 | 216 | fprintf(stderr, "Creating bus: %s\n", tty_bus_path); 217 | syslog(LOG_INFO, "Creating bus: %s\n", tty_bus_path); 218 | 219 | atexit(exiting); 220 | sigset(SIGTERM, signaled); 221 | sigset(SIGINT, signaled); 222 | 223 | init_dev_array((int **) &tty); 224 | listenfd = bus_init(tty_bus_path); 225 | if (listenfd < 0) { 226 | fprintf(stderr, "Cannot bind to %s: %s\n", tty_bus_path, strerror(errno)); 227 | syslog(LOG_ERR, "Cannot bind to %s: %s\n", tty_bus_path, strerror(errno)); 228 | exit(1); 229 | } 230 | for (;;) { 231 | n = prepare_poll(tty, (struct pollfd **) &pfd, listenfd, POLLIN | POLLHUP); 232 | pollret = poll(pfd, n, POLL_R_TIMEOUT); 233 | if (pollret < 0) { 234 | fprintf(stderr, "Poll error: %s, n is %d\n", strerror(errno), n); 235 | syslog(LOG_WARNING, "Poll error: %s, n is %d\n", strerror(errno), n); 236 | sleep(1); 237 | continue; 238 | } 239 | if (check_poll_errors(pfd, n, tty) > 0) 240 | continue; 241 | 242 | if (pfd[0].revents & POLLIN) { 243 | struct sockaddr_un cliaddr; 244 | socklen_t len = sizeof(struct sockaddr_un); 245 | int connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &len); 246 | if (connfd == -1) { 247 | bus_destroy(listenfd, tty_bus_path); 248 | listenfd = bus_init(tty_bus_path); 249 | } else { 250 | for (i = 0; i < MAX_TTY; i++) 251 | if (tty[i] == -1) { 252 | tty[i] = connfd; 253 | break; 254 | } 255 | } 256 | continue; 257 | } 258 | if (pollret > 0) { 259 | for (;;) { 260 | if (pfd[i].revents & POLLIN) { 261 | r = read(pfd[i].fd, buffer, BUFFER_SIZE); 262 | if (r > 0) 263 | recvbuff(pfd[i].fd, buffer, r, tty); 264 | break; 265 | } 266 | if (++i >= n) 267 | i = 1; 268 | } 269 | } 270 | } 271 | } -------------------------------------------------------------------------------- /tty_fake.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | 21 | #include "configure.h" 22 | 23 | #define MAX_TTY 256 24 | #define BUFFER_SIZE 4096 25 | #define POLL_R_TIMEOUT 100 26 | #define POLL_W_TIMEOUT 50 27 | #define POLL_INTERVAL 10000000 28 | 29 | static char *ttyfake; 30 | static char *tty_bus_path; 31 | static char *ttybak; 32 | static int force_overwrite = 0; 33 | static int restore = 0; 34 | 35 | 36 | static void usage(char *app) { 37 | fprintf(stderr, "%s, Ver %s.%s.%s\n", basename(app), MAJORV, MINORV, SVNVERSION); 38 | fprintf(stderr, "Usage: %s [-h] [-s bus_path] tty_device\n", app); 39 | fprintf(stderr, "-h: shows this help\n"); 40 | fprintf(stderr, "-d: detach from terminal and run as daemon\n"); 41 | fprintf(stderr, "-s bus_path: uses bus_path as bus path name (default: /tmp/ttybus)\n"); 42 | fprintf(stderr, "-o: temporarly backup tty_device to tty_device.bak, if it exists, and restore the original file at exit\n\n"); 43 | fprintf(stderr, "Please also see: tty_bus, tty_attach, tty_plug, dpipe\n"); 44 | fprintf(stderr, "Example of usage:\n"); 45 | fprintf(stderr, " Create a new bus called /tmp/ttyS0mux\n"); 46 | fprintf(stderr, " tty_bus -d -s /tmp/ttyS0mux\n"); 47 | fprintf(stderr, " Connect a real device to the bus /tmp/ttyS0mux\n"); 48 | fprintf(stderr, " tty_attach -d -s /tmp/ttyS0mux /dev/ttyS0\n"); 49 | fprintf(stderr, " Create two fake ttyS0 devices, attached to the bus /tmp/ttyS0mux\n"); 50 | fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.0\n"); 51 | fprintf(stderr, " tty_fake -d -s /tmp/ttyS0mux /dev/ttyS0.1\n"); 52 | exit(2); 53 | } 54 | 55 | 56 | int tty_connect(char *path) { 57 | struct sockaddr_un sun; 58 | int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0); 59 | memset(&sun, 0, sizeof(struct sockaddr_un)); 60 | sun.sun_family = AF_UNIX; 61 | strncpy(sun.sun_path, path, strlen(path)); 62 | if (connect(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { 63 | perror("Cannot connect to socket"); 64 | syslog(LOG_ERR, "Cannot connect to socket\n"); 65 | exit(-1); 66 | } 67 | return connect_fd; 68 | } 69 | 70 | static void _tty_cleanup(void) { 71 | fprintf(stderr, "Removing symlink\n"); 72 | syslog(LOG_INFO, "Removing symlink\n"); 73 | unlink(ttyfake); 74 | if(restore) { 75 | fprintf(stderr, "Restoring original device\n"); 76 | syslog(LOG_INFO, "Restoring original device\n"); 77 | link(ttybak, ttyfake); 78 | unlink(ttybak); 79 | } 80 | } 81 | 82 | 83 | static void tty_restore(int __attribute__((unused)) signo) { 84 | exit(0); 85 | } 86 | 87 | int main(int argc, char *argv[]) 88 | { 89 | int fd; 90 | struct pollfd pfd[2]; 91 | int pollret, r; 92 | char buffer[BUFFER_SIZE]; 93 | char *pts; 94 | int ptmx; 95 | int daemonize = 0; 96 | struct timespec poll_interval, remaining; 97 | poll_interval.tv_sec = 0; 98 | poll_interval.tv_nsec = POLL_INTERVAL; 99 | 100 | while (1) { 101 | int c; 102 | c = getopt(argc, argv, "dhos:"); 103 | if (c == -1) 104 | break; 105 | 106 | switch (c) { 107 | case 'd': 108 | daemonize = 1; 109 | break; 110 | 111 | case 'h': 112 | usage(argv[0]); // implies exit 113 | break; 114 | 115 | case 's': 116 | tty_bus_path = strdup(optarg); 117 | break; 118 | 119 | case 'o': 120 | force_overwrite = 1; 121 | break; 122 | 123 | default: 124 | usage(argv[0]); // implies exit 125 | } 126 | } 127 | 128 | if (optind != (argc - 1)) 129 | usage(argv[0]); // implies exit 130 | 131 | if (daemonize) 132 | daemon(0, 0); 133 | 134 | ttyfake = strdup(argv[optind]); 135 | if (access(ttyfake, W_OK) == 0) { 136 | if (force_overwrite) { 137 | ttybak = malloc(strlen(ttyfake) + 5); 138 | sprintf(ttybak, "%s.bak", ttyfake); 139 | unlink(ttybak); 140 | link(ttyfake, ttybak); 141 | unlink(ttyfake); 142 | restore = 1; 143 | } else { 144 | fprintf(stderr, "%s already exists! use -o to force overwrite\n", ttyfake); 145 | syslog(LOG_ERR, "%s already exists! use -o to force overwrite\n", ttyfake); 146 | exit(1); 147 | } 148 | } 149 | 150 | 151 | if (!tty_bus_path) 152 | tty_bus_path = strdup("/tmp/ttybus"); 153 | 154 | fprintf(stderr, "Connecting to bus: %s\n", tty_bus_path); 155 | syslog(LOG_INFO, "Connecting to bus: %s\n", tty_bus_path); 156 | fd = tty_connect(tty_bus_path); 157 | 158 | 159 | ptmx = open("/dev/ptmx", O_RDWR); 160 | pts = (char *) ptsname(ptmx); 161 | fprintf(stderr, "Device: %s is now %s\n", pts, ttyfake); 162 | syslog(LOG_INFO, "Device: %s is now %s\n", pts, ttyfake); 163 | grantpt(ptmx); 164 | unlockpt(ptmx); 165 | 166 | symlink(pts, ttyfake); 167 | chmod(pts, 00777); 168 | atexit(_tty_cleanup); 169 | sigset(SIGTERM, tty_restore); 170 | sigset(SIGINT, tty_restore); 171 | sigset(SIGUSR1, tty_restore); 172 | sigset(SIGUSR2, tty_restore); 173 | 174 | for (;;) { 175 | nanosleep(&poll_interval, &remaining); 176 | pfd[0].fd = ptmx; 177 | pfd[0].events = POLLIN; 178 | pfd[1].fd = fd; 179 | pfd[1].events = POLLIN; 180 | pollret = poll(pfd, 2, 1000); 181 | if (pollret < 0) { 182 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 183 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 184 | exit(1); 185 | } 186 | if (pollret == 0) { 187 | continue; 188 | } 189 | 190 | if ((pfd[0].revents & POLLERR || pfd[0].revents & POLLNVAL) || 191 | (pfd[1].revents & POLLHUP || pfd[1].revents & POLLERR || pfd[1].revents & POLLNVAL)) { 192 | syslog(LOG_INFO, "Terminating: %d %d\n", pfd[0].revents, pfd[1].revents); 193 | exit(1); 194 | } 195 | if (pfd[0].revents & POLLIN) { 196 | r = read(ptmx, buffer, BUFFER_SIZE); 197 | pfd[1].events = POLLOUT; 198 | pollret = poll(&pfd[1], 1, 50); 199 | if (pollret < 0) { 200 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 201 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 202 | exit(1); 203 | } 204 | if (pfd[1].revents & POLLOUT) { 205 | write(fd, buffer, r); 206 | } 207 | } 208 | if (pfd[1].revents & POLLIN) { 209 | r = read(fd, buffer, BUFFER_SIZE); 210 | pfd[0].fd = ptmx; 211 | pfd[0].events = POLLOUT; 212 | pollret = poll(&pfd[0], 1, 50); 213 | if (pollret < 0) { 214 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 215 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 216 | exit(1); 217 | } 218 | if (pfd[0].revents & POLLOUT) { 219 | write(ptmx, buffer, r); 220 | } 221 | } 222 | } 223 | } 224 | -------------------------------------------------------------------------------- /tty_plug.c: -------------------------------------------------------------------------------- 1 | #define _GNU_SOURCE 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | #include "configure.h" 20 | 21 | #define MAX_TTY 256 22 | #define BUFFER_SIZE 4096 23 | #define POLL_R_TIMEOUT 100 24 | #define POLL_W_TIMEOUT 50 25 | 26 | static char *tty_bus_path; 27 | static char *init_string; 28 | 29 | 30 | static void usage(char *app) { 31 | fprintf(stderr, "%s, Ver %s.%s.%s\n", basename(app), MAJORV, MINORV, SVNVERSION); 32 | fprintf(stderr, "Usage: %s [-h] [-s bus_path]\n", app); 33 | fprintf(stderr, "-h: shows this help\n"); 34 | fprintf(stderr, "-d: detach from terminal and run as daemon\n"); 35 | fprintf(stderr, "-s bus_path: uses bus_path as bus path name (default: /tmp/ttybus)\n"); 36 | fprintf(stderr, "-i init_string: send init string to plug's STDOUT\n\n"); 37 | fprintf(stderr, "Please also see: tty_bus, tty_attach, tty_fake, dpipe\n"); 38 | fprintf(stderr, "Example of usage:\n"); 39 | fprintf(stderr, " Create two tty_bus, one per machine\n"); 40 | fprintf(stderr, " mars:$ tty_bus -d -s /tmp/exported_ttybus\n"); 41 | fprintf(stderr, " venus:$ tty_bus -d -s /tmp/remote_ttybus\n"); 42 | fprintf(stderr, " On mars, attach the device to the local bus\n"); 43 | fprintf(stderr, " mars:$ tty_attach -d -s /tmp/exported_ttybus /dev/ttyUSB0\n"); 44 | fprintf(stderr, " On venus, prepare the fake tty device, on the same path,\n"); 45 | fprintf(stderr, " temporarly overriding the local /dev/ttyUSB0 device\n"); 46 | fprintf(stderr, " venus:$ tty_fake -d -s /tmp/remote_ttybus -o /dev/ttyUSB0\n"); 47 | fprintf(stderr, " Connect the two buses on the two hosts, using remote ssh command and tty_plug\n"); 48 | fprintf(stderr, " venus:$ dpipe tty_plug -s /tmp/remote_ttybus = ssh mars tty_plug -s /tmp/exported_ttybus\n"); 49 | exit(2); 50 | } 51 | 52 | 53 | int tty_connect(char *path) { 54 | struct sockaddr_un sun; 55 | int connect_fd = socket(PF_UNIX, SOCK_STREAM, 0); 56 | memset(&sun, 0, sizeof(struct sockaddr_un)); 57 | sun.sun_family = AF_UNIX; 58 | strncpy(sun.sun_path, path, strlen(path)); 59 | if (connect(connect_fd, (struct sockaddr *) &sun, sizeof(sun)) < 0) { 60 | perror("Cannot connect to socket"); 61 | syslog(LOG_ERR, "Cannot connect to socket\n"); 62 | exit(-1); 63 | } 64 | return connect_fd; 65 | } 66 | 67 | 68 | int main(int argc, char *argv[]) { 69 | int fd; 70 | struct pollfd pfd[2]; 71 | int pollret, r; 72 | char buffer[BUFFER_SIZE]; 73 | int daemonize = 0; 74 | 75 | while (1) { 76 | int c; 77 | c = getopt(argc, argv, "dhs:i:"); 78 | if (c == -1) 79 | break; 80 | 81 | switch (c) { 82 | case 'd': 83 | daemonize = 1; 84 | break; 85 | case 'h': 86 | usage(argv[0]); // implies exit 87 | break; 88 | case 's': 89 | tty_bus_path = strdup(optarg); 90 | break; 91 | case 'i': 92 | init_string = strdup(optarg); 93 | break; 94 | default: 95 | usage(argv[0]); // implies exit 96 | } 97 | } 98 | if (optind < argc) 99 | usage(argv[0]); // implies exit 100 | 101 | if (daemonize) 102 | daemon(0, 0); 103 | 104 | if (!tty_bus_path) 105 | tty_bus_path = strdup("/tmp/ttybus"); 106 | 107 | fprintf(stderr, "Connecting to bus: %s\n", tty_bus_path); 108 | syslog(LOG_INFO, "Connecting to bus: %s\n", tty_bus_path); 109 | fd = tty_connect(tty_bus_path); 110 | 111 | if (init_string) { 112 | write(STDOUT_FILENO, init_string, strlen(init_string)); 113 | write(STDOUT_FILENO, "\n", 1); 114 | } 115 | 116 | for (;;) { 117 | pfd[0].fd = STDIN_FILENO; 118 | pfd[0].events = POLLIN; 119 | pfd[1].fd = fd; 120 | pfd[1].events = POLLIN; 121 | pollret = poll(pfd, 2, 1000); 122 | if (pollret < 0) { 123 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 124 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 125 | exit(1); 126 | } 127 | if (pollret == 0) 128 | continue; 129 | 130 | if ((pfd[0].revents & POLLHUP || pfd[0].revents & POLLERR || pfd[0].revents & POLLNVAL) || 131 | (pfd[1].revents & POLLHUP || pfd[1].revents & POLLERR || pfd[1].revents & POLLNVAL)) { 132 | syslog(LOG_INFO, "Terminating: %d %d\n", pfd[0].revents, pfd[1].revents); 133 | exit(1); 134 | } 135 | 136 | if (pfd[0].revents & POLLIN) { 137 | r = read(STDIN_FILENO, buffer, BUFFER_SIZE); 138 | pfd[1].events = POLLOUT; 139 | pollret = poll(&pfd[1], 1, 50); 140 | if (pollret < 0) { 141 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 142 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 143 | exit(1); 144 | } 145 | if (pfd[1].revents & POLLOUT) 146 | write(fd, buffer, r); 147 | } 148 | if (pfd[1].revents & POLLIN) { 149 | r = read(fd, buffer, BUFFER_SIZE); 150 | pfd[0].fd = STDOUT_FILENO; 151 | pfd[0].events = POLLOUT; 152 | pollret = poll(&pfd[0], 1, 50); 153 | if (pollret < 0) { 154 | fprintf(stderr, "Poll error: %s\n", strerror(errno)); 155 | syslog(LOG_ERR, "Poll error: %s\n", strerror(errno)); 156 | exit(1); 157 | } 158 | if (pfd[0].revents & POLLOUT) 159 | write(STDOUT_FILENO, buffer, r); 160 | } 161 | } 162 | } 163 | --------------------------------------------------------------------------------