#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <stropts.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/dkio.h>
#include <locale.h>
#include <libintl.h>
#include <libdevinfo.h>
#include <secdb.h>
#include <deflt.h>
#include <auth_attr.h>
#include <auth_list.h>
#include <bsm/devices.h>
#include <bsm/devalloc.h>
#include <tsol/label.h>
#ifndef TEXT_DOMAIN
#define TEXT_DOMAIN "SUNW_OST_OSCMD"
#endif
#define MKDEVALLOC "mkdevalloc"
#define MKDEVMAPS "mkdevmaps"
#define DELTA 5
#define SECLIB "/etc/security/lib"
struct tape {
char *name;
char *device;
int number;
} *tape;
#define DFLT_NTAPE 10
#define SIZE_OF_RST 3
#define SIZE_OF_NRST 4
#define SIZE_OF_TMP 4
#define SIZE_OF_RMT 8
#define TAPE_CLEAN SECLIB"/st_clean"
struct audio {
char *name;
char *device;
int number;
} *audio;
#define DFLT_NAUDIO 10
#define SIZE_OF_SOUND 10
#define AUDIO_CLEAN SECLIB"/audio_clean"
struct cd {
char *name;
char *device;
int id;
int controller;
int number;
} *cd;
#define DFLT_NCD 10
#define SIZE_OF_SR 2
#define SIZE_OF_RSR 3
#define SIZE_OF_DSK 8
#define SIZE_OF_RDSK 9
#define CD_CLEAN SECLIB"/sr_clean"
struct rmdisk {
char *name;
char *device;
int id;
int controller;
int number;
} *rmdisk, *rmdisk_r;
#define DFLT_RMDISK 10
struct fp {
char *name;
char *device;
int number;
} *fp;
#define DFLT_NFP 10
#define SIZE_OF_FD0 3
#define SIZE_OF_RFD0 4
#define FLOPPY_CLEAN SECLIB"/fd_clean"
static void dotape();
static void doaudio();
static void dofloppy();
static int docd();
static void dormdisk(int);
static void initmem();
static int expandmem(int, void **, int);
static void no_memory(void);
int system_labeled = 0;
int do_devalloc = 0;
int do_devmaps = 0;
int do_files = 0;
devlist_t devlist;
int
main(int argc, char **argv)
{
int cd_count = 0;
char *progname;
(void) setlocale(LC_ALL, "");
(void) textdomain(TEXT_DOMAIN);
if ((progname = strrchr(argv[0], '/')) == NULL)
progname = argv[0];
else
progname++;
if (strcmp(progname, MKDEVALLOC) == 0)
do_devalloc = 1;
else if (strcmp(progname, MKDEVMAPS) == 0)
do_devmaps = 1;
else
exit(1);
system_labeled = is_system_labeled();
if (!system_labeled) {
if (defopen("/etc/system") == 0) {
if (defread("set sys_labeling=1") != NULL)
system_labeled = 1;
(void) defopen(NULL);
}
}
#ifdef DEBUG
if (!system_labeled) {
struct stat tx_stat;
system_labeled = is_system_labeled_debug(&tx_stat);
if (system_labeled) {
fprintf(stderr, "/ALLOCATE_FORCE_LABEL is set,\n"
"forcing system label on for testing...\n");
}
}
#endif
if (system_labeled && do_devalloc && (argc == 2) &&
(strcmp(argv[1], DA_IS_LABELED) == 0)) {
do_files = 1;
}
initmem();
dotape();
doaudio();
dofloppy();
cd_count = docd();
if (system_labeled)
dormdisk(cd_count);
return (0);
}
static void
dotape()
{
DIR *dirp;
struct dirent *dep;
int i, j;
char *nm;
char linkvalue[2048];
struct stat stat;
int sz;
char *cp;
int ntape;
int tape_count;
int first = 0;
char *dname, *dtype, *dclean;
da_args dargs;
deventry_t *entry;
ntape = DFLT_NTAPE;
if ((dirp = opendir("/dev")) == NULL) {
perror(gettext("open /dev failure"));
exit(1);
}
i = 0;
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, "rst", SIZE_OF_RST) &&
strncmp(dep->d_name, "nrst", SIZE_OF_NRST))
continue;
if (i == ntape) {
ntape = expandmem(i, (void **)&tape,
sizeof (struct tape));
}
nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/");
(void) strcat(nm, dep->d_name);
tape[i].name = nm;
if (lstat(tape[i].name, &stat) < 0) {
perror("stat(2) failed ");
exit(1);
}
if ((stat.st_mode & S_IFMT) != S_IFLNK)
continue;
if ((sz = readlink(tape[i].name, linkvalue,
sizeof (linkvalue))) < 0)
continue;
nm = (char *)malloc(sz + 1);
if (nm == NULL)
no_memory();
(void) strncpy(nm, linkvalue, sz);
nm[sz] = '\0';
tape[i].device = nm;
cp = strrchr(tape[i].device, '/');
cp++;
(void) sscanf(cp, "%d", &tape[i].number);
i++;
}
(void) closedir(dirp);
if ((dirp = opendir("/dev/rmt")) == NULL) {
perror(gettext("open /dev failure"));
exit(1);
}
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, ".", 1) == 0)
continue;
if (i == ntape) {
ntape = expandmem(i, (void **)&tape,
sizeof (struct tape));
}
nm = (char *)malloc(SIZE_OF_RMT + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/rmt/");
(void) strcat(nm, dep->d_name);
tape[i].name = nm;
nm = (char *)malloc(SIZE_OF_TMP + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "rmt/");
(void) strcat(nm, dep->d_name);
tape[i].device = nm;
(void) sscanf(dep->d_name, "%d", &tape[i].number);
i++;
}
tape_count = i;
(void) closedir(dirp);
for (i = 0; i < tape_count - 1; i++) {
for (j = i + 1; j < tape_count; j++) {
if (strcmp(tape[i].device, tape[j].device))
continue;
tape[j].number = -1;
}
}
if (system_labeled) {
dname = DA_TAPE_NAME;
dtype = DA_TAPE_TYPE;
dclean = DA_DEFAULT_TAPE_CLEAN;
} else {
dname = "st";
dtype = "st";
dclean = TAPE_CLEAN;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < tape_count; j++) {
if (tape[j].number != i)
continue;
if (do_files) {
(void) da_add_list(&devlist, tape[j].name, i,
DA_TAPE);
} else if (do_devalloc) {
if (system_labeled) {
(void) printf("%s%d%s\\\n",
dname, i, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_TAPE_TYPE, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DEFAULT_DEV_ALLOC_AUTH,
KV_DELIMITER);
(void) printf("\t%s\n\n", dclean);
} else {
(void) printf(
"st%d;st;reserved;reserved;%s;",
i, DEFAULT_DEV_ALLOC_AUTH);
(void) printf("%s%s\n", SECLIB,
"/st_clean");
}
break;
} else if (do_devmaps) {
if (first) {
(void) printf(" ");
} else {
if (system_labeled) {
(void) printf("%s%d%s\\\n",
dname, i, KV_TOKEN_DELIMIT);
(void) printf("\t%s%s\\\n",
dtype, KV_TOKEN_DELIMIT);
(void) printf("\t");
} else {
(void) printf("st%d:\\\n", i);
(void) printf("\trmt:\\\n");
(void) printf("\t");
}
first++;
}
(void) printf("%s", tape[j].name);
}
}
if (do_devmaps && first) {
(void) printf("\n\n");
first = 0;
}
}
if (do_files && tape_count) {
dargs.rootdir = NULL;
dargs.devnames = NULL;
dargs.optflag = DA_ADD;
for (entry = devlist.tape; entry != NULL; entry = entry->next) {
dargs.devinfo = &(entry->devinfo);
(void) da_update_device(&dargs);
}
}
}
static void
doaudio()
{
DIR *dirp;
struct dirent *dep;
int i, j;
char *nm;
char linkvalue[2048];
struct stat stat;
int sz;
char *cp;
int naudio;
int audio_count = 0;
int len, slen;
int first = 0;
char dname[128];
char *dclean;
da_args dargs;
deventry_t *entry;
naudio = DFLT_NAUDIO;
if ((dirp = opendir("/dev")) == NULL) {
perror(gettext("open /dev failure"));
exit(1);
}
i = 0;
while (dep = readdir(dirp)) {
if (strcmp(dep->d_name, "audio") &&
strcmp(dep->d_name, "audioctl"))
continue;
if (i == naudio) {
naudio = expandmem(i, (void **)&audio,
sizeof (struct audio));
}
nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/");
(void) strcat(nm, dep->d_name);
audio[i].name = nm;
if (lstat(audio[i].name, &stat) < 0) {
perror(gettext("stat(2) failed "));
exit(1);
}
if ((stat.st_mode & S_IFMT) != S_IFLNK)
continue;
if ((sz = readlink(audio[i].name, linkvalue,
sizeof (linkvalue))) < 0)
continue;
nm = (char *)malloc(sz + 1);
if (nm == NULL)
no_memory();
(void) strncpy(nm, linkvalue, sz);
nm[sz] = '\0';
audio[i].device = nm;
cp = strrchr(audio[i].device, '/');
cp++;
(void) sscanf(cp, "%d", &audio[i].number);
i++;
}
(void) closedir(dirp);
if ((dirp = opendir("/dev/sound")) == NULL) {
goto skip;
}
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, ".", 1) == 0)
continue;
if (i == naudio) {
naudio = expandmem(i, (void **)&audio,
sizeof (struct audio));
}
nm = (char *)malloc(SIZE_OF_SOUND + 1 +
strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/sound/");
(void) strcat(nm, dep->d_name);
audio[i].name = nm;
nm = (char *)malloc(SIZE_OF_SOUND + 1 +
strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/sound/");
(void) strcat(nm, dep->d_name);
audio[i].device = nm;
(void) sscanf(dep->d_name, "%d", &audio[i].number);
i++;
}
(void) closedir(dirp);
skip:
audio_count = i;
for (i = 0; i < audio_count - 1; i++) {
for (j = i + 1; j < audio_count; j++) {
if (strcmp(audio[i].device, audio[j].device))
continue;
audio[j].number = -1;
}
}
(void) strcpy(dname, DA_AUDIO_NAME);
slen = strlen(DA_AUDIO_NAME);
len = sizeof (dname) - slen;
dclean = system_labeled ? DA_DEFAULT_AUDIO_CLEAN : AUDIO_CLEAN;
for (i = 0; i < 8; i++) {
for (j = 0; j < audio_count; j++) {
if (audio[j].number != i)
continue;
if (system_labeled)
(void) snprintf(dname+slen, len, "%d", i);
if (do_files) {
(void) da_add_list(&devlist, audio[j].name,
i, DA_AUDIO);
} else if (do_devalloc) {
if (system_labeled) {
(void) printf("%s%s\\\n",
dname, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_AUDIO_TYPE, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DEFAULT_DEV_ALLOC_AUTH,
KV_DELIMITER);
(void) printf("\t%s\n\n", dclean);
} else {
(void) printf("audio;audio;");
(void) printf("reserved;reserved;%s;",
DEFAULT_DEV_ALLOC_AUTH);
(void) printf("%s%s\n", SECLIB,
"/audio_clean");
}
break;
} else if (do_devmaps) {
if (first) {
(void) printf(" ");
} else {
if (system_labeled) {
(void) printf("%s%s\\\n",
dname, KV_TOKEN_DELIMIT);
(void) printf("\t%s%s\\\n",
DA_AUDIO_TYPE,
KV_TOKEN_DELIMIT);
(void) printf("\t");
} else {
(void) printf("audio:\\\n");
(void) printf("\taudio:\\\n");
(void) printf("\t");
}
first++;
}
(void) printf("%s", audio[j].name);
}
}
if (do_devmaps && first) {
(void) printf("\n\n");
first = 0;
}
}
if (do_files && audio_count) {
dargs.rootdir = NULL;
dargs.devnames = NULL;
dargs.optflag = DA_ADD;
for (entry = devlist.audio; entry != NULL;
entry = entry->next) {
dargs.devinfo = &(entry->devinfo);
(void) da_update_device(&dargs);
}
}
}
static void
dofloppy()
{
DIR *dirp;
struct dirent *dep;
int i, j;
char *nm;
char linkvalue[2048];
struct stat stat;
int sz;
char *cp;
int nfp;
int floppy_count = 0;
int first = 0;
char *dname, *dclean;
da_args dargs;
deventry_t *entry;
nfp = DFLT_NFP;
if ((dirp = opendir("/dev")) == NULL) {
perror(gettext("open /dev failure"));
exit(1);
}
i = 0;
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, "fd0", SIZE_OF_FD0) &&
strncmp(dep->d_name, "rfd0", SIZE_OF_RFD0) &&
strncmp(dep->d_name, "fd1", SIZE_OF_FD0) &&
strncmp(dep->d_name, "rfd0", SIZE_OF_RFD0))
continue;
if (i == nfp) {
nfp = expandmem(i, (void **)&fp, sizeof (struct fp));
}
nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/");
(void) strcat(nm, dep->d_name);
fp[i].name = nm;
if (lstat(fp[i].name, &stat) < 0) {
perror(gettext("stat(2) failed "));
exit(1);
}
if ((stat.st_mode&S_IFMT) != S_IFLNK)
continue;
if ((sz = readlink(fp[i].name, linkvalue,
sizeof (linkvalue))) < 0)
continue;
nm = (char *)malloc(sz+1);
if (nm == NULL)
no_memory();
(void) strncpy(nm, linkvalue, sz);
nm[sz] = '\0';
fp[i].device = nm;
cp = strchr(fp[i].name, 'd');
cp++;
cp = strchr(cp, 'd');
cp++;
(void) sscanf(cp, "%d", &fp[i].number);
i++;
}
(void) closedir(dirp);
floppy_count = i;
if (system_labeled) {
dname = DA_FLOPPY_NAME;
dclean = DA_DEFAULT_DISK_CLEAN;
} else {
dname = "fd";
dclean = FLOPPY_CLEAN;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < floppy_count; j++) {
if (fp[j].number != i)
continue;
if (do_files) {
(void) da_add_list(&devlist, fp[j].name, i,
DA_FLOPPY);
} else if (do_devalloc) {
if (system_labeled) {
(void) printf("%s%d%s\\\n",
dname, i, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_FLOPPY_TYPE, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DEFAULT_DEV_ALLOC_AUTH,
KV_DELIMITER);
(void) printf("\t%s\n\n", dclean);
} else {
(void) printf(
"fd%d;fd;reserved;reserved;%s;",
i, DEFAULT_DEV_ALLOC_AUTH);
(void) printf("%s%s\n", SECLIB,
"/fd_clean");
}
break;
} else if (do_devmaps) {
if (first) {
(void) printf(" ");
} else {
if (system_labeled) {
(void) printf("%s%d%s\\\n",
dname, i, KV_TOKEN_DELIMIT);
(void) printf("\t%s%s\\\n",
DA_FLOPPY_TYPE,
KV_TOKEN_DELIMIT);
(void) printf("\t");
} else {
(void) printf("fd%d:\\\n", i);
(void) printf("\tfd:\\\n");
(void) printf("\t");
}
if (i == 0) {
(void) printf("/dev/diskette ");
(void) printf(
"/dev/rdiskette ");
}
first++;
}
(void) printf("%s", fp[j].name);
}
}
if (do_devmaps && first) {
(void) printf("\n\n");
first = 0;
}
}
if (do_files && floppy_count) {
dargs.rootdir = NULL;
dargs.devnames = NULL;
dargs.optflag = DA_ADD;
for (entry = devlist.floppy; entry != NULL;
entry = entry->next) {
dargs.devinfo = &(entry->devinfo);
(void) da_update_device(&dargs);
}
}
}
static int
docd()
{
DIR *dirp;
struct dirent *dep;
int i, j;
char *nm;
char linkvalue[2048];
struct stat stat;
int sz;
char *cp;
int id;
int ctrl;
int ncd;
int cd_count = 0;
int first = 0;
char *dname, *dclean;
da_args dargs;
deventry_t *entry;
ncd = DFLT_NCD;
if ((dirp = opendir("/dev")) == NULL) {
perror(gettext("open /dev failure"));
exit(1);
}
i = 0;
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, "sr", SIZE_OF_SR) &&
strncmp(dep->d_name, "rsr", SIZE_OF_RSR))
continue;
if (i == ncd) {
ncd = expandmem(i, (void **)&cd, sizeof (struct cd));
}
nm = (char *)malloc(SIZE_OF_TMP + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/");
(void) strcat(nm, dep->d_name);
cd[i].name = nm;
if (lstat(cd[i].name, &stat) < 0) {
perror(gettext("stat(2) failed "));
exit(1);
}
if ((stat.st_mode & S_IFMT) != S_IFLNK)
continue;
if ((sz = readlink(cd[i].name, linkvalue, sizeof (linkvalue))) <
0)
continue;
nm = (char *)malloc(sz + 1);
if (nm == NULL)
no_memory();
(void) strncpy(nm, linkvalue, sz);
nm[sz] = '\0';
cd[i].device = nm;
cp = strrchr(cd[i].device, '/');
cp++;
(void) sscanf(cp, "c%dt%d", &cd[i].controller, &cd[i].number);
cd[i].id = cd[i].number;
i++;
}
cd_count = i;
(void) closedir(dirp);
if ((dirp = opendir("/dev/dsk")) == NULL) {
perror("gettext(open /dev/dsk failure)");
exit(1);
}
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, ".", 1) == 0)
continue;
if (sscanf(dep->d_name, "c%dt%d", &ctrl, &id) != 2)
continue;
for (j = 0; j < cd_count; j++) {
if (cd[j].number == id && cd[j].controller == ctrl)
goto found;
}
continue;
found:
if (i == ncd) {
ncd = expandmem(i, (void **)&cd, sizeof (struct cd));
}
nm = (char *)malloc(SIZE_OF_DSK + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/dsk/");
(void) strcat(nm, dep->d_name);
cd[i].name = nm;
cd[i].id = cd[j].id;
cd[i].device = "";
cd[i].number = id;
i++;
}
(void) closedir(dirp);
if ((dirp = opendir("/dev/rdsk")) == NULL) {
perror(gettext("open /dev/dsk failure"));
exit(1);
}
while (dep = readdir(dirp)) {
if (strncmp(dep->d_name, ".", 1) == 0)
continue;
if (sscanf(dep->d_name, "c%dt%d", &ctrl, &id) != 2)
continue;
for (j = 0; j < cd_count; j++) {
if (cd[j].number == id && cd[j].controller == ctrl)
goto found1;
}
continue;
found1:
if (i == ncd) {
ncd = expandmem(i, (void **)&cd, sizeof (struct cd));
}
nm = (char *)malloc(SIZE_OF_RDSK + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/rdsk/");
(void) strcat(nm, dep->d_name);
cd[i].name = nm;
cd[i].id = cd[j].id;
cd[i].device = "";
cd[i].number = id;
cd[i].controller = ctrl;
i++;
}
(void) closedir(dirp);
cd_count = i;
if (system_labeled) {
dname = DA_CD_NAME;
dclean = DA_DEFAULT_DISK_CLEAN;
} else {
dname = "sr";
dclean = CD_CLEAN;
}
for (i = 0; i < 8; i++) {
for (j = 0; j < cd_count; j++) {
if (cd[j].id != i)
continue;
if (do_files) {
(void) da_add_list(&devlist, cd[j].name, i,
DA_CD);
} else if (do_devalloc) {
if (system_labeled) {
(void) printf("%s%d%s\\\n",
dname, i, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_CD_TYPE, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DEFAULT_DEV_ALLOC_AUTH,
KV_DELIMITER);
(void) printf("\t%s\n\n", dclean);
} else {
(void) printf(
"sr%d;sr;reserved;reserved;%s;",
i, DEFAULT_DEV_ALLOC_AUTH);
(void) printf("%s%s\n", SECLIB,
"/sr_clean");
}
break;
} else if (do_devmaps) {
if (first) {
(void) printf(" ");
} else {
if (system_labeled) {
(void) printf("%s%d%s\\\n",
dname, i, KV_TOKEN_DELIMIT);
(void) printf("\t%s%s\\\n",
DA_CD_TYPE,
KV_TOKEN_DELIMIT);
(void) printf("\t");
} else {
(void) printf("sr%d:\\\n", i);
(void) printf("\tsr:\\\n");
(void) printf("\t");
}
first++;
}
(void) printf("%s", cd[j].name);
}
}
if (do_devmaps && first) {
(void) printf("\n\n");
first = 0;
}
}
if (do_files && cd_count) {
dargs.rootdir = NULL;
dargs.devnames = NULL;
dargs.optflag = DA_ADD;
for (entry = devlist.cd; entry != NULL; entry = entry->next) {
dargs.devinfo = &(entry->devinfo);
(void) da_update_device(&dargs);
}
}
return (cd_count);
}
static void
dormdisk(int cd_count)
{
DIR *dirp;
struct dirent *dep;
int i, j;
char *nm;
int id;
int ctrl;
int nrmdisk;
int fd = -1;
int rmdisk_count;
int first = 0;
int is_cd;
int checked;
int removable;
char path[MAXPATHLEN];
da_args dargs;
deventry_t *entry;
nrmdisk = DFLT_RMDISK;
i = rmdisk_count = 0;
if ((dirp = opendir("/dev/dsk")) == NULL) {
perror("gettext(open /dev/dsk failure)");
exit(1);
}
while (dep = readdir(dirp)) {
is_cd = 0;
checked = 0;
removable = 0;
if (strncmp(dep->d_name, ".", 1) == 0)
continue;
if (sscanf(dep->d_name, "c%dt%d", &ctrl, &id) != 2)
continue;
for (j = 0; j < i; j++) {
if (id == rmdisk[j].id &&
ctrl == rmdisk[j].controller &&
(strcmp(dep->d_name, rmdisk[j].name) == 0)) {
checked = 1;
break;
}
if (id == rmdisk[j].id && ctrl != rmdisk[j].controller)
id = rmdisk[j].id + 1;
}
if (checked)
continue;
for (j = 0; j < cd_count; j++) {
if (id == cd[j].id && ctrl == cd[j].controller) {
is_cd = 1;
break;
}
}
if (is_cd)
continue;
(void) snprintf(path, sizeof (path), "%s%s", "/dev/rdsk/",
dep->d_name);
if ((fd = open(path, O_RDONLY | O_NONBLOCK)) < 0)
continue;
(void) ioctl(fd, DKIOCREMOVABLE, &removable);
(void) close(fd);
if (removable == 0)
continue;
if (i == nrmdisk) {
nrmdisk = expandmem(i, (void **)&rmdisk,
sizeof (struct rmdisk));
(void) expandmem(i, (void **)&rmdisk_r,
sizeof (struct rmdisk));
}
nm = (char *)malloc(SIZE_OF_DSK + 1 + strlen(dep->d_name) + 1);
if (nm == NULL)
no_memory();
(void) strcpy(nm, "/dev/dsk/");
(void) strcat(nm, dep->d_name);
rmdisk[i].name = nm;
rmdisk[i].id = id;
rmdisk[i].controller = ctrl;
rmdisk[i].device = "";
rmdisk[i].number = id;
rmdisk_r[i].name = strdup(path);
i++;
}
rmdisk_count = i;
(void) closedir(dirp);
for (i = 0, j = rmdisk_count; i < rmdisk_count; i++, j++) {
if (j == nrmdisk) {
nrmdisk = expandmem(j, (void **)&rmdisk,
sizeof (struct rmdisk));
}
rmdisk[j].name = rmdisk_r[i].name;
rmdisk[j].id = rmdisk[i].id;
rmdisk[j].controller = rmdisk[i].controller;
rmdisk[j].device = rmdisk[i].device;
rmdisk[j].number = rmdisk[i].number;
}
rmdisk_count = j;
for (i = 0; i < 8; i++) {
for (j = 0; j < rmdisk_count; j++) {
if (rmdisk[j].id != i)
continue;
if (do_files) {
(void) da_add_list(&devlist, rmdisk[j].name, i,
DA_RMDISK);
} else if (do_devalloc) {
(void) printf("%s%d%s\\\n",
DA_RMDISK_NAME, i, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RMDISK_TYPE, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DA_RESERVED, KV_DELIMITER);
(void) printf("\t%s%s\\\n",
DEFAULT_DEV_ALLOC_AUTH, KV_DELIMITER);
(void) printf("\t%s\n", DA_DEFAULT_DISK_CLEAN);
break;
} else if (do_devmaps) {
if (first) {
(void) printf(" ");
} else {
(void) printf("%s%d%s\\\n",
DA_RMDISK_NAME, i,
KV_TOKEN_DELIMIT);
(void) printf("\t%s%s\\\n",
DA_RMDISK_TYPE, KV_TOKEN_DELIMIT);
(void) printf("\t");
first++;
}
(void) printf("%s", rmdisk[j].name);
}
}
if (do_devmaps && first) {
(void) printf("\n\n");
first = 0;
}
}
if (do_files && rmdisk_count) {
dargs.rootdir = NULL;
dargs.devnames = NULL;
dargs.optflag = DA_ADD;
for (entry = devlist.rmdisk; entry != NULL;
entry = entry->next) {
dargs.devinfo = &(entry->devinfo);
(void) da_update_device(&dargs);
}
}
}
static void
initmem()
{
tape = (struct tape *)calloc(DFLT_NTAPE, sizeof (struct tape));
audio = (struct audio *)calloc(DFLT_NAUDIO, sizeof (struct audio));
cd = (struct cd *)calloc(DFLT_NCD, sizeof (struct cd));
fp = (struct fp *)calloc(DFLT_NFP, sizeof (struct fp));
if (system_labeled) {
rmdisk = (struct rmdisk *)calloc(DFLT_RMDISK,
sizeof (struct rmdisk));
if (rmdisk == NULL)
no_memory();
rmdisk_r = (struct rmdisk *)calloc(DFLT_RMDISK,
sizeof (struct rmdisk));
if (rmdisk_r == NULL)
no_memory();
}
if (tape == NULL || audio == NULL || cd == NULL || fp == NULL)
no_memory();
devlist.audio = devlist.cd = devlist.floppy = devlist.rmdisk =
devlist.tape = NULL;
}
static int
expandmem(int n, void **array, int size)
{
void *old = *array;
void *new;
new = (void *)calloc(n + DELTA, size);
if (new == NULL) {
perror("memory allocation failed");
exit(1);
}
bcopy(old, new, n * size);
free(old);
*array = new;
return (n + DELTA);
}
static void
no_memory(void)
{
(void) fprintf(stderr, "%s: %s\n", "mkdevalloc",
gettext("out of memory"));
exit(1);
}