static sys_return_t
sud_set_common (az_allocid_t allocid, const char *name,
                const char *buf, size_t len)
{
    char fname[256];
    char tmpfname[256];
    int rv = 0;
    ssize_t sz = 0;
    int fd;

    snprintf (fname, sizeof(fname)-1, "%s/%ld/%s", SUD_DIR, allocid, name);
    snprintf (tmpfname, sizeof(tmpfname)-1, "%s/%ld/%s.tmp", SUD_DIR, allocid, name);

    int flags = OPEN_CREATE_FLAGS;
    fd = MY_OPEN (tmpfname, flags, 0644);
    if (fd < 0) {
        int e = errno;
        perror ("sud open");
        syslog (LOG_ERR, "ERROR: sud open, %d", e);
        return SYSERR_NO_PRIVILEGE;
    }

    sz = fullWrite (fd, buf, len);
    if ((size_t)sz != len) {
        perror ("sud write");
        syslog (LOG_ERR, "ERROR: sud write");
        rv = MY_CLOSE (fd);
        if (rv < 0) {
            perror ("close");
            syslog (LOG_ERR, "ERROR: sud close");
        }
        return SYSERR_NO_PRIVILEGE;
    }

    rv = MY_CLOSE (fd);
    if (rv < 0) {
        perror ("sud close");
        syslog (LOG_ERR, "ERROR: sud close");
        return SYSERR_NO_PRIVILEGE;
    }

    rv = MY_RENAME (tmpfname, fname);
    if (rv < 0) {
        perror ("sud rename");
        syslog (LOG_ERR, "ERROR: sud rename");
        return SYSERR_NO_PRIVILEGE;
    }

    return SYSERR_NONE;
}
static sys_return_t
sud_get_common (az_allocid_t allocid, const char *name,
                char *buf, size_t len, uint64_t expectedRevision)
{
    char fname[256];
    int rv = 0;
    ssize_t sz = 0;
    int fd;
    int flags;

    assert (len >= 8);

    snprintf (fname, sizeof(fname)-1, "%s/%ld/%s", SUD_DIR, allocid, name);

    flags = OPEN_RDONLY_FLAGS;
    fd = MY_OPEN (fname, flags, 0);
    if (fd < 0) {
        // Handle the normal case of an invalid allocid being requested.
        if (errno == ENOENT) {
            return SYSERR_NOT_FOUND;
        }

        // All other errors are catastrophic.
        perror ("open");
        syslog (LOG_ERR, "ERROR: sud open");
        assert (0);
    }

    // Read in the revision number first.
    sz = fullRead (fd, buf, 8);
    if (sz != 8) {
        perror ("read 1");
        syslog (LOG_ERR, "ERROR: sud read 1");
        assert (0);
    }

    uint64_t actualRevision = *((uint64_t *) buf);
    if (expectedRevision != actualRevision) {
        // This is a normal condition.  Return an error and allow the 
        // caller to try again with a different expectedRevision.

        rv = MY_CLOSE (fd);
        if (rv < 0) {
            perror ("close");
            syslog (LOG_ERR, "ERROR: sud close");
        }

        return SYSERR_INVALID_STATE;
    }

    // Read the remainder of the record after the revision number.
    sz = fullRead (fd, buf+8, len-8);
    if ((size_t)sz != (len-8)) {
        perror ("read 2");
        syslog (LOG_ERR, "ERROR: sud read 2");
        assert (0);
    }

    rv = MY_CLOSE (fd);
    if (rv < 0) {
        perror ("close");
        syslog (LOG_ERR, "ERROR: sud close");
    }

    return SYSERR_NONE;
}
예제 #3
0
static inline void __azul_openup(void)
{
    if ((__azulfd = MY_OPEN(AZUL_PGROUP_DEV, O_RDWR)) < 0)
        __azul_errno = errno;
}