2008-02-27 14:33:28 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* Mini getpty implementation for busybox
|
|
|
|
* Bjorn Wesen, Axis Communications AB (bjornw@axis.com)
|
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2008-02-27 14:33:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "libbb.h"
|
|
|
|
|
2008-03-05 22:01:45 +00:00
|
|
|
#define DEBUG 0
|
|
|
|
|
2008-06-27 02:52:20 +00:00
|
|
|
int FAST_FUNC xgetpty(char *line)
|
2008-02-27 14:33:28 +00:00
|
|
|
{
|
|
|
|
int p;
|
2008-08-17 12:47:19 +00:00
|
|
|
|
2008-02-27 14:33:28 +00:00
|
|
|
#if ENABLE_FEATURE_DEVPTS
|
|
|
|
p = open("/dev/ptmx", O_RDWR);
|
|
|
|
if (p > 0) {
|
2008-08-17 12:47:19 +00:00
|
|
|
grantpt(p); /* chmod+chown corresponding slave pty */
|
|
|
|
unlockpt(p); /* (what does this do?) */
|
2011-08-28 03:31:49 +00:00
|
|
|
# ifndef HAVE_PTSNAME_R
|
|
|
|
{
|
|
|
|
const char *name;
|
|
|
|
name = ptsname(p); /* find out the name of slave pty */
|
|
|
|
if (!name) {
|
|
|
|
bb_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
|
|
|
|
}
|
|
|
|
safe_strncpy(line, name, GETPTY_BUFSIZE);
|
2008-02-27 14:33:28 +00:00
|
|
|
}
|
2011-08-28 03:31:49 +00:00
|
|
|
# else
|
2008-08-17 12:47:19 +00:00
|
|
|
/* find out the name of slave pty */
|
|
|
|
if (ptsname_r(p, line, GETPTY_BUFSIZE-1) != 0) {
|
|
|
|
bb_perror_msg_and_die("ptsname error (is /dev/pts mounted?)");
|
|
|
|
}
|
|
|
|
line[GETPTY_BUFSIZE-1] = '\0';
|
2011-08-28 03:31:49 +00:00
|
|
|
# endif
|
2008-02-27 14:33:28 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
struct stat stb;
|
|
|
|
int i;
|
|
|
|
int j;
|
|
|
|
|
|
|
|
strcpy(line, "/dev/ptyXX");
|
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
line[8] = "pqrstuvwxyzabcde"[i];
|
|
|
|
line[9] = '0';
|
|
|
|
if (stat(line, &stb) < 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (j = 0; j < 16; j++) {
|
|
|
|
line[9] = j < 10 ? j + '0' : j - 10 + 'a';
|
|
|
|
if (DEBUG)
|
|
|
|
fprintf(stderr, "Trying to open device: %s\n", line);
|
|
|
|
p = open(line, O_RDWR | O_NOCTTY);
|
|
|
|
if (p >= 0) {
|
|
|
|
line[5] = 't';
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* FEATURE_DEVPTS */
|
2008-08-17 12:47:19 +00:00
|
|
|
bb_error_msg_and_die("can't find free pty");
|
2008-02-27 14:33:28 +00:00
|
|
|
}
|