annotate lib/pty.c @ 249:094e83b55cb1

2003-8-7 Brian Masney <masneyb@gftp.org> * lib/cache.c lib/misc.c lib/protocols.c lib/pty.c - make sure a NUL byte appears at the end of the buffer after the call to strncpy * lib/rfc959.c - increased buffer size to directory parsing routine
author masneyb
date Fri, 08 Aug 2003 02:24:54 +0000
parents 1638e32e6a35
children 656a0b3d1403
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
1 /*****************************************************************************/
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
2 /* pty.c - general purpose routines */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
3 /* Copyright (C) 1998-2003 Brian Masney <masneyb@gftp.org> */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
4 /* */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
5 /* This program is free software; you can redistribute it and/or modify */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
6 /* it under the terms of the GNU General Public License as published by */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
7 /* the Free Software Foundation; either version 2 of the License, or */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
8 /* (at your option) any later version. */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
9 /* */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
10 /* This program is distributed in the hope that it will be useful, */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
13 /* GNU General Public License for more details. */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
14 /* */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
15 /* You should have received a copy of the GNU General Public License */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
16 /* along with this program; if not, write to the Free Software */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
17 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111 USA */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
18 /*****************************************************************************/
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
19
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
20 static const char cvsid[] = "$Id$";
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
21
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
22 #include "gftp.h"
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
23
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
24 #ifdef __sgi
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
25
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
26 char *
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
27 gftp_get_pty_impl (void)
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
28 {
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
29 return ("sgi");
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
30 }
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
31
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
32
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
33 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
34 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
35 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
36 char *new_pts_name;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
37 int fdm;
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
38
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
39 if ((new_pts_name = _getpty (&fdm, O_RDWR, 0600, 0)) == NULL)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
40 return (GFTP_ERETRYABLE);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
41
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
42 strncpy (pts_name, new_pts_name, len);
249
094e83b55cb1 2003-8-7 Brian Masney <masneyb@gftp.org>
masneyb
parents: 235
diff changeset
43 pts_name[len - 1] = '\0';
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
44
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
45 return (fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
46 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
47
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
48
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
49 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
50 _gftp_ptys_open (int fdm, int fds, char *pts_name)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
51 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
52 int new_fds;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
53
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
54 if ((new_fds = open (pts_name, O_RDWR)) < 0)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
55 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
56 close (fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
57 return (-1);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
58 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
59
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
60 return (new_fds);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
61 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
62
232
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
63 #elif HAVE_OPENPTY
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
64
235
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
65 #ifdef HAVE_PTY_H
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
66 #include <pty.h>
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
67 #include <utmp.h> /* for login_tty */
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
68 #elif HAVE_LIBUTIL_H
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
69 #include <libutil.h>
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
70 #include <utmp.h> /* for login_tty */
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
71 #else
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
72 extern int openpty(int *amaster, int *aslave, char *name, struct termios *termp, struct winsize * winp);
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
73 extern int login_tty(int fd);
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
74 #endif
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
75
232
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
76 char *
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
77 gftp_get_pty_impl (void)
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
78 {
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
79 return ("openpty");
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
80 }
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
81
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
82
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
83 static int
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
84 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
85 {
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
86 int fdm;
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
87
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
88 if (openpty (&fdm, fds, pts_name, NULL, NULL) < 0)
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
89 return (GFTP_ERETRYABLE);
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
90
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
91 ioctl (*fds, TIOCSCTTY, NULL);
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
92
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
93 return (fdm);
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
94 }
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
95
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
96
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
97 static int
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
98 _gftp_ptys_open (int fdm, int fds, char *pts_name)
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
99 {
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
100 if (login_tty (fds) < 0)
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
101 return (GFTP_EFATAL);
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
102
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
103 return (fds);
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
104 }
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
105
178
8beb7bfca92b 2003-6-11 Brian Masney <masneyb@gftp.org>
masneyb
parents: 146
diff changeset
106 #elif HAVE_GRANTPT
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
107
235
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
108 #include <stropts.h>
1638e32e6a35 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 232
diff changeset
109
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
110 char *
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
111 gftp_get_pty_impl (void)
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
112 {
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
113 return ("unix98");
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
114 }
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
115
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
116
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
117 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
118 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
119 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
120 char *new_pts_name;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
121 int fdm;
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
122
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
123 if ((fdm = open ("/dev/ptmx", O_RDWR)) < 0)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
124 return (GFTP_ERETRYABLE);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
125
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
126 if (grantpt (fdm) < 0)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
127 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
128 close (fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
129 return (GFTP_ERETRYABLE);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
130 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
131
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
132 if (unlockpt (fdm) < 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
133 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
134 close (fdm);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
135 return (GFTP_ERETRYABLE);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
136 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
137
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
138 if ((new_pts_name = ptsname (fdm)) == NULL)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
139 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
140 close (fdm);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
141 return (GFTP_ERETRYABLE);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
142 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
143
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
144 strncpy (pts_name, new_pts_name, len);
249
094e83b55cb1 2003-8-7 Brian Masney <masneyb@gftp.org>
masneyb
parents: 235
diff changeset
145 pts_name[len - 1] = '\0';
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
146
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
147 return (fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
148 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
149
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
150
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
151 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
152 _gftp_ptys_open (int fdm, int fds, char *pts_name)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
153 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
154 int new_fds;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
155
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
156 if ((new_fds = open (pts_name, O_RDWR)) < 0)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
157 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
158 close (fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
159 return (-1);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
160 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
161
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
162 #ifdef SYSV
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
163 /* I intentionally ignore these errors */
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
164 ioctl (new_fds, I_PUSH, "ptem");
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
165 ioctl (new_fds, I_PUSH, "ldterm");
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
166 ioctl (new_fds, I_PUSH, "ttcompat");
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
167 #endif
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
168
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
169 return (new_fds);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
170 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
171
232
b8d14c2c3097 2003-7-25 Brian Masney <masneyb@gftp.org>
masneyb
parents: 210
diff changeset
172 #else /* !HAVE_GRANTPT */
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
173
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
174 /* Fall back to *BSD... */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
175
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
176 char *
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
177 gftp_get_pty_impl (void)
179
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
178 {
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
179 return ("bsd");
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
180 }
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
181
8d933999bba6 2003-6-14 Brian Masney <masneyb@gftp.org>
masneyb
parents: 178
diff changeset
182
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
183 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
184 _gftp_ptym_open (char *pts_name, size_t len, int *fds)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
185 {
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
186 char *pos1, *pos2;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
187 int fdm;
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
188
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
189 g_return_val_if_fail (len >= 10, GFTP_EFATAL);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
190
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
191 strncpy (pts_name, "/dev/ptyXY", len);
249
094e83b55cb1 2003-8-7 Brian Masney <masneyb@gftp.org>
masneyb
parents: 235
diff changeset
192 pts_name[len - 1] = '\0';
094e83b55cb1 2003-8-7 Brian Masney <masneyb@gftp.org>
masneyb
parents: 235
diff changeset
193
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
194 for (pos1 = "pqrstuvwxyzPQRST"; *pos1 != '\0'; pos1++)
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
195 {
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
196 pts_name[8] = *pos1;
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
197 for (pos2 = "0123456789abcdef"; *pos2 != '\0'; pos2++)
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
198 {
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
199 pts_name[9] = *pos2;
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
200 if ((fdm = open (pts_name, O_RDWR)) < 0)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
201 continue;
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
202
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
203 pts_name[5] = 't';
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
204 chmod (pts_name, S_IRUSR | S_IWUSR);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
205 chown (pts_name, getuid (), -1);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
206
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
207 return (fdm);
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
208 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
209 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
210
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
211 return (GFTP_ERETRYABLE);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
212 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
213
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
214
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
215 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
216 _gftp_ptys_open (int fdm, int fds, char *pts_name)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
217 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
218 int new_fds;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
219
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
220 if ((new_fds = open (pts_name, O_RDWR)) < 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
221 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
222 close (fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
223 return (-1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
224 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
225
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
226 #if defined(TIOCSCTTY) && !defined(CIBAUD)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
227 ioctl (new_fds, TIOCSCTTY, NULL);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
228 #endif
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
229
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
230 return (new_fds);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
231 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
232
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
233 #endif /* __sgi */
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
234
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
235
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
236 static int
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
237 _gftp_tty_raw (int fd)
146
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
238 {
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
239 struct termios buf;
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
240
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
241 if (tcgetattr (fd, &buf) < 0)
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
242 return (-1);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
243
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
244 buf.c_iflag |= IGNPAR;
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
245 buf.c_iflag &= ~(ICRNL | ISTRIP | IXON | IGNCR | IXANY | IXOFF | INLCR);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
246 buf.c_lflag &= ~(ECHO | ICANON | ISIG | ECHOE | ECHOK | ECHONL);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
247 #ifdef IEXTEN
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
248 buf.c_lflag &= ~(IEXTEN);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
249 #endif
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
250
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
251 buf.c_oflag &= ~(OPOST);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
252 buf.c_cc[VMIN] = 1;
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
253 buf.c_cc[VTIME] = 0;
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
254
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
255 if (tcsetattr (fd, TCSADRAIN, &buf) < 0)
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
256 return (-1);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
257 return (0);
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
258 }
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
259
782f84694489 2003-4-25 Brian Masney <masneyb@gftp.org>
masneyb
parents:
diff changeset
260
210
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
261 static void
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
262 _gftp_close_all_fds (void)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
263 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
264 int i, maxfds;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
265
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
266 #ifdef HAVE_GETDTABLESIZE
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
267 maxfds = getdtablesize () - 1;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
268 #elif defined (OPEN_MAX)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
269 maxfds = OPEN_MAX;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
270 #else
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
271 maxfds = -1;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
272 #endif
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
273
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
274 for (i=3; i<maxfds; i++)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
275 close (i);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
276 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
277
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
278
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
279 pid_t
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
280 gftp_exec_without_new_pty (gftp_request * request, int *fdm, char **args)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
281 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
282 pid_t child;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
283 int s[2];
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
284
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
285 if (socketpair (AF_LOCAL, SOCK_STREAM, 0, s) < 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
286 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
287 request->logging_function (gftp_logging_error, request,
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
288 _("Cannot create a socket pair: %s\n"),
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
289 g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
290 return (-1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
291 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
292
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
293 if ((child = fork ()) == 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
294 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
295 setsid ();
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
296
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
297 close (s[0]);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
298
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
299 _gftp_tty_raw (s[1]);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
300 dup2 (s[1], 0);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
301 dup2 (s[1], 1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
302 dup2 (s[1], 2);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
303 _gftp_close_all_fds ();
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
304
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
305 execvp (args[0], args);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
306
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
307 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
308 exit (1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
309 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
310 else if (child > 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
311 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
312 close (s[1]);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
313 _gftp_tty_raw (s[0]);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
314 *fdm = s[0];
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
315 return (child);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
316 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
317 else
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
318 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
319 request->logging_function (gftp_logging_error, request->user_data,
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
320 _("Cannot fork another process: %s\n"),
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
321 g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
322 return (-1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
323 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
324 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
325
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
326
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
327 pid_t
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
328 gftp_exec_with_new_pty (gftp_request * request, int *fdm, char **args)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
329 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
330 char pts_name[64];
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
331 pid_t child;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
332 int fds;
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
333
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
334 *pts_name = '\0';
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
335 if ((*fdm = _gftp_ptym_open (pts_name, sizeof (pts_name), &fds)) < 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
336 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
337 request->logging_function (gftp_logging_error, request->user_data,
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
338 _("Cannot open master pty %s: %s\n"), pts_name,
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
339 g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
340 return (-1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
341 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
342
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
343 if ((child = fork ()) == 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
344 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
345 setsid ();
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
346
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
347 if ((fds = _gftp_ptys_open (*fdm, fds, pts_name)) < 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
348 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
349 printf ("Cannot open slave pts %s: %s\n", pts_name,
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
350 g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
351 return (-1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
352 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
353
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
354 close (*fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
355
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
356 _gftp_tty_raw (fds);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
357 dup2 (fds, 0);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
358 dup2 (fds, 1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
359 dup2 (fds, 2);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
360 _gftp_close_all_fds ();
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
361
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
362 execvp (args[0], args);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
363
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
364 printf (_("Error: Cannot execute ssh: %s\n"), g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
365 exit (1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
366 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
367 else if (child > 0)
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
368 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
369 _gftp_tty_raw (*fdm);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
370 return (child);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
371 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
372 else
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
373 {
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
374 request->logging_function (gftp_logging_error, request->user_data,
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
375 _("Cannot fork another process: %s\n"),
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
376 g_strerror (errno));
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
377 return (-1);
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
378 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
379 }
82ebd1b05345 2003-7-6 Brian Masney <masneyb@gftp.org>
masneyb
parents: 179
diff changeset
380