#include <stdio.h>
#include <string.h>
#include <dirent.h>
#include <fcntl.h>
#include <string.h>
#ifdef DEBUG
#include <errno.h>
#endif
#include <libintl.h>
#include <limits.h>
#include <unistd.h>
#include <stdlib.h>
#include <volmgt.h>
#include <sys/types.h>
#include <sys/mkdev.h>
#include <sys/stat.h>
#include <sys/dkio.h>
#include <sys/param.h>
#include <sys/wait.h>
#include <sys/mnttab.h>
#include "volmgt_private.h"
#define NULL_PATH "/dev/null"
static int vol_getmntdev(FILE *, struct mnttab *, dev_t,
struct dk_cinfo *);
int
_dev_mounted(char *path)
{
int fd = -1;
struct dk_cinfo info;
static FILE *fp = NULL;
struct mnttab mnt;
char *cn = NULL;
struct stat64 sb;
int ret_val = 0;
if ((cn = (char *)volmgt_getfullrawname(path)) == NULL) {
goto dun;
}
if ((fp = fopen(MNTTAB, "rF")) == NULL) {
goto dun;
}
if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
goto dun;
}
if (fstat64(fd, &sb) < 0) {
goto dun;
}
if (ioctl(fd, DKIOCINFO, &info) != 0) {
goto dun;
}
if (vol_getmntdev(fp, &mnt, sb.st_rdev, &info) != 0) {
ret_val = 1;
}
dun:
if (cn != NULL) {
free(cn);
}
if (fp != NULL) {
(void) fclose(fp);
}
if (fd >= 0) {
(void) close(fd);
}
return (ret_val);
}
static int call_unmount_prog(int, int, char *, int, char *,
char *);
static int get_media_info(char *, char **, int *, char **);
int
_dev_unmount(char *path)
{
char *bn = NULL;
char *mtype = NULL;
char *spcl = NULL;
char *spcl_failed = NULL;
int ret_val = FALSE;
char *vr;
int media_info_gotten = 0;
int mnum = 0;
int volume_is_not_managed;
char *pathbuf, *absname;
if ((bn = (char *)volmgt_getfullblkname(path)) == NULL) {
goto dun;
}
if ((pathbuf = malloc(PATH_MAX+1)) == NULL)
goto dun;
absname = bn;
if (realpath(bn, pathbuf) != NULL)
absname = pathbuf;
volume_is_not_managed = !volmgt_running() ||
(!volmgt_ownspath(absname) && volmgt_symname(bn) == NULL);
free(pathbuf);
if (!volume_is_not_managed) {
int use_rmm = FALSE;
vr = (char *)volmgt_root();
if (strncmp(bn, vr, strlen(vr)) == 0) {
use_rmm = TRUE;
}
media_info_gotten = get_media_info(bn, &mtype, &mnum, &spcl);
ret_val = call_unmount_prog(media_info_gotten, use_rmm, mtype,
mnum, spcl, bn);
} else {
if (get_media_info(bn, &mtype, &mnum, &spcl)) {
do {
if (spcl_failed != NULL) {
if (strcmp(spcl, spcl_failed) == 0) {
break;
}
}
ret_val = call_unmount_prog(TRUE, FALSE,
mtype, mnum, spcl, bn);
if (!ret_val) {
spcl_failed = strdup(spcl);
} else {
if (spcl_failed != NULL) {
free(spcl_failed);
spcl_failed = NULL;
}
}
} while (get_media_info(bn, &mtype, &mnum, &spcl));
} else {
ret_val = call_unmount_prog(FALSE, FALSE, NULL, 0,
NULL, bn);
}
}
if (mtype != NULL) {
free(mtype);
}
if (spcl != NULL) {
free(spcl);
}
if (spcl_failed != NULL) {
free(spcl_failed);
}
if (bn != NULL) {
free(bn);
}
dun:
return (ret_val);
}
static int
vol_getmntdev(FILE *fp, struct mnttab *mp, dev_t dev, struct dk_cinfo *ip)
{
int fd;
struct stat64 sb;
int ret_val = 0;
char *cn;
struct dk_cinfo dkinfo;
#ifdef DEBUG
denter(
"vol_getmntdev: entering for %d.%d, ctype/cnum/unit = %d/%d/%d\n",
(int)major(dev), (int)minor(dev), ip->dki_ctype, ip->dki_cnum,
ip->dki_unit);
#endif
rewind(fp);
while (getmntent(fp, mp) == 0) {
if (mp->mnt_special[0] != '/') {
continue;
}
if ((cn = volmgt_getfullrawname(mp->mnt_special)) == NULL) {
continue;
}
if (cn[0] == NULLC) {
free(cn);
continue;
}
if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
free(cn);
continue;
}
if (fstat64(fd, &sb) < 0) {
free(cn);
(void) close(fd);
continue;
}
if (!S_ISBLK(sb.st_mode) && !S_ISCHR(sb.st_mode)) {
free(cn);
(void) close(fd);
continue;
}
if (sb.st_rdev == dev) {
ret_val = 1;
free(cn);
(void) close(fd);
break;
}
if (major(sb.st_rdev) != major(dev)) {
free(cn);
(void) close(fd);
continue;
}
if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
free(cn);
(void) close(fd);
continue;
}
free(cn);
(void) close(fd);
if ((ip->dki_ctype == dkinfo.dki_ctype) &&
(ip->dki_cnum == dkinfo.dki_cnum) &&
(ip->dki_unit == dkinfo.dki_unit)) {
ret_val = 1;
break;
}
}
return (ret_val);
}
char *
vol_basename(char *path)
{
char *cp;
if (strcmp(path, "/") == 0) {
return (path);
}
if ((cp = strrchr(path, '/')) == NULL) {
return (path);
}
if (*++cp != NULLC) {
return (cp);
}
while (cp != path) {
if (*--cp == '/') {
return (--cp);
}
}
return (path);
}
static int vol_getmntdev(FILE *, struct mnttab *, dev_t,
struct dk_cinfo *);
static int
get_media_info(char *path, char **mtypep, int *mnump, char **spclp)
{
FILE *fp = NULL;
int fd = -1;
char *cn = NULL;
struct stat64 sb;
struct dk_cinfo info;
struct mnttab mnt;
int ret_val = FALSE;
if ((fp = fopen(MNTTAB, "rF")) == NULL) {
goto dun;
}
if ((cn = volmgt_getfullrawname(path)) == NULL) {
goto dun;
}
if (cn[0] == NULLC) {
goto dun;
}
if ((fd = open(cn, O_RDONLY|O_NDELAY)) < 0) {
goto dun;
}
if (fstat64(fd, &sb) < 0) {
goto dun;
}
if (ioctl(fd, DKIOCINFO, &info) != 0) {
goto dun;
}
if (vol_getmntdev(fp, &mnt, sb.st_rdev, &info) != 0) {
char *cp;
char *mtype;
char *mnt_dir;
int mtype_len;
DIR *dirp = NULL;
struct dirent64 *dp;
char *volname;
*spclp = strdup(mnt.mnt_special);
if (!volmgt_running() ||
(!volmgt_ownspath(*spclp) &&
volmgt_symname(*spclp) == NULL)) {
ret_val = TRUE;
goto dun;
}
cp = mnt.mnt_mountp;
if (*cp++ != '/') {
goto dun;
}
mtype = cp;
if ((cp = strchr(mtype, '/')) == NULL) {
goto dun;
}
*cp++ = NULLC;
mnt_dir = mnt.mnt_mountp;
volname = cp;
if ((dirp = opendir(mnt_dir)) == NULL) {
goto dun;
}
mtype_len = strlen(mtype);
while ((dp = readdir64(dirp)) != NULL) {
char lpath[2 * (MAXNAMELEN+1)];
char linkbuf[MAXPATHLEN+4];
int lb_len;
struct stat64 sb;
if (strncmp(dp->d_name, mtype, mtype_len) != 0) {
continue;
}
(void) sprintf(lpath, "%s/%s", mnt_dir,
dp->d_name);
if (lstat64(lpath, &sb) < 0) {
continue;
}
if (!S_ISLNK(sb.st_mode)) {
continue;
}
if ((lb_len = readlink(lpath, linkbuf,
sizeof (linkbuf))) < 0) {
continue;
}
linkbuf[lb_len] = NULLC;
if ((cp = vol_basename(linkbuf)) == NULL) {
continue;
}
if (strcmp(cp, volname) == 0) {
if (sscanf(dp->d_name + mtype_len, "%d",
mnump) == 1) {
*mtypep = strdup(mtype);
ret_val = TRUE;
}
break;
}
}
(void) closedir(dirp);
}
dun:
if (fp != NULL) {
(void) fclose(fp);
}
if (fd >= 0) {
(void) close(fd);
}
if (cn != NULL) {
free(cn);
}
#ifdef DEBUG
if (ret_val) {
dexit("get_media_info: returning mtype=%s, mnum=%d, spcl=%s\n",
*mtypep == NULL ? "<null ptr>" : *mtypep,
*mnump,
*spclp == NULL ? "<null ptr>" : *spclp);
} else {
dexit("get_media_info: FAILED\n");
}
#endif
return (ret_val);
}
static int
call_unmount_prog(int mi_gotten, int use_rmm, char *mtype, int mnum,
char *spcl, char *bn)
{
pid_t pid;
int ret_val = FALSE;
const char *etc_umount = "/etc/umount";
const char *rmm = "/usr/sbin/rmmount";
int rval;
#ifdef DEBUG
denter(
"call_unmount_prog(%s, %s, \"%s\", %d, \"%s\", \"%s\"): entering\n",
mi_gotten ? "TRUE" : "FALSE", use_rmm ? "TRUE" : "FALSE",
mtype ? mtype : "<null ptr>", mnum, spcl ? spcl : "<null ptr>",
bn);
#endif
if ((pid = fork()) < 0) {
goto dun;
}
if (pid == 0) {
#ifndef DEBUG
int xfd;
#endif
char env_buf[MAXPATHLEN];
#ifndef DEBUG
if ((xfd = open(NULL_PATH, O_RDWR)) >= 0) {
(void) dup2(xfd, fileno(stdin));
(void) dup2(xfd, fileno(stdout));
(void) dup2(xfd, fileno(stderr));
}
#endif
if (use_rmm) {
(void) putenv("VOLUME_ACTION=eject");
(void) putenv(strdup(env_buf));
if (mi_gotten) {
(void) sprintf(env_buf,
"VOLUME_MEDIATYPE=%s", mtype);
(void) putenv(strdup(env_buf));
(void) sprintf(env_buf, "VOLUME_SYMDEV=%s%d",
mtype, mnum);
(void) putenv(strdup(env_buf));
(void) sprintf(env_buf, "VOLUME_PATH=%s",
spcl);
(void) putenv(strdup(env_buf));
(void) sprintf(env_buf, "VOLUME_NAME=%s",
vol_basename(spcl));
(void) putenv(strdup(env_buf));
} else {
(void) sprintf(env_buf, "VOLUME_PATH=%s", bn);
(void) putenv(strdup(env_buf));
(void) sprintf(env_buf, "VOLUME_NAME=%s",
vol_basename(bn));
(void) putenv(strdup(env_buf));
}
#ifdef DEBUG
dprintf("call_unmount_prog: calling \"%s -D\"\n", rmm);
(void) execl(rmm, rmm, "-D", NULL);
#else
(void) execl(rmm, rmm, NULL);
#endif
} else {
#ifdef DEBUG
dprintf("call_unmount_prog: calling \"%s %s\"\n",
etc_umount, mi_gotten ? spcl : bn);
#endif
(void) execl(etc_umount, etc_umount,
mi_gotten ? spcl : bn,
NULL);
}
exit(-1);
}
if (waitpid(pid, &rval, 0) == pid) {
if (WIFEXITED(rval)) {
if (WEXITSTATUS(rval) == 0) {
ret_val = TRUE;
}
}
}
dun:
return (ret_val);
}