bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: "Bastien Roucariès" <roucaries.bastien@gmail.com>
To: "bug-gnulib@gnu.org" <bug-gnulib@gnu.org>,
	Erik Blake <eblake@redhat.com>,
	mtk.manpages@gmail.com
Subject: Portabilty of poll trick
Date: Tue, 27 Apr 2021 07:14:53 +0000	[thread overview]
Message-ID: <2051587.bS8k0qy9eM@portable-bastien> (raw)

[-- Attachment #1: Type: text/plain, Size: 1840 bytes --]

Hi,

I want to assess the safety and portability of the following trick, for getting outside poll. Replacing by using dup2 a read poll object by a writtable one. I think to use this for stopping polling in multithread program
It work really well, and could for instance allow me to tear down eventfd of other event like file that does not support shutdown

CC also   Michael Kerrisk (mtk.manpages@gmail.com) for comments on this trick

#include <errno.h>
#include <poll.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

static void *run_poll (void *arg)
{
    struct pollfd   fds[1];
    int             rc;

    fds[0].fd = ((int *)arg)[0];
    fds[0].events = POLLERR | POLLHUP | POLLNVAL | POLLIN;
 
            //
            // Poll indefinitely
            //

    printf("starting poll\n");
    fflush(stdout);
    rc = poll((struct pollfd *) &fds, 1, -1);

    if ( rc == -1 )
    {
            printf("POLL returned error %d: %s\n", errno, strerror(errno));
    }
    else
    {
            printf("POLL returned %d (revents = 0x%08x): %s %s %s %s %s\n",
                   rc,
                   fds[0].revents,
		   ( ( fds[0].revents & POLLIN  ) ? "pollin" : "noin" ),
		   ( ( fds[0].revents & POLLOUT  ) ? "pollout" : "noout" ),
                   ( ( fds[0].revents & POLLERR  ) ? "pollerr" : "noerr" ),
                   ( ( fds[0].revents & POLLHUP  ) ? "pollhup" : "nohup" ),
                   ( ( fds[0].revents & POLLNVAL ) ? "pollnval" : "nonval" ));
    }

    return  NULL;
}

int main (int argc, char **argv)
{
    pthread_t       thread;
    int             rc;

    int fdst[2];
    pipe(fdst);
    rc = pthread_create(&thread, NULL, run_poll, &fdst);

    sleep(3);
    printf("removing access\n");
    dup2(fdst[0],fdst[1]);
    sleep(3);

    return  0;
}

[-- Attachment #2: This is a digitally signed message part. --]
[-- Type: application/pgp-signature, Size: 833 bytes --]

             reply	other threads:[~2021-04-27  7:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27  7:14 Bastien Roucariès [this message]
2021-04-27 22:40 ` Portabilty of poll trick Bruno Haible
2021-04-27 22:46   ` Bastien ROUCARIES
2021-04-27 22:51     ` Ben Pfaff
2021-04-28 15:43       ` Bastien ROUCARIES
2021-05-14 10:43     ` Bruno Haible

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2051587.bS8k0qy9eM@portable-bastien \
    --to=roucaries.bastien@gmail.com \
    --cc=bug-gnulib@gnu.org \
    --cc=eblake@redhat.com \
    --cc=mtk.manpages@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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 read-only IMAP folder(s) and NNTP newsgroup(s).