#include <unistd.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <ctype.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/stropts.h>
#include <sys/sad.h>
#include "ttymon.h"
#include "tmstruct.h"
#include "tmextern.h"
#define NSTRPUSH 9
int
check_device(char *device)
{
struct stat statbuf;
if ((device == NULL) || (*device == '\0')) {
log("error -- device field is missing");
return (-1);
}
if (*device != '/') {
log("error -- must specify full path name for \"%s\".", device);
return (-1);
}
if (access(device, 0) == 0) {
if (stat(device, &statbuf) < 0) {
log("stat(%s) failed: %s", device, strerror(errno));
return (-1);
}
if ((statbuf.st_mode & S_IFMT) != S_IFCHR) {
log("error -- \"%s\" not character special device",
device);
return (-1);
}
} else {
log("error -- device \"%s\" does not exist", device);
return (-1);
}
return (0);
}
int
check_cmd(char *cmd)
{
struct stat statbuf;
char tbuf[BUFSIZ];
char *tp = tbuf;
if ((cmd == NULL) || (*cmd == '\0')) {
log("error -- server command is missing");
return (-1);
}
(void) strcpy(tp, cmd);
(void) strtok(tp, " \t");
if (*tp != '/') {
log("error -- must specify full path name for \"%s\".", tp);
return (-1);
}
if (access(tp, 0) == 0) {
if (stat(tp, &statbuf) < 0) {
log("stat(%s) failed.", tp);
return (-1);
}
if (!(statbuf.st_mode & 0111)) {
log("error -- \"%s\" not executable\n", tp);
return (-1);
}
if ((statbuf.st_mode & S_IFMT) != S_IFREG) {
log("error -- \"%s\" not a regular file", tp);
return (-1);
}
} else {
log("error -- \"%s\" does not exist", tp);
return (-1);
}
return (0);
}
int
strcheck(char *sp, int flag)
{
char *cp;
if (flag == NUM) {
for (cp = sp; *cp; cp++) {
if (!isdigit(*cp)) {
return (-1);
}
}
} else {
for (cp = sp; *cp; cp++) {
if (!isalnum(*cp)) {
return (-1);
}
}
}
return (0);
}
int
vml(char *modules)
{
char *modp, *svmodp;
int i, fd;
struct str_mlist newmods[NSTRPUSH];
struct str_list newlist;
if ((modules == NULL) || (*modules == '\0'))
return (0);
newlist.sl_modlist = newmods;
newlist.sl_nmods = NSTRPUSH;
if ((modp = malloc(strlen(modules) + 1)) == NULL) {
log("vml: malloc failed");
return (-1);
};
svmodp = modp;
(void) strcpy(modp, modules);
for (i = 0, modp = strtok(modp, ",");
modp != NULL; i++, modp = strtok(NULL, ",")) {
if (i >= NSTRPUSH) {
log("too many modules in <%s>", modules);
i = -1;
break;
}
(void) strncpy(newlist.sl_modlist[i].l_name, modp, FMNAMESZ);
newlist.sl_modlist[i].l_name[FMNAMESZ] = '\0';
}
free(svmodp);
if (i == -1)
return (-1);
newlist.sl_nmods = i;
if ((fd = open(USERDEV, O_RDWR)) == -1) {
if (errno == EBUSY) {
log("Warning - can't validate module list, "
"/dev/sad/user busy");
return (0);
}
log("open /dev/sad/user failed: %s", strerror(errno));
return (-1);
}
if ((i = ioctl(fd, SAD_VML, &newlist)) < 0) {
log("Validate modules ioctl failed, modules = <%s>: %s",
modules, strerror(errno));
(void) close(fd);
return (-1);
}
if (i != 0) {
log("Error - invalid STREAMS module list <%s>.", modules);
(void) close(fd);
return (-1);
}
(void) close(fd);
return (0);
}
void
copystr(char *s1, char *s2)
{
while (*s2) {
if (*s2 == ':') {
*s1++ = '\\';
}
*s1++ = *s2++;
}
*s1 = '\0';
}
void
cons_printf(const char *fmt, ...)
{
char buf[MAXPATHLEN * 2];
int fd;
va_list ap;
va_start(ap, fmt);
(void) vsnprintf(buf, sizeof (buf), fmt, ap);
va_end(ap);
if ((fd = open(CONSOLE, O_WRONLY|O_NOCTTY)) != -1)
(void) write(fd, buf, strlen(buf) + 1);
(void) close(fd);
}