C++ (Cpp) stat Examples

C++ (Cpp) stat - 30 examples found. These are the top rated real world C++ (Cpp) examples of stat extracted from open source projects. You can rate examples to help us improve the quality of examples.
Example #1
0
static void extract_user_namespace(pid_t pid) {
    // test user namespaces available in the kernel
    struct stat s1;
    struct stat s2;
    struct stat s3;
    if (stat("/proc/self/ns/user", &s1) == 0 &&
            stat("/proc/self/uid_map", &s2) == 0 &&
            stat("/proc/self/gid_map", &s3) == 0);
    else
        return;

    // read uid map
    char *uidmap;
    if (asprintf(&uidmap, "/proc/%u/uid_map", pid) == -1)
        errExit("asprintf");
    FILE *fp = fopen(uidmap, "r");
    if (!fp) {
        free(uidmap);
        return;
    }

    // check uid map
    int u1;
    int u2;
    if (fscanf(fp, "%d %d", &u1, &u2) == 2) {
        if (arg_debug)
            printf("User namespace detected: %s, %d, %d\n", uidmap, u1, u2);
        if (u1 != 0 || u2 != 0)
            arg_noroot = 1;
    }
    fclose(fp);
    free(uidmap);
}
Example #2
0
struct ref *fetch_pack(struct fetch_pack_args *my_args,
                       int fd[], struct child_process *conn,
                       const struct ref *ref,
                       const char *dest,
                       int nr_heads,
                       char **heads,
                       char **pack_lockfile)
{
    struct stat st;
    struct ref *ref_cpy;

    fetch_pack_setup();
    memcpy(&args, my_args, sizeof(args));
    if (args.depth > 0) {
        if (stat(git_path("shallow"), &st))
            st.st_mtime = 0;
    }

    if (heads && nr_heads)
        nr_heads = remove_duplicates(nr_heads, heads);
    if (!ref) {
        packet_flush(fd[1]);
        die("no matching remote head");
    }
    ref_cpy = do_fetch_pack(fd, ref, nr_heads, heads, pack_lockfile);

    if (args.depth > 0) {
        struct cache_time mtime;
        char *shallow = git_path("shallow");
        int fd;

        mtime.sec = st.st_mtime;
#ifdef USE_NSEC
        mtime.usec = st.st_mtim.usec;
#endif
        if (stat(shallow, &st)) {
            if (mtime.sec)
                die("shallow file was removed during fetch");
        } else if (st.st_mtime != mtime.sec
#ifdef USE_NSEC
                   || st.st_mtim.usec != mtime.usec
#endif
                  )
            die("shallow file was changed during fetch");

        fd = hold_lock_file_for_update(&lock, shallow, 1);
        if (!write_shallow_commits(fd, 0)) {
            unlink(shallow);
            rollback_lock_file(&lock);
        } else {
            commit_lock_file(&lock);
        }
    }

    return ref_cpy;
}
void
gkrellm_sys_proc_read_users(void)
{
    gint		n_users;
    struct stat	sb, s;
    gchar		ttybuf[MAXPATHLEN];
#ifdef _PATH_UTMP
    FILE		*ut;
    struct utmp	utmp;
#define UTMP_FILE	_PATH_UTMP
#else
    struct utmpx	*utmp;
#define UTMP_FILE	"/var/run/utx.active"
#endif
    static time_t	utmp_mtime;

    if (stat(UTMP_FILE, &s) != 0 || s.st_mtime == utmp_mtime)
        return;

#ifdef _PATH_UTMP
    if ((ut = fopen(_PATH_UTMP, "r")) != NULL)
    {
        n_users = 0;
        while (fread(&utmp, sizeof(utmp), 1, ut))
        {
            if (utmp.ut_name[0] == '\0')
                continue;
            (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s",
                           _PATH_DEV, utmp.ut_line);
            if (stat(ttybuf, &sb))
                continue;	/* corrupted record */
            ++n_users;
        }
        (void)fclose(ut);
        gkrellm_proc_assign_users(n_users);
    }
#else
    setutxent();
    n_users = 0;
    while ((utmp = getutxent()) != NULL)
    {
        if (utmp->ut_type != USER_PROCESS)
            continue;
        (void)snprintf(ttybuf, sizeof(ttybuf), "%s/%s",
                       _PATH_DEV, utmp->ut_line);
        if (stat(ttybuf, &sb))
            continue;		/* corrupted record */
        ++n_users;
    }
    endutxent();
    gkrellm_proc_assign_users(n_users);
#endif

    utmp_mtime = s.st_mtime;
}
Example #4
0
static void *scan_thread(void *unused)
{
    struct stat st;
    DIR *dir;
    struct dirent *de;
    char fn[256];
    int res;
    time_t last = 0, next = 0, now;
    for(;;) {
        /* Wait a sec */
        sleep(1);
        time(&now);
        if (!stat(qdir, &st)) {
            if ((st.st_mtime != last) || (next && (now > next))) {
#if 0
                printf("atime: %ld, mtime: %ld, ctime: %ld\n", st.st_atime, st.st_mtime, st.st_ctime);
                printf("Ooh, something changed / timeout\n");
#endif
                next = 0;
                last = st.st_mtime;
                dir = opendir(qdir);
                if (dir) {
                    while((de = readdir(dir))) {
                        snprintf(fn, sizeof(fn), "%s/%s", qdir, de->d_name);
                        if (!stat(fn, &st)) {
                            if (S_ISREG(st.st_mode)) {
                                if (st.st_mtime <= now) {
                                    res = scan_service(fn, now, st.st_atime);
                                    if (res > 0) {
                                        /* Update next service time */
                                        if (!next || (res < next)) {
                                            next = res;
                                        }
                                    } else if (res)
                                        ast_log(LOG_WARNING, "Failed to scan service '%s'\n", fn);
                                } else {
                                    /* Update "next" update if necessary */
                                    if (!next || (st.st_mtime < next))
                                        next = st.st_mtime;
                                }
                            }
                        } else
                            ast_log(LOG_WARNING, "Unable to stat %s: %s\n", fn, strerror(errno));
                    }
                    closedir(dir);
                } else
                    ast_log(LOG_WARNING, "Unable to open directory %s: %s\n", qdir, strerror(errno));
            }
        } else
            ast_log(LOG_WARNING, "Unable to stat %s\n", qdir);
    }
    return NULL;
}
Example #5
0
/* When /dev/fd isn't mounted it is often a static directory populated
 * with 0 1 2 or entries for 0 .. 63 on FreeBSD, NetBSD and OpenBSD.
 * NetBSD and OpenBSD have a /proc fs available (though not necessarily
 * mounted) and do not have fdescfs for /dev/fd.  MacOS X has a devfs
 * that properly supports /dev/fd.
 */
static int
_is_fdescfs_mounted_on_dev_fd(void)
{
    struct stat dev_stat;
    struct stat dev_fd_stat;
    if (stat("/dev", &dev_stat) != 0)
        return 0;
    if (stat(FD_DIR, &dev_fd_stat) != 0)
        return 0;
    if (dev_stat.st_dev == dev_fd_stat.st_dev)
        return 0;  /* / == /dev == /dev/fd means it is static. #fail */
    return 1;
}
/**
 * set_lmb_state
 *
 * @param lmb lmb to set the state for
 * @param state 1 = online, 0 = offline
 * @returns 0 on success, !0 otherwise
 */
static int
set_lmb_state(struct dr_node *lmb, int state)
{
    struct mem_scn *scn;
    int rc = 0;
    struct stat sbuf;

    say(INFO, "Attempting to %s %s.\n", state_strs[state], lmb->drc_name);

    if (state == ONLINE) {
        rc = probe_lmb(lmb);
        if (rc)
            return rc;
    }

    for (scn = lmb->lmb_mem_scns; scn; scn = scn->next) {
        if (stat(scn->sysfs_path, &sbuf))
            continue;

        rc = set_mem_scn_state(scn, state);
        if (rc)
            break;
    }

    if (rc) {
        /* Revert state of any memory sections of this lmb to their
         * original state
         */
        int new_state = (state == OFFLINE) ? ONLINE : OFFLINE;

        for (scn = lmb->lmb_mem_scns; scn; scn = scn->next) {
            if (stat(scn->sysfs_path, &sbuf))
                continue;

            if (get_mem_scn_state(scn) == state)
                set_mem_scn_state(scn, new_state);
        }
    }

    if (rc) {
        if (rc == EAGAIN)
            say(INFO, "Could not %s %s at this time.\n",
                state_strs[state], lmb->drc_name);
        else
            report_unknown_error(__FILE__, __LINE__);

    } else
        say(INFO, "%s is %s.\n", lmb->drc_name, state_strs[state]);

    return rc;
}