Don't forget to set $TERM when we spawn the pty. Of course I haven't
[sgt/putty] / unix / pty.c
1 #define _XOPEN_SOURCE
2 #define _XOPEN_SOURCE_EXTENDED
3 #include <features.h>
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <termios.h>
11 #include <sys/ioctl.h>
12
13 #include "putty.h"
14
15 #ifndef FALSE
16 #define FALSE 0
17 #endif
18 #ifndef TRUE
19 #define TRUE 1
20 #endif
21
22 int pty_master_fd;
23 char **pty_argv;
24
25 static void pty_size(void);
26
27 /*
28 * Called to set up the pty.
29 *
30 * Returns an error message, or NULL on success.
31 *
32 * Also places the canonical host name into `realhost'. It must be
33 * freed by the caller.
34 */
35 static char *pty_init(char *host, int port, char **realhost, int nodelay)
36 {
37 int slavefd;
38 char name[FILENAME_MAX];
39 pid_t pid;
40
41 pty_master_fd = open("/dev/ptmx", O_RDWR);
42
43 if (pty_master_fd < 0) {
44 perror("/dev/ptmx: open");
45 exit(1);
46 }
47
48 if (grantpt(pty_master_fd) < 0) {
49 perror("grantpt");
50 exit(1);
51 }
52
53 if (unlockpt(pty_master_fd) < 0) {
54 perror("unlockpt");
55 exit(1);
56 }
57
58 name[FILENAME_MAX-1] = '\0';
59 strncpy(name, ptsname(pty_master_fd), FILENAME_MAX-1);
60
61 slavefd = open(name, O_RDWR);
62 if (slavefd < 0) {
63 perror("slave pty: open");
64 exit(1);
65 }
66
67 /*
68 * Fork and execute the command.
69 */
70 pid = fork();
71 if (pid < 0) {
72 perror("fork");
73 exit(1);
74 }
75
76 if (pid == 0) {
77 int i;
78 /*
79 * We are the child.
80 */
81 close(pty_master_fd);
82 close(0);
83 close(1);
84 close(2);
85 fcntl(slavefd, F_SETFD, 0); /* don't close on exec */
86 dup2(slavefd, 0);
87 dup2(slavefd, 1);
88 dup2(slavefd, 2);
89 setsid();
90 setpgrp();
91 tcsetpgrp(slavefd, getpgrp());
92 /* Close everything _else_, for tidiness. */
93 for (i = 3; i < 1024; i++)
94 close(i);
95 {
96 char term_env_var[10 + sizeof(cfg.termtype)];
97 sprintf(term_env_var, "TERM=%s", cfg.termtype);
98 putenv(term_env_var);
99 }
100 if (pty_argv)
101 execvp(pty_argv[0], pty_argv);
102 else
103 execl(getenv("SHELL"), getenv("SHELL"), NULL);
104 /*
105 * If we're here, exec has gone badly foom.
106 */
107 perror("exec");
108 exit(127);
109 } else {
110 close(slavefd);
111 }
112
113 return NULL;
114 }
115
116 /*
117 * Called to send data down the pty.
118 */
119 static int pty_send(char *buf, int len)
120 {
121 while (len > 0) {
122 int ret = write(pty_master_fd, buf, len);
123 if (ret < 0) {
124 perror("write pty master");
125 exit(1);
126 }
127 buf += ret;
128 len -= ret;
129 }
130 return 0;
131 }
132
133 /*
134 * Called to query the current socket sendability status.
135 */
136 static int pty_sendbuffer(void)
137 {
138 return 0;
139 }
140
141 /*
142 * Called to set the size of the window
143 */
144 static void pty_size(void)
145 {
146 struct winsize size;
147
148 size.ws_row = (unsigned short)rows;
149 size.ws_col = (unsigned short)cols;
150 ioctl(pty_master_fd, TIOCSWINSZ, (void *)&size);
151 return;
152 }
153
154 /*
155 * Send special codes.
156 */
157 static void pty_special(Telnet_Special code)
158 {
159 /* Do nothing! */
160 return;
161 }
162
163 static Socket pty_socket(void)
164 {
165 return NULL; /* shouldn't ever be needed */
166 }
167
168 static int pty_sendok(void)
169 {
170 return 1;
171 }
172
173 static void pty_unthrottle(int backlog)
174 {
175 /* do nothing */
176 }
177
178 static int pty_ldisc(int option)
179 {
180 return 0; /* neither editing nor echoing */
181 }
182
183 static int pty_exitcode(void)
184 {
185 /* Shouldn't ever be required */
186 return 0;
187 }
188
189 Backend pty_backend = {
190 pty_init,
191 pty_send,
192 pty_sendbuffer,
193 pty_size,
194 pty_special,
195 pty_socket,
196 pty_exitcode,
197 pty_sendok,
198 pty_ldisc,
199 pty_unthrottle,
200 1
201 };