/*
 *	Network Queueing System (NQS)
 *  This version of NQS is Copyright (C) 1992  John Roman
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
/*++ readreq.c - Network Queueing System
 *
 * $Source: /usr2/jrroma/nqs/nqs-3.30/lib/RCS/readreq.c,v $
 *
 * DESCRIPTION:
 *
 *	Read the request header portion of the control file for an
 *	NQS request.
 *
 *
 *	Author:
 *	-------
 *	Brent A. Kingsbury, Sterling Software Incorporated.
 *	August 12, 1985.
 *
 *
 * STANDARDS VIOLATIONS:
 *   None.
 *
 * REVISION HISTORY: ($Revision: 1.3 $ $Date: 1992/12/22 15:46:23 $ $State: Exp $)
 * $Log: readreq.c,v $
 * Revision 1.3  1992/12/22  15:46:23  jrroma
 * Version 3.30
 *
 * Revision 1.2  92/06/18  13:24:33  jrroma
 * Added gnu header
 * 
 * Revision 1.1  92/06/18  12:46:16  jrroma
 * Initial revision
 * 
 *
 */

#include "nqs.h"
#include <unistd.h>
#include <errno.h>


/*** readreq
 *
 *
 *	int readreq():
 *
 *	Read the request header portion of the control file for an
 *	NQS request.
 *
 *	If the return value of this function is 0, then the lseek()
 *	position of the control file descriptor upon return, is
 *	ALWAYS left at the first byte of the varying portion of the
 *	control file.
 *
 *
 *	Returns:
 *		0: if the successful, and the request is valid;
 *	       -1: otherwise.
 *
 *			Note:	errno is returned 0, unless a
 *				system call error occurred.
 */
int readreq (fd, rawreq)
int fd;				/* Control file descriptor */
struct rawreq *rawreq;		/* Raw request information to be returned */
{
	register int sizeread;
	register int headersize;

	/*
	 *  Read the entire control file header in one read() system
	 *  call.
	 *
	 *  We do this for reasons of efficiency, and also so that the
	 *  first block of the control file is read in one atomic I/O
	 *  operation so that concurrent Qmod updates (being done by the
	 *  careful indivisible rewrite of the first control file block),
	 *  and multiple read accesses do not step on each other.
	 */
	errno = 0;			/* Clear errno */
	if (lseek (fd, 0L, 0) == -1) {	/* Seek to the start of the file */
		return (-1);		/* Return EBADF */
	}
	sizeread = read (fd, (char *) rawreq, sizeof (struct rawreq));
	headersize = ((char *) &rawreq->v) - ((char *) rawreq);
	if (rawreq->type == RTYPE_DEVICE) {
		/*
		 *  The request is a device-oriented request.
		 */
		headersize += sizeof (struct rawdevreq);
		if (sizeread < headersize) {
			/*
			 *  Part of the control file is missing!
			 */
			return (-1);
		}
	}
	else {
		/*
		 *  The request is not device-oriented.
		 */
		headersize += sizeof (struct rawbatreq);
		if (sizeread < headersize) {
			/*
			 *  Part of the control file is missing!
			 */
			return (-1);
		}
	}
	/*
	 *  Move file pointer to the end of the header area.
	 *  This is CRITICAL.
	 */
	lseek (fd, (long) headersize, 0);
	/*
	 *  Return results of header verification.
	 */
	return (verifyreq (rawreq));
}
