<html>
    <head>
      <base href="https://bugs.freedesktop.org/" />
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Priority</th>
          <td>medium
          </td>
        </tr>

        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW --- - sd_readahead retries closing an fd on EINTR"
   href="https://bugs.freedesktop.org/show_bug.cgi?id=83024">83024</a>
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>systemd-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>sd_readahead retries closing an fd on EINTR
          </td>
        </tr>

        <tr>
          <th>QA Contact</th>
          <td>systemd-bugs@lists.freedesktop.org
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>sstewartgallus00@mylangara.bc.ca
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>Other
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>unspecified
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>general
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>systemd
          </td>
        </tr></table>
      <p>
        <div>
        <pre>The code in the touch function of sd-readahead.c:

        for (;;) {
                if (close(fd) >= 0)
                        break;

                if (errno != EINTR)
                        return -errno;
        }

The ultra-paranoid ultra portable correct code:

    /*
     * The state of a file descriptor after close gives an EINTR error
     * is unspecified by POSIX so this function avoids the problem by
     * simply blocking all signals.
     */
    int errnum;
    sigset_t sigset;

    /* First use the signal set for the full set */
    sigfillset(&sigset);

    pthread_sigmask(SIG_BLOCK, &sigset, &sigset);

    /* Then reuse the signal set for the old set */

    if (-1 == close(fd)) {
        errnum = errno;
        assert(errnum != 0);
    } else {
        errnum = 0;
    }

    pthread_sigmask(SIG_SETMASK, &sigset, NULL);

    return errnum;

The system specific but mostly standard code you probably want:

    int errnum;
    if (-1 == close(fd)) {
        errnum = errno;
        assert(errnum != 0);
        /* On Linux and probably most platforms EINTR can't happen or
         * would close the file succesfully anyways. */
        if (EINTR == errnum) {
            errnum = 0;
        }
    } else {
        errnum = 0;
    }
    return errnum;</pre>
        </div>
      </p>
      <hr>
      <span>You are receiving this mail because:</span>
      
      <ul>
          <li>You are the QA Contact for the bug.</li>
          <li>You are the assignee for the bug.</li>
      </ul>
    </body>
</html>