122438Sdist /* 222438Sdist * Copyright (c) 1983 Regents of the University of California. 322438Sdist * All rights reserved. The Berkeley software License Agreement 422438Sdist * specifies the terms and conditions for redistribution. 522438Sdist */ 622438Sdist 713955Ssam #ifndef lint 8*25498Seric static char sccsid[] = "@(#)recvjob.c 5.2 (Berkeley) 11/17/85"; 922438Sdist #endif not lint 1013955Ssam 1112115Sralph /* 1212115Sralph * Receive printer jobs from the network, queue them and 1312115Sralph * start the printer daemon. 1412115Sralph */ 1512115Sralph 1612115Sralph #include "lp.h" 1716763Sralph #include <sys/fs.h> 1812115Sralph 1916763Sralph char *sp = ""; 2016763Sralph #define ack() (void) write(1, sp, 1); 2112115Sralph 2216763Sralph char tfname[40]; /* tmp copy of cf before linking */ 2316763Sralph char *dfname; /* data files */ 2416763Sralph int minfree; /* keep at least minfree blocks available */ 2516763Sralph char *ddev; /* disk device (for checking free space) */ 2616763Sralph int dfd; /* file system device descriptor */ 2716763Sralph 2816763Sralph char *find_dev(); 2916763Sralph 3012115Sralph recvjob() 3112115Sralph { 3212464Sralph struct stat stb; 3312115Sralph char *bp = pbuf; 3416763Sralph int status, rcleanup(); 3512115Sralph 3612115Sralph /* 3712115Sralph * Perform lookup for printer name or abbreviation 3812115Sralph */ 3912115Sralph if ((status = pgetent(line, printer)) < 0) 4016763Sralph frecverr("cannot open printer description file"); 4112115Sralph else if (status == 0) 4216763Sralph frecverr("unknown printer %s", printer); 4312115Sralph if ((LF = pgetstr("lf", &bp)) == NULL) 4412115Sralph LF = DEFLOGF; 4512115Sralph if ((SD = pgetstr("sd", &bp)) == NULL) 4612115Sralph SD = DEFSPOOL; 4712464Sralph if ((LO = pgetstr("lo", &bp)) == NULL) 4812464Sralph LO = DEFLOCK; 4912115Sralph 50*25498Seric (void) close(2); /* set up log file */ 51*25498Seric if (open(LF, O_WRONLY|O_APPEND, 0664) < 0) { 52*25498Seric syslog(LOG_ERR, "%s: %m", LF); 53*25498Seric (void) open("/dev/null", O_WRONLY); 54*25498Seric } 55*25498Seric 5612115Sralph if (chdir(SD) < 0) 5716763Sralph frecverr("%s: %s: %m", printer, SD); 5816763Sralph if (stat(LO, &stb) == 0) { 5916763Sralph if (stb.st_mode & 010) { 6016763Sralph /* queue is disabled */ 6116763Sralph putchar('\1'); /* return error code */ 6216763Sralph exit(1); 6316763Sralph } 6416763Sralph } else if (stat(SD, &stb) < 0) 6516763Sralph frecverr("%s: %s: %m", printer, SD); 6616763Sralph minfree = read_number("minfree"); 6716763Sralph ddev = find_dev(stb.st_dev, S_IFBLK); 6816763Sralph if ((dfd = open(ddev, O_RDONLY)) < 0) 6916763Sralph syslog(LOG_WARNING, "%s: %s: %m", printer, ddev); 7016763Sralph signal(SIGTERM, rcleanup); 7116763Sralph signal(SIGPIPE, rcleanup); 7212115Sralph 7312115Sralph if (readjob()) 7412115Sralph printjob(); 7512115Sralph } 7612115Sralph 7716763Sralph char * 7816763Sralph find_dev(dev, type) 7916763Sralph register dev_t dev; 8016763Sralph register int type; 8116763Sralph { 8216763Sralph register DIR *dfd = opendir("/dev"); 8316763Sralph struct direct *dir; 8416763Sralph struct stat stb; 8516763Sralph char devname[MAXNAMLEN+6]; 8616763Sralph char *dp; 8712115Sralph 8816763Sralph strcpy(devname, "/dev/"); 8916763Sralph while ((dir = readdir(dfd))) { 9016763Sralph strcpy(devname + 5, dir->d_name); 9116763Sralph if (stat(devname, &stb)) 9216763Sralph continue; 9316763Sralph if ((stb.st_mode & S_IFMT) != type) 9416763Sralph continue; 9516763Sralph if (dev == stb.st_rdev) { 9616763Sralph closedir(dfd); 9716763Sralph dp = (char *)malloc(strlen(devname)+1); 9816763Sralph strcpy(dp, devname); 9916763Sralph return(dp); 10016763Sralph } 10116763Sralph } 10216763Sralph closedir(dfd); 10316763Sralph frecverr("cannot find device %d, %d", major(dev), minor(dev)); 10416763Sralph /*NOTREACHED*/ 10516763Sralph } 10616763Sralph 10712115Sralph /* 10812115Sralph * Read printer jobs sent by lpd and copy them to the spooling directory. 10912115Sralph * Return the number of jobs successfully transfered. 11012115Sralph */ 11116763Sralph readjob() 11212115Sralph { 11312115Sralph register int size, nfiles; 11412115Sralph register char *cp; 11512115Sralph 11612115Sralph ack(); 11712115Sralph nfiles = 0; 11812115Sralph for (;;) { 11912115Sralph /* 12012115Sralph * Read a command to tell us what to do 12112115Sralph */ 12212115Sralph cp = line; 12312115Sralph do { 12412115Sralph if ((size = read(1, cp, 1)) != 1) { 12512115Sralph if (size < 0) 12616763Sralph frecverr("%s: Lost connection",printer); 12712115Sralph return(nfiles); 12812115Sralph } 12912115Sralph } while (*cp++ != '\n'); 13012115Sralph *--cp = '\0'; 13112115Sralph cp = line; 13212115Sralph switch (*cp++) { 13312115Sralph case '\1': /* cleanup because data sent was bad */ 13416763Sralph rcleanup(); 13512115Sralph continue; 13612115Sralph 13712115Sralph case '\2': /* read cf file */ 13812115Sralph size = 0; 13912115Sralph while (*cp >= '0' && *cp <= '9') 14012115Sralph size = size * 10 + (*cp++ - '0'); 14112115Sralph if (*cp++ != ' ') 14212115Sralph break; 14312115Sralph strcpy(tfname, cp); 14412115Sralph tfname[0] = 't'; 14516763Sralph if (!chksize(size)) { 14616763Sralph (void) write(1, "\2", 1); 14716763Sralph continue; 14816763Sralph } 14912115Sralph if (!readfile(tfname, size)) { 15016763Sralph rcleanup(); 15112115Sralph continue; 15212115Sralph } 15312115Sralph if (link(tfname, cp) < 0) 15416763Sralph frecverr("%s: %m", tfname); 15512115Sralph (void) unlink(tfname); 15612115Sralph tfname[0] = '\0'; 15712115Sralph nfiles++; 15812115Sralph continue; 15912115Sralph 16012115Sralph case '\3': /* read df file */ 16112115Sralph size = 0; 16212115Sralph while (*cp >= '0' && *cp <= '9') 16312115Sralph size = size * 10 + (*cp++ - '0'); 16412115Sralph if (*cp++ != ' ') 16512115Sralph break; 16616763Sralph if (!chksize(size)) { 16716763Sralph (void) write(1, "\2", 1); 16816763Sralph continue; 16916763Sralph } 17012115Sralph (void) readfile(dfname = cp, size); 17112115Sralph continue; 17212115Sralph } 17316763Sralph frecverr("protocol screwup"); 17412115Sralph } 17512115Sralph } 17612115Sralph 17712115Sralph /* 17812115Sralph * Read files send by lpd and copy them to the spooling directory. 17912115Sralph */ 18012115Sralph readfile(file, size) 18112115Sralph char *file; 18212115Sralph int size; 18312115Sralph { 18412115Sralph register char *cp; 18512115Sralph char buf[BUFSIZ]; 18612115Sralph register int i, j, amt; 18712115Sralph int fd, err; 18812115Sralph 18913149Ssam fd = open(file, O_WRONLY|O_CREAT, FILMOD); 19012115Sralph if (fd < 0) 19116763Sralph frecverr("%s: %m", file); 19212115Sralph ack(); 19312115Sralph err = 0; 19412115Sralph for (i = 0; i < size; i += BUFSIZ) { 19512115Sralph amt = BUFSIZ; 19612115Sralph cp = buf; 19712115Sralph if (i + amt > size) 19812115Sralph amt = size - i; 19912115Sralph do { 20012115Sralph j = read(1, cp, amt); 20112115Sralph if (j <= 0) 20216763Sralph frecverr("Lost connection"); 20312115Sralph amt -= j; 20412115Sralph cp += j; 20512115Sralph } while (amt > 0); 20612115Sralph amt = BUFSIZ; 20712115Sralph if (i + amt > size) 20812115Sralph amt = size - i; 20913170Sralph if (write(fd, buf, amt) != amt) { 21012115Sralph err++; 21113170Sralph break; 21213170Sralph } 21312115Sralph } 21412115Sralph (void) close(fd); 21512115Sralph if (err) 21616763Sralph frecverr("%s: write error", file); 21712115Sralph if (noresponse()) { /* file sent had bad data in it */ 21812115Sralph (void) unlink(file); 21912115Sralph return(0); 22012115Sralph } 22112115Sralph ack(); 22212115Sralph return(1); 22312115Sralph } 22412115Sralph 22512115Sralph noresponse() 22612115Sralph { 22712115Sralph char resp; 22812115Sralph 22912115Sralph if (read(1, &resp, 1) != 1) 23016763Sralph frecverr("Lost connection"); 23112115Sralph if (resp == '\0') 23212115Sralph return(0); 23312115Sralph return(1); 23412115Sralph } 23512115Sralph 23612115Sralph /* 23716763Sralph * Check to see if there is enough space on the disk for size bytes. 23816763Sralph * 1 == OK, 0 == Not OK. 23916763Sralph */ 24016763Sralph chksize(size) 24116763Sralph int size; 24216763Sralph { 24316763Sralph struct stat stb; 24416763Sralph register char *ddev; 24516763Sralph int spacefree; 24616763Sralph struct fs fs; 24716763Sralph 24816763Sralph if (dfd < 0 || lseek(dfd, (long)(SBLOCK * DEV_BSIZE), 0) < 0) 24916763Sralph return(1); 25016763Sralph if (read(dfd, (char *)&fs, sizeof fs) != sizeof fs) 25116763Sralph return(1); 25216763Sralph spacefree = (fs.fs_cstotal.cs_nbfree * fs.fs_frag + 25316763Sralph fs.fs_cstotal.cs_nffree - fs.fs_dsize * fs.fs_minfree / 100) * 25416763Sralph fs.fs_fsize / 1024; 25516763Sralph size = (size + 1023) / 1024; 25616763Sralph if (minfree + size > spacefree) 25716763Sralph return(0); 25816763Sralph return(1); 25916763Sralph } 26016763Sralph 26116763Sralph read_number(fn) 26216763Sralph char *fn; 26316763Sralph { 26416763Sralph char lin[80]; 26516763Sralph register FILE *fp; 26616763Sralph 26716763Sralph if ((fp = fopen(fn, "r")) == NULL) 26816763Sralph return (0); 26916763Sralph if (fgets(lin, 80, fp) == NULL) { 27016763Sralph fclose(fp); 27116763Sralph return (0); 27216763Sralph } 27316763Sralph fclose(fp); 27416763Sralph return (atoi(lin)); 27516763Sralph } 27616763Sralph 27716763Sralph /* 27812115Sralph * Remove all the files associated with the current job being transfered. 27912115Sralph */ 28016763Sralph rcleanup() 28112115Sralph { 28212115Sralph 28312115Sralph if (tfname[0]) 28412115Sralph (void) unlink(tfname); 28512115Sralph if (dfname) 28612115Sralph do { 28712115Sralph do 28812115Sralph (void) unlink(dfname); 28916938Sralph while (dfname[2]-- != 'A'); 29016938Sralph dfname[2] = 'z'; 29116938Sralph } while (dfname[0]-- != 'd'); 29212115Sralph } 29312115Sralph 29416763Sralph frecverr(msg, a1, a2) 29512115Sralph char *msg; 29612115Sralph { 29716763Sralph rcleanup(); 29816763Sralph syslog(LOG_ERR, msg, a1, a2); 29912115Sralph putchar('\1'); /* return error code */ 30012115Sralph exit(1); 30112115Sralph } 302