Cannot set stty parameters and read from /dev/ttyU0

classic Classic list List threaded Threaded
16 messages Options
Reply | Threaded
Open this post in threaded view
|

Cannot set stty parameters and read from /dev/ttyU0

Jurjen Oskam-3
Hi everybody,

Earlier I had a Linux machine (well, a Raspberry Pi actually) which
I used to read out my energy meter. The energy meter was connected
to a USB port with a custom FTDI cable. The energy meter only
supports reading from it, writing to it is not possible.

On Linux, I set the necessary parameters on the USB tty as follows:

/bin/stty -F /dev/ttyUSB0 9600 sane evenp crtscts cs7 igncr

And after that, issuing "cat /dev/ttyUSB0" resulted in the output
I'm interested in: the energy meter outputs its data every ten seconds.

Wrapping this in a Perl script was easy: just set issue the stty
command before invoking the Perl script, and in Perl I could just open
/dev/ttyUSB0 for reading and process the output line by line:

open(METER,"/dev/ttyUSB0") or die;
while(<METER>) {
  # do stuff
done

This worked fine.


I replaced the Raspberry Pi with a more powerful amd64 machine
(full dmesg below). The USB cable shows up as:

uftdi0 at uhub3 port 6 "FTDI P1 Converter Cable" rev 2.00/6.00 addr 3
ucom0 at uftdi0 portno 1


But now I can't figure out how to read from /dev/ttyU0.

The first problem I'm having is that the stty setting doesn't seem to
stick:

$ stty -f /dev/ttyU0
ispeed 0 baud; ospeed 9600 baud;
lflags: echoe echoke echoctl
cflags: cs8 -parenb

$ stty -f /dev/ttyU0 9600 sane parenb -parodd crtscts cs7 igncr
$

$ stty -f /dev/ttyU0
ispeed 0 baud; ospeed 9600 baud;
lflags: echoe echoke echoctl
cflags: cs8 -parenb

When I do a "cat /dev/ttyU0" in one terminal, it just hangs without
displaying anything. If I execute "stty -f /dev/ttyU0" in another terminal,
cat suddenly outputs a few lines of garbage and then exits.


When I again do a "cat /dev/ttyU0" in one terminal, but then use stty in
another terminal to set the correct settings, the cat command starts
outputting the correct data: several lines of output every ten seconds.
After while, it stops though, and I can't interrupt the cat command
anymore. A stty in another terminal also hangs uninterruptibly. Even a
kill -9 and disconnecting the SSH session doesn't work:

joskam   21226  0.0  0.0   180   152 p0  D+     3:56PM    0:00.00 stty -f
/dev/ttyU0
joskam   22302  0.0  0.0   236   176 p1- IE     3:54PM    0:00.00 (cat)

I did read the manual pages, but I probably overlooked something.


How can I set the correct parameters on /dev/ttyU0 and read from it?


Thank you,

Jurjen Oskam



Full dmesg:

OpenBSD 5.4 (GENERIC.MP) #1: Tue Nov 12 10:57:06 CET 2013
   
[hidden email]:/home/jasper/binpatchng/work-binpatch54-
amd64/src/sys/arch/amd64/compile/GENERIC.MP
real mem = 4204191744 (4009MB)
avail mem = 4084551680 (3895MB)
mainbus0 at root
bios0 at mainbus0: SMBIOS rev. 2.7 @ 0xeb420 (76 entries)
bios0: vendor American Megatrends Inc. version "2.04" date 04/16/2013
bios0: Shuttle Inc. DS61
acpi0 at bios0: rev 2
acpi0: sleep states S0 S1 S3 S4 S5
acpi0: tables DSDT FACP APIC MCFG SLIC HPET SSDT SSDT SSDT
acpi0: wakeup devices P0P1(S4) USB1(S3) USB2(S3) USB3(S3) USB4(S3) USB5(S3)
USB6(S3) USB7(S3) PXSX(S4) RP01(S4) PXSX(S4) RP02(S4) PXSX(S4) RP03(S4)
PXSX(S4) RP04(S4) [...]
acpitimer0 at acpi0: 3579545 Hz, 24 bits
acpimadt0 at acpi0 addr 0xfee00000: PC-AT compat
cpu0 at mainbus0: apid 0 (boot processor)
cpu0: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz, 2893.81 MHz
cpu0:
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,
CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,
DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,POPCNT,DEADLINE,
XSAVE,NXE,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS
cpu0: 256KB 64b/line 8-way L2 cache
cpu0: smt 0, core 0, package 0
cpu0: apic clock running at 99MHz
cpu1 at mainbus0: apid 2 (application processor)
cpu1: Intel(R) Pentium(R) CPU G2020 @ 2.90GHz, 2893.43 MHz
cpu1:
FPU,VME,DE,PSE,TSC,MSR,PAE,MCE,CX8,APIC,SEP,MTRR,PGE,MCA,CMOV,PAT,PSE36,
CFLUSH,DS,ACPI,MMX,FXSR,SSE,SSE2,SS,HTT,TM,PBE,SSE3,PCLMUL,DTES64,MWAIT,
DS-CPL,VMX,EST,TM2,SSSE3,CX16,xTPR,PDCM,PCID,SSE4.1,SSE4.2,POPCNT,DEADLINE,
XSAVE,NXE,LONG,LAHF,PERF,ITSC,FSGSBASE,SMEP,ERMS
cpu1: 256KB 64b/line 8-way L2 cache
cpu1: smt 0, core 1, package 0
ioapic0 at mainbus0: apid 2 pa 0xfec00000, version 20, 24 pins
acpimcfg0 at acpi0 addr 0xf8000000, bus 0-63
acpihpet0 at acpi0: 14318179 Hz
acpiprt0 at acpi0: bus 0 (PCI0)
acpiprt1 at acpi0: bus -1 (P0P1)
acpiprt2 at acpi0: bus 2 (RP01)
acpiprt3 at acpi0: bus 3 (RP02)
acpiprt4 at acpi0: bus -1 (RP03)
acpiprt5 at acpi0: bus 4 (RP04)
acpiprt6 at acpi0: bus -1 (RP05)
acpiprt7 at acpi0: bus -1 (RP06)
acpiprt8 at acpi0: bus -1 (RP07)
acpiprt9 at acpi0: bus -1 (RP08)
acpiprt10 at acpi0: bus 1 (PEG0)
acpiec0 at acpi0: Failed to read resource settings
acpicpu0 at acpi0: C3, C2, C1, PSS
acpicpu1 at acpi0: C3, C2, C1, PSS
acpipwrres0 at acpi0: FN00
acpipwrres1 at acpi0: FN01
acpipwrres2 at acpi0: FN02
acpipwrres3 at acpi0: FN03
acpipwrres4 at acpi0: FN04
acpitz0 at acpi0: critical temperature is 106 degC
acpitz1 at acpi0: critical temperature is 106 degC
acpibat0 at acpi0: BAT0 not present
acpibat1 at acpi0: BAT1 not present
acpibat2 at acpi0: BAT2 not present
acpibtn0 at acpi0: PWRB
acpibtn1 at acpi0: LID0
acpivideo0 at acpi0: GFX0
acpivout0 at acpivideo0: DD02
cpu0: Enhanced SpeedStep 2893 MHz: speeds: 2900, 2800, 2700, 2600, 2500,
2400, 2300, 2200, 2100, 2000, 1900, 1800, 1700, 1600 MHz
pci0 at mainbus0 bus 0
pchb0 at pci0 dev 0 function 0 "Intel Xeon E3-1200v2 Host" rev 0x09
ppb0 at pci0 dev 1 function 0 "Intel Xeon E3-1200v2 PCIE" rev 0x09: msi
pci1 at ppb0 bus 1
vga1 at pci0 dev 2 function 0 "Intel HD Graphics 2500" rev 0x09
intagp0 at vga1
agp0 at intagp0: aperture at 0xe0000000, size 0x10000000
inteldrm0 at vga1
drm0 at inteldrm0
No connectors reported connected with modes
Cannot find any crtc or sizes - going 1024x768
inteldrm0: 1024x768
wsdisplay0 at vga1 mux 1: console (std, vt100 emulation)
wsdisplay0: screen 1-5 added (std, vt100 emulation)
"Intel 6 Series MEI" rev 0x04 at pci0 dev 22 function 0 not configured
ehci0 at pci0 dev 26 function 0 "Intel 6 Series USB" rev 0x05: apic 2 int 16
usb0 at ehci0: USB revision 2.0
uhub0 at usb0 "Intel EHCI root hub" rev 2.00/1.00 addr 1
ppb1 at pci0 dev 28 function 0 "Intel 6 Series PCIE" rev 0xb5: msi
pci2 at ppb1 bus 2
ppb2 at pci0 dev 28 function 1 "Intel 6 Series PCIE" rev 0xb5: msi
pci3 at ppb2 bus 3
re0 at pci3 dev 0 function 0 "Realtek 8168" rev 0x06: RTL8168E/8111E-VL
(0x2c80), apic 2 int 17, address 80:ee:73:73:59:f7
rgephy0 at re0 phy 7: RTL8169S/8110S PHY, rev. 5
ppb3 at pci0 dev 28 function 3 "Intel 6 Series PCIE" rev 0xb5: msi
pci4 at ppb3 bus 4
re1 at pci4 dev 0 function 0 "Realtek 8168" rev 0x06: RTL8168E/8111E-VL
(0x2c80), apic 2 int 19, address 80:ee:73:73:59:f6
rgephy1 at re1 phy 7: RTL8169S/8110S PHY, rev. 5
ehci1 at pci0 dev 29 function 0 "Intel 6 Series USB" rev 0x05: apic 2 int 23
usb1 at ehci1: USB revision 2.0
uhub1 at usb1 "Intel EHCI root hub" rev 2.00/1.00 addr 1
pcib0 at pci0 dev 31 function 0 "Intel H61 LPC" rev 0x05
ahci0 at pci0 dev 31 function 2 "Intel 6 Series AHCI" rev 0x05: msi, AHCI 1.3
scsibus0 at ahci0: 32 targets
sd0 at scsibus0 targ 1 lun 0: <ATA, Samsung SSD 840, EXT0> SCSI3 0/direct
fixed naa.50025388a00f2d01
sd0: 114473MB, 512 bytes/sector, 234441648 sectors, thin
ichiic0 at pci0 dev 31 function 3 "Intel 6 Series SMBus" rev 0x05: apic 2 int 18
iic0 at ichiic0
spdmem0 at iic0 addr 0x50: 2GB DDR3 SDRAM PC3-10600 SO-DIMM
spdmem1 at iic0 addr 0x52: 2GB DDR3 SDRAM PC3-10600 SO-DIMM
isa0 at pcib0
isadma0 at isa0
com0 at isa0 port 0x3f8/8 irq 4: ns16550a, 16 byte fifo
com1 at isa0 port 0x2f8/8 irq 3: ns16550a, 16 byte fifo
pckbc0 at isa0 port 0x60/5
kbc: cmd word write error
pcppi0 at isa0 port 0x61
spkr0 at pcppi0
it0 at isa0 port 0x2e/2: IT8728F rev 1, EC port 0xa30
mtrr: Pentium Pro MTRR support
uhub2 at uhub0 port 1 "Intel Rate Matching Hub" rev 2.00/0.00 addr 2
umass0 at uhub2 port 2 configuration 1 interface 0 "Generic USB Storage" rev
2.00/94.54 addr 3
umass0: using SCSI over Bulk-Only
scsibus1 at umass0: 2 targets, initiator 0
sd1 at scsibus1 targ 1 lun 0: <Generic, STORAGE DEVICE, 9454> SCSI0 0/direct
removable
uhub3 at uhub1 port 1 "Intel Rate Matching Hub" rev 2.00/0.00 addr 2
uftdi0 at uhub3 port 6 "FTDI P1 Converter Cable" rev 2.00/6.00 addr 3
ucom0 at uftdi0 portno 1
vscsi0 at root
scsibus2 at vscsi0: 256 targets
softraid0 at root
scsibus3 at softraid0: 256 targets
root on sd0a (32b01165a87fc2c2.a) swap on sd0b dump on sd0b

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Theo de Raadt
> /bin/stty -F /dev/ttyUSB0 9600 sane evenp crtscts cs7 igncr

            ^^ --- no a standard stty option
> The first problem I'm having is that the stty setting doesn't seem to
> stick:
>
> $ stty -f /dev/ttyU0
> ispeed 0 baud; ospeed 9600 baud;
> lflags: echoe echoke echoctl
> cflags: cs8 -parenb
>
> $ stty -f /dev/ttyU0 9600 sane parenb -parodd crtscts cs7 igncr

-f is not -F

When the tty is closed, it reverts to the default configuration.
There is no support for this -F method.

What you need to instead is wrap all this in a way which keeps the
tty open

    (
        stty 9600 sane parenb -parodd crtscts cs7 igncr
        do your IO loop
    ) </dev/ttyU0 0>&1 0>&2

Something like that.

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Philip Guenther-2
On Tue, Jan 7, 2014 at 8:23 AM, Theo de Raadt <[hidden email]> wrote:
> What you need to instead is wrap all this in a way which keeps the
> tty open
>
>     (
>         stty 9600 sane parenb -parodd crtscts cs7 igncr
>         do your IO loop
>     ) </dev/ttyU0 0>&1 0>&2
>
> Something like that.

I think the desired redirections on the subshell-close would make that
last line:
        ) <>/dev/ttyU0 >&0

("open /dev/ttyU0 read-write as stdin, and then dup that to stdout")


Philip Guenther

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Remco-2
In reply to this post by Jurjen Oskam-3
Jurjen Oskam wrote:

> uftdi0 at uhub3 port 6 "FTDI P1 Converter Cable" rev 2.00/6.00 addr 3
> ucom0 at uftdi0 portno 1
>
>
> But now I can't figure out how to read from /dev/ttyU0.
>

I usually use /dev/cuaU0 when reading from serial-to-USB converters.

Unless your cable is different in some way I think running cu(1) on a cua(4)
device (something like "cu -l/dev/cuaU0 -s9600") may show you the desired
information.

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Jurjen Oskam-3
In reply to this post by Philip Guenther-2
Philip Guenther <guenther <at> gmail.com> writes:

> On Tue, Jan 7, 2014 at 8:23 AM, Theo de Raadt <deraadt <at>
cvs.openbsd.org> wrote:

> > What you need to instead is wrap all this in a way which keeps the
> > tty open
> >
> >     (
> >         stty 9600 sane parenb -parodd crtscts cs7 igncr
> >         do your IO loop
> >     ) </dev/ttyU0 0>&1 0>&2
> >
> > Something like that.
>
> I think the desired redirections on the subshell-close would make that
> last line:
>         ) <>/dev/ttyU0 >&0
>
> ("open /dev/ttyU0 read-write as stdin, and then dup that to stdout")

Thank you for the responses. I sort of figured out that the stty settings
are set to default each time the device is opened, but now that's confirmed
I ran into the problem that open() does not seem to be returning.

I created the following simple shell script:

#!/bin/sh

( stty 9600 sane parenb -parodd crtscts cs7 igncr
while read line
do
echo $line
done
) <>/dev/ttyU0 >&0

Running it results in no output at all, without the prompt coming back.
Interrupting the process results in the following ktrace snippet:

   486 sh       1389125130.342774 CALL  
open(0x208ee2c50,0x202<O_RDWR|O_CREAT>,0x1b6<S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
|S_IROTH|S_IWOTH>)
   486 sh       1389125130.342776 NAMI  "/dev/ttyU0"
   486 sh       1389125151.417307 PSIG  SIGINT caught handler=0x4214f0
mask=0<>
   486 sh       1389125151.417312 RET   open -1 errno 4 Interrupted system
call

Looking at the timestamps, the open() only returns when I Ctrl-C the process.

The same happens with the following trivial Perl script:

#!/usr/bin/perl -w

$|=1;
open(METER,"/dev/ttyU0") or die;
print "opened terminal\n";
close(METER);


Running it produces no output without the prompt coming back, at least not
until I Ctrl-C the Perl script:

 15860 perl     1389125426.222462 CALL  open(0x12f6694a1d70,0<O_RDONLY>)
 15860 perl     1389125426.222465 NAMI  "/dev/ttyU0"
 15860 perl     1389125451.261414 PSIG  SIGINT SIG_DFL

Again, open() doesn't seem to return.

Am I doing something wrong here?

Regards,

Jurjen Oskam

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Philip Guenther-2
On Tue, Jan 7, 2014 at 12:17 PM, Jurjen Oskam <[hidden email]> wrote:

> Thank you for the responses. I sort of figured out that the stty settings
> are set to default each time the device is opened, but now that's confirmed
> I ran into the problem that open() does not seem to be returning.
>
> I created the following simple shell script:
>
> #!/bin/sh
>
> ( stty 9600 sane parenb -parodd crtscts cs7 igncr
> while read line
> do
> echo $line
> done
> ) <>/dev/ttyU0 >&0
>
> Running it results in no output at all, without the prompt coming back.
> Interrupting the process results in the following ktrace snippet:
>
>    486 sh       1389125130.342774 CALL
> open(0x208ee2c50,0x202<O_RDWR|O_CREAT>,0x1b6<S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
> |S_IROTH|S_IWOTH>)
>    486 sh       1389125130.342776 NAMI  "/dev/ttyU0"
>    486 sh       1389125151.417307 PSIG  SIGINT caught handler=0x4214f0
> mask=0<>
>    486 sh       1389125151.417312 RET   open -1 errno 4 Interrupted system
> call
>
> Looking at the timestamps, the open() only returns when I Ctrl-C the process.

This is where Remco's response comes into play.  As described on the
tty(4)/cua(4) manpage, /dev/ttyU* blocks on open until the external
device signals that it's active via some hardware signal (DTR, iirc).
If you want to initiate an outgoing connection to a potentially
inactive device, use the matching /dev/cuaU* device.


Philip Guenther

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Alexander Hall
In reply to this post by Jurjen Oskam-3
On 01/07/14 21:17, Jurjen Oskam wrote:

> Philip Guenther <guenther <at> gmail.com> writes:
>
>> On Tue, Jan 7, 2014 at 8:23 AM, Theo de Raadt <deraadt <at>
> cvs.openbsd.org> wrote:
>>> What you need to instead is wrap all this in a way which keeps the
>>> tty open
>>>
>>>      (
>>>          stty 9600 sane parenb -parodd crtscts cs7 igncr
>>>          do your IO loop
>>>      ) </dev/ttyU0 0>&1 0>&2
>>>
>>> Something like that.
>>
>> I think the desired redirections on the subshell-close would make that
>> last line:
>>          ) <>/dev/ttyU0 >&0
>>
>> ("open /dev/ttyU0 read-write as stdin, and then dup that to stdout")
>
> Thank you for the responses. I sort of figured out that the stty settings
> are set to default each time the device is opened, but now that's confirmed
> I ran into the problem that open() does not seem to be returning.
>
> I created the following simple shell script:
>
> #!/bin/sh
>
> ( stty 9600 sane parenb -parodd crtscts cs7 igncr
> while read line
> do
> echo $line
> done
> ) <>/dev/ttyU0 >&0
>
> Running it results in no output at all, without the prompt coming back.

No surprise, really, as you redireced the printed lines back into the
tty... :-)

Either print it to stderr (echo $line >&2), or I simply suspect you
don't want the '>&0' part.

/Alexander

> Interrupting the process results in the following ktrace snippet:
>
>     486 sh       1389125130.342774 CALL
> open(0x208ee2c50,0x202<O_RDWR|O_CREAT>,0x1b6<S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP
> |S_IROTH|S_IWOTH>)
>     486 sh       1389125130.342776 NAMI  "/dev/ttyU0"
>     486 sh       1389125151.417307 PSIG  SIGINT caught handler=0x4214f0
> mask=0<>
>     486 sh       1389125151.417312 RET   open -1 errno 4 Interrupted system
> call
>
> Looking at the timestamps, the open() only returns when I Ctrl-C the process.
>
> The same happens with the following trivial Perl script:
>
> #!/usr/bin/perl -w
>
> $|=1;
> open(METER,"/dev/ttyU0") or die;
> print "opened terminal\n";
> close(METER);
>
>
> Running it produces no output without the prompt coming back, at least not
> until I Ctrl-C the Perl script:
>
>   15860 perl     1389125426.222462 CALL  open(0x12f6694a1d70,0<O_RDONLY>)
>   15860 perl     1389125426.222465 NAMI  "/dev/ttyU0"
>   15860 perl     1389125451.261414 PSIG  SIGINT SIG_DFL
>
> Again, open() doesn't seem to return.
>
> Am I doing something wrong here?
>
> Regards,
>
> Jurjen Oskam

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Daniel Bolgheroni-3
In reply to this post by Jurjen Oskam-3
On Tue, Jan 07, 2014 at 03:05:39PM +0000, Jurjen Oskam wrote:
> Hi everybody,
>
> Earlier I had a Linux machine (well, a Raspberry Pi actually) which
> I used to read out my energy meter. The energy meter was connected
> to a USB port with a custom FTDI cable. The energy meter only
> supports reading from it, writing to it is not possible.

Some years ago I used a Arduino Duemilanove in a project. This board
uses the FTDI chipset uftdi(4). As Remco, it worked pretty well for me
too, using cu(1) or tip(1) + /dev/cuaUx.

--
db

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Stuart Henderson
In reply to this post by Jurjen Oskam-3
On 2014-01-07, Jurjen Oskam <[hidden email]> wrote:
> open(METER,"/dev/ttyUSB0") or die;
> while(<METER>) {
>   # do stuff
> done

http://www.jibble.org/currentcost/ uses Device::SerialPort
(p5-Device-SerialPort-1.04p0v0 in packages) to setup the serial port
from perl. This might be easier than wrapping your perl script in a
shell script.

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Jurjen Oskam-3
In reply to this post by Philip Guenther-2
Philip Guenther <guenther <at> gmail.com> writes:

> This is where Remco's response comes into play.  As described on the
> tty(4)/cua(4) manpage, /dev/ttyU* blocks on open until the external
> device signals that it's active via some hardware signal (DTR, iirc).
> If you want to initiate an outgoing connection to a potentially
> inactive device, use the matching /dev/cuaU* device.

OK, I've got it to work using a /dev/cua* device. I still have problems with
processes not exiting though. Since this machine has real com ports, I also
made a cable that is attached to cua01 (no USB involved).

This little Perl script shows the problem:

#!/usr/bin/perl -w

$|=1;
close(STDIN) or die;
open(STDIN,"</dev/cua01");
system("/bin/stty 9600 sane parenb -parodd crtscts cs7 igncr");
system("/bin/stty");
while (my $line = <>) {
        print $line;
}

I run it using ktrace:

$ ktrace -i ./try.pl
speed 9600 baud;
lflags: echoe echoke echoctl
iflags: igncr
cflags: cs7 parenb crtscts
/XMX5XMXABCE000035164

0-0:96.1.1(31333031333735352020202020202020)
1-0:1.8.1(02932.937*kWh)
   [ ... output snipped ...]

It shows as follows in the output of ps:

$ ps
  PID TT  STAT       TIME COMMAND
15727 p0  Ss      0:00.01 -ksh (ksh)
13444 p0  S+      0:00.02 kdump -l -T
26314 p1  Ss      0:00.00 -ksh (ksh)
13670 p1  S+      0:00.01 /usr/bin/perl -w ./try.pl
 6996 p2  Ss      0:00.00 -ksh (ksh)
29731 p2  R+/1    0:00.00 ps

Then when I kill it, I get this:

$ kill 13670
$ ps
  PID TT  STAT       TIME COMMAND
15727 p0  Is      0:00.01 -ksh (ksh)
13444 p0  S+      0:00.02 kdump -l -T
26314 p1  Is      0:00.00 -ksh (ksh)
13670 p1  SE+     0:00.01 (perl)
 6996 p2  Ss      0:00.00 -ksh (ksh)
13352 p2  R+/1    0:00.00 ps

... and after a while, this:

$ ps
  PID TT  STAT       TIME COMMAND
15727 p0  Is      0:00.01 -ksh (ksh)
13444 p0  S+      0:00.02 kdump -l -T
26314 p1  Is      0:00.00 -ksh (ksh)
13670 p1  IE+     0:00.01 (perl)
 6996 p2  Ss      0:00.00 -ksh (ksh)
23612 p2  R+/1    0:00.00 ps

The terminal on which the Perl script was running didn't return the prompt.
The script was also no longer running, because it didn't display new data
coming from cua00 anymore.

The last few lines from ktrace.out are:

 13670 perl     1389283736.782851 RET   write 2
 13670 perl     1389283736.782855 CALL  read(0,0x10dbb9977000,0x2000)
 13670 perl     1389283738.376985 PSIG  SIGTERM SIG_DFL

The read normally blocks for 10 seconds, because that is the interval the
meter sends it data. The kill happened while this read was waiting for data.

I am now no longer able to terminate the process. Disconnecting its terminal
just gives:

$ ps
  PID TT  STAT       TIME COMMAND
15727 p0  Is      0:00.01 -ksh (ksh)
13444 p0  S+      0:00.02 kdump -l -T
13670 p1- IE      0:00.01 (perl)
 6996 p2  Ss      0:00.00 -ksh (ksh)
32590 p2  R+/1    0:00.00 ps

Killing it with -9 also does not work.

cua01 is still busy though:

$ stty -f /dev/cua01
stty: /dev/cua01: Device busy


The only way to get rid of the process is to reboot the system.


Am I doing anything wrong here? I expected the process to just exit and
release cua01 after I killed it.

Regards,

Jurjen Oskam

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Philip Guenther-2
On Thu, Jan 9, 2014 at 8:20 AM, Jurjen Oskam <[hidden email]> wrote:
> OK, I've got it to work using a /dev/cua* device. I still have problems with
> processes not exiting though. Since this machine has real com ports, I also
> made a cable that is attached to cua01 (no USB involved).
...

> Then when I kill it, I get this:
>
> $ kill 13670
> $ ps
>   PID TT  STAT       TIME COMMAND
> 15727 p0  Is      0:00.01 -ksh (ksh)
> 13444 p0  S+      0:00.02 kdump -l -T
> 26314 p1  Is      0:00.00 -ksh (ksh)
> 13670 p1  SE+     0:00.01 (perl)
>  6996 p2  Ss      0:00.00 -ksh (ksh)
> 13352 p2  R+/1    0:00.00 ps

What's the output of ps -Owchan,procflags,f for the process?


Philip Guenther

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Jurjen Oskam-3
Philip Guenther <guenther <at> gmail.com> writes:

>
> On Thu, Jan 9, 2014 at 8:20 AM, Jurjen Oskam <jurjen <at> osk.am> wrote:
> > OK, I've got it to work using a /dev/cua* device. I still have problems
> > with processes not exiting though. Since this machine has real com
> > ports, I also made a cable that is attached to cua01 (no USB involved).
> ...
> > Then when I kill it, I get this:
> >
> > $ kill 13670
> > $ ps
> >   PID TT  STAT       TIME COMMAND
> > 15727 p0  Is      0:00.01 -ksh (ksh)
> > 13444 p0  S+      0:00.02 kdump -l -T
> > 26314 p1  Is      0:00.00 -ksh (ksh)
> > 13670 p1  SE+     0:00.01 (perl)
> >  6996 p2  Ss      0:00.00 -ksh (ksh)
> > 13352 p2  R+/1    0:00.00 ps
>
> What's the output of ps -Owchan,procflags,f for the process?

$ ps -Owchan,procflags,f
ps: procflags: keyword not found
  PID WCHAN         F TT  STAT       TIME COMMAND
 1579 pause      408e p0  Is      0:00.00 -ksh (ksh)
21410 ttyout  80006086 p0  IE+     0:00.00 (perl)
 8608 pause      408e p1  Ss      0:00.00 -ksh (ksh)
27149 -          4006 p1  R+/1    0:00.00 ps -Owchan

(Running 5.4-stable, so that's why procflags isn't recognized. I'll try a
snapshot to see what happens.)

Regards,

Jurjen Oskam

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Philip Guenther-2
On Fri, Jan 10, 2014 at 7:33 AM, Jurjen Oskam <[hidden email]> wrote:
> (Running 5.4-stable, so that's why procflags isn't recognized. I'll try a
> snapshot to see what happens.)

Oh, you're running 5.4-stable?  I thought you were running -current
and was worried there was another hang in there.  I'm now 99% sure
you're hitting the one I fixed back in October.


Philip Guenther

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Jurjen Oskam-3
Philip Guenther <guenther <at> gmail.com> writes:


> Oh, you're running 5.4-stable?  I thought you were running -current
> and was worried there was another hang in there.  I'm now 99% sure
> you're hitting the one I fixed back in October.

I did some more experimenting, and I found that the issue does not happen
when I leave out the "crtscts" option to stty.

With "crtscts" however, things get weird:

Opening and immediately closing cua01 is no problem:

open cua01
stty 9600 sane parenb -parodd crtscts cs7 igncr
close cua01 # device probably didn't send anything yet
exit        # exits normally



Opening cua01 and closing it later *is* a problem:

open cua01
stty 9600 sane parenb -parodd crtscts cs7 igncr
sleep 15    # to make sure the device on the other end has sent some data
close cua01 # HANGS, but is interruptible after which everything is nicely
              cleaned up
exit        # never reached



Interrupting a read from cua01 results in a process that can't exit:

open cua01
stty 9600 sane parenb -parodd crtscts cs7 igncr
while-loop {
read cua01  # blocks when the other end isn't sending anything; if the
              process is interrupted now it won't ever exit;
              cua01 is not released and only a reboot clears the process
}
close cua01 # HANGS, but is interruptible after which everything is nicely
              cleaned up
exit        # never reached


Again, if I leave out "crtscts" (and leave everything else the same),
everything works as expected: no hangs at all, and interrupting a read is no
problem.

I'm sorry, but I don't know which hang you fixed in October. With this
information, do you think it's the same hang?

Thanks,

Jurjen Oskam

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Stuart Henderson
On 2014-01-10, Jurjen Oskam <[hidden email]> wrote:
> Philip Guenther <guenther <at> gmail.com> writes:
>
>> Oh, you're running 5.4-stable?  I thought you were running -current
>> and was worried there was another hang in there.  I'm now 99% sure
>> you're hitting the one I fixed back in October.
>
>
> I'm sorry, but I don't know which hang you fixed in October. With this
> information, do you think it's the same hang?

It's this one, which is in 5.4-stable updated after 2013/11/12
http://ftp.openbsd.org/pub/OpenBSD/patches/5.4/common/003_vnode.patch

Reply | Threaded
Open this post in threaded view
|

Re: Cannot set stty parameters and read from /dev/ttyU0

Jurjen Oskam-3
Stuart Henderson <stu <at> spacehopper.org> writes:

>
> On 2014-01-10, Jurjen Oskam <jurjen <at> osk.am> wrote:
> > Philip Guenther <guenther <at> gmail.com> writes:
> >
> >> Oh, you're running 5.4-stable?  I thought you were running -current
> >> and was worried there was another hang in there.  I'm now 99% sure
> >> you're hitting the one I fixed back in October.
> >
> >
> > I'm sorry, but I don't know which hang you fixed in October. With this
> > information, do you think it's the same hang?
>
> It's this one, which is in 5.4-stable updated after 2013/11/12
> http://ftp.openbsd.org/pub/OpenBSD/patches/5.4/common/003_vnode.patch

Hmm, I'm running -stable as provided by M:Tier, which should have that fix
in it:

OpenBSD 5.4 (GENERIC.MP) #1: Tue Nov 12 10:57:06 CET 2013
    [hidden email]:/home/jasper/binpatchng/work-binpatch54-
amd64/src/sys/arch/amd64/compile/GENERIC.MP

# pkg_info
binpatch54-amd64-kernel-2.0 Binary Patch for 003_vnode.patch
  [...]

It seems like this is a different issue then. What can I do to investigate
this further?

Regards,

Jurjen Oskam