kernelnewbies.kernelnewbies.org archive mirror
 help / color / mirror / Atom feed
* undefined reference to `ioctl_tty'
@ 2019-03-06  6:58 Jeffrey Walton
       [not found] ` <CAJ1xhMXLdhsj4R8MmbR1WkbO7wBV+gJdeJW+XH+7ONV3WkNHNw@mail.gmail.com>
  2019-03-06  8:36 ` valdis.kletnieks
  0 siblings, 2 replies; 6+ messages in thread
From: Jeffrey Walton @ 2019-03-06  6:58 UTC (permalink / raw)
  To: kernelnewbies

Hi Everyone,

I'm working from http://man7.org/linux/man-pages/man4/tty_ioctl.4.html
. According to the man page:

       TIOCEXCL  void
              Put the terminal into exclusive mode.  No further open(2)
              operations on the terminal are permitted.  (They fail with
              EBUSY, except for a process with the CAP_SYS_ADMIN
              capability.)

The page goes on to say in the colophon it is part of the 4.16 kernel.
I am running on the 4.20 kernel:

$ uname -a
Linux silo 4.20.13-200.fc29.x86_64 #1 ... GNU/Linux

However:

gcc -D_GNU_SOURCE -g2 -std=gnu99 test.c -o tttt -lrt
test.c: In function ‘main’:
test.c:11:7: warning: implicit declaration of function ‘ioctl_tty’;
did you mean ‘ioctl’? [-Wimplicit-function-declaration]
   if (ioctl_tty(1 /*STDOUT_FILENO*/, TIOCEXCL, NULL) == -1) {
       ^~~~~~~~~
       ioctl
/usr/bin/ld: /tmp/ccCNNyG3.o: in function `main':
/home/test/test.c:11: undefined reference to `ioctl_tty'
collect2: error: ld returned 1 exit status

Search is producing a lot of non-relevant hits.

Any ideas why I can't find ioctl_tty during link when using a 4.20 kernel?

Thanks in advance.

----------

$ cat test.c
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
#include <fcntl.h>
#include <sys/ioctl.h>

int main(int argc, char* argv[])
{
  if (ioctl_tty(1 /*STDOUT_FILENO*/, TIOCEXCL, NULL) == -1) {
    fprintf(stderr, "%s\n", strerror(errno));
    return 1;
  }
  return 0;
}

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Fwd: undefined reference to `ioctl_tty'
       [not found] ` <CAJ1xhMXLdhsj4R8MmbR1WkbO7wBV+gJdeJW+XH+7ONV3WkNHNw@mail.gmail.com>
@ 2019-03-06  7:31   ` Alexander Kapshuk
  2019-03-06  7:41     ` Jeffrey Walton
  0 siblings, 1 reply; 6+ messages in thread
From: Alexander Kapshuk @ 2019-03-06  7:31 UTC (permalink / raw)
  To: kernelnewbies

---------- Forwarded message ---------
From: Alexander Kapshuk <alexander.kapshuk@gmail.com>
Date: Wed, Mar 6, 2019 at 9:30 AM
Subject: Re: undefined reference to `ioctl_tty'
To: <noloader@gmail.com>


On Wed, Mar 6, 2019 at 9:00 AM Jeffrey Walton <noloader@gmail.com> wrote:
>
> Hi Everyone,
>
> I'm working from http://man7.org/linux/man-pages/man4/tty_ioctl.4.html
> . According to the man page:
>
>        TIOCEXCL  void
>               Put the terminal into exclusive mode.  No further open(2)
>               operations on the terminal are permitted.  (They fail with
>               EBUSY, except for a process with the CAP_SYS_ADMIN
>               capability.)
>
> The page goes on to say in the colophon it is part of the 4.16 kernel.
> I am running on the 4.20 kernel:
>
> $ uname -a
> Linux silo 4.20.13-200.fc29.x86_64 #1 ... GNU/Linux
>
> However:
>
> gcc -D_GNU_SOURCE -g2 -std=gnu99 test.c -o tttt -lrt
> test.c: In function ‘main’:
> test.c:11:7: warning: implicit declaration of function ‘ioctl_tty’;
> did you mean ‘ioctl’? [-Wimplicit-function-declaration]
>    if (ioctl_tty(1 /*STDOUT_FILENO*/, TIOCEXCL, NULL) == -1) {
>        ^~~~~~~~~
>        ioctl
> /usr/bin/ld: /tmp/ccCNNyG3.o: in function `main':
> /home/test/test.c:11: undefined reference to `ioctl_tty'
> collect2: error: ld returned 1 exit status
>
> Search is producing a lot of non-relevant hits.
>
> Any ideas why I can't find ioctl_tty during link when using a 4.20 kernel?
>
> Thanks in advance.
>
> ----------
>
> $ cat test.c
> #include <stdio.h>
> #include <string.h>
> #include <errno.h>
> #include <termios.h>
> #include <fcntl.h>
> #include <sys/ioctl.h>
>
> int main(int argc, char* argv[])
> {
>   if (ioctl_tty(1 /*STDOUT_FILENO*/, TIOCEXCL, NULL) == -1) {
>     fprintf(stderr, "%s\n", strerror(errno));
>     return 1;
>   }
>   return 0;
> }
>
> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies@kernelnewbies.org
> https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

While the man page does refer to this function as ioctl_tty, the
actual signature, as shown in the man page, is:
int ioctl(int fd, int cmd, ...);

Not ioctl_tty.

The example section of the man pages gives this usage example:

fd = open("/dev/ttyS0", O_RDONLY);
           ioctl(fd, TIOCMGET, &serial);

Hope this helps.

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `ioctl_tty'
  2019-03-06  7:31   ` Fwd: " Alexander Kapshuk
@ 2019-03-06  7:41     ` Jeffrey Walton
  0 siblings, 0 replies; 6+ messages in thread
From: Jeffrey Walton @ 2019-03-06  7:41 UTC (permalink / raw)
  To: Alexander Kapshuk; +Cc: kernelnewbies

On Wed, Mar 6, 2019 at 2:32 AM Alexander Kapshuk
<alexander.kapshuk@gmail.com> wrote:
>
> ---------- Forwarded message ---------
> From: Alexander Kapshuk <alexander.kapshuk@gmail.com>
> Date: Wed, Mar 6, 2019 at 9:30 AM
> Subject: Re: undefined reference to `ioctl_tty'
> To: <noloader@gmail.com>
> ...
> > $ cat test.c
> > #include <stdio.h>
> > #include <string.h>
> > #include <errno.h>
> > #include <termios.h>
> > #include <fcntl.h>
> > #include <sys/ioctl.h>
> >
> > int main(int argc, char* argv[])
> > {
> >   if (ioctl_tty(1 /*STDOUT_FILENO*/, TIOCEXCL, NULL) == -1) {
> >     fprintf(stderr, "%s\n", strerror(errno));
> >     return 1;
> >   }
> >   return 0;
> > }
>
> While the man page does refer to this function as ioctl_tty, the
> actual signature, as shown in the man page, is:
> int ioctl(int fd, int cmd, ...);
>
> Not ioctl_tty.

Derp, thanks.

Jeff

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `ioctl_tty'
  2019-03-06  6:58 undefined reference to `ioctl_tty' Jeffrey Walton
       [not found] ` <CAJ1xhMXLdhsj4R8MmbR1WkbO7wBV+gJdeJW+XH+7ONV3WkNHNw@mail.gmail.com>
@ 2019-03-06  8:36 ` valdis.kletnieks
  2019-03-06  8:50   ` Jeffrey Walton
  1 sibling, 1 reply; 6+ messages in thread
From: valdis.kletnieks @ 2019-03-06  8:36 UTC (permalink / raw)
  To: noloader; +Cc: kernelnewbies

On Wed, 06 Mar 2019 01:58:41 -0500, Jeffrey Walton said:

>        TIOCEXCL  void
>               Put the terminal into exclusive mode.  No further open(2)
>               operations on the terminal are permitted.  (They fail with
>               EBUSY, except for a process with the CAP_SYS_ADMIN
>               capability.)

Note that this is still slightly racy - if two processes both try to do an open
and an ioctl, this can happen:

Process 1	open()
		Process 2 open()
Process 1 ioctl(TIOEXCL)
		Process 2 ioctl()

But at this point, the second process has already succeeded in opening the
device, so "no further opens" doesn't save you.

Usually, this sort of race would be really hard to hit, especially if the
ioctl() happens right after the open() - except that many systems will use cron
or similar to launch stuff "once per hour" or similar.  And they end up
batching together two processes that each try to do this. Suddenly, it's a lot
easier to hit that hole when cron launches two processes at 12:54:17 PM.....

If there's one thing I've learned from almost 4 decades of sysadmin'ing on all
sorts of systems, saying "Oh that timing hole is so tiny it won't ever get hit
in production" guarantees that the Clock Gods will smite you for your hubris.

:)


_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `ioctl_tty'
  2019-03-06  8:36 ` valdis.kletnieks
@ 2019-03-06  8:50   ` Jeffrey Walton
  2019-03-07  8:59     ` valdis.kletnieks
  0 siblings, 1 reply; 6+ messages in thread
From: Jeffrey Walton @ 2019-03-06  8:50 UTC (permalink / raw)
  To: Valdis Kletnieks; +Cc: kernelnewbies

On Wed, Mar 6, 2019 at 3:36 AM <valdis.kletnieks@vt.edu> wrote:
>
> On Wed, 06 Mar 2019 01:58:41 -0500, Jeffrey Walton said:
>
> >        TIOCEXCL  void
> >               Put the terminal into exclusive mode.  No further open(2)
> >               operations on the terminal are permitted.  (They fail with
> >               EBUSY, except for a process with the CAP_SYS_ADMIN
> >               capability.)
>
> Note that this is still slightly racy - if two processes both try to do an open
> and an ioctl, this can happen:
>
> Process 1       open()
>                 Process 2 open()
> Process 1 ioctl(TIOEXCL)
>                 Process 2 ioctl()
>
> But at this point, the second process has already succeeded in opening the
> device, so "no further opens" doesn't save you.
>
> Usually, this sort of race would be really hard to hit, especially if the
> ioctl() happens right after the open() - except that many systems will use cron
> or similar to launch stuff "once per hour" or similar.  And they end up
> batching together two processes that each try to do this. Suddenly, it's a lot
> easier to hit that hole when cron launches two processes at 12:54:17 PM.....
>
> If there's one thing I've learned from almost 4 decades of sysadmin'ing on all
> sorts of systems, saying "Oh that timing hole is so tiny it won't ever get hit
> in production" guarantees that the Clock Gods will smite you for your hubris.

Yeah, the race seems to be the downside to ioctl and TIOEXCL.

It is too bad Linux does not honor O_EXCL for the device, or provide a
similar open flag like SOCK_NONBLOCK was added to avoid the race in
sockets. It would avoid a lot of problems.

Jeff

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: undefined reference to `ioctl_tty'
  2019-03-06  8:50   ` Jeffrey Walton
@ 2019-03-07  8:59     ` valdis.kletnieks
  0 siblings, 0 replies; 6+ messages in thread
From: valdis.kletnieks @ 2019-03-07  8:59 UTC (permalink / raw)
  To: noloader; +Cc: kernelnewbies

On Wed, 06 Mar 2019 03:50:10 -0500, Jeffrey Walton said:

> Yeah, the race seems to be the downside to ioctl and TIOEXCL.
>
> It is too bad Linux does not honor O_EXCL for the device, or provide a
> similar open flag like SOCK_NONBLOCK was added to avoid the race in
> sockets. It would avoid a lot of problems.

-ENOPATCH :)

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@kernelnewbies.org
https://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2019-03-07  9:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-03-06  6:58 undefined reference to `ioctl_tty' Jeffrey Walton
     [not found] ` <CAJ1xhMXLdhsj4R8MmbR1WkbO7wBV+gJdeJW+XH+7ONV3WkNHNw@mail.gmail.com>
2019-03-06  7:31   ` Fwd: " Alexander Kapshuk
2019-03-06  7:41     ` Jeffrey Walton
2019-03-06  8:36 ` valdis.kletnieks
2019-03-06  8:50   ` Jeffrey Walton
2019-03-07  8:59     ` valdis.kletnieks

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).