scanf failling with ICANON mode when in daemon mode (screen -dmS)
"Presseau, Michael" <[email protected]> Fri, 25 Apr 2025 18:29:02 +0000
| Newsgroups | gmane.comp.gnu.screen.user |
|---|---|
| Message-ID | <YT2P288MB0002BE8485972CB3795BED44C8842@YT2P288MB0002.CANP288.PROD.OUTLOOK.COM> |
--_000_YT2P288MB0002BE8485972CB3795BED44C8842YT2P288MB0002CANP_
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
Hi,
I have done my own application console with scanf(). When I run in the scre=
en (screen -mS test ./test), it work as expected with each characters print=
ed. But when I run it in screen as daemon, it's failing in loop:
$ screen -dmS test ./test
$ screen -r test
stdin_thread scanf failed! ret: -1
stdin_thread scanf failed! ret: -1
Do you have any clue on how I can make this work in deamon mode?
Here is my sample code:
```
#include <stdio.h>
#include <errno.h>
#include <termios.h>
#include <unistd.h>
void enableRawMode()
{
#ifdef __linux__
struct termios raw;
// Get current terminal attributes
tcgetattr(STDIN_FILENO, &raw);
// Modify terminal attributes for raw mode
raw.c_lflag &=3D ~(ECHO | ICANON);
// Set modified attributes
tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw);
#endif
}
int main() {
enableRawMode();
char c;
while (1) {
int ret =3D scanf("%c", &c);
if ( ret > 0) {
printf("%c", c);
} else {
printf("stdin_thread scanf f=
ailed! ret: %d\n", ret);
}
}
return 0;
}
```
Michael
--_000_YT2P288MB0002BE8485972CB3795BED44C8842YT2P288MB0002CANP_
Content-Type: text/html; charset="us-ascii"
Content-Transfer-Encoding: quoted-printable
<html xmlns:v=3D"urn:schemas-microsoft-com:vml" xmlns:o=3D"urn:schemas-micr=
osoft-com:office:office" xmlns:w=3D"urn:schemas-microsoft-com:office:word" =
xmlns:m=3D"http://schemas.microsoft.com/office/2004/12/omml" xmlns=3D"http:=
//www.w3.org/TR/REC-html40">
<head>
<meta http-equiv=3D"Content-Type" content=3D"text/html; charset=3Dus-ascii"=
>
<meta name=3D"Generator" content=3D"Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
{font-family:"Cambria Math";
panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0cm;
font-size:11.0pt;
font-family:"Calibri",sans-serif;
mso-ligatures:standardcontextual;
mso-fareast-language:EN-US;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Calibri",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:11.0pt;
mso-fareast-language:EN-US;}
@page WordSection1
{size:612.0pt 792.0pt;
margin:72.0pt 90.0pt 72.0pt 90.0pt;}
div.WordSection1
{page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext=3D"edit" spidmax=3D"1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext=3D"edit">
<o:idmap v:ext=3D"edit" data=3D"1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang=3D"FR-CA" link=3D"#0563C1" vlink=3D"#954F72" style=3D"word-wrap:=
break-word">
<div class=3D"WordSection1">
<p class=3D"MsoNormal"><span lang=3D"EN-CA">Hi, <o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">I have done my own application =
console with scanf(). When I run in the screen (screen -mS test ./test), it=
work as expected with each characters printed. But when I run it in screen=
as daemon, it's failing in loop:<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">$ screen -dmS test ./test<o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">$ screen -r test<o:p></o:p></sp=
an></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">stdin_thread scanf failed! ret:=
-1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">stdin_thread scanf failed! ret:=
-1<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">Do you have any clue on how I c=
an make this work in deamon mode?<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">Here is my sample code:<o:p></o=
:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">```<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">#include <stdio.h><o:p></=
o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">#include <errno.h><o:p></=
o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">#include <termios.h><o:p>=
</o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">#include <unistd.h><o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">void enableRawMode()<o:p></o:p>=
</span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">{<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">#ifdef __linux__<o:p></o:p></sp=
an></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; struct termios =
raw;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; // Get current =
terminal attributes<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; tcgetattr(STDIN=
_FILENO, &raw);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; // Modify termi=
nal attributes for raw mode<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; raw.c_lflag &am=
p;=3D ~(ECHO | ICANON);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; // Set modified=
attributes<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; tcsetattr(STDIN=
_FILENO, TCSAFLUSH, &raw);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">#endif<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">}<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">int main() {<o:p></o:p></span><=
/p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; enableRawMode()=
;<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; <o:p></o:p></sp=
an></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; char c;<o:p></o=
:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; while (1) {<o:p=
></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; &nbs=
p; =
</span>int ret =3D scanf("%c", &c);<o:p></o:p></p>
<p class=3D"MsoNormal"> &nbs=
p; &=
nbsp; <span lang=3D"EN-CA">=
if ( ret > 0) {<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; &nbs=
p; &=
nbsp; &nbs=
p; printf("%c", c);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; &nbs=
p; =
} else {<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; &nbs=
p; &=
nbsp; &nbs=
p; printf("stdin_thread scanf failed! ret: %d\n"=
;, ret);<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; &nbs=
p; =
}<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; }<o:p></o:p></s=
pan></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"> &=
nbsp; return 0;<o:p><=
/o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">}<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">```<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA">Michael<o:p></o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
<p class=3D"MsoNormal"><span lang=3D"EN-CA"><o:p> </o:p></span></p>
</div>
</body>
</html>
--_000_YT2P288MB0002BE8485972CB3795BED44C8842YT2P288MB0002CANP_--