xref: /illumos-gate/usr/src/cmd/lp/cmd/lpsched/files.c (revision 55fea89d)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50a44ef6dSjacobs  * Common Development and Distribution License (the "License").
60a44ef6dSjacobs  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23f48205beScasper  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27df1eb1adSjacobs /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28df1eb1adSjacobs /*	  All Rights Reserved  	*/
29df1eb1adSjacobs 
307c478bd9Sstevel@tonic-gate #include "lpsched.h"
317c478bd9Sstevel@tonic-gate #include <syslog.h>
324cb30b8aSwendyp #include <strings.h>
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate static char time_buf[50];
357c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
367c478bd9Sstevel@tonic-gate static char *extractReqno(char *req_file);
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /**
407c478bd9Sstevel@tonic-gate  ** chfiles() - CHANGE OWNERSHIP OF FILES, RETURN TOTAL SIZE
417c478bd9Sstevel@tonic-gate  **/
427c478bd9Sstevel@tonic-gate 
chfiles(char ** list,uid_t uid,gid_t gid)437c478bd9Sstevel@tonic-gate off_t chfiles ( char * * list, uid_t uid, gid_t gid )	/* funcdef */
447c478bd9Sstevel@tonic-gate {
457c478bd9Sstevel@tonic-gate     size_t	total;
467c478bd9Sstevel@tonic-gate     struct stat	stbuf;
477c478bd9Sstevel@tonic-gate     char	*file;
48*55fea89dSDan Cross 
497c478bd9Sstevel@tonic-gate     total = 0;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate     while(file = *list++)
527c478bd9Sstevel@tonic-gate     {
537c478bd9Sstevel@tonic-gate 	if (STRNEQU(Lp_Temp, file, strlen(Lp_Temp)) ||
547c478bd9Sstevel@tonic-gate 	    STRNEQU(Lp_Tmp, file, strlen(Lp_Tmp)))
557c478bd9Sstevel@tonic-gate 	{
567c478bd9Sstevel@tonic-gate 	    /*
577c478bd9Sstevel@tonic-gate 	     * Once this routine (chfiles) is called for a request,
587c478bd9Sstevel@tonic-gate 	     * any temporary files are ``ours'', i.e. they are on our
597c478bd9Sstevel@tonic-gate 	     * machine. A user running on an RFS-connected remote machine
607c478bd9Sstevel@tonic-gate 	     * can't necessarily know our machine name, so can't put
617c478bd9Sstevel@tonic-gate 	     * the files where they belong (Lp_Tmp/machine). But now we
627c478bd9Sstevel@tonic-gate 	     * can. Of course, this is all done with mirrors, as Lp_Temp
637c478bd9Sstevel@tonic-gate 	     * and Lp_Tmp/local-machine are symbolicly linked. So we just
647c478bd9Sstevel@tonic-gate 	     * change the name. This saves on wear and tear later.
657c478bd9Sstevel@tonic-gate 	     */
667c478bd9Sstevel@tonic-gate 	    if (STRNEQU(Lp_Temp, file, strlen(Lp_Temp)))
677c478bd9Sstevel@tonic-gate 	    {
687c478bd9Sstevel@tonic-gate 		char *newfile = makepath(Lp_Tmp, Local_System,
697c478bd9Sstevel@tonic-gate 				file + strlen(Lp_Temp) + 1, NULL);
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate 		Free(file);
727c478bd9Sstevel@tonic-gate 		list[-1] = file = newfile;
737c478bd9Sstevel@tonic-gate 	    }
74*55fea89dSDan Cross 
75df1eb1adSjacobs 	    (void) chownmod(file, uid, gid, 0600);
767c478bd9Sstevel@tonic-gate 	}
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (Stat(file, &stbuf) == -1)
797c478bd9Sstevel@tonic-gate 	    return(-1);
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate 	switch (stbuf.st_mode & S_IFMT) {
827c478bd9Sstevel@tonic-gate 	case 0:
837c478bd9Sstevel@tonic-gate 	case S_IFREG:
847c478bd9Sstevel@tonic-gate 	    break;
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	case S_IFIFO:
877c478bd9Sstevel@tonic-gate 	    if (!isadmin(uid))
887c478bd9Sstevel@tonic-gate 		return(-1);
897c478bd9Sstevel@tonic-gate 	    /*
907c478bd9Sstevel@tonic-gate 	     * If any of the files is a FIFO, the size indicator
917c478bd9Sstevel@tonic-gate 	     * becomes meaningless. On the other hand, returning
927c478bd9Sstevel@tonic-gate 	     * a total of zero causes the request to be rejected,
937c478bd9Sstevel@tonic-gate 	     * so we return something > 0.
947c478bd9Sstevel@tonic-gate 	     */
957c478bd9Sstevel@tonic-gate 	    stbuf.st_size = 1;
967c478bd9Sstevel@tonic-gate 	    break;
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	case S_IFDIR:
997c478bd9Sstevel@tonic-gate 	case S_IFCHR:
1007c478bd9Sstevel@tonic-gate 	case S_IFBLK:
1017c478bd9Sstevel@tonic-gate 	default:
1027c478bd9Sstevel@tonic-gate 	    return(-1);
1037c478bd9Sstevel@tonic-gate 	}
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	total += stbuf.st_size;
1067c478bd9Sstevel@tonic-gate     }
1077c478bd9Sstevel@tonic-gate     return(total);
1087c478bd9Sstevel@tonic-gate }
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /**
1117c478bd9Sstevel@tonic-gate  ** rmfiles() - DELETE/LOG FILES FOR DEFUNCT REQUEST
1127c478bd9Sstevel@tonic-gate  **/
1137c478bd9Sstevel@tonic-gate 
rmfiles(RSTATUS * rp,int log_it)1147c478bd9Sstevel@tonic-gate void rmfiles ( RSTATUS * rp, int log_it )	/* funcdef */
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate     char	**file	= rp->request->file_list;
1177c478bd9Sstevel@tonic-gate     char	*path;
1187c478bd9Sstevel@tonic-gate     char	num[STRSIZE(MOST_FILES) + 1];
1197c478bd9Sstevel@tonic-gate     static int	fd	= -1;
1207c478bd9Sstevel@tonic-gate     int		reqfd;
1217c478bd9Sstevel@tonic-gate     int		count	= 0;
1227c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
1237c478bd9Sstevel@tonic-gate     struct stat	tmpBuf;
1247c478bd9Sstevel@tonic-gate     char	*idno = NULL;
1257c478bd9Sstevel@tonic-gate     char 	tmpName[BUFSIZ];
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate     if (rp->req_file) {
1307c478bd9Sstevel@tonic-gate 	    char *p, *q;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	   /*
1337c478bd9Sstevel@tonic-gate 	    * The secure request file is removed first to prevent
1347c478bd9Sstevel@tonic-gate 	    * reloading should the system crash while in rmfiles().
1357c478bd9Sstevel@tonic-gate 	    */
1367c478bd9Sstevel@tonic-gate 	    path = makepath(Lp_Requests, rp->req_file, (char *)0);
1377c478bd9Sstevel@tonic-gate 	    (void) Unlink(path);
1387c478bd9Sstevel@tonic-gate 	    Free(path);
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 	    /*
1417c478bd9Sstevel@tonic-gate 	     * Copy the request file to the log file, if asked,
1427c478bd9Sstevel@tonic-gate 	     * or simply remove it.
1437c478bd9Sstevel@tonic-gate 	     */
1447c478bd9Sstevel@tonic-gate 	    path = makepath(Lp_Tmp, rp->req_file, (char *)0);
1457c478bd9Sstevel@tonic-gate 	    if (log_it && rp->secure && rp->secure->req_id) {
1467c478bd9Sstevel@tonic-gate 		if (fd == -1)
1477c478bd9Sstevel@tonic-gate 		    fd = open_locked(Lp_ReqLog, "a", MODE_NOREAD);
1487c478bd9Sstevel@tonic-gate 		if ((fd  >= 0) && (reqfd = Open(path, O_RDONLY, 0)) != -1) {
1497c478bd9Sstevel@tonic-gate 		    register int	n;
1507c478bd9Sstevel@tonic-gate 		    char		buf[BUFSIZ];
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		    (void) strftime(time_buf, sizeof (time_buf),
1537c478bd9Sstevel@tonic-gate 			NULL, localtime(&(rp->secure->date)));
154f48205beScasper 		    fdprintf(fd, "= %s, uid %u, gid %u, size %ld, %s\n",
1557c478bd9Sstevel@tonic-gate 			rp->secure->req_id, rp->secure->uid, rp->secure->gid,
1567c478bd9Sstevel@tonic-gate 			rp->secure->size, time_buf);
1577c478bd9Sstevel@tonic-gate 		    if (rp->slow)
1587c478bd9Sstevel@tonic-gate 			fdprintf(fd, "x %s\n", rp->slow);
1597c478bd9Sstevel@tonic-gate 		    if (rp->fast)
1607c478bd9Sstevel@tonic-gate 			fdprintf(fd, "y %s\n", rp->fast);
1610a44ef6dSjacobs 		    if (rp->printer && rp->printer->printer)
1627c478bd9Sstevel@tonic-gate 			fdprintf(fd, "z %s\n", rp->printer->printer->name);
1637c478bd9Sstevel@tonic-gate 		    while ((n = Read(reqfd, buf, BUFSIZ)) > 0)
1647c478bd9Sstevel@tonic-gate 			write (fd, buf, n);
1657c478bd9Sstevel@tonic-gate 		    Close (reqfd);
1667c478bd9Sstevel@tonic-gate 		}
1677c478bd9Sstevel@tonic-gate 	    }
1687c478bd9Sstevel@tonic-gate 	    (void)Unlink (path);		/* remove request file */
1697c478bd9Sstevel@tonic-gate 	    Free (path);
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	    p = strdup(rp->req_file);		/* remove host/id file */
1727c478bd9Sstevel@tonic-gate 	    if (q = strrchr(p, '-')) {
173e4fb8a5fSToomas Soome 		    *q = '\0';
1747c478bd9Sstevel@tonic-gate 		    path = makepath(Lp_Tmp, p, NULL);
1757c478bd9Sstevel@tonic-gate 		    (void) Unlink(path);
1767c478bd9Sstevel@tonic-gate 		    Free(path);
1777c478bd9Sstevel@tonic-gate 	    }
1787c478bd9Sstevel@tonic-gate 	    Free(p);
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
1817c478bd9Sstevel@tonic-gate 	/* Look for a PAPI job attribute file, if it exists remove it */
1827c478bd9Sstevel@tonic-gate 	idno = extractReqno(rp->req_file);
1837c478bd9Sstevel@tonic-gate 	snprintf(tmpName, sizeof (tmpName), "%s-%s", idno, LP_PAPIATTRNAME);
1847c478bd9Sstevel@tonic-gate 	path = makepath(Lp_Temp, tmpName, (char *)0);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	if (((path != NULL) && (idno != NULL)) && (stat(path, &tmpBuf) == 0))
1877c478bd9Sstevel@tonic-gate 	{
1887c478bd9Sstevel@tonic-gate 	    /* PAPI job attribute file exists for this job so remove it */
1897c478bd9Sstevel@tonic-gate 	    (void) Unlink(path);
1907c478bd9Sstevel@tonic-gate 	}
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	Free(idno);
1937c478bd9Sstevel@tonic-gate 	Free(path);
1947c478bd9Sstevel@tonic-gate #endif
1957c478bd9Sstevel@tonic-gate     }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate     if (file)					/* remove file in filelist */
1987c478bd9Sstevel@tonic-gate 	while(*file)
1997c478bd9Sstevel@tonic-gate 	{
2007c478bd9Sstevel@tonic-gate 		/*
2017c478bd9Sstevel@tonic-gate 		 * The copies of user files.
2027c478bd9Sstevel@tonic-gate 		 */
2034cb30b8aSwendyp 		if ((STRNEQU(Lp_Temp, *file, strlen(Lp_Temp)) ||
2044cb30b8aSwendyp 		    STRNEQU(Lp_Tmp, *file, strlen(Lp_Tmp))) &&
2054cb30b8aSwendyp 		    (! strstr(*file, "../")))
2064cb30b8aSwendyp 
2077c478bd9Sstevel@tonic-gate 		    (void) Unlink(*file);
2084cb30b8aSwendyp 
2097c478bd9Sstevel@tonic-gate 		count++;
2107c478bd9Sstevel@tonic-gate 		file++;
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate     if (rp->secure && rp->secure->req_id) {
2147c478bd9Sstevel@tonic-gate 	char *p;
2157c478bd9Sstevel@tonic-gate 	p = getreqno(rp->secure->req_id);
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	/*
2187c478bd9Sstevel@tonic-gate 	 * The filtered files. We can't rely on just the RS_FILTERED
2197c478bd9Sstevel@tonic-gate 	 * flag, since the request may have been cancelled while
2207c478bd9Sstevel@tonic-gate 	 * filtering. On the other hand, just checking "rp->slow"
2217c478bd9Sstevel@tonic-gate 	 * doesn't mean that the files exist, because the request
2227c478bd9Sstevel@tonic-gate 	 * may have been canceled BEFORE filtering started. Oh well.
2237c478bd9Sstevel@tonic-gate 	 */
2247c478bd9Sstevel@tonic-gate 	if (rp->slow)
2257c478bd9Sstevel@tonic-gate 	    while(count > 0)
2267c478bd9Sstevel@tonic-gate 	    {
2277c478bd9Sstevel@tonic-gate 		sprintf(num, "%d", count--);
2280a44ef6dSjacobs 		path = makestr(Lp_Temp, "/F", p, "-", num, (char *)0);
2297c478bd9Sstevel@tonic-gate 		Unlink(path);
2307c478bd9Sstevel@tonic-gate 		Free(path);
2317c478bd9Sstevel@tonic-gate 	    }
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 	/*
2347c478bd9Sstevel@tonic-gate 	 * The notify/error file.
2357c478bd9Sstevel@tonic-gate 	 */
2367c478bd9Sstevel@tonic-gate 	path = makepath(Lp_Temp, p, (char *)0);
2377c478bd9Sstevel@tonic-gate 	(void) Unlink(path);
2387c478bd9Sstevel@tonic-gate 	Free(path);
2397c478bd9Sstevel@tonic-gate     }
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate /**
2437c478bd9Sstevel@tonic-gate  ** _alloc_req_id(void) - ALLOCATE NEXT REQUEST ID
2447c478bd9Sstevel@tonic-gate  **/
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate #define	SEQF_DEF_START	1
2477c478bd9Sstevel@tonic-gate #define	SEQF_DEF_END	59999
2487c478bd9Sstevel@tonic-gate #define	SEQF_DEF_INCR	1
2497c478bd9Sstevel@tonic-gate #define	SEQF		".SEQF"
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate long
_alloc_req_id(void)2537c478bd9Sstevel@tonic-gate _alloc_req_id ( void )
2547c478bd9Sstevel@tonic-gate {
2557c478bd9Sstevel@tonic-gate 	static short		started	= 0;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	static int		fd;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	static long		start;
2607c478bd9Sstevel@tonic-gate 	static long		end;
2617c478bd9Sstevel@tonic-gate 	static long		incr;
2627c478bd9Sstevel@tonic-gate 	static long		curr;
2637c478bd9Sstevel@tonic-gate 	static long		wrap;
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	static char		fmt[
2667c478bd9Sstevel@tonic-gate 				STRSIZE(BIGGEST_REQID_S)/* start   */
2677c478bd9Sstevel@tonic-gate 			      + 1			/* :       */
2687c478bd9Sstevel@tonic-gate 			      + STRSIZE(BIGGEST_REQID_S)/* end     */
2697c478bd9Sstevel@tonic-gate 			      + 1			/* :       */
2707c478bd9Sstevel@tonic-gate 			      + STRSIZE(BIGGEST_REQID_S)/* incr    */
2717c478bd9Sstevel@tonic-gate 			      + 1			/* :       */
2727c478bd9Sstevel@tonic-gate 			      + 4			/* %ld\n   */
2737c478bd9Sstevel@tonic-gate 			      + 1			/* (nul)   */
2747c478bd9Sstevel@tonic-gate 				];
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	char 			buf[256];
2777c478bd9Sstevel@tonic-gate 	int len;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	long			ret;
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	if (!started) {
2837c478bd9Sstevel@tonic-gate 		snprintf(buf, sizeof (buf), "%s/%s", Lp_Temp, SEQF);
2847c478bd9Sstevel@tonic-gate 		if (((fd = open_locked(buf, "r+", 0644)) < 0) &&
2857c478bd9Sstevel@tonic-gate 		    ((fd = open_locked(buf, "w", 0644)) < 0))
2867c478bd9Sstevel@tonic-gate 			fail ("Can't open file %s (%s).\n", buf, PERROR);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 		lseek(fd, 0, SEEK_SET);
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 		read(fd, buf, sizeof (buf));
2917c478bd9Sstevel@tonic-gate 		if (sscanf(buf, "%ld:%ld:%ld:%ld\n", &start, &end, &incr, &curr) != 4) {
2927c478bd9Sstevel@tonic-gate 			start = SEQF_DEF_START;
2937c478bd9Sstevel@tonic-gate 			end = SEQF_DEF_END;
2947c478bd9Sstevel@tonic-gate 			curr = start;
2957c478bd9Sstevel@tonic-gate 			incr = SEQF_DEF_INCR;
2967c478bd9Sstevel@tonic-gate 		}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 		if (start < 0)
2997c478bd9Sstevel@tonic-gate 			start = SEQF_DEF_START;
3007c478bd9Sstevel@tonic-gate 		if (end > SEQF_DEF_END)
3017c478bd9Sstevel@tonic-gate 			end = SEQF_DEF_END;
3027c478bd9Sstevel@tonic-gate 		if (curr < start || curr > end)
3037c478bd9Sstevel@tonic-gate 			curr = start;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 		sprintf (fmt, "%ld:%ld:%ld:%%ld\n", start, end, incr);
3067c478bd9Sstevel@tonic-gate 		started = 1;
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	wrap = curr;
310*55fea89dSDan Cross 	do {
3117c478bd9Sstevel@tonic-gate 		ret = curr;
3127c478bd9Sstevel@tonic-gate 		if ((curr += incr) > end)
3137c478bd9Sstevel@tonic-gate 	    	curr = start;
3147c478bd9Sstevel@tonic-gate 
315*55fea89dSDan Cross 	} while ( wrap != curr && ((RSTATUS *)request_by_id_num(ret)) ) ;
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	/* write the new id file */
3187c478bd9Sstevel@tonic-gate 	lseek(fd, 0, SEEK_SET);
3197c478bd9Sstevel@tonic-gate 	len = sprintf(buf, fmt, curr);
3207c478bd9Sstevel@tonic-gate 	write(fd, buf, len);
3217c478bd9Sstevel@tonic-gate 	ftruncate(fd, len);
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate 	if (curr == wrap) {
3247c478bd9Sstevel@tonic-gate 		note("alloc_req_id(): out of ids\n");
3257c478bd9Sstevel@tonic-gate 		errno = EEXIST;
3267c478bd9Sstevel@tonic-gate 		return(SEQF_DEF_START-1);
3277c478bd9Sstevel@tonic-gate 	} else
3287c478bd9Sstevel@tonic-gate 		return (ret);
3297c478bd9Sstevel@tonic-gate }
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate /**
3327c478bd9Sstevel@tonic-gate  ** _alloc_file() - ALLOCATE FILES FOR A REQUEST
3337c478bd9Sstevel@tonic-gate  **/
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate char *
_alloc_files(int num,char * prefix,uid_t uid,gid_t gid)3367c478bd9Sstevel@tonic-gate _alloc_files (
3377c478bd9Sstevel@tonic-gate 	int			num,
3387c478bd9Sstevel@tonic-gate 	char *			prefix,
3397c478bd9Sstevel@tonic-gate 	uid_t			uid,
3400a44ef6dSjacobs 	gid_t			gid
3417c478bd9Sstevel@tonic-gate )
3427c478bd9Sstevel@tonic-gate {
3437c478bd9Sstevel@tonic-gate 	static char		base[
3447c478bd9Sstevel@tonic-gate 				1			/* F       */
3457c478bd9Sstevel@tonic-gate 			      + STRSIZE(BIGGEST_REQID_S)/* req-id  */
3467c478bd9Sstevel@tonic-gate 			      + 1			/* -       */
3477c478bd9Sstevel@tonic-gate 			      + STRSIZE(MOST_FILES_S)	/* file-no */
3487c478bd9Sstevel@tonic-gate 			      + 1			/* (nul)   */
3497c478bd9Sstevel@tonic-gate 				];
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	char *			file;
3527c478bd9Sstevel@tonic-gate 	char *			cp;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	int			fd;
3557c478bd9Sstevel@tonic-gate 	int			plus;
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (num > BIGGEST_REQID)
3597c478bd9Sstevel@tonic-gate 		return (0);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	if (!prefix) {
3627c478bd9Sstevel@tonic-gate 		int id;
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		if ((id = _alloc_req_id()) < SEQF_DEF_START )
3657c478bd9Sstevel@tonic-gate 			return(NULL); /* Out of request IDs (errno = EEXIST) */
3667c478bd9Sstevel@tonic-gate 		snprintf (base, sizeof (base), "%d-%d", id, MOST_FILES);
3677c478bd9Sstevel@tonic-gate 		plus = 0;
3687c478bd9Sstevel@tonic-gate 	} else {
3697c478bd9Sstevel@tonic-gate 		if (strlen(prefix) > (size_t) 6)
3707c478bd9Sstevel@tonic-gate 			return (0);
3717c478bd9Sstevel@tonic-gate 		snprintf (base, sizeof (base), "F%s-%d", prefix, MOST_FILES);
3727c478bd9Sstevel@tonic-gate 		plus = 1;
3737c478bd9Sstevel@tonic-gate 	}
3747c478bd9Sstevel@tonic-gate 
3750a44ef6dSjacobs 	file = makepath(Lp_Temp, base, (char *)0);
376*55fea89dSDan Cross 
3777c478bd9Sstevel@tonic-gate 	cp = strrchr(file, '-') + 1;
3787c478bd9Sstevel@tonic-gate 	while (num--) {
3797c478bd9Sstevel@tonic-gate 		sprintf (cp, "%d", num + plus);
3807c478bd9Sstevel@tonic-gate 		if ((fd = Open(file, O_CREAT|O_TRUNC, 0600)) == -1) {
3817c478bd9Sstevel@tonic-gate 			Free (file);
3827c478bd9Sstevel@tonic-gate 			return (0);
3837c478bd9Sstevel@tonic-gate 		} else {
3847c478bd9Sstevel@tonic-gate 			Close (fd);
385df1eb1adSjacobs 			(void) chownmod(file, uid, gid, 0600);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
3907c478bd9Sstevel@tonic-gate 	if (prefix == NULL)
3917c478bd9Sstevel@tonic-gate 	{
3927c478bd9Sstevel@tonic-gate 		/*
3937c478bd9Sstevel@tonic-gate 		 * Initial job request (s_alloc_files) so create an empty PAPI
3947c478bd9Sstevel@tonic-gate 		 * Attribute file; note, this file will only be used if the
3957c478bd9Sstevel@tonic-gate 		 * print job has been submitted via the PAPI interface.
3967c478bd9Sstevel@tonic-gate 		 */
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 		file = (char *)Realloc(file, strlen(file) +
3997c478bd9Sstevel@tonic-gate 					strlen(LP_PAPIATTRNAME) + 1);
4007c478bd9Sstevel@tonic-gate 		if (file != NULL)
4017c478bd9Sstevel@tonic-gate 		{
4027c478bd9Sstevel@tonic-gate 			cp = strrchr(file, '-') + 1;
4037c478bd9Sstevel@tonic-gate 			sprintf(cp, "%s", LP_PAPIATTRNAME);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 			if ((fd = Open(file, O_CREAT|O_TRUNC, 0600)) == -1)
4067c478bd9Sstevel@tonic-gate 			{
4077c478bd9Sstevel@tonic-gate 				Free(file);
4087c478bd9Sstevel@tonic-gate 				return (0);
4097c478bd9Sstevel@tonic-gate 			}
4107c478bd9Sstevel@tonic-gate 			else
4117c478bd9Sstevel@tonic-gate 			{
4127c478bd9Sstevel@tonic-gate 				Close(fd);
413df1eb1adSjacobs 				(void) chownmod(file, uid, gid, 0600);
4147c478bd9Sstevel@tonic-gate 			}
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 			Free(file);
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate #endif
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	if ((cp = strrchr(base, '-')))
4237c478bd9Sstevel@tonic-gate 		*cp = 0;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	return (base);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate #ifdef LP_USE_PAPI_ATTR
extractReqno(char * req_file)4307c478bd9Sstevel@tonic-gate static char *extractReqno(char *req_file)
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate {
4337c478bd9Sstevel@tonic-gate 	char *start = NULL;
4347c478bd9Sstevel@tonic-gate 	char *end = NULL;
4357c478bd9Sstevel@tonic-gate 	char *result = NULL;
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	start = strrchr(req_file, '/');
4387c478bd9Sstevel@tonic-gate 	end = strrchr(req_file, '-');
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate 	if ((start != NULL) && (end != NULL))
4417c478bd9Sstevel@tonic-gate 	{
4427c478bd9Sstevel@tonic-gate 		start++;
4437c478bd9Sstevel@tonic-gate 		if (end > start)
4447c478bd9Sstevel@tonic-gate 		{
4457c478bd9Sstevel@tonic-gate 			int n = end - start;
4467c478bd9Sstevel@tonic-gate 			result = (char *)Malloc(n+1);
4477c478bd9Sstevel@tonic-gate 			strncpy(result, start, n);
4487c478bd9Sstevel@tonic-gate 			result[n] = '\0';
4497c478bd9Sstevel@tonic-gate 		}
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	return (result);
4537c478bd9Sstevel@tonic-gate } /* extractReqno() */
4547c478bd9Sstevel@tonic-gate #endif
455