Unix : Epoll, catch ctrl+d and ctrl+c in server
Asked Answered
E

1

0

I use epoll to build a server, this is the code where I init epoll :

core->fd_epoll = epoll_create(LIMIT_CLIENT);
ev.events = EPOLLIN | EPOLLPRI | EPOLLERR | EPOLLHUP;
ev.data.fd = core->socket_main;
epoll_ctl(core->fd_epoll, EPOLL_CTL_ADD, core->socket_main, &ev);
while (1)
{
  nfds = epoll_wait(core->fd_epoll, &ev, 90000, -1);
  ...
}

And when I use it to check if there's something new on my fds :

for (i = 0; i < nfds; i++)
{
  fd = ev[i].data.fd;
  if (fd == core->socket_main)
    {
      socket_fils = socket_accept(core->socket_main, 0);
      event.data.fd = socket_fils;
      event.events = EPOLLIN | EPOLLET | EPOLLRDHUP;
      xepoll_ctl(core->fd_epoll, EPOLL_CTL_ADD, socket_fils, &event);
      printf("Incoming => FD fils %d\n", socket_fils);
    }
  else
    printf("Event %x\n", ev[i].events);
}

When I use netcat to send a message to the server the bitfield events is equal to 1 (EPOLLIN) When I send a ctrl+c, netcat quits and my bitfield is equal to 2001 (EPOLLIN and EPOLLRDHUP) When I send a ctrl+d, netcat doesn't quit but my bitfield is equal to 2001 too...

After a ctrl+d, my server closes the socket. It's not normal... A ctrl+d should'nt close the socket and return a different bitfield.

How can I know, in the server, if it's ctrl+c or ctrl+d ?

Thank you.

Epistemic answered 11/6, 2012 at 14:10 Comment(0)
V
3

ctrl+c and ctrl+d keypresses on the terminal that is running netcat cannot be "seen" directly by your server. They cause, respectively, a SIGINT signal to be sent to netcat, and an EOF condition to be seen by netcat on its stdin. What netcat does with that is really up to netcat, not up to your server. Here's what they do for me:

  • ctrl+c which sends SIGINT to netcat: netcat is killed because that is the default action of SIGINT, and netcat doesn't change it. When netcat dies the socket is automatically closed. The server senses this as available incoming data, consistent with the EPOLLIN|EPOLLRDHUP condition you are seeing. If you read the socket, you will find that an EOF is waiting for you.

  • ctrl+d which sends an EOF on netcat's stdin: netcat noticed the EOF. It will send no further data through the socket. However, it continues running and reading from the socket in case the server has more data to send.

In other words, I can't reproduce the netcat behaviour you are seeing (with Linux 2.6 and netcat v1.10-38). Perhaps your version of netcat shuts down the socket for writing after reading an EOF on stdin?

Villeinage answered 11/6, 2012 at 16:39 Comment(4)
I don't know how to see the version of netcat (no --version option) But I see in the man : > The connection may be terminated using an EOF (‘^D’). So, this behaviour is normal ? The difference between ctrl+c and ctrl+d is that netcat wait for incoming data when a ctrl+d occurs and close the socket as ctrl+c ?Epistemic
Looks like that's the case, yes.Villeinage
You can use telnet instead of netcat. It sends ^D (0x04) character for Ctrl-D.Euchology
@VasilyRedkin using telnet is a hack, not really the right solution if you are not using the actual TELNET protocol. If you want to send literal characters through a connection with nc (or pretty much anything else), just quote the special characters with ^v or reconfigure your terminal with stty.Villeinage

© 2022 - 2024 — McMap. All rights reserved.