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 | * |
| 6 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 7 | */ |
| 8 | |
| 9 | #include "libbb.h" |
| 10 | |
Paul Fox | b4a5087 | 2008-03-05 22:01:45 +0000 | [diff] [blame] | 11 | #define DEBUG 0 |
| 12 | |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 13 | int FAST_FUNC xgetpty(char *line) |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 14 | { |
| 15 | int p; |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 16 | |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 17 | #if ENABLE_FEATURE_DEVPTS |
| 18 | p = open("/dev/ptmx", O_RDWR); |
| 19 | if (p > 0) { |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 20 | grantpt(p); /* chmod+chown corresponding slave pty */ |
| 21 | unlockpt(p); /* (what does this do?) */ |
| 22 | #if 0 /* if ptsname_r is not available... */ |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 23 | const char *name; |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 24 | name = ptsname(p); /* find out the name of slave pty */ |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 25 | if (!name) { |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 26 | bb_perror_msg_and_die("ptsname error (is /dev/pts mounted?)"); |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 27 | } |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 28 | safe_strncpy(line, name, GETPTY_BUFSIZE); |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 29 | #else |
| 30 | /* find out the name of slave pty */ |
| 31 | if (ptsname_r(p, line, GETPTY_BUFSIZE-1) != 0) { |
| 32 | bb_perror_msg_and_die("ptsname error (is /dev/pts mounted?)"); |
| 33 | } |
| 34 | line[GETPTY_BUFSIZE-1] = '\0'; |
| 35 | #endif |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 36 | return p; |
| 37 | } |
| 38 | #else |
| 39 | struct stat stb; |
| 40 | int i; |
| 41 | int j; |
| 42 | |
| 43 | strcpy(line, "/dev/ptyXX"); |
| 44 | |
| 45 | for (i = 0; i < 16; i++) { |
| 46 | line[8] = "pqrstuvwxyzabcde"[i]; |
| 47 | line[9] = '0'; |
| 48 | if (stat(line, &stb) < 0) { |
| 49 | continue; |
| 50 | } |
| 51 | for (j = 0; j < 16; j++) { |
| 52 | line[9] = j < 10 ? j + '0' : j - 10 + 'a'; |
| 53 | if (DEBUG) |
| 54 | fprintf(stderr, "Trying to open device: %s\n", line); |
| 55 | p = open(line, O_RDWR | O_NOCTTY); |
| 56 | if (p >= 0) { |
| 57 | line[5] = 't'; |
| 58 | return p; |
| 59 | } |
| 60 | } |
| 61 | } |
| 62 | #endif /* FEATURE_DEVPTS */ |
Denis Vlasenko | eb08477 | 2008-08-17 12:47:19 +0000 | [diff] [blame] | 63 | bb_error_msg_and_die("can't find free pty"); |
Denis Vlasenko | c8f2f74 | 2008-02-27 14:33:28 +0000 | [diff] [blame] | 64 | } |