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