-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdrv_pty.cxx
637 lines (552 loc) · 15.4 KB
/
drv_pty.cxx
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
/*
* drv_pty.cxx
*
* T38FAX Pseudo Modem
*
* Copyright (c) 2001-2009 Vyacheslav Frolov
*
* Open H323 Project
*
* The contents of this file are subject to the Mozilla Public License
* Version 1.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is Open H323 Library.
*
* The Initial Developer of the Original Code is Vyacheslav Frolov
*
* Contributor(s): Equivalence Pty ltd
*
* $Log: drv_pty.cxx,v $
* Revision 1.11 2009-07-08 18:43:44 vfrolov
* Added PseudoModem::ttyName()
*
* Revision 1.11 2009/07/08 18:43:44 vfrolov
* Added PseudoModem::ttyName()
*
* Revision 1.10 2009/05/19 07:29:04 vfrolov
* Commented Termios code uncompatible with some systems
*
* Revision 1.9 2009/03/13 09:44:32 vfrolov
* Fixed Segmentation fault (wrong PString usage)
*
* Revision 1.8 2008/09/10 11:15:00 frolov
* Ported to OPAL SVN trunk
*
* Revision 1.7 2007/07/17 10:03:22 vfrolov
* Added Unix98 PTY support
*
* Revision 1.6 2007/02/21 08:04:20 vfrolov
* Added printing message to stdout if no pty device
*
* Revision 1.5 2007/01/29 12:44:41 vfrolov
* Added ability to put args to drivers
*
* Revision 1.4 2006/10/19 10:44:15 vfrolov
* Fixed big file descriptors problem (replaced select() by poll())
*
* Revision 1.3 2006/10/18 14:54:53 vfrolov
* Added hPty >= FD_SETSIZE check
*
* Revision 1.2 2004/10/20 14:00:17 vfrolov
* Fixed race condition with SignalDataReady()/WaitDataReady()
*
* Revision 1.1 2004/07/07 12:38:32 vfrolov
* The code for pseudo-tty (pty) devices that communicates with fax application formed to PTY driver.
*
*
* Log: pty.cxx,v
*
* Revision 1.7 2002/12/30 12:49:42 vfrolov
* Added tracing thread's CPU usage (Linux only)
*
* Revision 1.6 2002/12/20 10:13:04 vfrolov
* Implemented tracing with PID of thread (for LinuxThreads)
* or ID of thread (for other POSIX Threads)
*
* Revision 1.5 2002/03/05 12:37:45 vfrolov
* Some OS specific code moved from pmodem.cxx to pty.cxx
*
* Revision 1.4 2002/03/01 08:53:16 vfrolov
* Added Copyright header
* Some OS specific code moved from pmodemi.cxx to pty.cxx
* Added error code string to log
* Fixed race condition with fast close and open slave tty
* Some other changes
*
* Revision 1.3 2002/01/10 06:10:03 craigs
* Added MPL header
*
* Revision 1.2 2002/01/01 23:43:57 craigs
* Added fix from Vyacheslav Frolov
*
* Revision 1.1 2002/01/01 23:06:54 craigs
* Initial version
*
*/
#include <ptlib.h>
#include "drv_pty.h"
#ifdef MODEM_DRIVER_Pty
#include <sys/poll.h>
#define new PNEW
///////////////////////////////////////////////////////////////
class UniPty : public ModemThreadChild
{
PCLASSINFO(UniPty, ModemThreadChild);
public:
UniPty(PseudoModemPty &_parent, int _hPty);
protected:
PseudoModemPty &Parent() { return (PseudoModemPty &)parent; }
int hPty;
};
///////////////////////////////////////////////////////////////
class InPty : public UniPty
{
PCLASSINFO(InPty, UniPty);
public:
InPty(PseudoModemPty &_parent, int _hPty);
protected:
virtual void Main();
};
///////////////////////////////////////////////////////////////
class OutPty : public UniPty
{
PCLASSINFO(OutPty, UniPty);
public:
OutPty(PseudoModemPty &_parent, int _hPty);
protected:
virtual void Main();
};
///////////////////////////////////////////////////////////////
UniPty::UniPty(PseudoModemPty &_parent, int _hPty)
: ModemThreadChild(_parent),
hPty(_hPty)
{
}
///////////////////////////////////////////////////////////////
InPty::InPty(PseudoModemPty &_parent, int _hPty)
: UniPty(_parent, _hPty)
{
}
void InPty::Main()
{
RenameCurrentThread(Parent().ptyName() + "(i)");
myPTRACE(1, "T38Modem\t--> Started");
for (;;) {
pollfd pollfd;
pollfd.fd = hPty;
pollfd.events = POLLIN;
if (stop)
break;
::poll(&pollfd, 1, 5000);
if (pollfd.revents) {
char cbuf[1024];
int len;
if (stop)
break;
len = ::read(hPty, cbuf, sizeof(cbuf));
if (len < 0) {
int err = errno;
myPTRACE(1, "T38Modem\t--> read ERROR " << len << " " << strerror(err));
SignalStop();
break;
}
if (len == 0) {
SignalStop();
break;
}
if (len > 0) {
Parent().ToInPtyQ(cbuf, len);
if (stop)
break;
}
}
}
myPTRACE(1, "T38Modem\t--> Stopped" << GetThreadTimes(", CPU usage: "));
}
///////////////////////////////////////////////////////////////
OutPty::OutPty(PseudoModemPty &_parent, int _hPty)
: UniPty(_parent, _hPty)
{
}
void OutPty::Main()
{
RenameCurrentThread(Parent().ptyName() + "(o)");
myPTRACE(1, "T38Modem\t<-- Started");
PBYTEArray *buf = NULL;
PINDEX done = 0;
for (;;) {
pollfd pollfd;
pollfd.fd = hPty;
pollfd.events = POLLOUT;
while (!buf) {
if (stop)
break;
buf = Parent().FromOutPtyQ();
if (buf) {
done = 0;
break;
}
WaitDataReady();
}
if (stop)
break;
::poll(&pollfd, 1, 5000);
if (pollfd.revents) {
int len;
if (stop)
break;
len = ::write(hPty, (const BYTE *)*buf + done, buf->GetSize() - done);
if (len < 0) {
int err = errno;
myPTRACE(1, "T38Modem\t<-- write ERROR " << len << " " << strerror(err));
SignalStop();
break;
}
done += len;
if (buf->GetSize() <= done) {
if (buf->GetSize() < done) {
myPTRACE(1, "T38Modem\t<-- " << buf->GetSize() << "(size) < (done)" << done << " " << len);
}
delete buf;
buf = NULL;
}
}
}
if (buf) {
if (buf->GetSize() != done)
myPTRACE(1, "T38Modem\t<-- Not sent " << PRTHEX(PBYTEArray((const BYTE *)*buf + done, buf->GetSize() - done)));
delete buf;
}
myPTRACE(1, "T38Modem\t<-- Stopped" << GetThreadTimes(", CPU usage: "));
}
///////////////////////////////////////////////////////////////
#ifdef USE_LEGACY_PTY
static const char *ttyPatternLegacy()
{
#if defined(P_LINUX)
#define TTY_PATTERN "^(/dev/)?tty[pqrstuvwxyzabcde][0123456789abcdef]$"
#endif
#if defined(P_FREEBSD)
#define TTY_PATTERN "^(/dev/)?tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]$"
#endif
#ifndef TTY_PATTERN
#define TTY_PATTERN "^(/dev/)?tty..$"
#endif
return TTY_PATTERN;
#undef TTY_PATTERN
}
static PBoolean ttyCheckLegacy(const PString &_tty)
{
PRegularExpression regLegacy(ttyPatternLegacy(), PRegularExpression::Extended);
return _tty.FindRegEx(regLegacy) == 0;
}
#endif // USE_LEGACY_PTY
///////////////////////////////////////////////////////////////
#ifdef USE_UNIX98_PTY
static const char *ttyPatternUnix98()
{
return "^\\+.+$";
}
static PBoolean ttyCheckUnix98(const PString &_tty)
{
PRegularExpression regUnix98(ttyPatternUnix98(), PRegularExpression::Extended);
return _tty.FindRegEx(regUnix98) == 0;
}
static const char *ptyPathUnix98()
{
return "/dev/ptmx";
}
static void ttyUnlinkUnix98(const char *ttypath)
{
char ptsName[64];
memset(ptsName, 0, sizeof(ptsName));
if (readlink(ttypath, ptsName, sizeof(ptsName) - 1) >= 0 && ::unlink(ttypath) == 0)
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty removed link " << ttypath << " -> " << ptsName);
}
#endif // USE_UNIX98_PTY
///////////////////////////////////////////////////////////////
PseudoModemPty::PseudoModemPty(
const PString &_tty,
const PString &_route,
#ifdef USE_UNIX98_PTY
const PConfigArgs &args,
#else
const PConfigArgs &/*args*/,
#endif
const PNotifier &_callbackEndPoint)
: PseudoModemBody(_tty, _route, _callbackEndPoint),
hPty(-1),
inPty(NULL),
outPty(NULL)
{
valid = TRUE;
#ifdef USE_LEGACY_PTY
if (ttyCheckLegacy(_tty)) {
if (_tty[0] != '/')
ttypath = "/dev/" + _tty;
else
ttypath = _tty;
(ptypath = ttypath)[5] = 'p';
ptyname = ptypath.Mid(5);
}
else
#endif // USE_LEGACY_PTY
#ifdef USE_UNIX98_PTY
if (ttyCheckUnix98(_tty)) {
if (args.HasOption("pts-dir")) {
ttypath = args.GetOptionString("pts-dir");
if (!ttypath.IsEmpty() && ttypath.Right(1) != "/")
ttypath += "/";
}
ttypath += _tty.Mid(1);
ptypath = ptyPathUnix98();
PINDEX i = ttypath.FindLast('/');
if (i == P_MAX_INDEX)
i = 0;
else
i++;
ptyname = ttypath.Mid(i);
}
else
#endif // USE_UNIX98_PTY
{
myPTRACE(1, "T38Modem\tPseudoModemPty::PseudoModemPty bad on " << _tty);
valid = FALSE;
}
}
PseudoModemPty::~PseudoModemPty()
{
cout << "Deleting " << ptyName() << "..." << endl;
myPTRACE(1, "T38Modem\tPseudoModemPty::~PseudoModemPty");
stop = true;
StopAll();
ClosePty();
cout << "" << ptyName() << " deleted" << endl;
}
PBoolean PseudoModemPty::CheckTty(const PString &_tty)
{
#ifdef USE_LEGACY_PTY
if (ttyCheckLegacy(_tty))
return TRUE;
#endif
#ifdef USE_UNIX98_PTY
if (ttyCheckUnix98(_tty))
return TRUE;
#endif
return FALSE;
}
PString PseudoModemPty::ArgSpec()
{
return
"[Pseudoterminal Options:]"
#ifdef USE_UNIX98_PTY
"-pts-dir: Set a base directory for Unix98 ptys.\r"
"For Unix98 ptys the tty should match to the regexp\r"
" '" + PString(ttyPatternUnix98()) + "'\r"
"(the first character '+' will be replaced by the base directory).\n"
#endif
#ifdef USE_LEGACY_PTY
"-legacy. Unused option\r"
"For legacy ptys the tty should match to the regexp\r"
" '" + PString(ttyPatternLegacy()) + "'\n"
#endif
;
}
PStringArray PseudoModemPty::Description()
{
PStringArray descriptions = PString(
"Uses pseudo-tty (pty) devices to communicate with a fax application.\n"
#ifdef USE_LEGACY_PTY
"For legacy ptys the tty should match to the regexp\n"
" '" + PString(ttyPatternLegacy()) + "'\n"
#endif
#ifdef USE_UNIX98_PTY
"For Unix98 ptys the tty should match to the regexp\n"
" '" + PString(ttyPatternUnix98()) + "'\n"
"(the first character '+' will be replaced by a base directory).\n"
"Options:\n"
" --pts-dir dir : Set a base directory for Unix98 scheme,\n"
" default is empty.\n"
#endif
).Lines();
return descriptions;
}
const PString &PseudoModemPty::ttyPath() const
{
return ttypath;
}
ModemThreadChild *PseudoModemPty::GetPtyNotifier()
{
return outPty;
}
PBoolean PseudoModemPty::StartAll()
{
if (IsOpenPty()
&& (inPty = new InPty(*this, hPty))
&& (outPty = new OutPty(*this, hPty))
&& (PseudoModemBody::StartAll())
) {
inPty->Resume();
outPty->Resume();
return TRUE;
}
StopAll();
ClosePty();
return FALSE;
}
void PseudoModemPty::StopAll()
{
myPTRACE(4, "T38Modem\tPseudoModemPty::StopAll() inPty = " << inPty << " outPty = " << outPty);
if (inPty) {
inPty->SignalStop();
inPty->WaitForTermination();
PWaitAndSignal mutexWait(Mutex);
delete inPty;
inPty = NULL;
}
if (outPty) {
outPty->SignalStop();
outPty->WaitForTermination();
PWaitAndSignal mutexWait(Mutex);
delete outPty;
outPty = NULL;
}
PseudoModemBody::StopAll();
}
PBoolean PseudoModemPty::OpenPty()
{
if (IsOpenPty()) {
// check hPty health
pollfd pollfd;
pollfd.fd = hPty;
pollfd.events = POLLIN;
::poll(&pollfd, 1, 0);
if (pollfd.revents) {
char cbuf[1];
int len;
len = ::read(hPty, cbuf, 1);
if (len < 0) {
// hPty is ill (slave tty was closed and still was not opened)
int err = errno;
// close hPty ASAP
ClosePty();
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty read ERROR " << len << " " << strerror(err));
} else if (len > 0) {
PBYTEArray *buf = new PBYTEArray((const BYTE *)cbuf, len);
myPTRACE(3, "T38Modem\tPseudoModemPty::OpenPty read " << PRTHEX(*buf));
ToInPtyQ(buf);
}
}
if (IsOpenPty()) {
// hPty is good (slave tty was not closed or was opened after closing)
return TRUE;
}
}
int delay = 0;
while ((hPty = ::open(ptypath, O_RDWR | O_NOCTTY)) < 0) {
int err = errno;
myPTRACE(delay + 1, "T38Modem\tPseudoModemPty::OpenPty open " << ptypath << " ERROR: " << strerror(err));
if (err == ENOENT) {
cout << "Could not open " << ptypath << ": " << strerror(err) << endl;
return FALSE;
}
myPTRACE(delay + 1, "T38Modem\tPseudoModemPty::OpenPty will try again to open " << ptypath);
if (delay > 0)
PThread::Sleep(delay * 1000);
if (++delay > 5)
delay = 5;
}
#if defined FD_TRACE_LEVEL && defined PTRACING
#ifdef FD_SETSIZE
if (hPty > (FD_SETSIZE*3)/4)
myPTRACE(FD_TRACE_LEVEL, "T38Modem\tPseudoModemPty::OpenPty WARNING: hPty=" << hPty << ", FD_SETSIZE=" << FD_SETSIZE);
#else
#warning FD_SETSIZE not defined!
#endif
#endif
#if 0
struct termios Termios;
if (::tcgetattr(hPty, &Termios) != 0) {
int err = errno;
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty tcgetattr " << ptyname << " ERROR: " << strerror(err));
ClosePty();
return FALSE;
}
Termios.c_lflag &= ~(ICANON | ISIG | ECHO | ECHOCTL | ECHOE | ECHOK | ECHOKE | ECHONL | ECHOPRT);
Termios.c_iflag |= IGNBRK;
//Termios.c_iflag &= ~IGNBRK;
//Termios.c_iflag |= BRKINT;
Termios.c_cc[VMIN] = 1;
Termios.c_cc[VTIME] = 0;
if (::tcsetattr(hPty, TCSANOW, &Termios) != 0) {
int err = errno;
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty tcsetattr " << ptyname << " ERROR: " << strerror(err));
ClosePty();
return FALSE;
}
#endif
#ifdef USE_UNIX98_PTY
if (ptypath == ptyPathUnix98()) {
ttyUnlinkUnix98(ttypath);
if (::unlockpt(hPty) != 0) {
int err = errno;
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty unlockpt " << ptyname << " ERROR: " << strerror(err));
ClosePty();
return FALSE;
}
char ptsName[64];
if (::ptsname_r(hPty, ptsName, sizeof(ptsName)) != 0) {
int err = errno;
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty ptsname_r " << ptyname << " ERROR: " << strerror(err));
ClosePty();
return FALSE;
}
if (::symlink(ptsName, ttypath) != 0) {
int err = errno;
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty symlink " << ttypath << " -> " << ptsName << " ERROR: " << strerror(err));
ClosePty();
return FALSE;
}
myPTRACE(1, "T38Modem\tPseudoModemPty::OpenPty added link " << ttypath << " -> " << ptsName);
}
#endif // USE_UNIX98_PTY
return TRUE;
}
void PseudoModemPty::ClosePty()
{
if (!IsOpenPty())
return;
#ifdef USE_UNIX98_PTY
ttyUnlinkUnix98(ttypath);
#endif
if (::close(hPty) != 0) {
int err = errno;
myPTRACE(1, "T38Modem\tPseudoModemPty::ClosePty close " << ptyname << " ERROR: " << strerror(err));
}
hPty = -1;
}
void PseudoModemPty::MainLoop()
{
if (AddModem()) {
while (!stop && OpenPty() && StartAll()) {
while (!stop && !childstop) {
WaitDataReady();
}
myPTRACE(4, "T38Modem\tPseudoModemPty::MainLoop() Ending -- stop = " << stop << " childstop = " << childstop);
if (!stop)
StopAll();
}
myPTRACE(2, "T38Modem\tPseudoModemPty::MainLoop() ClosePty()");
ClosePty();
}
myPTRACE(1, "T38Modem\tPseudoModemPty::MainLoop() Done");
}
///////////////////////////////////////////////////////////////
#endif // MODEM_DRIVER_Pty