| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | dev_pt_common.h |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file is a test for the Pseudo Terminals: /dev/ptmx, /dev/pts/<n> |
| 12 | devices. |
| 13 | |
| 14 | --*/ |
| 15 | |
| 16 | #ifndef _DEV_PT_COMMON |
| 17 | #define _DEV_PT_COMMON |
| 18 | |
| 19 | #include <errno.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/wait.h> |
| 22 | #include <sys/cdefs.h> |
| 23 | #include <linux/limits.h> |
| 24 | #include <fcntl.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <termios.h> |
| 28 | #include <pthread.h> |
| 29 | #include <stdbool.h> |
| 30 | #include <unistd.h> |
| 31 | #include <pty.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/uio.h> |
| 34 | #include <termios.h> |
| 35 | #include "lxtcommon.h" |
| 36 | #include "unittests.h" |
| 37 | |
| 38 | // |
| 39 | // min, max macros |
| 40 | // |
| 41 | |
| 42 | #define min(a, b) (((a) < (b)) ? (a) : (b)) |
| 43 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 44 | |
| 45 | // |
| 46 | // TRUE, FALSE macros |
| 47 | // |
| 48 | |
| 49 | #define FALSE 0 |
| 50 | #define TRUE 1 |
| 51 | |
| 52 | #define PTS_DEV_NAME_BUFFER_SIZE 50 |
| 53 | |
| 54 | #define IS_CONTROL_CHAR_ECHO_STRING(s, c) (((s)[0] == '^') && (((s)[1] > 0x40)) && (((s)[1] - 0x40) == (c))) |
| 55 | |
| 56 | #define LxtCheckFnResults(fn, result, expectedresult) \ |
| 57 | { \ |
| 58 | if ((result) != (expectedresult)) \ |
| 59 | { \ |
| 60 | LxtLogError("Unexpected results. %s returned with result:%d, expected:%d.", #fn, result, expectedresult); \ |
| 61 | Result = LXT_RESULT_FAILURE; \ |
| 62 | goto ErrorExit; \ |
| 63 | } \ |
| 64 | \ |
| 65 | Result = LXT_RESULT_SUCCESS; \ |
| 66 | } |
| 67 | |
| 68 | #define LxtClose(_fd) \ |
| 69 | { \ |
| 70 | if ((_fd) >= 0) \ |
| 71 | { \ |
| 72 | LxtCheckErrno(close(_fd)); \ |
| 73 | (_fd) = -1; \ |
| 74 | } \ |
| 75 | } |
| 76 | |
| 77 | #ifndef STDIN |
| 78 | #define STDIN 0 |
| 79 | #endif |
| 80 | |
| 81 | #ifndef STDOUT |
| 82 | #define STDOUT 1 |
| 83 | #endif |
| 84 | |
| 85 | typedef enum _SIMPLE_READ_WRITE_MODE |
| 86 | { |
| 87 | SimpleReadWriteForeground, |
| 88 | SimpleReadWriteBackgroundNoSignal, |
| 89 | SimpleReadWriteBackgroundSignal, |
| 90 | SimpleReadWriteBackgroundSignalNoStop |
| 91 | } SIMPLE_READ_WRITE_MODE; |
| 92 | |
| 93 | // |
| 94 | // Functions. |
| 95 | // |
| 96 | |
| 97 | void DumpBuffer(const char Data[], size_t DataSize); |
| 98 | |
| 99 | int GetPtSerialNumFromDeviceString(const char PtsNameString[]); |
| 100 | |
| 101 | int GetRandomMessage(char Message[], size_t MessageSize, bool CompleteMessage); |
| 102 | |
| 103 | int OpenMasterSubordinate(int* PtmFd, int* PtsFd, char* PtsDevName, int* SerialNumber); |
| 104 | |
| 105 | pid_t ForkPty(int* PtmFdOut, int* PtsFdOut); |
| 106 | |
| 107 | pid_t ForkPtyBackground(int* PtmFdOut, int* PtsFdOut, pid_t* ForegroundIdOut); |
| 108 | |
| 109 | pid_t ForkPtyMaster(int* PtmFdOut, int* PtsFdOut); |
| 110 | |
| 111 | void PtSignalHandler(int signal, siginfo_t* signalInfo, void* context); |
| 112 | |
| 113 | int RawInit(int Fd); |
| 114 | |
| 115 | int SimpleReadWriteCheck(int PtmFd, int PtsFd); |
| 116 | |
| 117 | int SimpleReadWriteCheckEx(int PtmFd, int PtsFd, SIMPLE_READ_WRITE_MODE Mode); |
| 118 | |
| 119 | int TerminalSettingsGet(int Fd, cc_t* ControlArrayOut, tcflag_t* ControlFlagsOut, tcflag_t* InputFlagsOut, tcflag_t* LocalFlagsOut, tcflag_t* OutputFlagsOut); |
| 120 | |
| 121 | int TerminalSettingsGetControlArray(int Fd, cc_t* ControlArrayOut); |
| 122 | |
| 123 | int TerminalSettingsGetControlFlags(int Fd, tcflag_t* ControlFlagsOut); |
| 124 | |
| 125 | int TerminalSettingsGetInputFlags(int Fd, tcflag_t* InputFlagsOut); |
| 126 | |
| 127 | int TerminalSettingsGetLocalFlags(int Fd, tcflag_t* LocalFlagsOut); |
| 128 | |
| 129 | int TerminalSettingsGetOutputFlags(int Fd, tcflag_t* OutputFlagsOut); |
| 130 | |
| 131 | int TerminalSettingsSet(int Fd, cc_t* ControlArray, tcflag_t ControlFlags, tcflag_t InputFlags, tcflag_t LocalFlags, tcflag_t OutputFlags); |
| 132 | |
| 133 | int TerminalSettingsSetControlArray(int Fd, cc_t* ControlArray); |
| 134 | |
| 135 | int TerminalSettingsSetControlFlags(int Fd, tcflag_t ControlFlags); |
| 136 | |
| 137 | int TerminalSettingsSetInputFlags(int Fd, tcflag_t InputFlags); |
| 138 | |
| 139 | int TerminalSettingsSetLocalFlags(int Fd, tcflag_t LocalFlags); |
| 140 | |
| 141 | int TerminalSettingsSetOutputFlags(int Fd, tcflag_t OutputFlags); |
| 142 | |
| 143 | int WriteReadFdCommon(int WriteFd, size_t WriteSizes[], size_t NumWriteSizes, int ReadFd, size_t ReadSizes[], size_t NumReadSizes); |
| 144 | |
| 145 | #endif // _DEV_PT_COMMON |