Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Mini getpty implementation for busybox |
| 4 | * Bjorn Wesen, Axis Communications AB (bjornw@axis.com) |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 7 | */ |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 8 | #include "libbb.h" |
| 9 | |
Paul Fox | b4a5087 | 2008-03-05 22:01:45 +0000 | [diff] [blame] | 10 | #define DEBUG 0 |
| 11 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 12 | int FAST_FUNC xgetpty(char *line) |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 13 | { |
| 14 | int p; |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 16 | #if ENABLE_FEATURE_DEVPTS |
| 17 | p = open("/dev/ptmx", O_RDWR); |
Denys Vlasenko | 438b4ac | 2014-03-16 12:34:53 +0100 | [diff] [blame] | 18 | if (p >= 0) { |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 19 | grantpt(p); /* chmod+chown corresponding slave pty */ |
| 20 | unlockpt(p); /* (what does this do?) */ |
Denys Vlasenko | 2491511 | 2011-08-28 05:31:49 +0200 | [diff] [blame] | 21 | # ifndef HAVE_PTSNAME_R |
| 22 | { |
| 23 | const char *name; |
| 24 | name = ptsname(p); /* find out the name of slave pty */ |
| 25 | if (!name) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 26 | bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)"); |
Denys Vlasenko | 2491511 | 2011-08-28 05:31:49 +0200 | [diff] [blame] | 27 | } |
| 28 | safe_strncpy(line, name, GETPTY_BUFSIZE); |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 29 | } |
Denys Vlasenko | 2491511 | 2011-08-28 05:31:49 +0200 | [diff] [blame] | 30 | # else |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 31 | /* find out the name of slave pty */ |
| 32 | if (ptsname_r(p, line, GETPTY_BUFSIZE-1) != 0) { |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 33 | bb_simple_perror_msg_and_die("ptsname error (is /dev/pts mounted?)"); |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 34 | } |
| 35 | line[GETPTY_BUFSIZE-1] = '\0'; |
Denys Vlasenko | 2491511 | 2011-08-28 05:31:49 +0200 | [diff] [blame] | 36 | # endif |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 37 | return p; |
| 38 | } |
| 39 | #else |
| 40 | struct stat stb; |
| 41 | int i; |
| 42 | int j; |
| 43 | |
| 44 | strcpy(line, "/dev/ptyXX"); |
| 45 | |
| 46 | for (i = 0; i < 16; i++) { |
| 47 | line[8] = "pqrstuvwxyzabcde"[i]; |
| 48 | line[9] = '0'; |
| 49 | if (stat(line, &stb) < 0) { |
| 50 | continue; |
| 51 | } |
| 52 | for (j = 0; j < 16; j++) { |
| 53 | line[9] = j < 10 ? j + '0' : j - 10 + 'a'; |
| 54 | if (DEBUG) |
| 55 | fprintf(stderr, "Trying to open device: %s\n", line); |
| 56 | p = open(line, O_RDWR | O_NOCTTY); |
| 57 | if (p >= 0) { |
| 58 | line[5] = 't'; |
| 59 | return p; |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | #endif /* FEATURE_DEVPTS */ |
James Byrne | 6937487 | 2019-07-02 11:35:03 +0200 | [diff] [blame] | 64 | bb_simple_error_msg_and_die("can't find free pty"); |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 65 | } |