1 2 #include <petscsys.h> 3 #include <errno.h> 4 #if defined(PETSC_HAVE_PWD_H) 5 #include <pwd.h> 6 #endif 7 #include <ctype.h> 8 #include <sys/types.h> 9 #include <sys/stat.h> 10 #if defined(PETSC_HAVE_UNISTD_H) 11 #include <unistd.h> 12 #endif 13 #if defined(PETSC_HAVE_STDLIB_H) 14 #include <stdlib.h> 15 #endif 16 #if defined(PETSC_HAVE_SYS_UTSNAME_H) 17 #include <sys/utsname.h> 18 #endif 19 #if defined(PETSC_HAVE_IO_H) 20 #include <io.h> 21 #endif 22 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H) 23 #include <sys/systeminfo.h> 24 #endif 25 26 #if defined (PETSC_HAVE__ACCESS) || defined(PETSC_HAVE_ACCESS) 27 28 #undef __FUNCT__ 29 #define __FUNCT__ "PetscTestOwnership" 30 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) 31 { 32 int m = R_OK; 33 PetscErrorCode ierr; 34 35 PetscFunctionBegin; 36 if (mode == 'r') m = R_OK; 37 else if (mode == 'w') m = W_OK; 38 else if (mode == 'x') m = X_OK; 39 else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Mode must be one of r, w, or x"); 40 #if defined(PETSC_HAVE_ACCESS) 41 if (!access(fname, m)) { 42 ierr = PetscInfo1(PETSC_NULL,"System call access() succeeded on file %s\n",fname);CHKERRQ(ierr); 43 *flg = PETSC_TRUE; 44 } else { 45 ierr = PetscInfo1(PETSC_NULL,"System call access() failed on file %s\n",fname);CHKERRQ(ierr); 46 *flg = PETSC_FALSE; 47 } 48 #else 49 if (m == X_OK) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP, "Unable to check execute permission for file %s", fname); 50 if (!_access(fname, m)) *flg = PETSC_TRUE; 51 #endif 52 PetscFunctionReturn(0); 53 } 54 55 #else /* PETSC_HAVE_ACCESS or PETSC_HAVE__ACCESS */ 56 57 #undef __FUNCT__ 58 #define __FUNCT__ "PetscTestOwnership" 59 static PetscErrorCode PetscTestOwnership(const char fname[], char mode, uid_t fuid, gid_t fgid, int fmode, PetscBool *flg) 60 { 61 uid_t uid; 62 gid_t *gid = PETSC_NULL; 63 int numGroups; 64 int rbit = S_IROTH; 65 int wbit = S_IWOTH; 66 int ebit = S_IXOTH; 67 PetscErrorCode ierr; 68 69 PetscFunctionBegin; 70 /* Get the number of supplementary group IDs */ 71 #if !defined(PETSC_MISSING_GETGROUPS) 72 numGroups = getgroups(0, gid); if (numGroups < 0) SETERRQ(PETSC_COMM_SELF,numGroups, "Unable to count supplementary group IDs"); 73 ierr = PetscMalloc((numGroups+1) * sizeof(gid_t), &gid);CHKERRQ(ierr); 74 #else 75 numGroups = 0; 76 #endif 77 78 /* Get the (effective) user and group of the caller */ 79 uid = geteuid(); 80 gid[0] = getegid(); 81 82 /* Get supplementary group IDs */ 83 #if !defined(PETSC_MISSING_GETGROUPS) 84 ierr = getgroups(numGroups, gid+1); if (ierr < 0) SETERRQ(PETSC_COMM_SELF,ierr, "Unable to obtain supplementary group IDs"); 85 #endif 86 87 /* Test for accessibility */ 88 if (fuid == uid) { 89 rbit = S_IRUSR; 90 wbit = S_IWUSR; 91 ebit = S_IXUSR; 92 } else { 93 int g; 94 95 for (g = 0; g <= numGroups; g++) { 96 if (fgid == gid[g]) { 97 rbit = S_IRGRP; 98 wbit = S_IWGRP; 99 ebit = S_IXGRP; 100 break; 101 } 102 } 103 } 104 ierr = PetscFree(gid);CHKERRQ(ierr); 105 106 if (mode == 'r') { 107 if (fmode & rbit) *flg = PETSC_TRUE; 108 } else if (mode == 'w') { 109 if (fmode & wbit) *flg = PETSC_TRUE; 110 } else if (mode == 'x') { 111 if (fmode & ebit) *flg = PETSC_TRUE; 112 } 113 PetscFunctionReturn(0); 114 } 115 116 #endif /* PETSC_HAVE_ACCESS */ 117 118 #undef __FUNCT__ 119 #define __FUNCT__ "PetscGetFileStat" 120 static PetscErrorCode PetscGetFileStat(const char fname[], uid_t *fileUid, gid_t *fileGid, int *fileMode,PetscBool *exists) 121 { 122 struct stat statbuf; 123 PetscErrorCode ierr; 124 125 PetscFunctionBegin; 126 #if defined(PETSC_HAVE_STAT_NO_CONST) 127 ierr = stat((char*) fname, &statbuf); 128 #else 129 ierr = stat(fname, &statbuf); 130 #endif 131 if (ierr) { 132 #if defined(EOVERFLOW) 133 if (errno == EOVERFLOW) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"EOVERFLOW in stat(), configure PETSc --with-large-file-io=1 to support files larger than 2GiB"); 134 #endif 135 ierr = PetscInfo1(PETSC_NULL,"System call stat() failed on file %s\n",fname);CHKERRQ(ierr); 136 *exists = PETSC_FALSE; 137 } else { 138 ierr = PetscInfo1(PETSC_NULL,"System call stat() succeeded on file %s\n",fname);CHKERRQ(ierr); 139 *exists = PETSC_TRUE; 140 *fileUid = statbuf.st_uid; 141 *fileGid = statbuf.st_gid; 142 *fileMode = statbuf.st_mode; 143 } 144 PetscFunctionReturn(0); 145 } 146 147 #undef __FUNCT__ 148 #define __FUNCT__ "PetscTestFile" 149 PetscErrorCode PetscTestFile(const char fname[], char mode, PetscBool *flg) 150 { 151 uid_t fuid; 152 gid_t fgid; 153 int fmode; 154 PetscErrorCode ierr; 155 PetscBool exists; 156 157 PetscFunctionBegin; 158 *flg = PETSC_FALSE; 159 if (!fname) PetscFunctionReturn(0); 160 161 ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr); 162 if (!exists) PetscFunctionReturn(0); 163 /* Except for systems that have this broken stat macros (rare), this is the correct way to check for a regular file */ 164 if (!S_ISREG(fmode)) PetscFunctionReturn(0); 165 166 ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr); 167 PetscFunctionReturn(0); 168 } 169 170 #undef __FUNCT__ 171 #define __FUNCT__ "PetscTestDirectory" 172 PetscErrorCode PetscTestDirectory(const char fname[],char mode,PetscBool *flg) 173 { 174 uid_t fuid; 175 gid_t fgid; 176 int fmode; 177 PetscErrorCode ierr; 178 PetscBool exists; 179 180 PetscFunctionBegin; 181 *flg = PETSC_FALSE; 182 if (!fname) PetscFunctionReturn(0); 183 184 ierr = PetscGetFileStat(fname, &fuid, &fgid, &fmode,&exists);CHKERRQ(ierr); 185 if (!exists) PetscFunctionReturn(0); 186 /* Except for systems that have this broken stat macros (rare), this 187 is the correct way to check for a directory */ 188 if (!S_ISDIR(fmode)) PetscFunctionReturn(0); 189 190 ierr = PetscTestOwnership(fname, mode, fuid, fgid, fmode, flg);CHKERRQ(ierr); 191 PetscFunctionReturn(0); 192 } 193 194 #undef __FUNCT__ 195 #define __FUNCT__ "PetscLs" 196 PetscErrorCode PetscLs(MPI_Comm comm,const char libname[],char found[],size_t tlen,PetscBool *flg) 197 { 198 PetscErrorCode ierr; 199 size_t len; 200 char *f,program[PETSC_MAX_PATH_LEN]; 201 FILE *fp; 202 203 PetscFunctionBegin; 204 ierr = PetscStrcpy(program,"ls ");CHKERRQ(ierr); 205 ierr = PetscStrcat(program,libname);CHKERRQ(ierr); 206 #if defined(PETSC_HAVE_POPEN) 207 ierr = PetscPOpen(comm,PETSC_NULL,program,"r",&fp);CHKERRQ(ierr); 208 #else 209 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 210 #endif 211 f = fgets(found,tlen,fp); 212 if (f) *flg = PETSC_TRUE; else *flg = PETSC_FALSE; 213 while (f) { 214 ierr = PetscStrlen(found,&len);CHKERRQ(ierr); 215 f = fgets(found+len,tlen-len,fp); 216 } 217 if (*flg) {ierr = PetscInfo2(0,"ls on %s gives \n%s\n",libname,found);CHKERRQ(ierr);} 218 #if defined(PETSC_HAVE_POPEN) 219 ierr = PetscPClose(comm,fp,PETSC_NULL);CHKERRQ(ierr); 220 #else 221 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP_SYS,"Cannot run external programs on this machine"); 222 #endif 223 PetscFunctionReturn(0); 224 } 225