BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.sbin / lpr / lpd / recvjob.c
index 6aa6f6b..f199bd9 100644 (file)
@@ -2,23 +2,37 @@
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
  * Copyright (c) 1983 Regents of the University of California.
  * All rights reserved.
  *
- * Redistribution and use in source and binary forms are permitted provided
- * that: (1) source distributions retain this entire copyright notice and
- * comment, and (2) distributions including binaries display the following
- * acknowledgement:  ``This product includes software developed by the
- * University of California, Berkeley and its contributors'' in the
- * documentation or other materials provided with the distribution and in
- * all advertising materials mentioning features or use of this software.
- * Neither the name of the University nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
- * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
- * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-static char sccsid[] = "@(#)recvjob.c  5.11 (Berkeley) 6/1/90";
+static char sccsid[] = "@(#)recvjob.c  5.15 (Berkeley) 5/4/91";
 #endif /* not lint */
 
 /*
 #endif /* not lint */
 
 /*
@@ -27,8 +41,8 @@ static char sccsid[] = "@(#)recvjob.c 5.11 (Berkeley) 6/1/90";
  */
 
 #include "lp.h"
  */
 
 #include "lp.h"
-#include <ufs/fs.h>
 #include "pathnames.h"
 #include "pathnames.h"
+#include <sys/mount.h>
 
 char   *sp = "";
 #define ack()  (void) write(1, sp, 1);
 
 char   *sp = "";
 #define ack()  (void) write(1, sp, 1);
@@ -36,16 +50,14 @@ char        *sp = "";
 char    tfname[40];            /* tmp copy of cf before linking */
 char    dfname[40];            /* data files */
 int    minfree;                /* keep at least minfree blocks available */
 char    tfname[40];            /* tmp copy of cf before linking */
 char    dfname[40];            /* data files */
 int    minfree;                /* keep at least minfree blocks available */
-char   *ddev;                  /* disk device (for checking free space) */
-int    dfd;                    /* file system device descriptor */
 
 
-char   *find_dev();
+void   rcleanup();
 
 recvjob()
 {
        struct stat stb;
        char *bp = pbuf;
 
 recvjob()
 {
        struct stat stb;
        char *bp = pbuf;
-       int status, rcleanup();
+       int status;
 
        /*
         * Perform lookup for printer name or abbreviation
 
        /*
         * Perform lookup for printer name or abbreviation
@@ -77,10 +89,7 @@ recvjob()
                }
        } else if (stat(SD, &stb) < 0)
                frecverr("%s: %s: %m", printer, SD);
                }
        } else if (stat(SD, &stb) < 0)
                frecverr("%s: %s: %m", printer, SD);
-       minfree = read_number("minfree");
-       ddev = find_dev(stb.st_dev, S_IFBLK);
-       if ((dfd = open(ddev, O_RDONLY)) < 0)
-               syslog(LOG_WARNING, "%s: %s: %m", printer, ddev);
+       minfree = 2 * read_number("minfree");   /* scale KB to 512 blocks */
        signal(SIGTERM, rcleanup);
        signal(SIGPIPE, rcleanup);
 
        signal(SIGTERM, rcleanup);
        signal(SIGPIPE, rcleanup);
 
@@ -88,36 +97,6 @@ recvjob()
                printjob();
 }
 
                printjob();
 }
 
-char *
-find_dev(dev, type)
-       register dev_t dev;
-       register int type;
-{
-       register DIR *dfd = opendir(_PATH_DEV);
-       struct direct *dir;
-       struct stat stb;
-       char devname[MAXNAMLEN+6];
-       char *dp;
-
-       strcpy(devname, _PATH_DEV);
-       while ((dir = readdir(dfd))) {
-               strcpy(devname + 5, dir->d_name);
-               if (stat(devname, &stb))
-                       continue;
-               if ((stb.st_mode & S_IFMT) != type)
-                       continue;
-               if (dev == stb.st_rdev) {
-                       closedir(dfd);
-                       dp = (char *)malloc(strlen(devname)+1);
-                       strcpy(dp, devname);
-                       return(dp);
-               }
-       }
-       closedir(dfd);
-       frecverr("cannot find device %d, %d", major(dev), minor(dev));
-       /*NOTREACHED*/
-}
-
 /*
  * Read printer jobs sent by lpd and copy them to the spooling directory.
  * Return the number of jobs successfully transfered.
 /*
  * Read printer jobs sent by lpd and copy them to the spooling directory.
  * Return the number of jobs successfully transfered.
@@ -190,7 +169,8 @@ readjob()
                        }
                        (void) strcpy(dfname, cp);
                        if (index(dfname, '/'))
                        }
                        (void) strcpy(dfname, cp);
                        if (index(dfname, '/'))
-                               frecverr("illegal path name");
+                               frecverr("readjob: %s: illegal path name",
+                                       dfname);
                        (void) readfile(dfname, size);
                        continue;
                }
                        (void) readfile(dfname, size);
                        continue;
                }
@@ -210,9 +190,9 @@ readfile(file, size)
        register int i, j, amt;
        int fd, err;
 
        register int i, j, amt;
        int fd, err;
 
-       fd = open(file, O_WRONLY|O_CREAT, FILMOD);
+       fd = open(file, O_CREAT|O_EXCL|O_WRONLY, FILMOD);
        if (fd < 0)
        if (fd < 0)
-               frecverr("%s: %m", file);
+               frecverr("readfile: %s: illegal path name: %m", file);
        ack();
        err = 0;
        for (i = 0; i < size; i += BUFSIZ) {
        ack();
        err = 0;
        for (i = 0; i < size; i += BUFSIZ) {
@@ -265,14 +245,14 @@ chksize(size)
        int size;
 {
        int spacefree;
        int size;
 {
        int spacefree;
-       struct fs fs;
+       struct statfs sfb;
 
 
-       if (dfd < 0 || lseek(dfd, (long)(SBOFF), 0) < 0)
-               return(1);
-       if (read(dfd, (char *)&fs, sizeof fs) != sizeof fs)
-               return(1);
-       spacefree = freespace(&fs, fs.fs_minfree) * fs.fs_fsize / 1024;
-       size = (size + 1023) / 1024;
+       if (statfs(".", &sfb) < 0) {
+               syslog(LOG_ERR, "%s: %m", "statfs(\".\")");
+               return (1);
+       }
+       spacefree = sfb.f_bavail * (sfb.f_fsize / 512);
+       size = (size + 511) / 512;
        if (minfree + size > spacefree)
                return(0);
        return(1);
        if (minfree + size > spacefree)
                return(0);
        return(1);
@@ -297,6 +277,7 @@ read_number(fn)
 /*
  * Remove all the files associated with the current job being transfered.
  */
 /*
  * Remove all the files associated with the current job being transfered.
  */
+void
 rcleanup()
 {
 
 rcleanup()
 {