-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
pseudo.c
1306 lines (1018 loc) · 31.1 KB
/
pseudo.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* libslack - http://libslack.org/
*
* Copyright (C) 1999-2004 raf <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
* or visit http://www.gnu.org/copyleft/gpl.html
*
* 20040102 raf <[email protected]>
*/
/*
* Author: Tatu Ylonen <[email protected]>
* Copyright (c) 1995 Tatu Ylonen <[email protected]>, Espoo, Finland
* All rights reserved
* Allocating a pseudo-terminal, and making it the controlling tty.
*
* As far as I am concerned, the code I have written for this software
* can be used freely for any purpose. Any derived versions of this
* software must be clearly marked as such, and if the derived work is
* incompatible with the protocol description in the RFC file, it must be
* called by a name other than "ssh" or "Secure Shell".
*/
/*
* 20010930 raf <[email protected]>
* Librarified the code (no calls to fatal(), return errors instead)
* Made slave device name truncation an error (rather than ignoring it)
* Made it thread safe on some systems (use ttyname_r/ptsname_r)
* Added a manpage (perldoc -F pseudo.c)
* Made pty_allocate() more like openpty() but safe (called it pty_open())
* Added safe version of forkpty() (called it pty_fork())
*/
/*
=head1 NAME
I<libslack(pseudo)> - pseudo terminal module
=head1 SYNOPSIS
#include <slack/std.h>
#include <slack/pseudo.h>
int pty_open(int *masterfd, int *slavefd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize);
int pty_release(const char *slavename);
int pty_set_owner(const char *slavename, uid_t uid);
int pty_make_controlling_tty(int *slavefd, const char *slavename);
int pty_change_window_size(int masterfd, int row, int col, int xpixel, int ypixel);
pid_t pty_fork(int *masterfd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize);
=head1 DESCRIPTION
This module provides functions for opening pseudo terminals, changing their
ownership, making them the controlling terminal, changing their window size
and forking a new process whose standard input, output and error and
attached to a pseudo terminal which is made the controlling terminal.
=over 4
=cut
*/
#define _GNU_SOURCE /* ptsname_r() under Linux */
#include "config.h"
#include <signal.h>
/** Contents of std.h **/
#ifndef NO_STDC
#ifndef __STDC__
#define __STDC__
#endif
#endif
#ifndef NO_STRICT_ANSI
#ifndef __STRICT_ANSI__
#define __STRICT_ANSI__
#endif
#endif
#ifndef NO_POSIX_SOURCE
#ifndef _POSIX_SOURCE
#define _POSIX_SOURCE
#endif
#endif
#ifndef NO_POSIX_C_SOURCE
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199506L
#endif
#endif
#ifndef NO_XOPEN_SOURCE
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 500
#endif
#endif
#ifndef NO_ISOC9X_SOURCE
#ifndef _ISOC9X_SOURCE
#define _ISOC9X_SOURCE
#endif
#endif
#ifndef NO_EXTENSIONS
#ifndef __EXTENSIONS__
#define __EXTENSIONS__
#endif
#endif
#ifndef NO_REENTRANT
#ifndef _REENTRANT
#define _REENTRANT
#endif
#endif
#ifndef NO_THREAD_SAFE
#ifndef _THREAD_SAFE
#define _THREAD_SAFE
#endif
#endif
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <math.h>
#include <setjmp.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef HAVE_STDINT_H
#include <stdint.h>
#endif
#include <unistd.h>
#include <stdarg.h>
#include <signal.h>
#include <fcntl.h>
#include <grp.h>
#include <pwd.h>
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#ifdef HAVE_UTIL_H
#include <util.h>
#endif
#ifdef HAVE_LIBUTIL_H
#include <libutil.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include "pseudo.h"
#ifndef TEST
/* That's the way it is detected in configure, but how that original code handles it */
#ifdef HAVE__DEV_PTNX
#define HAVE_DEV_PTMX
#endif
#if defined(HAVE__DEV_PTS) && defined(HAVE__DEV_PTC)
# define HAVE_DEV_PTS_AND_PTC
#endif
/* Pty allocated with _getpty gets broken if we do I_PUSH:es to it */
#if defined(HAVE__GETPTY) || defined(HAVE_OPENPTY)
#undef HAVE_DEV_PTMX
#endif
#ifdef HAVE_PTY_H
#include <pty.h>
#endif
#if defined(HAVE_DEV_PTMX) && defined(HAVE_SYS_STROPTS_H)
#include <sys/stropts.h>
#endif
#if defined(HAVE_VHANGUP) && !defined(HAVE_DEV_PTMX)
#define USE_VHANGUP
#endif
#ifndef O_NOCTTY
#define O_NOCTTY 0
#endif
#ifndef PATH_TTY
#define PATH_TTY "/dev/tty"
#endif
#ifndef HAVE_STRLCPY
static size_t strlcpy(char *dst, const char *src, size_t size)
{
const char *s = src;
char *d = dst;
size_t n = size;
if (n)
while (--n && (*d++ = *s++))
{}
if (n == 0) {
if (size)
*d = '\0';
while (*s++)
{}
}
return s - src - 1;
}
#endif
#define set_errno(errnum) (errno = (errnum), -1)
/*
=item C<int pty_open(int *masterfd, int *slavefd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize)>
A safe version of I<openpty(3)>. Allocates and opens a pseudo terminal. The
new descriptor for the master side of the pseudo terminal is stored in
C<*masterfd>. The new descriptor for the slave side of the pseudo terminal
is stored in C<*slavefd>. The device name of the slave side of the pseudo
terminal is stored in the buffer pointed to by C<slavename> which must be
able to hold at least 64 characters. C<slavenamesize> is the size of the
buffer pointed to by C<slavename>. No more than C<slavenamesize> bytes will
be written into the buffer pointed to by C<slavename>, including the
terminating C<nul> byte. If C<slave_termios> is not null, it is passed to
I<tcsetattr(3)> with the command C<TCSANOW> to set the terminal attributes
of the slave device. If C<slave_winsize> is not null, it is passed to
I<ioctl(2)> with the command C<TIOCSWINSZ> to set the window size of the
slave device. On success, returns C<0>. On error, returns C<-1> with
C<errno> set appropriately.
=cut
*/
static int groupname2gid(const char *groupname)
{
FILE *group = fopen("/etc/group", "r");
char line[BUFSIZ], *gid;
int ret = -1;
while (fgets(line, BUFSIZ, group))
{
if (!strncmp(line, "tty:", 4))
{
if ((gid = strchr(line + 4, ':')))
ret = atoi(gid + 1);
break;
}
}
fclose(group);
return ret;
}
static int uid2gid(uid_t uid)
{
FILE *passwd = fopen("/etc/passwd", "r");
char line[BUFSIZ], *ptr;
int ret = -1;
while (fgets(line, BUFSIZ, passwd))
{
if ((ptr = strchr(line, ':')) && (ptr = strchr(ptr + 1, ':')) &&
atoi(ptr + 1) == (int)uid && (ptr = strchr(ptr + 1, ':')))
{
ret = atoi(ptr + 1);
break;
}
}
fclose(passwd);
return ret;
}
int pty_open(int *masterfd, int *slavefd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize)
{
#if defined(HAVE_OPENPTY) || defined(BSD4_4)
/* openpty(3) exists in OSF/1 and some other os'es */
#ifdef HAVE_TTYNAME_R
char buf[64], *name = buf;
int err;
#else
char *name;
#endif
if (!masterfd || !slavefd || !slavename || slavenamesize < 64)
return set_errno(EINVAL);
/* Open the master and slave descriptors, set ownership and permissions */
if (openpty(masterfd, slavefd, NULL, NULL, NULL) == -1) {
perror("openpty");
return -1;
}
/* Retrieve the device name of the slave */
#ifdef HAVE_TTYNAME_R
if ((err = ttyname_r(*slavefd, buf, 64)))
{
close(*masterfd);
close(*slavefd);
perror("ttyname_r");
return set_errno(err);
}
#else
if (!(name = ttyname(*slavefd)))
{
close(*masterfd);
close(*slavefd);
perror("ttyname");
return set_errno(ENOTTY);
}
#endif
/* Return it to the caller */
if (strlcpy(slavename, name, slavenamesize) >= slavenamesize)
{
close(*masterfd);
close(*slavefd);
return set_errno(ENOSPC);
}
#ifdef DEBUG
fprintf(stderr, "Slave terminal is %s\n", slavename);
#endif
#else /* HAVE_OPENPTY */
#ifdef HAVE__GETPTY
/*
* _getpty(3) exists in SGI Irix 4.x, 5.x & 6.x -- it generates more
* pty's automagically when needed
*/
char *slave;
if (!masterfd || !slavefd || !slavename || slavenamesize < 64)
return set_errno(EINVAL);
/* Open the master descriptor and get the slave's device name */
if (!(slave = _getpty(masterfd, O_RDWR, 0622, 0))) {
perror("_getpty");
return -1;
}
/* Return it to the caller */
if (strlcpy(slavename, slave, slavenamesize) >= slavenamesize)
{
close(*masterfd);
return set_errno(ENOSPC);
}
/* Open the slave descriptor */
if ((*slavefd = open(slavename, O_RDWR | O_NOCTTY)) == -1)
{
perror("open(slave)");
close(*masterfd);
return -1;
}
#else /* HAVE__GETPTY */
#if defined(HAVE_DEV_PTMX)
/*
* This code is used e.g. on Solaris 2.x. (Note that Solaris 2.3
* also has bsd-style ptys, but they simply do not work.)
*/
#ifdef HAVE_PTSNAME_R
char buf[64], *name = buf;
int err;
#else
char *name;
#endif
if (!masterfd || !slavefd || !slavename || slavenamesize < 64)
return set_errno(EINVAL);
/* Open the master descriptor */
if ((*masterfd = open("/dev/ptmx", O_RDWR | O_NOCTTY)) == -1) {
perror("open(/dev/ptmx)");
return -1;
}
/* Set slave ownership and permissions to real uid of process */
if (grantpt(*masterfd) == -1)
{
perror("grantpt");
close(*masterfd);
return -1;
}
/* Unlock the slave so it can be opened */
if (unlockpt(*masterfd) == -1)
{
perror("unlockpt");
close(*masterfd);
return -1;
}
/* Retrieve the device name of the slave */
#ifdef HAVE_PTSNAME_R
if ((err = ptsname_r(*masterfd, buf, 64)))
{
perror("ptsname_r");
close(*masterfd);
return set_errno(err);
}
#else
if (!(name = ptsname(*masterfd)))
{
perror("ptsname");
close(*masterfd);
return set_errno(ENOTTY);
}
#endif
/* Return it to the caller */
if (strlcpy(slavename, name, slavenamesize) >= slavenamesize)
{
close(*masterfd);
return set_errno(ENOSPC);
}
/* Open the slave descriptor */
if ((*slavefd = open(slavename, O_RDWR | O_NOCTTY)) == -1)
{
perror("open(slave)");
close(*masterfd);
return -1;
}
/* Turn the slave into a terminal */
#ifndef HAVE_CYGWIN
/*
* Push the appropriate streams modules, as described in Solaris pts(7).
* HP-UX pts(7) doesn't have ttcompat module.
*/
if (ioctl(*slavefd, I_PUSH, "ptem") == -1)
{
perror("ioctl(I_PUSH,ptem)");
close(*masterfd);
close(*slavefd);
return -1;
}
if (ioctl(*slavefd, I_PUSH, "ldterm") == -1)
{
perror("ioctl(I_PUSH,ldterm)");
close(*masterfd);
close(*slavefd);
return -1;
}
#ifndef __hpux
if (ioctl(*slavefd, I_PUSH, "ttcompat") == -1)
{
perror("ioctl(I_PUSH,ttcompat)");
close(*masterfd);
close(*slavefd);
return -1;
}
#endif
#endif
#else /* HAVE_DEV_PTMX */
#ifdef HAVE_DEV_PTS_AND_PTC
/* AIX-style pty code */
#ifdef HAVE_TTYNAME_R
char buf[64], *name = buf;
int err;
#else
char *name;
#endif
if (!masterfd || !slavefd || !slavename || slavenamesize < 64)
return set_errno(EINVAL);
/* Open the master descriptor */
if ((*masterfd = open("/dev/ptc", O_RDWR | O_NOCTTY)) == -1) {
perror("open(/dev/ptc)");
return -1;
}
/* Retrieve the device name of the slave */
#ifdef HAVE_TTYNAME_R
if ((err = ttyname_r(*masterfd, buf, 64)))
{
perror("ttyname_r");
close(*masterfd);
return set_errno(err);
}
#else
if (!(name = ttyname(*masterfd)))
{
perror("ttyname");
close(*masterfd);
return set_errno(ENOTTY);
}
#endif
/* Return it to the caller */
if (strlcpy(slavename, name, slavenamesize) >= slavenamesize)
{
close(*masterfd);
return set_errno(ENOSPC);
}
/* Open the slave descriptor */
if ((*slavefd = open(name, O_RDWR | O_NOCTTY)) == -1)
{
perror("open(slave)");
close(*masterfd);
return -1;
}
#else /* HAVE_DEV_PTS_AND_PTC */
/* BSD-style pty code */
const char * const ptymajors = "pqrstuvwxyzabcdefghijklmnoABCDEFGHIJKLMNOPQRSTUVWXYZ";
const char * const ptyminors = "0123456789abcdef";
int num_minors = strlen(ptyminors);
int num_ptys = strlen(ptymajors) * num_minors;
char buf[64];
int found = 0;
int i;
/* Identify the first available pty master device */
for (i = 0; !found && i < num_ptys; i++)
{
snprintf(buf, sizeof buf, "/dev/pty%c%c", ptymajors[i / num_minors], ptyminors[i % num_minors]);
snprintf(slavename, slavenamesize, "/dev/tty%c%c", ptymajors[i / num_minors], ptyminors[i % num_minors]);
/* Open the master descriptor */
if ((*masterfd = open(buf, O_RDWR | O_NOCTTY)) == -1)
{
/* Try SCO style naming */
snprintf(buf, sizeof buf, "/dev/ptyp%d", i);
snprintf(slavename, slavenamesize, "/dev/ttyp%d", i);
if ((*masterfd = open(buf, O_RDWR | O_NOCTTY)) == -1)
continue;
}
/* Set slave ownership and permissions to real uid of process */
pty_set_owner(slavename, getuid());
/* Open the slave descriptor */
if ((*slavefd = open(slavename, O_RDWR | O_NOCTTY)) == -1)
{
perror("open(slave)");
close(*masterfd);
return -1;
}
found = 1;
}
if (!found)
return set_errno(ENOENT);
#endif /* HAVE_DEV_PTS_AND_PTC */
#endif /* HAVE_DEV_PTMX */
#endif /* HAVE__GETPTY */
#endif /* HAVE_OPENPTY */
/* Set the slave's terminal attributes if requested */
if (slave_termios && tcsetattr(*slavefd, TCSANOW, slave_termios) == -1)
{
perror("tcsetattr");
close(*masterfd);
close(*slavefd);
return -1;
}
/* Set the slave's window size if required */
if (slave_winsize && ioctl(*slavefd, TIOCSWINSZ, slave_winsize) == -1)
{
perror("ioctl(TIOCSWINSZ)");
close(*masterfd);
close(*slavefd);
return -1;
}
return 0;
}
/*
=item C<int pty_release(const char *slavename)>
Releases the slave tty device whose name is in C<slavename>. Its ownership
is returned to root, and its permissions set to C<rw-rw-rw->. Note that only
root can execute this function successfully on most systems. On success,
returns C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int pty_release(const char *slavename)
{
if (!slavename)
return set_errno(EINVAL);
if (chown(slavename, (uid_t)0, (gid_t)0) == -1)
return -1;
if (chmod(slavename, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH) == -1)
return -1;
return 0;
}
/*
=item C<int pty_set_owner(const char *slavename, uid_t uid)>
Changes the ownership of the slave pty device referred to by C<slavename> to
the user id, C<uid>. Group ownership of the slave pty device will be changed
to the C<tty> group if it exists. Otherwise, it will be changed to the given
user's primary group. The slave pty device's permissions are set to
C<rw--w---->. Note that only root can execute this function successfully on
most systems. Also note that the ownership of the device is automatically
set to the real uid of the process by I<pty_open(3)> and I<pty_fork(3)>. The
permissions are also set automatically by these functions. So
I<pty_set_owner(3)> is only needed when the device needs to be owned by some
user other than the real user. On success, returns C<0>. On error, returns
C<-1> with C<errno> set appropriately.
=cut
*/
int pty_set_owner(const char *slavename, uid_t uid)
{
mode_t mode = S_IRUSR | S_IWUSR | S_IWGRP;
struct stat status[1];
int gid;
if (stat(slavename, status) == -1)
return -1;
if ((gid = groupname2gid("tty")) == -1)
{
gid = uid2gid(uid);
mode |= S_IWOTH;
}
if (status->st_uid != uid || status->st_gid != gid)
if (chown(slavename, uid, gid) == -1)
if (errno != EROFS || status->st_uid != uid)
return -1;
if ((status->st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) != mode)
if (chmod(slavename, mode) == -1)
if (errno != EROFS || (status->st_mode & (S_IRGRP | S_IROTH)))
return -1;
return 0;
}
/*
=item C<int pty_make_controlling_tty(int *slavefd, const char *slavename)>
Makes the slave pty the controlling terminal. C<*slavefd> contains the
descriptor for the slave side of a pseudo terminal. The descriptor of the
resulting controlling terminal will be stored in C<*slavefd>. C<slavename>
is the device name of the slave side of the pseudo terminal. On success,
returns C<0>. On error, returns C<-1> with C<errno> set appropriately.
=cut
*/
int pty_make_controlling_tty(int *slavefd, const char *slavename)
{
int fd;
#ifdef USE_VHANGUP
void (*old)(int);
#endif /* USE_VHANGUP */
if (!slavefd || *slavefd < 0 || !slavename)
return set_errno(EINVAL);
/* First disconnect from the old controlling tty */
#ifdef TIOCNOTTY
if ((fd = open(PATH_TTY, O_RDWR | O_NOCTTY)) >= 0)
{
ioctl(fd, TIOCNOTTY, NULL);
close(fd);
}
#endif /* TIOCNOTTY */
/* Privileged operation on AIX ? */
setsid();
/*
* Verify that we are successfully disconnected from the controlling
* tty.
*/
#if 0
if ((fd = open(PATH_TTY, O_RDWR | O_NOCTTY)) >= 0)
{
close(fd);
return set_errno(ENXIO);
}
#endif
/* Make it our controlling tty */
#ifdef TIOCSCTTY
if (ioctl(*slavefd, TIOCSCTTY, NULL) == -1) {
perror("ioctl(TIOCSCTTY)");
return -1;
}
#endif /* TIOCSCTTY */
#ifdef HAVE_NEWS4
setpgrp(0, 0);
#endif /* HAVE_NEWS4 */
#ifdef USE_VHANGUP
old = signal(SIGHUP, SIG_IGN);
vhangup();
signal(SIGHUP, old);
#endif /* USE_VHANGUP */
/* Why do this? */
if ((fd = open(slavename, O_RDWR)) >= 0)
{
#ifdef USE_VHANGUP
close(*slavefd);
*slavefd = fd;
#else /* USE_VHANGUP */
close(fd);
#endif /* USE_VHANGUP */
}
/* Verify that we now have a controlling tty */
if ((fd = open(PATH_TTY, O_RDWR)) == -1) {
perror("open(PATH_TTY)");
return -1;
}
close(fd);
return 0;
}
/*
=item C<int pty_change_window_size(int masterfd, int row, int col, int xpixel, int ypixel)>
Changes the window size associated with the pseudo terminal referred to by
C<masterfd>. The C<row>, C<col>, C<xpixel> and C<ypixel> specify the new
window size. On success, returns C<0>. On error, returns C<-1> with C<errno>
set appropriately.
=cut
*/
int pty_change_window_size(int masterfd, int row, int col, int xpixel, int ypixel)
{
struct winsize win;
if (masterfd < 0 || row < 0 || col < 0 || xpixel < 0 || ypixel < 0)
return set_errno(EINVAL);
win.ws_row = row;
win.ws_col = col;
win.ws_xpixel = xpixel;
win.ws_ypixel = ypixel;
return ioctl(masterfd, TIOCSWINSZ, &win);
}
/*
=item C<pid_t pty_fork(int *masterfd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize)>
A safe version of I<forkpty(3)>. Creates a pseudo terminal and then calls
I<fork(2)>. In the parent process, the slave side of the pseudo terminal is
closed. In the child process, the master side of the pseudo terminal is
closed and the slave side is made the controlling terminal. It is duplicated
onto standard input, output and error and then closed. The master side of
the pseudo terminal is stored in C<*masterfd> for the parent process. The
device name of the slave side of the pseudo terminal is stored in the buffer
pointed to by C<slavename> which must be able to hold at least 64 bytes.
C<slavenamesize> is the size of the buffer pointed to by C<slavename>. No
more than C<slavenamesize> bytes will be written to C<slavename>, including
the terminating C<nul> byte. If C<slave_termios> is not null, it is passed
to I<tcsetattr(3)> with the command C<TCSANOW> to set the terminal
attributes of the slave device. If C<slave_winsize> is not null, it is
passed to I<ioctl(2)> with the command C<TIOCSWINSZ> to set the window size
of the slave device. On success, returns C<0> to the child process and
returns the process id of the child process to the parent process. On error,
returns C<-1> with C<errno> set appropriately.
=cut
*/
pid_t pty_fork(int *masterfd, char *slavename, size_t slavenamesize, const struct termios *slave_termios, const struct winsize *slave_winsize)
{
int slavefd;
pid_t pid;
/*
** Note: we don't use forkpty() because it closes the master in the
** child process before making the slave the controlling terminal of the
** child proces and this can prevent the slave from becoming the
** controlling terminal (but I have no idea why).
*/
if (pty_open(masterfd, &slavefd, slavename, slavenamesize, slave_termios, slave_winsize) == -1)
return -1;
switch (pid = fork())
{
case -1:
pty_release(slavename);
close(slavefd);
close(*masterfd);
return -1;
case 0:
{
/* Make the slave our controlling tty */
if (pty_make_controlling_tty(&slavefd, slavename) == -1)
_exit(EXIT_FAILURE);
/* Redirect stdin, stdout and stderr from the pseudo tty */
if (slavefd != STDIN_FILENO && dup2(slavefd, STDIN_FILENO) == -1) {
perror("dup2(stdin)");
_exit(EXIT_FAILURE);
}
if (slavefd != STDOUT_FILENO && dup2(slavefd, STDOUT_FILENO) == -1) {
perror("dup2(stdout)");
_exit(EXIT_FAILURE);
}
if (slavefd != STDERR_FILENO && dup2(slavefd, STDERR_FILENO) == -1) {
perror("dup2(stderr)");
_exit(EXIT_FAILURE);
}
/* Close the extra descriptor for the pseudo tty */
if (slavefd != STDIN_FILENO && slavefd != STDOUT_FILENO && slavefd != STDERR_FILENO)
close(slavefd);
/* Close the master side of the pseudo tty in the child */
close(*masterfd);
return 0;
}
default:
{
/* Close the slave side of the pseudo tty in the parent */
close(slavefd);
return pid;
}
}
}
/*
=back
=head1 ERRORS
Additional errors may be generated and returned from the underlying system
calls. See their manual pages.
=over 4
=item EINVAL
Invalid arguments were passed to one of the functions.
=item ENOTTY
I<openpty(3)> or I<open("/dev/ptc")> returned a slave descriptor for which
I<ttyname(3)> failed to return the slave device name. I<open("/dev/ptmx")>
returned a master descriptor for which I<ptsname(3)> failed to return the
the slave device name.
=item ENOENT
The old BSD-style pty device search failed to locate an available pseudo
terminal.
=item ENOSPC
The device name of the slave side of the pseudo terminal was too large to
fit in the C<slavename> buffer passed to I<pty_open(3)> or I<pty_fork(3)>.
=item ENXIO
I<pty_make_controlling_tty(3)> failed to disconnect from the controlling
terminal.
=back
=head1 MT-Level
MT-Safe if and only if I<ttyname_r(3)> or I<ptsname_r(3)> are available when
needed. On systems that have I<openpty(3)> or C<"/dev/ptc">, I<ttyname_r(3)>
is required, otherwise the unsafe I<ttyname(3)> will be used. On systems
that have C<"/dev/ptmx">, I<ptsname_r(3)> is required, otherwise the unsafe
I<ptsname(3)> will be used. On systems that have I<_getpty(2)>,
I<pty_open(3)> is unsafe because I<_getpty(2)> is unsafe. In short, it's
MT-Safe under Linux, Unsafe under Solaris and OpenBSD.
=head1 EXAMPLE
A very simple pty program:
#include <slack/std.h>
#include <slack/pseudo.h>
#include <slack/sig.h>
#include <sys/select.h>
#include <sys/wait.h>
struct termios stdin_termios;
struct winsize stdin_winsize;
int havewin = 0;
char slavename[64];
int masterfd;
pid_t pid;
int tty_raw(int fd)
{
struct termios attr[1];
if (tcgetattr(fd, attr) == -1)