SMB_STRUCT_DIR *vfswrap_opendir(vfs_handle_struct *handle, connection_struct *conn, const char *fname, const char *mask, uint32 attr) { SMB_STRUCT_DIR *result; START_PROFILE(syscall_opendir); result = sys_opendir(fname); END_PROFILE(syscall_opendir); return result; }
/* * get the name of the newest ticket cache for the uid user. * pam_krb5 defines a non default ticket cache for each user */ static char * get_ticket_cache( uid_t uid ) { char *ticket_file = NULL; SMB_STRUCT_DIR *tcdir; /* directory where ticket caches are stored */ SMB_STRUCT_DIRENT *dirent; /* directory entry */ char *filename = NULL; /* holds file names on the tmp directory */ SMB_STRUCT_STAT buf; char user_cache_prefix[CC_MAX_FILE_LEN]; char file_path[CC_MAX_FILE_PATH_LEN]; time_t t = 0; snprintf(user_cache_prefix, CC_MAX_FILE_LEN, "%s%d", CC_PREFIX, uid ); tcdir = sys_opendir( TICKET_CC_DIR ); if ( tcdir == NULL ) return NULL; while ( (dirent = sys_readdir( tcdir ) ) ) { filename = dirent->d_name; snprintf(file_path, CC_MAX_FILE_PATH_LEN,"%s/%s", TICKET_CC_DIR, filename); if (sys_stat(file_path, &buf) == 0 ) { if ( ( buf.st_uid == uid ) && ( S_ISREG(buf.st_mode) ) ) { /* * check the user id of the file to prevent denial of * service attacks by creating fake ticket caches for the * user */ if ( strstr( filename, user_cache_prefix ) ) { if ( buf.st_mtime > t ) { /* * a newer ticket cache found */ free(ticket_file); ticket_file=SMB_STRDUP(file_path); t = buf.st_mtime; } } } } } sys_closedir(tcdir); if ( ticket_file == NULL ) { /* no ticket cache found */ fprintf(stderr, "ERROR: No ticket cache found for userid=%d\n", uid); return NULL; } return ticket_file; }
int main(void) { put_str("I am kernel\n"); init_all(); /******** 测试代码 ********/ printf("/dir1 content before delete /dir1/subdir1:\n"); struct dir* dir = sys_opendir("/dir1/"); char* type = NULL; struct dir_entry* dir_e = NULL; while((dir_e = sys_readdir(dir))) { if (dir_e->f_type == FT_REGULAR) { type = "regular"; } else { type = "directory"; } printf(" %s %s\n", type, dir_e->filename); } printf("try to delete nonempty directory /dir1/subdir1\n"); if (sys_rmdir("/dir1/subdir1") == -1) { printf("sys_rmdir: /dir1/subdir1 delete fail!\n"); } printf("try to delete /dir1/subdir1/file2\n"); if (sys_rmdir("/dir1/subdir1/file2") == -1) { printf("sys_rmdir: /dir1/subdir1/file2 delete fail!\n"); } if (sys_unlink("/dir1/subdir1/file2") == 0 ) { printf("sys_unlink: /dir1/subdir1/file2 delete done\n"); } printf("try to delete directory /dir1/subdir1 again\n"); if (sys_rmdir("/dir1/subdir1") == 0) { printf("/dir1/subdir1 delete done!\n"); } printf("/dir1 content after delete /dir1/subdir1:\n"); sys_rewinddir(dir); while((dir_e = sys_readdir(dir))) { if (dir_e->f_type == FT_REGULAR) { type = "regular"; } else { type = "directory"; } printf(" %s %s\n", type, dir_e->filename); } /******** 测试代码 ********/ while(1); return 0; }
int prociter(int (*proch)(pid_t pid, pid_t ppid, char *tmpname, void *data), void *data) { char *name = NULL; DIR *d = NULL; struct dirent *de = NULL; struct dirent scratch[2] = { { 0, }, }; pid_t pid = -1; pid_t ppid = -1; int ret = 0; d = sys_opendir(PROC); if (!d) return -1; for (;;) { errno = 0; de = sys_readdir(d, scratch); if (!de || errno != 0) break; if (gf_string2int(de->d_name, &pid) != -1 && pid >= 0) { ppid = pidinfo(pid, &name); switch (ppid) { case -1: continue; case -2: break; } ret = proch(pid, ppid, name, data); GF_FREE(name); if (ret) break; } } sys_closedir(d); if (!de && errno) { fprintf(stderr, "failed to traverse " PROC " (%s)\n", strerror(errno)); ret = -1; } return ret; }
static void atalk_rrmdir(TALLOC_CTX *ctx, char *path) { char *dpath; SMB_STRUCT_DIRENT *dent = 0; SMB_STRUCT_DIR *dir; if (!path) return; dir = sys_opendir(path); if (!dir) return; while (NULL != (dent = sys_readdir(dir))) { if (strcmp(dent->d_name, ".") == 0 || strcmp(dent->d_name, "..") == 0) continue; if (!(dpath = talloc_asprintf(ctx, "%s/%s", path, dent->d_name))) continue; atalk_unlink_file(dpath); } sys_closedir(dir); }
/* * change the current working directory to the specified path */ int chdir(const char * path) { char buf[MAX_PATH]; struct file * fp; int err; if((err = vfs_path_conv(path, buf)) !=0 ) return err; /* check if directory exits */ if((err = sys_opendir(buf, &fp)) != 0) return err; /* new fp for current work directory */ if(vfs_getcwdfp()) sys_closedir(vfs_getcwdfp()); vfs_setcwdfp(fp); /* set current work directory */ vfs_setcwd(buf); return 0; }
/* * open a directory */ void * opendir(const char * name) { char buf[MAX_PATH]; struct dir * dir; struct file * fp; int fd; int err; if((dir = malloc(sizeof(struct dir))) == NULL) return NULL; /* find empty slot for file descriptor. */ if((fd = fd_alloc(0)) < 0) { free(dir); return NULL; } if((err = vfs_path_conv(name, buf)) !=0 ) { free(dir); fd_free(fd); return NULL; } if((err = sys_opendir(buf, &fp)) != 0) { free(dir); fd_free(fd); return NULL; } set_fp(fd, fp); dir->fd = fd; return (void *)dir; }
int runner_start (runner_t *runner) { int pi[3][2] = {{-1, -1}, {-1, -1}, {-1, -1}}; int xpi[2]; int ret = 0; int errno_priv = 0; int i = 0; sigset_t set; if (runner->runerr) { errno = runner->runerr; return -1; } GF_ASSERT (runner->argv[0]); /* set up a channel to child to communicate back * possible execve(2) failures */ ret = pipe(xpi); if (ret != -1) ret = fcntl (xpi[1], F_SETFD, FD_CLOEXEC); for (i = 0; i < 3; i++) { if (runner->chfd[i] != -2) continue; ret = pipe (pi[i]); if (ret != -1) { runner->chio[i] = fdopen (pi[i][i ? 0 : 1], i ? "r" : "w"); if (!runner->chio[i]) ret = -1; } } if (ret != -1) runner->chpid = fork (); switch (runner->chpid) { case -1: errno_priv = errno; sys_close (xpi[0]); sys_close (xpi[1]); for (i = 0; i < 3; i++) { sys_close (pi[i][0]); sys_close (pi[i][1]); } errno = errno_priv; return -1; case 0: for (i = 0; i < 3; i++) sys_close (pi[i][i ? 0 : 1]); sys_close (xpi[0]); ret = 0; for (i = 0; i < 3; i++) { if (ret == -1) break; switch (runner->chfd[i]) { case -1: /* no redir */ break; case -2: /* redir to pipe */ ret = dup2 (pi[i][i ? 1 : 0], i); break; default: /* redir to file */ ret = dup2 (runner->chfd[i], i); } } if (ret != -1 ) { #ifdef GF_LINUX_HOST_OS DIR *d = NULL; struct dirent *de = NULL; char *e = NULL; d = sys_opendir ("/proc/self/fd"); if (d) { while ((de = sys_readdir (d))) { i = strtoul (de->d_name, &e, 10); if (*e == '\0' && i > 2 && i != dirfd (d) && i != xpi[1]) sys_close (i); } sys_closedir (d); } else ret = -1; #else /* !GF_LINUX_HOST_OS */ struct rlimit rl; ret = getrlimit (RLIMIT_NOFILE, &rl); GF_ASSERT (ret == 0); for (i = 3; i < rl.rlim_cur; i++) { if (i != xpi[1]) sys_close (i); } #endif /* !GF_LINUX_HOST_OS */ } if (ret != -1) { /* save child from inheriting our singal handling */ sigemptyset (&set); sigprocmask (SIG_SETMASK, &set, NULL); execvp (runner->argv[0], runner->argv); } ret = sys_write (xpi[1], &errno, sizeof (errno)); _exit (1); } errno_priv = errno; for (i = 0; i < 3; i++) sys_close (pi[i][i ? 1 : 0]); sys_close (xpi[1]); if (ret == -1) { for (i = 0; i < 3; i++) { if (runner->chio[i]) { fclose (runner->chio[i]); runner->chio[i] = NULL; } } } else { ret = sys_read (xpi[0], (char *)&errno_priv, sizeof (errno_priv)); sys_close (xpi[0]); if (ret <= 0) return 0; GF_ASSERT (ret == sizeof (errno_priv)); } errno = errno_priv; return -1; }
int xworker_do_crawl (struct xwork *xwork, struct dirjob *job) { DIR *dirp = NULL; int ret = -1; int boff; int plen; struct dirent *result; char dbuf[512]; char *path = NULL; struct dirjob *cjob = NULL; struct stat statbuf = {0,}; char gfid_path[4096] = {0,}; plen = strlen (job->dirname) + 256 + 2; path = alloca (plen); tdbg ("Entering: %s\n", job->dirname); dirp = sys_opendir (job->dirname); if (!dirp) { terr ("opendir failed on %s (%s)\n", job->dirname, strerror (errno)); goto out; } boff = sprintf (path, "%s/", job->dirname); for (;;) { ret = readdir_r (dirp, (struct dirent *)dbuf, &result); if (ret) { err ("readdir_r(%s): %s\n", job->dirname, strerror (errno)); goto out; } if (!result) /* EOF */ break; if (result->d_ino == 0) continue; if (skip_name (job->dirname, result->d_name)) continue; /* It is sure that, children and grandchildren of .glusterfs * are directories, just add them to global queue. */ if (skip_stat (job, result->d_name)) { strncpy (path + boff, result->d_name, (plen-boff)); cjob = dirjob_new (path, job); if (!cjob) { err ("dirjob_new(%s): %s\n", path, strerror (errno)); ret = -1; goto out; } xwork_addcrawl (xwork, cjob); continue; } strcpy (gfid_path, slavemnt); strcat (gfid_path, "/.gfid/"); strcat (gfid_path, result->d_name); ret = lstat (gfid_path, &statbuf); if (ret && errno == ENOENT) { out ("%s\n", result->d_name); BUMP (skipped_gfids); } if (ret && errno != ENOENT) { err ("stat on slave failed(%s): %s\n", gfid_path, strerror (errno)); goto out; } } ret = 0; out: if (dirp) sys_closedir (dirp); return ret; }
static int findpty(char **slave) { int master = -1; char *line = NULL; SMB_STRUCT_DIR *dirp = NULL; const char *dpname; *slave = NULL; #if defined(HAVE_GRANTPT) /* Try to open /dev/ptmx. If that fails, fall through to old method. */ if ((master = sys_open("/dev/ptmx", O_RDWR, 0)) >= 0) { grantpt(master); unlockpt(master); line = (char *)ptsname(master); if (line) { *slave = SMB_STRDUP(line); } if (*slave == NULL) { DEBUG(0, ("findpty: Unable to create master/slave pty pair.\n")); /* Stop fd leak on error. */ close(master); return -1; } else { DEBUG(10, ("findpty: Allocated slave pty %s\n", *slave)); return (master); } } #endif /* HAVE_GRANTPT */ line = SMB_STRDUP("/dev/ptyXX"); if (!line) { return (-1); } dirp = sys_opendir("/dev"); if (!dirp) { SAFE_FREE(line); return (-1); } while ((dpname = readdirname(dirp)) != NULL) { if (strncmp(dpname, "pty", 3) == 0 && strlen(dpname) == 5) { DEBUG(3, ("pty: try to open %s, line was %s\n", dpname, line)); line[8] = dpname[3]; line[9] = dpname[4]; if ((master = sys_open(line, O_RDWR, 0)) >= 0) { DEBUG(3, ("pty: opened %s\n", line)); line[5] = 't'; *slave = line; sys_closedir(dirp); return (master); } } } sys_closedir(dirp); SAFE_FREE(line); return (-1); }