home *** CD-ROM | disk | FTP | other *** search
- /* funcs.c -- support functions for mntdisk.c */
-
- /* Written by Mike J. Fuller (mikef@sarah.lerc.nasa.gov)
- for NASA Lewis Research Center */
-
- #ifndef lint
- static char ID[] = "@(#)funcs.c 1.00 91/07/02";
- #endif
-
- #include "mntdisk.h"
-
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <sys/wait.h>
- #include <sys/time.h>
- #include <sys/vnode.h>
- #include <sys/ioctl.h>
- #include <ufs/inode.h>
- #include <ufs/fs.h>
- #include <errno.h>
- #include <pwd.h>
- #include <malloc.h>
- #include <mntent.h>
- #include <signal.h>
- #include <unistd.h>
-
- extern char *prog;
- extern int quiet, uid, mask;
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- static int
- bread(int fd, daddr_t bno, char *buf, int cnt)
- #else
- static int bread(fd, bno, buf, cnt)
- int fd, cnt;
- daddr_t bno;
- char *buf;
- #endif
- {
- if(lseek(fd, bno * DEV_BSIZE, SEEK_SET) != (bno * DEV_BSIZE))
- return(1);
- if(read(fd, buf, cnt) != cnt)
- return(1);
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- static int
- bwrite(int fd, daddr_t bno, char *buf, int cnt)
- #else
- static int bwrite(fd, bno, buf, cnt)
- int fd, cnt;
- daddr_t bno;
- char *buf;
- #endif
- {
- if(lseek(fd, bno * DEV_BSIZE, SEEK_SET) != (bno * DEV_BSIZE))
- return(1);
- if(write(fd, buf, cnt) != cnt)
- return(1);
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- static int
- has_devices(char *device)
- #else
- static int has_devices(device)
- char *device;
- #endif
- {
- register int c, j;
- int fd;
- union {
- struct fs sblock;
- char buf[SBSIZE];
- } sblock;
- union {
- struct dinode *itab;
- char *buf;
- } itab;
-
- if((fd = open(device, O_RDONLY)) == -1) {
- perror(device);
- return(1);
- }
-
- if(bread(fd, SBLOCK, sblock.buf, SBSIZE)) {
- perror(device);
- (void) close(fd);
- return(1);
- }
-
- if(sblock.sblock.fs_magic != FS_MAGIC) {
- (void) fprintf(stderr, "%s: Error: '%s' is not a filesystem.\n",
- prog, device);
- (void) close(fd);
- return(1);
- }
-
- if((itab.buf = (char *) malloc ((unsigned int) (sblock.sblock.fs_ipg *
- sizeof(struct dinode)))) == (char *) 0) {
- (void) fprintf(stderr, "%s: Error: insufficient memory\n", prog);
- (void) close(fd);
- return(1);
- }
-
- for(c = 0; c < sblock.sblock.fs_ncg; c++) {
- if(bread(fd, fsbtodb(&sblock.sblock, cgimin(&sblock.sblock, c)),
- itab.buf, (int) (sblock.sblock.fs_ipg *
- sizeof(struct dinode)))) {
- perror(device);
- (void) close(fd);
- free((char *) itab.buf);
- return(1);
- }
- for(j = 0; j < sblock.sblock.fs_ipg; j++)
- if(((itab.itab[j].di_mode & IFMT) == IFCHR) ||
- ((itab.itab[j].di_mode & IFMT) == IFBLK)) {
- (void) close(fd);
- free((char *) itab.buf);
- return(1);
- }
- }
-
- free((char *) itab.buf);
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- run(char *command)
- #else
- int run(command)
- char *command;
- #endif
- {
- int pid;
-
- switch(pid = fork()) {
- case 0: {
- char **argv, *path;
- register int i = 0;
- register char *b = command, *c = command;
- int addflag = 0, word = 0, quote = 0;
-
- if(setreuid(0, -1) == -1) {
- perror("setreuid()");
- _exit(1);
- }
-
- if((argv = (char **) malloc(sizeof(char **))) == (char **) 0) {
- (void) fprintf(stderr, "%s: Error: insufficient memory\n", prog);
- _exit(1);
- }
-
- for(;;) {
- if((*c == '\'') && ! word)
- if(quote) {
- quote = 0;
- addflag = 1;
- }
- else {
- quote = 1;
- b = c + 1;
- }
- else if(*c == '\0')
- if(word && ! quote)
- addflag = 1;
- else
- break;
- else if(isspace(*c)) {
- if(word && ! quote) {
- word = 0;
- addflag = 1;
- }
- }
- else
- if(! word && ! quote) {
- word = 1;
- b = c;
- }
-
- if(addflag) {
- addflag = 0;
- if(! i)
- path = b;
- argv[i++] = b;
- if((argv = (char **)
- realloc((char *) argv, (unsigned int)
- ((i + 1) * sizeof(char **)))) == (char **) 0) {
- (void) fprintf(stderr, "%s: Error: insufficient memory\n",
- prog);
- _exit(1);
- }
- if(*c == '\0')
- break;
- *c = '\0';
- if((i == 1) && ((b = strrchr(b, (int) '/')) != (char *) 0))
- argv[0] = b + 1;
- }
- ++c;
- }
- argv[i] = (char *) 0;
-
- if(quote) {
- (void) fprintf(stderr, "%s: run: parse error.\n", prog);
- _exit(1);
- }
- #ifdef DEBUG
- else {
- (void) printf("path = '%s'\n", path);
- for(i = 0; argv[i] != (char *) 0; ++i)
- (void) printf("argv[%d] = '%s'\n", i, argv[i]);
- }
- #endif
-
- if(quiet)
- (void) close(1);
- (void) execv(path, argv);
- perror(path);
- _exit(1);
- }
-
- case -1:
- perror("fork");
- return(1);
-
- default: {
- int ret, status;
-
- do
- ret = wait(&status);
- while((ret != pid) && (ret != -1));
- if(ret == -1) {
- perror("wait");
- return(-1);
- }
- return(WEXITSTATUS(status));
- }
- }
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- char
- *username(int uid)
- #else
- char *username(uid)
- int uid;
- #endif
- {
- static char buf[BUFSIZ];
- struct passwd *pwp;
- register char *c;
-
- if((pwp = getpwuid(uid)) == (struct passwd *) 0)
- (void) sprintf(buf, "uid %d", uid);
- else {
- for(c = pwp->pw_gecos; *c != '\0'; c++)
- if(*c == ',')
- *c = '\0';
- (void) sprintf(buf, "%s (%s)", pwp->pw_name, pwp->pw_gecos);
- }
- return(buf);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- char
- *mounted(char *device)
- #else
- char *mounted(device)
- char *device;
- #endif
- {
- static char fsname[MAXPATHLEN];
- FILE *fp;
- struct mntent *mnt;
-
- if((fp = setmntent("/etc/mtab", "r")) == (FILE *) 0) {
- perror("/etc/mtab");
- return((char *) 0);
- }
- while((mnt = getmntent(fp)) != (struct mntent *) 0)
- if(! strcmp(mnt->mnt_fsname, device)) {
- (void) strcpy(fsname, mnt->mnt_dir);
- (void) endmntent(fp);
- return(fsname);
- }
- (void) endmntent(fp);
- return((char *) 0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- dounmount(char *device, char *mountpoint)
- #else
- int dounmount(device, mountpoint)
- char *device, *mountpoint;
- #endif
- {
- char tmpmtab[MAXPATHLEN], *mtab = "/etc/mtab";
- FILE *mtabfp, *tmpmtabfp;
- struct mntent *mnt;
-
- if (unmount(mountpoint)) {
- perror(mountpoint);
- return(1);
- }
-
- if((mtabfp = setmntent(mtab, "r+")) == (FILE *) 0) {
- (void) fprintf(stderr, "%s: Warning: couldn't read '%s'.\n",
- prog, mtab);
- return(0);
- }
- if((tmpmtabfp = setmntent(sprintf(tmpmtab, "%s.%d", mtab, getpid()), "w"))
- == (FILE *) 0) {
- (void) fprintf(stderr, "%s: Warning: couldn't create '%s'.\n",
- prog, tmpmtab);
- return(0);
- }
-
- while((mnt = getmntent(mtabfp)) != (struct mntent *) 0)
- if(strcmp(device, mnt->mnt_fsname) && (addmntent(tmpmtabfp, mnt) != 0))
- (void) fprintf(stderr,"%s: Warning: couldn't add '%s' to '%s'.\n",
- prog, mnt->mnt_fsname, tmpmtab);
- (void) endmntent(mtabfp);
- (void) endmntent(tmpmtabfp);
-
- if(unlink(mtab) == -1) {
- (void) fprintf(stderr, "%s: Warning: couldn't delete '%s'.\n",
- prog, mtab);
- return(0);
- }
- if(rename(tmpmtab, mtab) == -1)
- (void) fprintf(stderr, "%s: Warning: couldn't rename '%s' to '%s'.\n",
- prog, tmpmtab, mtab);
-
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef EXPORT
- #ifdef __STDC__
- int
- doexport(char *mountpoint, int ro, int unexport)
- #else
- int doexport(mountpoint, ro, unexport)
- char *mountpoint;
- int ro, unexport;
- #endif
- {
- char buf[MAXPATHLEN];
-
- if(access("/etc/exports", F_OK) == -1)
- return(0);
-
- if(unexport)
- (void) sprintf(buf, "/usr/etc/exportfs -iu %s", mountpoint);
- else
- (void) sprintf(buf, "/usr/etc/exportfs -i -o %s%s %s", EXPORT,
- ro ? ",ro" : "", mountpoint);
-
- return(run(buf));
- }
- #endif
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- domount(char *device, char *mountpoint, char *mtype, char *moptstr, int mopts)
- #else
- int domount(device, mountpoint, mtype, moptstr, mopts)
- char *device, *mountpoint, *moptstr, *mtype;
- int mopts;
- #endif
- {
- FILE *fp;
- struct mntent mnt;
-
- if (mount(mtype, mountpoint, mopts, &device)) {
- perror(device);
- return(1);
- }
-
- if((fp = setmntent("/etc/mtab", "r+")) != (FILE *) 0) {
- mnt.mnt_fsname = device;
- mnt.mnt_dir = mountpoint;
- mnt.mnt_type = mtype;
- mnt.mnt_opts = moptstr;
- mnt.mnt_freq = 0;
- mnt.mnt_passno = 0;
- if (addmntent(fp, &mnt) != 0)
- (void) fprintf(stderr,
- "%s: Warning: couldn't update '/etc/mtab'.\n", prog);
- (void) endmntent(fp);
- }
- else
- (void) fprintf(stderr, "%s: Warning: couldn't update '/etc/mtab'.\n",
- prog);
-
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- no_fs(char *device)
- #else
- int no_fs(device)
- char *device;
- #endif
- {
- int fd;
- union {
- char buf[SBSIZE];
- struct fs sblock;
- } sblock;
-
- if((fd = open(device, O_RDONLY)) == -1) {
- perror(device);
- return(-1);
- }
-
- if(bread(fd, SBLOCK, sblock.buf, SBSIZE)) {
- perror(device);
- (void) close(fd);
- return(-1);
- }
-
- (void) close(fd);
- return(sblock.sblock.fs_magic != FS_MAGIC);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- can_overwrite(char *device)
- #else
- int can_overwrite(device)
- char *device;
- #endif
- {
- int fd, duid, canwrite = 0;
- union {
- struct fs sblock;
- char buf[SBSIZE];
- } sblock;
- union {
- struct dinode itab[(DEV_BSIZE << 1) / sizeof(struct dinode)];
- char buf[DEV_BSIZE << 1];
- } itab;
-
- if((fd = open(device, O_RDONLY)) == -1) {
- if(errno != EIO)
- perror(device);
- return(errno == EIO);
- }
- if(bread(fd, SBLOCK, sblock.buf, SBSIZE)) {
- (void) close(fd);
- if(errno != EIO)
- perror(device);
- return(errno == EIO);
- }
- if(sblock.sblock.fs_magic != FS_MAGIC) {
- (void) close(fd);
- return(1);
- }
-
- #ifndef DEBUG
- {
- struct stat sbuf;
-
- if(fstat(fd, &sbuf) == -1) {
- perror(device);
- (void) close(fd);
- return(0);
- }
- if((sbuf.st_uid == uid) && (sbuf.st_mode & S_IWUSR))
- canwrite = 1;
- else
- if((sbuf.st_gid == (gid_t) getgid()) && (sbuf.st_mode & S_IWGRP))
- canwrite = 1;
- else
- if(sbuf.st_mode & S_IWOTH)
- canwrite = 1;
- }
- #endif
-
- if(bread(fd, fsbtodb(&sblock.sblock, cgimin(&sblock.sblock, 0)), itab.buf,
- DEV_BSIZE << 1)) {
- (void) close(fd);
- if(errno != EIO)
- perror(device);
- return(errno == EIO);
- }
-
- (void) close(fd);
- duid = itab.itab[ROOTINO].di_uid;
- if(! canwrite && (duid != uid)) {
- (void) fprintf(stderr,
- "%s: Error: the filesystem on '%s' is owned by %s.\n",
- prog, device, username((int) itab.itab[ROOTINO].di_uid));
- return(0);
- }
-
- if(! quiet)
- (void) printf("%s: Warning: The filesystem on '%s' is owned by %s.\n",
- prog, device, username(duid));
-
- return(1);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- mountpoint_ok(char *mp, char *nmp)
- #else
- int mountpoint_ok(mp, nmp)
- char *mp, *nmp;
- #endif
- {
- struct stat sbuf;
-
- if(! strcmp(mp, nmp) && (access(mp, F_OK) == -1) &&
- (mkdir(nmp, 0755) == -1)) {
- perror(nmp);
- return(0);
- }
-
- if(stat(nmp, &sbuf) == -1) {
- perror(nmp);
- return(0);
- }
-
- if (! S_ISDIR(sbuf.st_mode)) {
- (void) fprintf(stderr, "%s: Error: '%s' is not a directory.\n",
- prog, nmp);
- return(0);
- }
-
- if(uid && strcmp(mp, nmp) && (sbuf.st_uid != uid)) {
- (void) fprintf(stderr, "%s: Error: not owner of '%s'.\n", prog, nmp);
- return(0);
- }
-
- return(1);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- void
- setmodes(char *device, mode_t mode)
- #else
- void setmodes(device, mode)
- char *device;
- mode_t mode;
- #endif
- {
- struct dirent *dp;
- DIR *dirp;
- char buf[MAXPATHLEN], prefix[MAXPATHLEN], *c;
- int len;
- register int rlen, blen;
-
- len = strlen(strcpy(prefix, device)) - 1;
- prefix[len] = '\0';
- blen = len + 1;
- rlen = blen + 1;
-
- dirp = opendir("/dev");
- for(dp = readdir(dirp); dp != (struct dirent *) 0; dp = readdir(dirp))
- if((dp->d_namlen == blen) || (dp->d_namlen == rlen)) {
- c = strstr(dp->d_name, prefix);
- if((c == dp->d_name) || ((c == (dp->d_name + 1)) &&
- (*(dp->d_name) == 'r'))) {
- (void) strcat(strcpy(buf, "/dev/"), dp->d_name);
- if(chmod(buf, (int) mode) == -1)
- perror(buf);
- #ifdef DEBUG
- (void) printf("chmod %s %o\n", buf, mode);
- #endif
- }
- }
- (void) closedir(dirp);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- makeowner(char *device)
- #else
- int makeowner(device)
- char *device;
- #endif
- {
- int fd;
- daddr_t first_inodes;
- union {
- struct fs sblock;
- char buf[SBSIZE];
- } sblock;
- union {
- struct dinode itab[(DEV_BSIZE << 1) / sizeof(struct dinode)];
- char buf[DEV_BSIZE << 1];
- } itab;
-
- if((fd = open(device, O_RDWR)) == -1) {
- perror(device);
- return(1);
- }
- if(bread(fd, SBLOCK, sblock.buf, SBSIZE)) {
- perror(device);
- (void) close(fd);
- return(1);
- }
- if(sblock.sblock.fs_magic != FS_MAGIC) {
- (void) fprintf(stderr, "%s: '%s' has no filesystem.\n", prog, device);
- return(1);
- }
-
- first_inodes = fsbtodb(&sblock.sblock, cgimin(&sblock.sblock, 0));
- if(bread(fd, first_inodes, itab.buf, DEV_BSIZE << 1)) {
- perror(device);
- (void) close(fd);
- return(1);
- }
- itab.itab[ROOTINO].di_uid = (uid_t) uid;
- itab.itab[ROOTINO].di_gid = (gid_t) getgid();
- itab.itab[ROOTINO].di_mode |= ISGID | (0777 & ~mask);
- if(bwrite(fd, first_inodes, itab.buf, DEV_BSIZE << 1)) {
- perror(device);
- (void) close(fd);
- return(1);
- }
-
- (void) close(fd);
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- spinup(char *device)
- #else
- int spinup(device)
- char *device;
- #endif
- {
- char buf[SBSIZE];
- char rdevice[MAXPATHLEN];
- int fd, rc;
-
- (void) sprintf(rdevice, "/dev/r%s", device);
- if(((fd = open(rdevice, O_RDONLY)) != -1) &&
- ! bread(fd, SBLOCK, buf, SBSIZE)) {
- (void) close(fd);
- return(0);
- }
- else if(errno == EIO) {
- char eoddevice[MAXPATHLEN], command[MAXPATHLEN];
-
- (void) close(fd);
- if(! quiet)
- (void) printf("%s: Spinning up '%s'...\n", prog, device);
- (void) strcpy(eoddevice, device);
- eoddevice[strlen(eoddevice) - 1] = '\0';
- rc = run(sprintf(command, EODSTART, eoddevice));
- if(! rc) sleep(1);
- return(rc);
- }
- else {
- perror(rdevice);
- (void) close(fd);
- return(1);
- }
- }
-
- /*****************************************************************************/
-
- #if TIMEOUT > 0
- #ifdef __STDC__
- static int
- locked(char *device)
- #else
- static int locked(device)
- char *device;
- #endif
- {
- char disk[MAXPATHLEN], lockfile[MAXPATHLEN];
-
- (void) strcpy(disk, device);
- disk[strlen(disk) - 1] = '\0';
- (void) sprintf(lockfile, "/tmp/%s.lock", disk);
-
- return(access(lockfile, F_OK) != -1);
- }
- #endif
-
- /*****************************************************************************/
-
- #if TIMEOUT > 0
- #ifdef __STDC__
- void
- spindown(char *device, int time)
- #else
- void spindown(device, time)
- char *device;
- int time;
- #endif
- {
- char disk[MAXPATHLEN], spinfile[MAXPATHLEN];
-
- (void) strcpy(disk, device);
- disk[strlen(disk) - 1] = '\0';
- (void) sprintf(spinfile, "/tmp/%s.spindown", disk);
-
- if(access(spinfile, F_OK) != -1)
- return;
- if(creat(spinfile, 0444) == -1) {
- perror(spinfile);
- return;
- }
- if(chown(spinfile, uid, -1) == -1) {
- perror(spinfile);
- return;
- }
-
- switch(fork()) {
- case 0: {
- char bdevice[MAXPATHLEN], rdevice[MAXPATHLEN], eoddevice[MAXPATHLEN];
- char buf[MAXPATHLEN];
- struct stat asbuf, bsbuf;
- #ifndef DEBUG
- int fd;
- #endif
-
- (void) strcpy(eoddevice, device);
- eoddevice[strlen(eoddevice) - 1] = '\0';
- (void) sprintf(bdevice, "/dev/%s", device);
- (void) sprintf(rdevice, "/dev/r%s", device);
-
- #ifndef DEBUG
- if((fd = open("/dev/tty", O_RDONLY)) != -1) {
- ioctl(fd, TIOCNOTTY, 0);
- (void) close(fd);
- }
- for(fd = (getdtablesize() - 1); fd >= 0; fd--)
- if(isatty(fd) && (close(fd) == -1)) {
- perror("close");
- _exit(1);
- }
- (void) signal(SIGHUP, SIG_IGN);
- #endif
-
- do {
- if(stat(rdevice, &bsbuf) == -1) {
- #if defined(DEBUG) || ! defined(SYSLOG)
- perror(rdevice);
- #else
- syslog(LOG_ERR, "%s: %m", rdevice);
- #endif
- if(unlink(spinfile) == -1)
- #if defined(DEBUG) || ! defined(SYSLOG)
- perror(spinfile);
- #else
- syslog(LOG_ERR, "%s: %m", spinfile);
- #endif
- #ifdef SYSLOG
- closelog();
- #endif
- _exit(1);
- }
-
- sleep(time);
-
- if(access(spinfile, F_OK) == -1)
- _exit(0);
-
- if((mounted(bdevice) != (char *) 0) || locked(device)) {
- if(unlink(spinfile) == -1)
- #if defined(DEBUG) || ! defined(SYSLOG)
- perror(spinfile);
- #else
- syslog(LOG_ERR, "%s: %m", spinfile);
- #endif
- _exit(0);
- }
-
- if(stat(rdevice, &asbuf) == -1) {
- #if defined(DEBUG) || ! defined(SYSLOG)
- perror(rdevice);
- #else
- syslog(LOG_ERR, "%s: %m", rdevice);
- #endif
- if(unlink(spinfile) == -1)
- #if defined(DEBUG) || ! defined(SYSLOG)
- perror(spinfile);
- #else
- syslog(LOG_ERR, "%s: %m", spinfile);
- #endif
- #ifdef SYSLOG
- closelog();
- #endif
- _exit(1);
- }
- time = asbuf.st_ctime - bsbuf.st_ctime;
- } while(time > 0);
-
- if(unlink(spinfile) == -1)
- #if defined(DEBUG) || ! defined(SYSLOG)
- perror(spinfile);
- #else
- syslog(LOG_ERR, "%s: %m", spinfile);
- #endif
-
- if(run(sprintf(buf, EODSTOP, eoddevice))) {
- #ifdef SYSLOG
- closelog();
- #endif
- _exit(1);
- }
- #ifdef SYSLOG
- syslog(LOG_INFO, "%s spun down '%s'.", username(uid), device);
- closelog();
- #endif
- _exit(0);
- }
- case -1:
- perror("fork");
- return;
- default:
- return;
- }
- }
- #endif
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- lock(char *device)
- #else
- int lock(device)
- char *device;
- #endif
- {
- char disk[MAXPATHLEN], lockfile[MAXPATHLEN];
- struct stat sbuf;
-
- (void) strcpy(disk, device);
- disk[strlen(disk) - 1] = '\0';
- (void) sprintf(lockfile, "/tmp/%s.lock", disk);
-
- if(stat(lockfile, &sbuf) != -1) {
- if(uid == sbuf.st_uid)
- return(0);
- else
- (void) fprintf(stderr,
- "%s: Error: '%s' is currently locked by %s.\n",
- prog, device, username((int) sbuf.st_uid));
- return(1);
- }
-
- if(creat(lockfile, 0444) == -1) {
- perror(lockfile);
- return(1);
- }
- if(chown(lockfile, uid, -1) == -1) {
- perror(lockfile);
- return(1);
- }
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- unlock(char *device)
- #else
- int unlock(device)
- char *device;
- #endif
- {
- char disk[MAXPATHLEN], lockfile[MAXPATHLEN];
-
- (void) strcpy(disk, device);
- disk[strlen(disk) - 1] = '\0';
- (void) sprintf(lockfile, "/tmp/%s.lock", disk);
-
- return(unlink(lockfile) != -1);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- fsck(char *device)
- #else
- int fsck(device)
- char *device;
- #endif
- {
- char buf[MAXPATHLEN];
-
- if(! quiet)
- (void) printf("%s: Checking '%s'...\n", prog, device);
-
- if(run(sprintf(buf, "/usr/etc/fsck %s", device))) {
- (void) fprintf(stderr, "%s: Error: fsck failed on '%s'.\n",
- prog, device);
- return(1);
- }
-
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- ufschecks(char *device, int mode, int skip)
- #else
- int ufschecks(device, mode, skip)
- char *device;
- int mode, skip;
- #endif
- {
- char rdevice[MAXPATHLEN];
- #ifdef WHEEL
- int gidset[NGROUPS], ngroups;
- register int i;
- #endif
-
- (void) sprintf(rdevice, "/dev/r%s", device);
-
- if((mode == EOD) && spinup(device)) {
- (void) fprintf(stderr, "%s: Error: cannot spin up drive '%s'.\n",
- prog, rdevice);
- return(1);
- }
-
- if(no_fs(rdevice)) {
- (void) fprintf(stderr, "%s: Error: '%s' has no filesystem.\n",
- prog, rdevice);
- return(1);
- }
-
- #ifdef WHEEL
- if((ngroups = getgroups(NGROUPS, gidset)) == -1) {
- perror("getgroups");
- return(1);
- }
- for(i = (ngroups - 1); i >= 0; i--)
- if(gidset[i] == 0)
- break;
- if(uid && (i < 0)) {
- #else
- if(uid) {
- #endif
- if(! quiet)
- (void) printf("%s: Checking '%s' for device special files...\n",
- prog, rdevice);
- if(has_devices(rdevice)) {
- (void) fprintf(stderr,
- "%s: Error: '%s' contains device special files.\n",
- prog, rdevice);
- #ifdef SYSLOG
- syslog(LOG_NOTICE,
- "%s attempted to mount a disk in '%s' containing device special files.",
- username(uid), device);
- #endif
- return(1);
- }
-
- if(! skip && fsck(rdevice))
- return(1);
- }
- return(0);
- }
-
- /*****************************************************************************/
-
- #ifdef __STDC__
- int
- device_ok(char *device, char *ndevice)
- #else
- int device_ok(device, ndevice)
- char *device, *ndevice;
- #endif
- {
- char buf[MAXPATHLEN];
- int len;
-
- buf[(len = strlen(strcpy(buf, device))) - 2] = '\0';
- return((strstr(ndevice, buf) == ndevice) && (strlen(ndevice) == len) &&
- (strchr("01234567", *(ndevice + len - 2)) != (char *) 0) &&
- (strchr("abcdefgh", *(ndevice + len - 1)) != (char *) 0));
-
- }
-