예제 #1
0
int
main(int argc, char *argv[])
{
	/* Automatics */
	char		      **argp;		/* Ptr to current argument */
	struct reservdev      **rsvd;		/* Ptr to list of locks */
	struct reservdev      **plk;		/* Running ptr to locks */
	char		       *devtab;		/* Ptr to device table name */
	char		       *rsvtab;		/* Ptr to dev-rsv-tbl name */
	char		       *p;		/* Temp char pointer */
	int			argcount;	/* Number of args on cmd */
	long			lkey;		/* Key for locking (long) */
	int			key;		/* Key for locking */
	int			halt;		/* TRUE if we need to stop */
	int			sev;		/* Message severity */
	int			exitcode;	/* Value of command */
	int			syntaxerr;	/* Flag, TRUE if syntax error */
	int			exitcd;		/* Value for exit() */
	int			c;		/* Option character */


	/*
	 * Initializations
	 */

	/* Build a message label */
	if (p = strrchr(argv[0], '/')) p++;
	else p = argv[0];
	(void) strlcat(strcpy(lbl, "UX:"), p, sizeof(lbl));

	/* Make only the text component of messages appear (remove this in SVR4.1) */
	(void) putenv("MSGVERB=text");

	
	/*
	 * Parse the options from the command line
	 */

	opterr = 0;
	syntaxerr = FALSE;
	while ((c = getopt(argc, argv, "")) != EOF) switch(c) {
	default:
	    syntaxerr = FALSE;
	    break;
	}


	/* Argument initializations */
	argp = &argv[optind];
	if ((argcount = argc-optind) < 1) syntaxerr = TRUE;


	/* If there's (an obvious) syntax error, write a message and quit */
	if (syntaxerr) {
	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
	    exit(EX_ERROR);
	}


	/*
	 *  devfree key
	 *
	 *  	Free all devices that have been reserved using the key "key".
	 */

	if (argcount == 1) {

	    /* Extract the key from the command */
	    lkey = strtol(*argp, &p, 10);
	    if (*p || (lkey <= 0) || (lkey > MAXINT)) {
		(void) snprintf(msg, sizeof (msg), M_INVKEY, *argp);
		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
		exit(EX_ERROR);
	    }
	    key = (int) lkey;

	    /* Get the list of devices currently reserved */
	    if (rsvd = reservdev()) {
		exitcd = EX_OK;
		for (plk = rsvd ; *plk ; plk++) {
		    if ((*plk)->key == key) 
			if (devfree(key, (*plk)->devname) != 0) 
			    exitcd = EX_NOFREE;
		}
	    } else {
		if (((errno == ENOENT) || (errno == EACCES)) && (rsvtab = _rsvtabpath())) {
		    (void) snprintf(msg, sizeof(msg), M_RSVTAB, rsvtab);
		    exitcd = EX_TBLERR;
		    sev = MM_ERROR;
		} else {
		    (void) snprintf(msg, sizeof (msg), M_ERROR, errno);
		    exitcd = EX_ERROR;
		    sev = MM_HALT;
		}
		stdmsg(MM_NRECOV, lbl, sev, msg);
	    }

	    /* Done */
	    exit(exitcd);
	}


	/*
	 *  devfree key device [...]
	 *
	 *	Free specific devices
	 */

	/* Open the device file (if there's one to be opened) */
	if (!_opendevtab("r")) {
	    if (devtab = _devtabpath()) {
		(void) snprintf(msg, sizeof(msg), M_DEVTAB, devtab);
		exitcd = EX_TBLERR;
		sev = MM_ERROR;
	    } else {
		(void) snprintf(msg, sizeof (msg), M_ERROR, errno);
		exitcd = EX_ERROR;
		sev = MM_HALT;
	    }
	    stdmsg(MM_NRECOV, lbl, sev, msg);
	    exit(exitcd);
	}

	/* Extract the key from the command */
	lkey = strtol(*argp, &p, 10);
	if (*p || (lkey <= 0) || (lkey > MAXINT)) {
	    (void) snprintf(msg, sizeof(msg), M_INVKEY, *argp);
	    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
	    exit(EX_ERROR);
	}
	key = (int) lkey;
	argp++;

	/* Loop through the list of devices to free */
	exitcode = EX_OK;
	halt = FALSE;
	while (!halt && *argp) {

	    /* Try to free the device */
	    if (devfree(key, *argp) != 0) {
		if ((errno == EACCES) || (errno == ENOENT)) {
		    
		    /* Can't get at reservation file */
		    if (rsvtab = _rsvtabpath()) {
			exitcode = EX_TBLERR;
			(void) snprintf(msg, sizeof(msg), M_RSVTAB, rsvtab);
			sev = MM_ERROR;
		    }
		    else {
			exitcode = EX_ERROR;
			(void) snprintf(msg, sizeof (msg), M_ERROR, errno);
			sev = MM_HALT;
		    }
		    stdmsg(MM_NRECOV, lbl, sev, msg);
		    halt = TRUE;
		}
	        else if (errno == EPERM) {

		    /* Wrong key */
		    (void) snprintf(msg, sizeof(msg), M_NOTONKEY, *argp);
		    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
		    exitcode = EX_NOFREE;
		}
		else if (errno == EINVAL) {

		    /* Device not reserved */
		    (void) snprintf(msg, sizeof(msg), M_NOTRSVD, *argp);
		    stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
		    exitcode = EX_NOFREE;
		}
	
		else {

		    /* Some other strange error occurred */
		    (void) snprintf(msg, sizeof (msg), M_ERROR, errno);
		    stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
		    exitcode = EX_ERROR;
		    halt = TRUE;
		}
	    }
	    argp++;
	}


	/* Exit with the appropriate code */
	return(exitcode);
}
예제 #2
0
파일: listdgrp.c 프로젝트: andreiw/polaris
int
main(int argc, char **argv)
{

	/* 
	 *  Automatic data
	 */

	char	      **devices;	/* List of devices in the group */
	char	      **pp;		/* Running pointer to device names */
	char	       *cmdname;	/* Simple command name */
	char	       *dgrptab;	/* The device-group table name */
	char	       *dgroup;		/* Device group to list */
	int		exitcode;	/* Value to return to the caller */
	int		sev;		/* Message severity */
	int		optchar;	/* Option char (from getopt()) */
	int		usageerr;	/* TRUE if syntax error on command */


	/* Build the message label from the (simple) command name */
	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
	else cmdname = argv[0];
	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));

	/* Only write the text component of a message (this goes away in SVR4.1) */
	(void) putenv("MSGVERB=text");

	/* 
	 *  Parse the command line:
	 *	- Options
	 *	- Device group to display
	 */

	/* 
	 *  Extract options from the command line 
	 */

	/* Initializations */
	usageerr = FALSE;	/* No errors on the command line (yet) */

	/* 
	 *  Loop until all of the command line options have been parced 
	 *  (and don't let getopt() write messages) 
	 */

	opterr = FALSE;	
	while ((optchar = getopt(argc, argv, "")) != EOF) switch (optchar) {

	/* Default case -- command usage error */
	case '?':
	default:
	    usageerr = TRUE;
	    break;
	}

	/* Check the argument count and extract the device group name */
	if (usageerr || (optind != argc-1)) usageerr = TRUE;
	else dgroup = argv[optind];

	/* If there is a usage error, write an appropriate message and exit */
	if (usageerr) {
	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
	    exit(EX_ERROR);
	}

	/* Open the device-group file (if there's one to be opened) */
	if (!_opendgrptab("r")) {
	    if (dgrptab = _dgrptabpath()) {
		(void) snprintf(msg, sizeof(msg), M_DGRPTAB, dgrptab);
		exitcode = EX_NODGRPTAB;
		sev = MM_ERROR;
	    } else {
		(void) sprintf(msg, M_ERROR, errno);
		exitcode = EX_ERROR;
		sev = MM_HALT;
	    }
	    stdmsg(MM_NRECOV, lbl, sev, msg);
	    exit(exitcode);
	}


	/*
	 * Get the list of devices associated with the device group.
	 * If we get one, write the list to the standard output.
	 * Otherwise, write an appropriate error message
	 */

	exitcode = EX_OK;
	if (devices = listdgrp(dgroup)) {
	    for (pp = devices ; *pp ; pp++) (void) puts(*pp);
	}
	else {
	    if (errno == EINVAL) {
		(void) snprintf(msg, sizeof(msg), M_NODGRP, dgroup);
		stdmsg(MM_NRECOV, lbl, MM_ERROR, msg);
		exitcode = EX_NODGROUP;
	    }
	    else {
		(void) sprintf(msg, M_ERROR, errno);
		stdmsg(MM_NRECOV, lbl, MM_HALT, msg);
		exitcode = EX_ERROR;
	    }
	}

	/* Finished (now wasn't that special?) */
	return(exitcode);
}
예제 #3
0
int
main(int argc, char **argv)
{

	/* 
	 *  Automatic data
	 */

	char	      **arglist;	/* List of arguments */
	char	      **criterialist;	/* List of criteria */
	char	      **devicelist;	/* List of devices to search/ignore */
	char	      **fitdevlist;	/* List of devices that fit criteria */
	char	       *cmdname;	/* Simple command name */
	char	       *device;		/* Device name in the list */
	char	       *devtab;		/* The device table name */
	int		exitcode;	/* Value to return to the caller */
	int		sev;		/* Message severity */
	int		optchar;	/* Option character (from getopt()) */
	int		andflag;	/* TRUE if criteria are to be anded */
	int		excludeflag;	/* TRUE if exclude "devices" lists */
	int		options;	/* Options to pass to getdev() */
	int		usageerr;	/* TRUE if syntax error */


	/* Build the message label from the (simple) command name */
	if ((cmdname = strrchr(argv[0], '/')) != (char *) NULL) cmdname++;
	else cmdname = argv[0];
	(void) strlcat(strcpy(lbl, "UX:"), cmdname, sizeof(lbl));

	/* Write text-component of messages only (goes away in SVR4.1) */
	(void) putenv("MSGVERB=text");

	/* 
	 *  Parse the command line:
	 *	- Options
	 *	- Selection criteria
	 *	- Devices to include or exclude
	 */

	/* 
	 *  Extract options from the command line 
	 */

	/* Initializations */
	andflag = FALSE;	/* No -a -- Or criteria data */
	excludeflag = FALSE;	/* No -e -- Include only mentioned devices */
	usageerr = FALSE;	/* No errors on the command line (yet) */

	/* 
	 *  Loop until all of the command line options have been parced 
	 */
	opterr = FALSE;			/* Don't let getopt() write messages */
	while ((optchar = getopt(argc, argv, "ae")) != EOF) switch (optchar) {

	/* -a  List devices that fit all of the criteria listed */
	case 'a': 
	    if (andflag) usageerr = TRUE;
	    else andflag = TRUE;
	    break;

	/* -e  Exclude those devices mentioned on the command line */
	case 'e':
	    if (excludeflag) usageerr = TRUE;
	    else excludeflag = TRUE;
	    break;

	/* Default case -- command usage error */
	case '?':
	default:
	    usageerr = TRUE;
	    break;
	}

	/* If there is a usage error, write an appropriate message and exit */
	if (usageerr) {
	    stdmsg(MM_NRECOV, lbl, MM_ERROR, M_USAGE);
	    exit(EX_ERROR);
	}

	/* Open the device file (if there's one to be opened) */
	if (!_opendevtab("r")) {
	    if (devtab = _devtabpath()) {
		(void) snprintf(txt, sizeof(txt), M_DEVTAB, devtab);
		sev = MM_ERROR;
		exitcode = EX_DEVTAB;
	    } else {
		(void) sprintf(txt, M_ERROR, errno);
		sev = MM_HALT;
		exitcode = EX_ERROR;
	    }
	    stdmsg(MM_NRECOV, lbl, sev, txt);
	    exit(exitcode);
	}

	/* Build the list of criteria and devices */
	arglist = argv + optind;
	criterialist = buildcriterialist(arglist);
	devicelist = builddevlist(arglist);
	options = (excludeflag?DTAB_EXCLUDEFLAG:0)|(andflag?DTAB_ANDCRITERIA:0);

	/* 
	 *  Get the list of devices that meets the criteria requested.  If we 
	 *  got a list (that might be empty), write that list to the standard 
	 *  output file (stdout).
	 */

	exitcode = 0;
	if (!(fitdevlist = getdev(devicelist, criterialist, options))) {
	    exitcode = 1;
	}
	else for (device = *fitdevlist++ ; device ; device = *fitdevlist++)
		(void) puts(device);

	/* Finished */
	return(exitcode);
}