Index: head/bin/pax/buf_subs.c =================================================================== --- head/bin/pax/buf_subs.c (revision 327229) +++ head/bin/pax/buf_subs.c (revision 327230) @@ -1,990 +1,990 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1992 Keith Muller. * Copyright (c) 1992, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Keith Muller of the University of California, San Diego. * * 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. 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 #if 0 static char sccsid[] = "@(#)buf_subs.c 8.2 (Berkeley) 4/18/94"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include "pax.h" #include "extern.h" /* * routines which implement archive and file buffering */ #define MINFBSZ 512 /* default block size for hole detect */ #define MAXFLT 10 /* default media read error limit */ /* * Need to change bufmem to dynamic allocation when the upper * limit on blocking size is removed (though that will violate pax spec) * MAXBLK define and tests will also need to be updated. */ static char bufmem[MAXBLK+BLKMULT]; /* i/o buffer + pushback id space */ static char *buf; /* normal start of i/o buffer */ static char *bufend; /* end or last char in i/o buffer */ static char *bufpt; /* read/write point in i/o buffer */ int blksz = MAXBLK; /* block input/output size in bytes */ int wrblksz; /* user spec output size in bytes */ int maxflt = MAXFLT; /* MAX consecutive media errors */ int rdblksz; /* first read blksize (tapes only) */ off_t wrlimit; /* # of bytes written per archive vol */ off_t wrcnt; /* # of bytes written on current vol */ off_t rdcnt; /* # of bytes read on current vol */ /* * wr_start() * set up the buffering system to operate in a write mode * Return: * 0 if ok, -1 if the user specified write block size violates pax spec */ int wr_start(void) { buf = &(bufmem[BLKMULT]); /* * Check to make sure the write block size meets pax specs. If the user * does not specify a blocksize, we use the format default blocksize. * We must be picky on writes, so we do not allow the user to create an * archive that might be hard to read elsewhere. If all ok, we then * open the first archive volume */ if (!wrblksz) wrblksz = frmt->bsz; if (wrblksz > MAXBLK) { paxwarn(1, "Write block size of %d too large, maximum is: %d", wrblksz, MAXBLK); return(-1); } if (wrblksz % BLKMULT) { paxwarn(1, "Write block size of %d is not a %d byte multiple", wrblksz, BLKMULT); return(-1); } if (wrblksz > MAXBLK_POSIX) { paxwarn(0, "Write block size of %d larger than POSIX max %d, archive may not be portable", wrblksz, MAXBLK_POSIX); return(-1); } /* * we only allow wrblksz to be used with all archive operations */ blksz = rdblksz = wrblksz; if ((ar_open(arcname) < 0) && (ar_next() < 0)) return(-1); wrcnt = 0; bufend = buf + wrblksz; bufpt = buf; return(0); } /* * rd_start() * set up buffering system to read an archive * Return: * 0 if ok, -1 otherwise */ int rd_start(void) { /* * leave space for the header pushback (see get_arc()). If we are * going to append and user specified a write block size, check it * right away */ buf = &(bufmem[BLKMULT]); if ((act == APPND) && wrblksz) { if (wrblksz > MAXBLK) { paxwarn(1,"Write block size %d too large, maximum is: %d", wrblksz, MAXBLK); return(-1); } if (wrblksz % BLKMULT) { paxwarn(1, "Write block size %d is not a %d byte multiple", wrblksz, BLKMULT); return(-1); } } /* * open the archive */ if ((ar_open(arcname) < 0) && (ar_next() < 0)) return(-1); bufend = buf + rdblksz; bufpt = bufend; rdcnt = 0; return(0); } /* * cp_start() * set up buffer system for copying within the file system */ void cp_start(void) { buf = &(bufmem[BLKMULT]); rdblksz = blksz = MAXBLK; } /* * appnd_start() * Set up the buffering system to append new members to an archive that * was just read. The last block(s) of an archive may contain a format * specific trailer. To append a new member, this trailer has to be * removed from the archive. The first byte of the trailer is replaced by * the start of the header of the first file added to the archive. The * format specific end read function tells us how many bytes to move * backwards in the archive to be positioned BEFORE the trailer. Two * different positions have to be adjusted, the O.S. file offset (e.g. the * position of the tape head) and the write point within the data we have * stored in the read (soon to become write) buffer. We may have to move * back several records (the number depends on the size of the archive * record and the size of the format trailer) to read up the record where * the first byte of the trailer is recorded. Trailers may span (and * overlap) record boundaries. * We first calculate which record has the first byte of the trailer. We * move the OS file offset back to the start of this record and read it * up. We set the buffer write pointer to be at this byte (the byte where * the trailer starts). We then move the OS file pointer back to the * start of this record so a flush of this buffer will replace the record * in the archive. * A major problem is rewriting this last record. For archives stored * on disk files, this is trivial. However, many devices are really picky * about the conditions under which they will allow a write to occur. * Often devices restrict the conditions where writes can be made writes, * so it may not be feasible to append archives stored on all types of * devices. * Return: * 0 for success, -1 for failure */ int appnd_start(off_t skcnt) { int res; off_t cnt; if (exit_val != 0) { paxwarn(0, "Cannot append to an archive that may have flaws."); return(-1); } /* * if the user did not specify a write blocksize, inherit the size used * in the last archive volume read. (If a is set we still use rdblksz * until next volume, cannot shift sizes within a single volume). */ if (!wrblksz) wrblksz = blksz = rdblksz; else blksz = rdblksz; /* * make sure that this volume allows appends */ if (ar_app_ok() < 0) return(-1); /* * Calculate bytes to move back and move in front of record where we * need to start writing from. Remember we have to add in any padding * that might be in the buffer after the trailer in the last block. We * travel skcnt + padding ROUNDED UP to blksize. */ skcnt += bufend - bufpt; if ((cnt = (skcnt/blksz) * blksz) < skcnt) cnt += blksz; if (ar_rev((off_t)cnt) < 0) goto out; /* * We may have gone too far if there is valid data in the block we are * now in front of, read up the block and position the pointer after * the valid data. */ if ((cnt -= skcnt) > 0) { /* * watch out for stupid tape drives. ar_rev() will set rdblksz * to be real physical blocksize so we must loop until we get * the old rdblksz (now in blksz). If ar_rev() fouls up the * determination of the physical block size, we will fail. */ bufpt = buf; bufend = buf + blksz; while (bufpt < bufend) { if ((res = ar_read(bufpt, rdblksz)) <= 0) goto out; bufpt += res; } if (ar_rev((off_t)(bufpt - buf)) < 0) goto out; bufpt = buf + cnt; bufend = buf + blksz; } else { /* * buffer is empty */ bufend = buf + blksz; bufpt = buf; } rdblksz = blksz; rdcnt -= skcnt; wrcnt = 0; /* * At this point we are ready to write. If the device requires special * handling to write at a point were previously recorded data resides, * that is handled in ar_set_wr(). From now on we operate under normal * ARCHIVE mode (write) conditions */ if (ar_set_wr() < 0) return(-1); act = ARCHIVE; return(0); out: paxwarn(1, "Unable to rewrite archive trailer, cannot append."); return(-1); } /* * rd_sync() * A read error occurred on this archive volume. Resync the buffer and * try to reset the device (if possible) so we can continue to read. Keep * trying to do this until we get a valid read, or we reach the limit on * consecutive read faults (at which point we give up). The user can * adjust the read error limit through a command line option. * Returns: * 0 on success, and -1 on failure */ int rd_sync(void) { int errcnt = 0; int res; /* * if the user says bail out on first fault, we are out of here... */ if (maxflt == 0) return(-1); if (act == APPND) { paxwarn(1, "Unable to append when there are archive read errors."); return(-1); } /* * poke at device and try to get past media error */ if (ar_rdsync() < 0) { if (ar_next() < 0) return(-1); else rdcnt = 0; } for (;;) { if ((res = ar_read(buf, blksz)) > 0) { /* * All right! got some data, fill that buffer */ bufpt = buf; bufend = buf + res; rdcnt += res; return(0); } /* * Oh well, yet another failed read... * if error limit reached, ditch. o.w. poke device to move past * bad media and try again. if media is badly damaged, we ask * the poor (and upset user at this point) for the next archive * volume. remember the goal on reads is to get the most we * can extract out of the archive. */ if ((maxflt > 0) && (++errcnt > maxflt)) paxwarn(0,"Archive read error limit (%d) reached",maxflt); else if (ar_rdsync() == 0) continue; if (ar_next() < 0) break; rdcnt = 0; errcnt = 0; } return(-1); } /* * pback() * push the data used during the archive id phase back into the I/O * buffer. This is required as we cannot be sure that the header does NOT * overlap a block boundary (as in the case we are trying to recover a * flawed archived). This was not designed to be used for any other * purpose. (What software engineering, HA!) * WARNING: do not even THINK of pback greater than BLKMULT, unless the * pback space is increased. */ void pback(char *pt, int cnt) { bufpt -= cnt; memcpy(bufpt, pt, cnt); return; } /* * rd_skip() * skip forward in the archive during an archive read. Used to get quickly * past file data and padding for files the user did NOT select. * Return: * 0 if ok, -1 failure, and 1 when EOF on the archive volume was detected. */ int rd_skip(off_t skcnt) { off_t res; off_t cnt; off_t skipped = 0; /* * consume what data we have in the buffer. If we have to move forward * whole records, we call the low level skip function to see if we can * move within the archive without doing the expensive reads on data we * do not want. */ if (skcnt == 0) return(0); res = MIN((bufend - bufpt), skcnt); bufpt += res; skcnt -= res; /* * if skcnt is now 0, then no additional i/o is needed */ if (skcnt == 0) return(0); /* * We have to read more, calculate complete and partial record reads * based on rdblksz. we skip over "cnt" complete records */ res = skcnt%rdblksz; cnt = (skcnt/rdblksz) * rdblksz; /* * if the skip fails, we will have to resync. ar_fow will tell us * how much it can skip over. We will have to read the rest. */ if (ar_fow(cnt, &skipped) < 0) return(-1); res += cnt - skipped; rdcnt += skipped; /* * what is left we have to read (which may be the whole thing if * ar_fow() told us the device can only read to skip records); */ while (res > 0L) { cnt = bufend - bufpt; /* * if the read fails, we will have to resync */ if ((cnt <= 0) && ((cnt = buf_fill()) < 0)) return(-1); if (cnt == 0) return(1); cnt = MIN(cnt, res); bufpt += cnt; res -= cnt; } return(0); } /* * wr_fin() * flush out any data (and pad if required) the last block. We always pad * with zero (even though we do not have to). Padding with 0 makes it a * lot easier to recover if the archive is damaged. zero padding SHOULD * BE a requirement.... */ void wr_fin(void) { if (bufpt > buf) { memset(bufpt, 0, bufend - bufpt); bufpt = bufend; (void)buf_flush(blksz); } } /* * wr_rdbuf() * fill the write buffer from data passed to it in a buffer (usually used * by format specific write routines to pass a file header). On failure we * punt. We do not allow the user to continue to write flawed archives. * We assume these headers are not very large (the memory copy we use is * a bit expensive). * Return: * 0 if buffer was filled ok, -1 o.w. (buffer flush failure) */ int wr_rdbuf(char *out, int outcnt) { int cnt; /* - * while there is data to copy copy into the write buffer. when the + * while there is data to copy into the write buffer. when the * write buffer fills, flush it to the archive and continue */ while (outcnt > 0) { cnt = bufend - bufpt; if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) return(-1); /* * only move what we have space for */ cnt = MIN(cnt, outcnt); memcpy(bufpt, out, cnt); bufpt += cnt; out += cnt; outcnt -= cnt; } return(0); } /* * rd_wrbuf() * copy from the read buffer into a supplied buffer a specified number of * bytes. If the read buffer is empty fill it and continue to copy. * usually used to obtain a file header for processing by a format * specific read routine. * Return * number of bytes copied to the buffer, 0 indicates EOF on archive volume, * -1 is a read error */ int rd_wrbuf(char *in, int cpcnt) { int res; int cnt; int incnt = cpcnt; /* * loop until we fill the buffer with the requested number of bytes */ while (incnt > 0) { cnt = bufend - bufpt; if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) { /* * read error, return what we got (or the error if * no data was copied). The caller must know that an * error occurred and has the best knowledge what to * do with it */ if ((res = cpcnt - incnt) > 0) return(res); return(cnt); } /* * calculate how much data to copy based on whats left and * state of buffer */ cnt = MIN(cnt, incnt); memcpy(in, bufpt, cnt); bufpt += cnt; incnt -= cnt; in += cnt; } return(cpcnt); } /* * wr_skip() * skip forward during a write. In other words add padding to the file. * we add zero filled padding as it makes flawed archives much easier to * recover from. the caller tells us how many bytes of padding to add * This routine was not designed to add HUGE amount of padding, just small * amounts (a few 512 byte blocks at most) * Return: * 0 if ok, -1 if there was a buf_flush failure */ int wr_skip(off_t skcnt) { int cnt; /* * loop while there is more padding to add */ while (skcnt > 0L) { cnt = bufend - bufpt; if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) return(-1); cnt = MIN(cnt, skcnt); memset(bufpt, 0, cnt); bufpt += cnt; skcnt -= cnt; } return(0); } /* * wr_rdfile() * fill write buffer with the contents of a file. We are passed an open * file descriptor to the file and the archive structure that describes the * file we are storing. The variable "left" is modified to contain the * number of bytes of the file we were NOT able to write to the archive. * it is important that we always write EXACTLY the number of bytes that * the format specific write routine told us to. The file can also get * bigger, so reading to the end of file would create an improper archive, * we just detect this case and warn the user. We never create a bad * archive if we can avoid it. Of course trying to archive files that are * active is asking for trouble. It we fail, we pass back how much we * could NOT copy and let the caller deal with it. * Return: * 0 ok, -1 if archive write failure. a short read of the file returns a * 0, but "left" is set to be greater than zero. */ int wr_rdfile(ARCHD *arcn, int ifd, off_t *left) { int cnt; int res = 0; off_t size = arcn->sb.st_size; struct stat sb; /* * while there are more bytes to write */ while (size > 0L) { cnt = bufend - bufpt; if ((cnt <= 0) && ((cnt = buf_flush(blksz)) < 0)) { *left = size; return(-1); } cnt = MIN(cnt, size); if ((res = read(ifd, bufpt, cnt)) <= 0) break; size -= res; bufpt += res; } /* * better check the file did not change during this operation * or the file read failed. */ if (res < 0) syswarn(1, errno, "Read fault on %s", arcn->org_name); else if (size != 0L) paxwarn(1, "File changed size during read %s", arcn->org_name); else if (fstat(ifd, &sb) < 0) syswarn(1, errno, "Failed stat on %s", arcn->org_name); else if (arcn->sb.st_mtime != sb.st_mtime) paxwarn(1, "File %s was modified during copy to archive", arcn->org_name); *left = size; return(0); } /* * rd_wrfile() * extract the contents of a file from the archive. If we are unable to * extract the entire file (due to failure to write the file) we return * the numbers of bytes we did NOT process. This way the caller knows how * many bytes to skip past to find the next archive header. If the failure * was due to an archive read, we will catch that when we try to skip. If * the format supplies a file data crc value, we calculate the actual crc * so that it can be compared to the value stored in the header * NOTE: * We call a special function to write the file. This function attempts to * restore file holes (blocks of zeros) into the file. When files are * sparse this saves space, and is a LOT faster. For non sparse files * the performance hit is small. As of this writing, no archive supports * information on where the file holes are. * Return: * 0 ok, -1 if archive read failure. if we cannot write the entire file, * we return a 0 but "left" is set to be the amount unwritten */ int rd_wrfile(ARCHD *arcn, int ofd, off_t *left) { int cnt = 0; off_t size = arcn->sb.st_size; int res = 0; char *fnm = arcn->name; int isem = 1; int rem; int sz = MINFBSZ; struct stat sb; u_long crc = 0L; /* * pass the blocksize of the file being written to the write routine, * if the size is zero, use the default MINFBSZ */ if (fstat(ofd, &sb) == 0) { if (sb.st_blksize > 0) sz = (int)sb.st_blksize; } else syswarn(0,errno,"Unable to obtain block size for file %s",fnm); rem = sz; *left = 0L; /* * Copy the archive to the file the number of bytes specified. We have * to assume that we want to recover file holes as none of the archive * formats can record the location of file holes. */ while (size > 0L) { cnt = bufend - bufpt; /* * if we get a read error, we do not want to skip, as we may * miss a header, so we do not set left, but if we get a write * error, we do want to skip over the unprocessed data. */ if ((cnt <= 0) && ((cnt = buf_fill()) <= 0)) break; cnt = MIN(cnt, size); if ((res = file_write(ofd,bufpt,cnt,&rem,&isem,sz,fnm)) <= 0) { *left = size; break; } if (docrc) { /* * update the actual crc value */ cnt = res; while (--cnt >= 0) crc += *bufpt++ & 0xff; } else bufpt += res; size -= res; } /* * if the last block has a file hole (all zero), we must make sure this * gets updated in the file. We force the last block of zeros to be * written. just closing with the file offset moved forward may not put * a hole at the end of the file. */ if (isem && (arcn->sb.st_size > 0L)) file_flush(ofd, fnm, isem); /* * if we failed from archive read, we do not want to skip */ if ((size > 0L) && (*left == 0L)) return(-1); /* * some formats record a crc on file data. If so, then we compare the * calculated crc to the crc stored in the archive */ if (docrc && (size == 0L) && (arcn->crc != crc)) paxwarn(1,"Actual crc does not match expected crc %s",arcn->name); return(0); } /* * cp_file() * copy the contents of one file to another. used during -rw phase of pax * just as in rd_wrfile() we use a special write function to write the * destination file so we can properly copy files with holes. */ void cp_file(ARCHD *arcn, int fd1, int fd2) { int cnt; off_t cpcnt = 0L; int res = 0; char *fnm = arcn->name; int no_hole = 0; int isem = 1; int rem; int sz = MINFBSZ; struct stat sb; /* * check for holes in the source file. If none, we will use regular * write instead of file write. */ if (((off_t)(arcn->sb.st_blocks * BLKMULT)) >= arcn->sb.st_size) ++no_hole; /* * pass the blocksize of the file being written to the write routine, * if the size is zero, use the default MINFBSZ */ if (fstat(fd2, &sb) == 0) { if (sb.st_blksize > 0) sz = sb.st_blksize; } else syswarn(0,errno,"Unable to obtain block size for file %s",fnm); rem = sz; /* * read the source file and copy to destination file until EOF */ for(;;) { if ((cnt = read(fd1, buf, blksz)) <= 0) break; if (no_hole) res = write(fd2, buf, cnt); else res = file_write(fd2, buf, cnt, &rem, &isem, sz, fnm); if (res != cnt) break; cpcnt += cnt; } /* * check to make sure the copy is valid. */ if (res < 0) syswarn(1, errno, "Failed write during copy of %s to %s", arcn->org_name, arcn->name); else if (cpcnt != arcn->sb.st_size) paxwarn(1, "File %s changed size during copy to %s", arcn->org_name, arcn->name); else if (fstat(fd1, &sb) < 0) syswarn(1, errno, "Failed stat of %s", arcn->org_name); else if (arcn->sb.st_mtime != sb.st_mtime) paxwarn(1, "File %s was modified during copy to %s", arcn->org_name, arcn->name); /* * if the last block has a file hole (all zero), we must make sure this * gets updated in the file. We force the last block of zeros to be * written. just closing with the file offset moved forward may not put * a hole at the end of the file. */ if (!no_hole && isem && (arcn->sb.st_size > 0L)) file_flush(fd2, fnm, isem); return; } /* * buf_fill() * fill the read buffer with the next record (or what we can get) from * the archive volume. * Return: * Number of bytes of data in the read buffer, -1 for read error, and * 0 when finished (user specified termination in ar_next()). */ int buf_fill(void) { int cnt; static int fini = 0; if (fini) return(0); for(;;) { /* * try to fill the buffer. on error the next archive volume is * opened and we try again. */ if ((cnt = ar_read(buf, blksz)) > 0) { bufpt = buf; bufend = buf + cnt; rdcnt += cnt; return(cnt); } /* * errors require resync, EOF goes to next archive * but in case we have not determined yet the format, * this means that we have a very short file, so we * are done again. */ if (cnt < 0) break; if (frmt == NULL || ar_next() < 0) { fini = 1; return(0); } rdcnt = 0; } exit_val = 1; return(-1); } /* * buf_flush() * force the write buffer to the archive. We are passed the number of * bytes in the buffer at the point of the flush. When we change archives * the record size might change. (either larger or smaller). * Return: * 0 if all is ok, -1 when a write error occurs. */ int buf_flush(int bufcnt) { int cnt; int push = 0; int totcnt = 0; /* * if we have reached the user specified byte count for each archive * volume, prompt for the next volume. (The non-standard -R flag). * NOTE: If the wrlimit is smaller than wrcnt, we will always write * at least one record. We always round limit UP to next blocksize. */ if ((wrlimit > 0) && (wrcnt > wrlimit)) { paxwarn(0, "User specified archive volume byte limit reached."); if (ar_next() < 0) { wrcnt = 0; exit_val = 1; return(-1); } wrcnt = 0; /* * The new archive volume might have changed the size of the * write blocksize. if so we figure out if we need to write * (one or more times), or if there is now free space left in * the buffer (it is no longer full). bufcnt has the number of * bytes in the buffer, (the blocksize, at the point we were * CALLED). Push has the amount of "extra" data in the buffer * if the block size has shrunk from a volume change. */ bufend = buf + blksz; if (blksz > bufcnt) return(0); if (blksz < bufcnt) push = bufcnt - blksz; } /* * We have enough data to write at least one archive block */ for (;;) { /* * write a block and check if it all went out ok */ cnt = ar_write(buf, blksz); if (cnt == blksz) { /* * the write went ok */ wrcnt += cnt; totcnt += cnt; if (push > 0) { /* we have extra data to push to the front. * check for more than 1 block of push, and if * so we loop back to write again */ memcpy(buf, bufend, push); bufpt = buf + push; if (push >= blksz) { push -= blksz; continue; } } else bufpt = buf; return(totcnt); } else if (cnt > 0) { /* * Oh drat we got a partial write! * if format doesn't care about alignment let it go, * we warned the user in ar_write().... but this means * the last record on this volume violates pax spec.... */ totcnt += cnt; wrcnt += cnt; bufpt = buf + cnt; cnt = bufcnt - cnt; memcpy(buf, bufpt, cnt); bufpt = buf + cnt; if (!frmt->blkalgn || ((cnt % frmt->blkalgn) == 0)) return(totcnt); break; } /* * All done, go to next archive */ wrcnt = 0; if (ar_next() < 0) break; /* * The new archive volume might also have changed the block * size. if so, figure out if we have too much or too little * data for using the new block size */ bufend = buf + blksz; if (blksz > bufcnt) return(0); if (blksz < bufcnt) push = bufcnt - blksz; } /* * write failed, stop pax. we must not create a bad archive! */ exit_val = 1; return(-1); } Index: head/etc/devd/hyperv.conf =================================================================== --- head/etc/devd/hyperv.conf (revision 327229) +++ head/etc/devd/hyperv.conf (revision 327230) @@ -1,108 +1,108 @@ # $FreeBSD$ # # Hyper-V specific events notify 10 { match "system" "DEVFS"; match "subsystem" "CDEV"; match "type" "CREATE"; match "cdev" "hv_kvp_dev"; action "/usr/sbin/hv_kvp_daemon"; }; notify 10 { match "system" "DEVFS"; match "subsystem" "CDEV"; match "type" "DESTROY"; match "cdev" "hv_kvp_dev"; action "pkill -x hv_kvp_daemon"; }; notify 11 { match "system" "DEVFS"; match "subsystem" "CDEV"; match "type" "CREATE"; match "cdev" "hv_fsvss_dev"; action "/usr/sbin/hv_vss_daemon"; }; notify 11 { match "system" "DEVFS"; match "subsystem" "CDEV"; match "type" "DESTROY"; match "cdev" "hv_fsvss_dev"; action "pkill -x hv_vss_daemon"; }; # # Rules for non-transparent network VF. # # How network VF works with hn(4) on Hyper-V in non-transparent mode: # -# - Each network VF has a cooresponding hn(4). -# - The network VF and the it's cooresponding hn(4) have the same hardware +# - Each network VF has a corresponding hn(4). +# - The network VF and the it's corresponding hn(4) have the same hardware # address. # - Once the network VF is up, e.g. ifconfig VF up: # o All of the transmission should go through the network VF. # o Most of the reception goes through the network VF. -# o Small amount of reception may go through the cooresponding hn(4). -# This reception will happen, even if the the cooresponding hn(4) is -# down. The cooresponding hn(4) will change the reception interface +# o Small amount of reception may go through the corresponding hn(4). +# This reception will happen, even if the corresponding hn(4) is +# down. The corresponding hn(4) will change the reception interface # to the network VF, so that network layer and application layer will # be tricked into thinking that these packets were received by the # network VF. -# o The cooresponding hn(4) pretends the physical link is down. +# o The corresponding hn(4) pretends the physical link is down. # - Once the network VF is down or detached: -# o All of the transmission should go through the cooresponding hn(4). -# o All of the reception goes through the cooresponding hn(4). -# o The cooresponding hn(4) fallbacks to the original physical link +# o All of the transmission should go through the corresponding hn(4). +# o All of the reception goes through the corresponding hn(4). +# o The corresponding hn(4) fallbacks to the original physical link # detection logic. # # All these features are mainly used to help live migration, during which # the network VF will be detached, while the network communication to the # VM must not be cut off. In order to reach this level of live migration # transparency, we use failover mode lagg(4) with the network VF and the -# cooresponding hn(4) attached to it. +# corresponding hn(4) attached to it. # # To ease user configuration for both network VF and non-network VF, the # lagg(4) will be created by the following rules, and the configuration -# of the cooresponding hn(4) will be applied to the lagg(4) automatically. +# of the corresponding hn(4) will be applied to the lagg(4) automatically. # # NOTE: # If live migration is not needed at all, the following rules could be # commented out, and the network VF interface could be used exclusively. -# Most often the cooresponding hn(4) could be completely ignored. +# Most often the corresponding hn(4) could be completely ignored. # # # Default workflow for the network VF bringup: # 1) ETHERNET/IFATTACH -> VF interface up (delayed by rc.conf hyperv_vf_delay # seconds). This operation will trigger HYPERV_NIC_VF/VF_UP. # 2) HYPERV_NIC_VF/VF_UP: # a) Create laggX coresponding to hnX. # b) Add hnX and VF to laggX. # c) Whack all previous network configuration on hnX, including stopping # dhclient. # d) Apply rc.conf ifconfig_hnX to laggX; i.e. including starting dhclient. # # NOTE: # HYPERV_NIC_VF/VF_UP action script could be customized per-interface by # adding /usr/libexec/hyperv/hyperv_vfup.hnX script. # /usr/libexec/hyperv/hyperv_vfup could be used as the template for the # customized per-interface script. # # NOTE: # For transparent network VF, hyperv_vfattach does nothing and # HYPERV_NIC_VF/VF_UP will not be triggered at all. # notify 10 { match "system" "HYPERV_NIC_VF"; match "type" "VF_UP"; action "/usr/libexec/hyperv/hyperv_vfup $subsystem"; }; notify 10 { match "system" "ETHERNET"; match "type" "IFATTACH"; action "/usr/libexec/hyperv/hyperv_vfattach $subsystem 0"; }; Index: head/etc/portsnap.conf =================================================================== --- head/etc/portsnap.conf (revision 327229) +++ head/etc/portsnap.conf (revision 327230) @@ -1,36 +1,35 @@ # $FreeBSD$ # Default directory where compressed snapshots are stored. # WORKDIR=/var/db/portsnap # Default location of the ports tree (target for "update" and "extract"). # PORTSDIR=/usr/ports # Server or server pool from which to fetch updates. You can change # this to point at a specific server if you want, but in most cases # using a "nearby" server won't provide a measurable improvement in # performance. SERVERNAME=portsnap.FreeBSD.org # Trusted keyprint. Changing this is a Bad Idea unless you've received # a PGP-signed email from telling you to # change it and explaining why. KEYPRINT=9b5feee6d69f170e3dd0a2c8e469ddbd64f13f978f2f3aede40c98633216c330 # Example of ignoring parts of the ports tree. If you know that you # absolutely will not need certain parts of the tree, this will save # some bandwidth and disk space. See the manual page for more details. # # WARNING: Working with an incomplete ports tree is not supported and # can cause problems due to missing dependencies. If you have REFUSE # directives and experience problems, remove them and update your tree # before asking for help on the mailing lists. # # REFUSE arabic chinese french german hebrew hungarian japanese # REFUSE korean polish portuguese russian ukrainian vietnamese # List of INDEX files to build and the DESCRIBE file to use for each -#INDEX INDEX-9 DESCRIBE.9 #INDEX INDEX-10 DESCRIBE.10 #INDEX INDEX-11 DESCRIBE.11 INDEX INDEX-12 DESCRIBE.12 Index: head/etc/rc.initdiskless =================================================================== --- head/etc/rc.initdiskless (revision 327229) +++ head/etc/rc.initdiskless (revision 327230) @@ -1,382 +1,382 @@ #!/bin/sh # # Copyright (c) 1999 Matt Dillon # All rights reserved. # # 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. # # THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. # # $FreeBSD$ # On entry to this script the entire system consists of a read-only root # mounted via NFS. The kernel has run BOOTP and configured an interface # (otherwise it would not have been able to mount the NFS root!) # # We use the contents of /conf to create and populate memory filesystems # that are mounted on top of this root to implement the writable # (and host-specific) parts of the root filesystem, and other volatile # filesystems. # # The hierarchy in /conf has the form /conf/T/M/ where M are directories # for which memory filesystems will be created and filled, # and T is one of the "template" directories below: # # base universal base, typically a replica of the original root; # default secondary universal base, typically overriding some # of the files in the original root; # ${ipba} where ${ipba} is the assigned broadcast IP address # bcast/${ipba} same as above # ${class} where ${class} is a list of directories supplied by # bootp/dhcp through the T134 option. # ${ipba} and ${class} are typically used to configure features # for group of diskless clients, or even individual features; # ${ip} where ${ip} is the machine's assigned IP address, typically # used to set host-specific features; # ip/${ip} same as above # # Template directories are scanned in the order they are listed above, # with each successive directory overriding (merged into) the previous one; # non-existing directories are ignored. The subdirectory forms exist to # help keep the top level /conf manageable in large installations. # # The existence of a directory /conf/T/M causes this script to create a # memory filesystem mounted as /M on the client. # # Some files in /conf have special meaning, namely: # # Filename Action # ---------------------------------------------------------------- # /conf/T/M/remount # The contents of the file is a mount command. E.g. if # /conf/1.2.3.4/foo/remount contains "mount -o ro /dev/ad0s3", -# then /dev/ad0s3 will be be mounted on /conf/1.2.3.4/foo/ +# then /dev/ad0s3 will be mounted on /conf/1.2.3.4/foo/ # # /conf/T/M/remount_optional # If this file exists, then failure to execute the mount # command contained in /conf/T/M/remount is non-fatal. # # /conf/T/M/remount_subdir # If this file exists, then the behaviour of /conf/T/M/remount # changes as follows: # 1. /conf/T/M/remount is invoked to mount the root of the # filesystem where the configuration data exists on a # temporary mountpoint. # 2. /conf/T/M/remount_subdir is then invoked to mount a # *subdirectory* of the filesystem mounted by # /conf/T/M/remount on /conf/T/M/. # # /conf/T/M/diskless_remount # The contents of the file points to an NFS filesystem, # possibly followed by mount_nfs options. If the server name # is omitted, the script will prepend the root path used when # booting. E.g. if you booted from foo.com:/path/to/root, # an entry for /conf/base/etc/diskless_remount could be any of # foo.com:/path/to/root/etc # /etc -o ro # Because mount_nfs understands ".." in paths, it is # possible to mount from locations above the NFS root with # paths such as "/../../etc". # # /conf/T/M/md_size # The contents of the file specifies the size of the memory # filesystem to be created, in 512 byte blocks. # The default size is 10240 blocks (5MB). E.g. if # /conf/base/etc/md_size contains "30000" then a 15MB MFS # will be created. In case of multiple entries for the same # directory M, the last one in the scanning order is used. # NOTE: If you only need to create a memory filesystem but not # initialize it from a template, it is preferable to specify # it in fstab e.g. as "md /tmp mfs -s=30m,rw 0 0" # # /conf/T/SUBDIR.cpio.gz # The file is cpio'd into /SUBDIR (and a memory filesystem is # created for /SUBDIR if necessary). The presence of this file # prevents the copy from /conf/T/SUBDIR/ # # /conf/T/SUBDIR.remove # The list of paths contained in the file are rm -rf'd # relative to /SUBDIR. # # /conf/diskless_remount # Similar to /conf/T/M/diskless_remount above, but allows # all of /conf to be remounted. This can be used to allow # multiple roots to share the same /conf. # # # You will almost universally want to create the following files under /conf # # File Content # ---------------------------- ---------------------------------- # /conf/base/etc/md_size size of /etc filesystem # /conf/base/etc/diskless_remount "/etc" # /conf/default/etc/rc.conf generic diskless config parameters # /conf/default/etc/fstab generic diskless fstab e.g. like this # # foo:/root_part / nfs ro 0 0 # foo:/usr_part /usr nfs ro 0 0 # foo:/home_part /home nfs rw 0 0 # md /tmp mfs -s=30m,rw 0 0 # md /var mfs -s=30m,rw 0 0 # proc /proc procfs rw 0 0 # # plus, possibly, overrides for password files etc. # # NOTE! /var, /tmp, and /dev will be typically created elsewhere, e.g. # as entries in the fstab as above. # Those filesystems should not be specified in /conf. # # (end of documentation, now get to the real code) dlv=`/sbin/sysctl -n vfs.nfs.diskless_valid 2> /dev/null` # DEBUGGING # log something on stdout if verbose. o_verbose=0 # set to 1 or 2 if you want more debugging log() { [ ${o_verbose} -gt 0 ] && echo "*** $* ***" [ ${o_verbose} -gt 1 ] && read -p "=== Press enter to continue" foo } # chkerr: # # Routine to check for error # # checks error code and drops into shell on failure. # if shell exits, terminates script as well as /etc/rc. # if remount_optional exists under the mountpoint, skip this check. # chkerr() { lastitem () ( n=$(($# - 1)) ; shift $n ; echo $1 ) mountpoint="$(lastitem $2)" [ -r $mountpoint/remount_optional ] && ( echo "$2 failed: ignoring due to remount_optional" ; return ) case $1 in 0) ;; *) echo "$2 failed: dropping into /bin/sh" /bin/sh # RESUME ;; esac } # The list of filesystems to umount after the copy to_umount="" handle_remount() { # $1 = mount point local nfspt mountopts b b=$1 log handle_remount $1 [ -d $b -a -f $b/diskless_remount ] || return read nfspt mountopts < $b/diskless_remount log "nfspt ${nfspt} mountopts ${mountopts}" # prepend the nfs root if not present [ `expr "$nfspt" : '\(.\)'` = "/" ] && nfspt="${nfsroot}${nfspt}" mount_nfs $mountopts $nfspt $b chkerr $? "mount_nfs $nfspt $b" to_umount="$b ${to_umount}" } # Create a generic memory disk. # The 'auto' parameter will attempt to use tmpfs(5), falls back to md(4). # $1 is size in 512-byte sectors, $2 is the mount point. mount_md() { /sbin/mdmfs -s $1 auto $2 } # Create the memory filesystem if it has not already been created # create_md() { [ "x`eval echo \\$md_created_$1`" = "x" ] || return # only once if [ "x`eval echo \\$md_size_$1`" = "x" ]; then md_size=10240 else md_size=`eval echo \\$md_size_$1` fi log create_md $1 with size $md_size mount_md $md_size /$1 /bin/chmod 755 /$1 eval md_created_$1=created } # DEBUGGING # # set -v # Figure out our interface and IP. # bootp_ifc="" bootp_ipa="" bootp_ipbca="" class="" if [ ${dlv:=0} -ne 0 ] ; then iflist=`ifconfig -l` for i in ${iflist} ; do set -- `ifconfig ${i}` while [ $# -ge 1 ] ; do if [ "${bootp_ifc}" = "" -a "$1" = "inet" ] ; then bootp_ifc=${i} ; bootp_ipa=${2} ; shift fi if [ "${bootp_ipbca}" = "" -a "$1" = "broadcast" ] ; then bootp_ipbca=$2; shift fi shift done if [ "${bootp_ifc}" != "" ] ; then break fi done # Get the values passed with the T134 bootp cookie. class="`/sbin/sysctl -qn kern.bootp_cookie`" echo "Interface ${bootp_ifc} IP-Address ${bootp_ipa} Broadcast ${bootp_ipbca} ${class}" fi log Figure out our NFS root path # set -- `mount -t nfs` while [ $# -ge 1 ] ; do if [ "$2" = "on" -a "$3" = "/" ]; then nfsroot="$1" break fi shift done # The list of directories with template files templates="base default" if [ -n "${bootp_ipbca}" ]; then templates="${templates} ${bootp_ipbca} bcast/${bootp_ipbca}" fi if [ -n "${class}" ]; then templates="${templates} ${class}" fi if [ -n "${bootp_ipa}" ]; then templates="${templates} ${bootp_ipa} ip/${bootp_ipa}" fi # If /conf/diskless_remount exists, remount all of /conf. handle_remount /conf # Resolve templates in /conf/base, /conf/default, /conf/${bootp_ipbca}, # and /conf/${bootp_ipa}. For each subdirectory found within these # directories: # # - calculate memory filesystem sizes. If the subdirectory (prior to # NFS remounting) contains the file 'md_size', the contents specified # in 512 byte sectors will be used to size the memory filesystem. Otherwise # 8192 sectors (4MB) is used. # # - handle NFS remounts. If the subdirectory contains the file # diskless_remount, the contents of the file is NFS mounted over # the directory. For example /conf/base/etc/diskless_remount # might contain 'myserver:/etc'. NFS remounts allow you to avoid # having to dup your system directories in /conf. Your server must # be sure to export those filesystems -alldirs, however. # If the diskless_remount file contains a string beginning with a # '/' it is assumed that the local nfsroot should be prepended to # it before attemping to the remount. This allows the root to be # relocated without needing to change the remount files. # log "templates are ${templates}" for i in ${templates} ; do for j in /conf/$i/* ; do [ -d $j ] || continue # memory filesystem size specification subdir=${j##*/} [ -f $j/md_size ] && eval md_size_$subdir=`cat $j/md_size` # remount. Beware, the command is in the file itself! if [ -f $j/remount ]; then if [ -f $j/remount_subdir ]; then k="/conf.tmp/$i/$subdir" [ -d $k ] || continue # Mount the filesystem root where the config data is # on the temporary mount point. nfspt=`/bin/cat $j/remount` $nfspt $k chkerr $? "$nfspt $k" # Now use a nullfs mount to get the data where we # really want to see it. remount_subdir=`/bin/cat $j/remount_subdir` remount_subdir_cmd="mount -t nullfs $k/$remount_subdir" $remount_subdir_cmd $j chkerr $? "$remount_subdir_cmd $j" # XXX check order -- we must force $k to be unmounted # after j, as j depends on k. to_umount="$j $k ${to_umount}" else nfspt=`/bin/cat $j/remount` $nfspt $j chkerr $? "$nfspt $j" to_umount="$j ${to_umount}" # XXX hope it is really a mount! fi fi # NFS remount handle_remount $j done done # - Create all required MFS filesystems and populate them from # our templates. Support both a direct template and a dir.cpio.gz # archive. Support dir.remove files containing a list of relative # paths to remove. # # The dir.cpio.gz form is there to make the copy process more efficient, # so if the cpio archive is present, it prevents the files from dir/ # from being copied. for i in ${templates} ; do for j in /conf/$i/* ; do subdir=${j##*/} if [ -d $j -a ! -f $j.cpio.gz ]; then create_md $subdir cp -Rp $j/ /$subdir fi done for j in /conf/$i/*.cpio.gz ; do subdir=${j%*.cpio.gz} subdir=${subdir##*/} if [ -f $j ]; then create_md $subdir echo "Loading /$subdir from cpio archive $j" (cd / ; /rescue/tar -xpf $j) fi done for j in /conf/$i/*.remove ; do subdir=${j%*.remove} subdir=${subdir##*/} if [ -f $j ]; then # doubly sure it is a memory disk before rm -rf'ing create_md $subdir (cd /$subdir; rm -rf `/bin/cat $j`) fi done done # umount partitions used to fill the memory filesystems [ -n "${to_umount}" ] && umount $to_umount Index: head/etc/rc.subr =================================================================== --- head/etc/rc.subr (revision 327229) +++ head/etc/rc.subr (revision 327230) @@ -1,2128 +1,2128 @@ # $NetBSD: rc.subr,v 1.67 2006/10/07 11:25:15 elad Exp $ # $FreeBSD$ # # Copyright (c) 1997-2004 The NetBSD Foundation, Inc. # All rights reserved. # # This code is derived from software contributed to The NetBSD Foundation # by Luke Mewburn. # # 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. # # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. 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 FOUNDATION 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. # # rc.subr # functions used by various rc scripts # : ${RC_PID:=$$}; export RC_PID # # Operating System dependent/independent variables # if [ -n "${_rc_subr_loaded}" ]; then return fi _rc_subr_loaded="YES" SYSCTL="/sbin/sysctl" SYSCTL_N="${SYSCTL} -n" SYSCTL_W="${SYSCTL}" PROTECT="/usr/bin/protect" ID="/usr/bin/id" IDCMD="if [ -x $ID ]; then $ID -un; fi" PS="/bin/ps -ww" JID=0 # # functions # --------- # list_vars pattern # List vars matching pattern. # list_vars() { set | { while read LINE; do var="${LINE%%=*}" case "$var" in "$LINE"|*[!a-zA-Z0-9_]*) continue ;; $1) echo $var esac done; } } # set_rcvar [var] [defval] [desc] # # Echo or define a rc.conf(5) variable name. Global variable # $rcvars is used. # # If no argument is specified, echo "${name}_enable". # # If only a var is specified, echo "${var}_enable". # # If var and defval are specified, the ${var} is defined as # rc.conf(5) variable and the default value is ${defvar}. An # optional argument $desc can also be specified to add a # description for that. # set_rcvar() { local _var case $# in 0) echo ${name}_enable ;; 1) echo ${1}_enable ;; *) debug "set_rcvar: \$$1=$2 is added" \ " as a rc.conf(5) variable." _var=$1 rcvars="${rcvars# } $_var" eval ${_var}_defval=\"$2\" shift 2 eval ${_var}_desc=\"$*\" ;; esac } # set_rcvar_obsolete oldvar [newvar] [msg] # Define obsolete variable. # Global variable $rcvars_obsolete is used. # set_rcvar_obsolete() { local _var _var=$1 debug "set_rcvar_obsolete: \$$1(old) -> \$$2(new) is defined" rcvars_obsolete="${rcvars_obsolete# } $1" eval ${1}_newvar=\"$2\" shift 2 eval ${_var}_obsolete_msg=\"$*\" } # # force_depend script [rcvar] # Force a service to start. Intended for use by services # to resolve dependency issues. # $1 - filename of script, in /etc/rc.d, to run # $2 - name of the script's rcvar (minus the _enable) # force_depend() { local _depend _dep_rcvar _depend="$1" _dep_rcvar="${2:-$1}_enable" [ -n "$rc_fast" ] && ! checkyesno always_force_depends && checkyesno $_dep_rcvar && return 0 /etc/rc.d/${_depend} forcestatus >/dev/null 2>&1 && return 0 info "${name} depends on ${_depend}, which will be forced to start." if ! /etc/rc.d/${_depend} forcestart; then warn "Unable to force ${_depend}. It may already be running." return 1 fi } # # checkyesno var # Test $1 variable, and warn if not set to YES or NO. # Return 0 if it's "yes" (et al), nonzero otherwise. # checkyesno() { eval _value=\$${1} debug "checkyesno: $1 is set to $_value." case $_value in # "yes", "true", "on", or "1" [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) return 0 ;; # "no", "false", "off", or "0" [Nn][Oo]|[Ff][Aa][Ll][Ss][Ee]|[Oo][Ff][Ff]|0) return 1 ;; *) warn "\$${1} is not set properly - see rc.conf(5)." return 1 ;; esac } # # reverse_list list # print the list in reverse order # reverse_list() { _revlist= for _revfile; do _revlist="$_revfile $_revlist" done echo $_revlist } # stop_boot always # If booting directly to multiuser or $always is enabled, # send SIGTERM to the parent (/etc/rc) to abort the boot. # Otherwise just exit. # stop_boot() { local always case $1 in # "yes", "true", "on", or "1" [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) always=true ;; *) always=false ;; esac if [ "$autoboot" = yes -o "$always" = true ]; then echo "ERROR: ABORTING BOOT (sending SIGTERM to parent)!" kill -TERM ${RC_PID} fi exit 1 } # # mount_critical_filesystems type # Go through the list of critical filesystems as provided in # the rc.conf(5) variable $critical_filesystems_${type}, checking # each one to see if it is mounted, and if it is not, mounting it. # mount_critical_filesystems() { eval _fslist=\$critical_filesystems_${1} for _fs in $_fslist; do mount | ( _ismounted=false while read what _on on _type type; do if [ $on = $_fs ]; then _ismounted=true fi done if $_ismounted; then : else mount $_fs >/dev/null 2>&1 fi ) done } # # check_pidfile pidfile procname [interpreter] # Parses the first line of pidfile for a PID, and ensures # that the process is running and matches procname. # Prints the matching PID upon success, nothing otherwise. # interpreter is optional; see _find_processes() for details. # check_pidfile() { _pidfile=$1 _procname=$2 _interpreter=$3 if [ -z "$_pidfile" -o -z "$_procname" ]; then err 3 'USAGE: check_pidfile pidfile procname [interpreter]' fi if [ ! -f $_pidfile ]; then debug "pid file ($_pidfile): not readable." return fi read _pid _junk < $_pidfile if [ -z "$_pid" ]; then debug "pid file ($_pidfile): no pid in file." return fi _find_processes $_procname ${_interpreter:-.} '-p '"$_pid" } # # check_process procname [interpreter] # Ensures that a process (or processes) named procname is running. # Prints a list of matching PIDs. # interpreter is optional; see _find_processes() for details. # check_process() { _procname=$1 _interpreter=$2 if [ -z "$_procname" ]; then err 3 'USAGE: check_process procname [interpreter]' fi _find_processes $_procname ${_interpreter:-.} '-ax' } # # _find_processes procname interpreter psargs # Search for procname in the output of ps generated by psargs. # Prints the PIDs of any matching processes, space separated. # # If interpreter == ".", check the following variations of procname # against the first word of each command: # procname # `basename procname` # `basename procname` + ":" # "(" + `basename procname` + ")" # "[" + `basename procname` + "]" # # If interpreter != ".", read the first line of procname, remove the # leading #!, normalise whitespace, append procname, and attempt to # match that against each command, either as is, or with extra words # at the end. As an alternative, to deal with interpreted daemons # using perl, the basename of the interpreter plus a colon is also # tried as the prefix to procname. # _find_processes() { if [ $# -ne 3 ]; then err 3 'USAGE: _find_processes procname interpreter psargs' fi _procname=$1 _interpreter=$2 _psargs=$3 _pref= if [ $_interpreter != "." ]; then # an interpreted script _script="${_chroot}${_chroot:+/}$_procname" if [ -r "$_script" ]; then read _interp < $_script # read interpreter name case "$_interp" in \#!*) _interp=${_interp#\#!} # strip #! set -- $_interp case $1 in */bin/env) shift # drop env to get real name ;; esac if [ $_interpreter != $1 ]; then warn "\$command_interpreter $_interpreter != $1" fi ;; *) warn "no shebang line in $_script" set -- $_interpreter ;; esac else warn "cannot read shebang line from $_script" set -- $_interpreter fi _interp="$* $_procname" # cleanup spaces, add _procname _interpbn=${1##*/} _fp_args='_argv' _fp_match='case "$_argv" in ${_interp}|"${_interp} "*|"[${_interpbn}]"|"${_interpbn}: ${_procname}"*)' else # a normal daemon _procnamebn=${_procname##*/} _fp_args='_arg0 _argv' _fp_match='case "$_arg0" in $_procname|$_procnamebn|${_procnamebn}:|"(${_procnamebn})"|"[${_procnamebn}]")' fi _proccheck="\ $PS 2>/dev/null -o pid= -o jid= -o command= $_psargs"' | while read _npid _jid '"$_fp_args"'; do '"$_fp_match"' if [ "$JID" -eq "$_jid" ]; then echo -n "$_pref$_npid"; _pref=" "; fi ;; esac done' # debug "in _find_processes: proccheck is ($_proccheck)." eval $_proccheck } # sort_lite [-b] [-n] [-k POS] [-t SEP] # A lite version of sort(1) (supporting a few options) that can be used # before the real sort(1) is available (e.g., in scripts that run prior # to mountcritremote). Requires only shell built-in functionality. # sort_lite() { local funcname=sort_lite local sort_sep="$IFS" sort_ignore_leading_space= local sort_field=0 sort_strict_fields= sort_numeric= local nitems=0 skip_leading=0 trim= local OPTIND flag while getopts bnk:t: flag; do case "$flag" in b) sort_ignore_leading_space=1 ;; n) sort_numeric=1 sort_ignore_leading_space=1 ;; k) sort_field="${OPTARG%%,*}" ;; # only up to first comma # NB: Unlike sort(1) only one POS allowed t) sort_sep="$OPTARG" if [ ${#sort_sep} -gt 1 ]; then echo "$funcname: multi-character tab \`$sort_sep'" >&2 return 1 fi sort_strict_fields=1 ;; \?) return 1 ;; esac done shift $(( $OPTIND - 1 )) # Create transformation pattern to trim leading text if desired case "$sort_field" in ""|[!0-9]*|*[!0-9.]*) echo "$funcname: invalid sort field \`$sort_field'" >&2 return 1 ;; *.*) skip_leading=${sort_field#*.} sort_field=${sort_field%%.*} while [ ${skip_leading:-0} -gt 1 ] 2> /dev/null; do trim="$trim?" skip_leading=$(( $skip_leading - 1 )) done esac # Copy input to series of local numbered variables # NB: IFS of NULL preserves leading whitespace local LINE while IFS= read -r LINE || [ "$LINE" ]; do nitems=$(( $nitems + 1 )) local src_$nitems="$LINE" done # # Sort numbered locals using insertion sort # local curitem curitem_orig curitem_mod curitem_haskey local dest dest_orig dest_mod dest_haskey local d gt n local i=1 while [ $i -le $nitems ]; do curitem_haskey=1 # Assume sort field (-k POS) exists eval curitem=\"\$src_$i\" curitem_mod="$curitem" # for modified comparison curitem_orig="$curitem" # for original comparison # Trim leading whitespace if desired if [ "$sort_ignore_leading_space" ]; then while case "$curitem_orig" in [$IFS]*) : ;; *) false; esac do curitem_orig="${curitem_orig#?}" done curitem_mod="$curitem_orig" fi # Shift modified comparison value if sort field (-k POS) is > 1 n=$sort_field while [ $n -gt 1 ]; do case "$curitem_mod" in *[$sort_sep]*) # Cut text up-to (and incl.) first separator curitem_mod="${curitem_mod#*[$sort_sep]}" # Skip NULLs unless strict field splitting [ "$sort_strict_fields" ] || [ "${curitem_mod%%[$sort_sep]*}" ] || [ $n -eq 2 ] || continue ;; *) # Asked for a field that doesn't exist curitem_haskey= break esac n=$(( $n - 1 )) done # Trim trailing words if sort field >= 1 [ $sort_field -ge 1 -a "$sort_numeric" ] && curitem_mod="${curitem_mod%%[$sort_sep]*}" # Apply optional trim (-k POS.TRIM) to cut leading characters curitem_mod="${curitem_mod#$trim}" # Determine the type of modified comparison to use initially # NB: Prefer numerical if requested but fallback to standard case "$curitem_mod" in ""|[!0-9]*) # NULL or begins with non-number gt=">" [ "$sort_numeric" ] && curitem_mod=0 ;; *) if [ "$sort_numeric" ]; then gt="-gt" curitem_mod="${curitem_mod%%[!0-9]*}" # NB: trailing non-digits removed # otherwise numeric comparison fails else gt=">" fi esac # If first time through, short-circuit below position-search if [ $i -le 1 ]; then d=0 else d=1 fi # # Find appropriate element position # while [ $d -gt 0 ] do dest_haskey=$curitem_haskey eval dest=\"\$dest_$d\" dest_mod="$dest" # for modified comparison dest_orig="$dest" # for original comparison # Trim leading whitespace if desired if [ "$sort_ignore_leading_space" ]; then while case "$dest_orig" in [$IFS]*) : ;; *) false; esac do dest_orig="${dest_orig#?}" done dest_mod="$dest_orig" fi # Shift modified value if sort field (-k POS) is > 1 n=$sort_field while [ $n -gt 1 ]; do case "$dest_mod" in *[$sort_sep]*) # Cut text up-to (and incl.) 1st sep dest_mod="${dest_mod#*[$sort_sep]}" # Skip NULLs unless strict fields [ "$sort_strict_fields" ] || [ "${dest_mod%%[$sort_sep]*}" ] || [ $n -eq 2 ] || continue ;; *) # Asked for a field that doesn't exist dest_haskey= break esac n=$(( $n - 1 )) done # Trim trailing words if sort field >= 1 [ $sort_field -ge 1 -a "$sort_numeric" ] && dest_mod="${dest_mod%%[$sort_sep]*}" # Apply optional trim (-k POS.TRIM), cut leading chars dest_mod="${dest_mod#$trim}" # Determine type of modified comparison to use # NB: Prefer numerical if requested, fallback to std case "$dest_mod" in ""|[!0-9]*) # NULL or begins with non-number gt=">" [ "$sort_numeric" ] && dest_mod=0 ;; *) if [ "$sort_numeric" ]; then gt="-gt" dest_mod="${dest_mod%%[!0-9]*}" # NB: kill trailing non-digits # for numeric comparison safety else gt=">" fi esac # Break if we've found the proper element position if [ "$curitem_haskey" -a "$dest_haskey" ]; then if [ "$dest_mod" = "$curitem_mod" ]; then [ "$dest_orig" ">" "$curitem_orig" ] && break elif [ "$dest_mod" $gt "$curitem_mod" ] \ 2> /dev/null then break fi else [ "$dest_orig" ">" "$curitem_orig" ] && break fi # Break if we've hit the end [ $d -ge $i ] && break d=$(( $d + 1 )) done # Shift remaining positions forward, making room for new item n=$i while [ $n -ge $d ]; do # Shift destination item forward one placement eval dest_$(( $n + 1 ))=\"\$dest_$n\" n=$(( $n - 1 )) done # Place the element if [ $i -eq 1 ]; then local dest_1="$curitem" else local dest_$d="$curitem" fi i=$(( $i + 1 )) done # Print sorted results d=1 while [ $d -le $nitems ]; do eval echo \"\$dest_$d\" d=$(( $d + 1 )) done } # # wait_for_pids pid [pid ...] # spins until none of the pids exist # wait_for_pids() { local _list _prefix _nlist _j _list="$@" if [ -z "$_list" ]; then return fi _prefix= while true; do _nlist=""; for _j in $_list; do if kill -0 $_j 2>/dev/null; then _nlist="${_nlist}${_nlist:+ }$_j" [ -n "$_prefix" ] && sleep 1 fi done if [ -z "$_nlist" ]; then break fi _list=$_nlist echo -n ${_prefix:-"Waiting for PIDS: "}$_list _prefix=", " pwait $_list 2>/dev/null done if [ -n "$_prefix" ]; then echo "." fi } # # get_pidfile_from_conf string file # # Takes a string to search for in the specified file. # Ignores lines with traditional comment characters. # # Example: # # if get_pidfile_from_conf string file; then # pidfile="$_pidfile_from_conf" # else # pidfile='appropriate default' # fi # get_pidfile_from_conf() { if [ -z "$1" -o -z "$2" ]; then err 3 "USAGE: get_pidfile_from_conf string file ($name)" fi local string file line string="$1" ; file="$2" if [ ! -s "$file" ]; then err 3 "get_pidfile_from_conf: $file does not exist ($name)" fi while read line; do case "$line" in *[#\;]*${string}*) continue ;; *${string}*) break ;; esac done < $file if [ -n "$line" ]; then line=${line#*/} _pidfile_from_conf="/${line%%[\"\;]*}" else return 1 fi } # # check_startmsgs # If rc_quiet is set (usually as a result of using faststart at # boot time) check if rc_startmsgs is enabled. # check_startmsgs() { if [ -n "$rc_quiet" ]; then checkyesno rc_startmsgs else return 0 fi } # # run_rc_command argument # Search for argument in the list of supported commands, which is: # "start stop restart rcvar status poll ${extra_commands}" # If there's a match, run ${argument}_cmd or the default method # (see below). # # If argument has a given prefix, then change the operation as follows: # Prefix Operation # ------ --------- # fast Skip the pid check, and set rc_fast=yes, rc_quiet=yes # force Set ${rcvar} to YES, and set rc_force=yes # one Set ${rcvar} to YES # quiet Don't output some diagnostics, and set rc_quiet=yes # # The following globals are used: # # Name Needed Purpose # ---- ------ ------- # name y Name of script. # # command n Full path to command. # Not needed if ${rc_arg}_cmd is set for # each keyword. # # command_args n Optional args/shell directives for command. # # command_interpreter n If not empty, command is interpreted, so # call check_{pidfile,process}() appropriately. # # desc n Description of script. # # extra_commands n List of extra commands supported. # # pidfile n If set, use check_pidfile $pidfile $command, # otherwise use check_process $command. # In either case, only check if $command is set. # # procname n Process name to check for instead of $command. # # rcvar n This is checked with checkyesno to determine # if the action should be run. # # ${name}_program n Full path to command. # Meant to be used in /etc/rc.conf to override # ${command}. # # ${name}_chroot n Directory to chroot to before running ${command} # Requires /usr to be mounted. # # ${name}_chdir n Directory to cd to before running ${command} # (if not using ${name}_chroot). # # ${name}_flags n Arguments to call ${command} with. # NOTE: $flags from the parent environment # can be used to override this. # # ${name}_env n Environment variables to run ${command} with. # # ${name}_fib n Routing table number to run ${command} with. # # ${name}_nice n Nice level to run ${command} at. # # ${name}_oomprotect n Don't kill ${command} when swap space is exhausted. # # ${name}_user n User to run ${command} as, using su(1) if not # using ${name}_chroot. # Requires /usr to be mounted. # # ${name}_group n Group to run chrooted ${command} as. # Requires /usr to be mounted. # # ${name}_groups n Comma separated list of supplementary groups # to run the chrooted ${command} with. # Requires /usr to be mounted. # # ${name}_prepend n Command added before ${command}. # # ${name}_login_class n Login class to use, else "daemon". # # ${rc_arg}_cmd n If set, use this as the method when invoked; # Otherwise, use default command (see below) # # ${rc_arg}_precmd n If set, run just before performing the # ${rc_arg}_cmd method in the default # operation (i.e, after checking for required # bits and process (non)existence). # If this completes with a non-zero exit code, # don't run ${rc_arg}_cmd. # # ${rc_arg}_postcmd n If set, run just after performing the # ${rc_arg}_cmd method, if that method # returned a zero exit code. # # required_dirs n If set, check for the existence of the given # directories before running a (re)start command. # # required_files n If set, check for the readability of the given # files before running a (re)start command. # # required_modules n If set, ensure the given kernel modules are # loaded before running a (re)start command. # The check and possible loads are actually # done after start_precmd so that the modules # aren't loaded in vain, should the precmd # return a non-zero status to indicate a error. # If a word in the list looks like "foo:bar", # "foo" is the KLD file name and "bar" is the # module name. If a word looks like "foo~bar", # "foo" is the KLD file name and "bar" is a # egrep(1) pattern matching the module name. # Otherwise the module name is assumed to be # the same as the KLD file name, which is most # common. See load_kld(). # # required_vars n If set, perform checkyesno on each of the # listed variables before running the default # (re)start command. # # Default behaviour for a given argument, if no override method is # provided: # # Argument Default behaviour # -------- ----------------- # start if !running && checkyesno ${rcvar} # ${command} # # stop if ${pidfile} # rc_pid=$(check_pidfile $pidfile $command) # else # rc_pid=$(check_process $command) # kill $sig_stop $rc_pid # wait_for_pids $rc_pid # ($sig_stop defaults to TERM.) # # reload Similar to stop, except use $sig_reload instead, # and doesn't wait_for_pids. # $sig_reload defaults to HUP. # Note that `reload' isn't provided by default, # it should be enabled via $extra_commands. # # restart Run `stop' then `start'. # # status Show if ${command} is running, etc. # # poll Wait for ${command} to exit. # # rcvar Display what rc.conf variable is used (if any). # # enabled Return true if the service is enabled. # # describe Show the service's description # # extracommands Show the service's extra commands # # Variables available to methods, and after run_rc_command() has # completed: # # Variable Purpose # -------- ------- # rc_arg Argument to command, after fast/force/one processing # performed # # rc_flags Flags to start the default command with. # Defaults to ${name}_flags, unless overridden # by $flags from the environment. # This variable may be changed by the precmd method. # # rc_pid PID of command (if appropriate) # # rc_fast Not empty if "fast" was provided (q.v.) # # rc_force Not empty if "force" was provided (q.v.) # # rc_quiet Not empty if "quiet" was provided # # run_rc_command() { _return=0 rc_arg=$1 if [ -z "$name" ]; then err 3 'run_rc_command: $name is not set.' fi # Don't repeat the first argument when passing additional command- # line arguments to the command subroutines. # shift 1 rc_extra_args="$*" _rc_prefix= case "$rc_arg" in fast*) # "fast" prefix; don't check pid rc_arg=${rc_arg#fast} rc_fast=yes rc_quiet=yes ;; force*) # "force" prefix; always run rc_force=yes _rc_prefix=force rc_arg=${rc_arg#${_rc_prefix}} if [ -n "${rcvar}" ]; then eval ${rcvar}=YES fi ;; one*) # "one" prefix; set ${rcvar}=yes _rc_prefix=one rc_arg=${rc_arg#${_rc_prefix}} if [ -n "${rcvar}" ]; then eval ${rcvar}=YES fi ;; quiet*) # "quiet" prefix; omit some messages _rc_prefix=quiet rc_arg=${rc_arg#${_rc_prefix}} rc_quiet=yes ;; esac eval _override_command=\$${name}_program command=${_override_command:-$command} _keywords="start stop restart rcvar enabled describe extracommands $extra_commands" rc_pid= _pidcmd= _procname=${procname:-${command}} # setup pid check command if [ -n "$_procname" ]; then if [ -n "$pidfile" ]; then _pidcmd='rc_pid=$(check_pidfile '"$pidfile $_procname $command_interpreter"')' else _pidcmd='rc_pid=$(check_process '"$_procname $command_interpreter"')' fi _keywords="${_keywords} status poll" fi if [ -z "$rc_arg" ]; then rc_usage $_keywords fi if [ "$rc_arg" = "enabled" ] ; then checkyesno ${rcvar} return $? fi if [ -n "$flags" ]; then # allow override from environment rc_flags=$flags else eval rc_flags=\$${name}_flags fi eval _chdir=\$${name}_chdir _chroot=\$${name}_chroot \ _nice=\$${name}_nice _user=\$${name}_user \ _group=\$${name}_group _groups=\$${name}_groups \ _fib=\$${name}_fib _env=\$${name}_env \ _prepend=\$${name}_prepend _login_class=\${${name}_login_class:-daemon} \ _oomprotect=\$${name}_oomprotect if [ -n "$_user" ]; then # unset $_user if running as that user if [ "$_user" = "$(eval $IDCMD)" ]; then unset _user fi fi [ -z "$autoboot" ] && eval $_pidcmd # determine the pid if necessary for _elem in $_keywords; do if [ "$_elem" != "$rc_arg" ]; then continue fi # if ${rcvar} is set, $1 is not "rcvar" and not "describe" # and ${rc_pid} is not set, then run # checkyesno ${rcvar} # and return if that failed # if [ -n "${rcvar}" -a "$rc_arg" != "rcvar" -a "$rc_arg" != "stop" \ -a "$rc_arg" != "describe" ] || [ -n "${rcvar}" -a "$rc_arg" = "stop" -a -z "${rc_pid}" ]; then if ! checkyesno ${rcvar}; then if [ -n "${rc_quiet}" ]; then return 0 fi echo -n "Cannot '${rc_arg}' $name. Set ${rcvar} to " echo -n "YES in /etc/rc.conf or use 'one${rc_arg}' " echo "instead of '${rc_arg}'." return 0 fi fi if [ $rc_arg = "start" -a -z "$rc_fast" -a -n "$rc_pid" ]; then if [ -z "$rc_quiet" ]; then echo 1>&2 "${name} already running? " \ "(pid=$rc_pid)." fi return 1 fi # if there's a custom ${XXX_cmd}, # run that instead of the default # eval _cmd=\$${rc_arg}_cmd \ _precmd=\$${rc_arg}_precmd \ _postcmd=\$${rc_arg}_postcmd if [ -n "$_cmd" ]; then _run_rc_precmd || return 1 _run_rc_doit "$_cmd $rc_extra_args" || return 1 _run_rc_postcmd return $_return fi case "$rc_arg" in # default operations... describe) if [ -n "$desc" ]; then echo "$desc" fi ;; extracommands) echo "$extra_commands" ;; status) _run_rc_precmd || return 1 if [ -n "$rc_pid" ]; then echo "${name} is running as pid $rc_pid." else echo "${name} is not running." return 1 fi _run_rc_postcmd ;; start) if [ ! -x "${_chroot}${_chroot:+/}${command}" ]; then warn "run_rc_command: cannot run $command" return 1 fi if ! _run_rc_precmd; then warn "failed precmd routine for ${name}" return 1 fi # setup the full command to run # check_startmsgs && echo "Starting ${name}." if [ -n "$_chroot" ]; then _cd= _doit="\ ${_nice:+nice -n $_nice }\ ${_fib:+setfib -F $_fib }\ ${_env:+env $_env }\ chroot ${_user:+-u $_user }${_group:+-g $_group }${_groups:+-G $_groups }\ $_chroot $command $rc_flags $command_args" else _cd="${_chdir:+cd $_chdir && }" _doit="\ ${_fib:+setfib -F $_fib }\ ${_env:+env $_env }\ $command $rc_flags $command_args" if [ -n "$_user" ]; then _doit="su -m $_user -c 'sh -c \"$_doit\"'" fi if [ -n "$_nice" ]; then if [ -z "$_user" ]; then _doit="sh -c \"$_doit\"" fi _doit="nice -n $_nice $_doit" fi if [ -n "$_prepend" ]; then _doit="$_prepend $_doit" fi fi # Prepend default limits _doit="$_cd limits -C $_login_class $_doit" # run the full command # if ! _run_rc_doit "$_doit"; then warn "failed to start ${name}" return 1 fi # finally, run postcmd # _run_rc_postcmd ;; stop) if [ -z "$rc_pid" ]; then [ -n "$rc_fast" ] && return 0 _run_rc_notrunning return 1 fi _run_rc_precmd || return 1 # send the signal to stop # echo "Stopping ${name}." _doit=$(_run_rc_killcmd "${sig_stop:-TERM}") _run_rc_doit "$_doit" || return 1 # wait for the command to exit, # and run postcmd. wait_for_pids $rc_pid _run_rc_postcmd ;; reload) if [ -z "$rc_pid" ]; then _run_rc_notrunning return 1 fi _run_rc_precmd || return 1 _doit=$(_run_rc_killcmd "${sig_reload:-HUP}") _run_rc_doit "$_doit" || return 1 _run_rc_postcmd ;; restart) # prevent restart being called more # than once by any given script # if ${_rc_restart_done:-false}; then return 0 fi _rc_restart_done=true _run_rc_precmd || return 1 # run those in a subshell to keep global variables ( run_rc_command ${_rc_prefix}stop $rc_extra_args ) ( run_rc_command ${_rc_prefix}start $rc_extra_args ) _return=$? [ $_return -ne 0 ] && [ -z "$rc_force" ] && return 1 _run_rc_postcmd ;; poll) _run_rc_precmd || return 1 if [ -n "$rc_pid" ]; then wait_for_pids $rc_pid fi _run_rc_postcmd ;; rcvar) echo -n "# $name" if [ -n "$desc" ]; then echo " : $desc" else echo "" fi echo "#" # Get unique vars in $rcvar $rcvars for _v in $rcvar $rcvars; do case $v in $_v\ *|\ *$_v|*\ $_v\ *) ;; *) v="${v# } $_v" ;; esac done # Display variables. for _v in $v; do if [ -z "$_v" ]; then continue fi eval _desc=\$${_v}_desc eval _defval=\$${_v}_defval _h="-" eval echo \"$_v=\\\"\$$_v\\\"\" # decode multiple lines of _desc while [ -n "$_desc" ]; do case $_desc in *^^*) echo "# $_h ${_desc%%^^*}" _desc=${_desc#*^^} _h=" " ;; *) echo "# $_h ${_desc}" break ;; esac done echo "# (default: \"$_defval\")" done echo "" ;; *) rc_usage $_keywords ;; esac # Apply protect(1) to the PID if ${name}_oomprotect is set. case "$rc_arg" in start) # We cannot use protect(1) inside jails. if [ -n "$_oomprotect" ] && [ -f "${PROTECT}" ] && [ "$(sysctl -n security.jail.jailed)" -eq 0 ]; then pid=$(check_process $command) case $_oomprotect in [Aa][Ll][Ll]) ${PROTECT} -i -p ${pid} ;; [Yy][Ee][Ss]) ${PROTECT} -p ${pid} ;; esac fi ;; esac return $_return done echo 1>&2 "$0: unknown directive '$rc_arg'." rc_usage $_keywords # not reached } # # Helper functions for run_rc_command: common code. # They use such global variables besides the exported rc_* ones: # # name R/W # ------------------ # _precmd R # _postcmd R # _return W # _run_rc_precmd() { check_required_before "$rc_arg" || return 1 if [ -n "$_precmd" ]; then debug "run_rc_command: ${rc_arg}_precmd: $_precmd $rc_extra_args" eval "$_precmd $rc_extra_args" _return=$? # If precmd failed and force isn't set, request exit. if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then return 1 fi fi check_required_after "$rc_arg" || return 1 return 0 } _run_rc_postcmd() { if [ -n "$_postcmd" ]; then debug "run_rc_command: ${rc_arg}_postcmd: $_postcmd $rc_extra_args" eval "$_postcmd $rc_extra_args" _return=$? fi return 0 } _run_rc_doit() { debug "run_rc_command: doit: $*" eval "$@" _return=$? # If command failed and force isn't set, request exit. if [ $_return -ne 0 ] && [ -z "$rc_force" ]; then return 1 fi return 0 } _run_rc_notrunning() { local _pidmsg if [ -n "$pidfile" ]; then _pidmsg=" (check $pidfile)." else _pidmsg= fi echo 1>&2 "${name} not running?${_pidmsg}" } _run_rc_killcmd() { local _cmd _cmd="kill -$1 $rc_pid" if [ -n "$_user" ]; then _cmd="su -m ${_user} -c 'sh -c \"${_cmd}\"'" fi echo "$_cmd" } # # run_rc_script file arg # Start the script `file' with `arg', and correctly handle the # return value from the script. # If `file' ends with `.sh' and lives in /etc/rc.d, ignore it as it's # an old-style startup file. # If `file' ends with `.sh' and does not live in /etc/rc.d, it's sourced # into the current environment if $rc_fast_and_loose is set; otherwise # it is run as a child process. # If `file' appears to be a backup or scratch file, ignore it. # Otherwise if it is executable run as a child process. # run_rc_script() { _file=$1 _arg=$2 if [ -z "$_file" -o -z "$_arg" ]; then err 3 'USAGE: run_rc_script file arg' fi unset name command command_args command_interpreter \ extra_commands pidfile procname \ rcvar rcvars rcvars_obsolete required_dirs required_files \ required_vars eval unset ${_arg}_cmd ${_arg}_precmd ${_arg}_postcmd case "$_file" in /etc/rc.d/*.sh) # no longer allowed in the base warn "Ignoring old-style startup script $_file" ;; *[~#]|*.OLD|*.bak|*.orig|*,v) # scratch file; skip warn "Ignoring scratch file $_file" ;; *) # run in subshell if [ -x $_file ]; then if [ -n "$rc_fast_and_loose" ]; then set $_arg; . $_file else ( trap "echo Script $_file interrupted >&2 ; kill -QUIT $$" 3 trap "echo Script $_file interrupted >&2 ; exit 1" 2 trap "echo Script $_file running >&2" 29 set $_arg; . $_file ) fi fi ;; esac } # # load_rc_config [service] # Source in the configuration file(s) for a given service. # If no service is specified, only the global configuration # file(s) will be loaded. # load_rc_config() { local _name _rcvar_val _var _defval _v _msg _new _d _name=$1 if ${_rc_conf_loaded:-false}; then : else if [ -r /etc/defaults/rc.conf ]; then debug "Sourcing /etc/defaults/rc.conf" . /etc/defaults/rc.conf source_rc_confs elif [ -r /etc/rc.conf ]; then debug "Sourcing /etc/rc.conf (/etc/defaults/rc.conf doesn't exist)." . /etc/rc.conf fi _rc_conf_loaded=true fi # If a service name was specified, attempt to load # service-specific configuration if [ -n "$_name" ] ; then for _d in /etc ${local_startup}; do _d=${_d%/rc.d} if [ -f ${_d}/rc.conf.d/"$_name" ]; then debug "Sourcing ${_d}/rc.conf.d/$_name" . ${_d}/rc.conf.d/"$_name" elif [ -d ${_d}/rc.conf.d/"$_name" ] ; then local _rc for _rc in ${_d}/rc.conf.d/"$_name"/* ; do if [ -f "$_rc" ] ; then debug "Sourcing $_rc" . "$_rc" fi done fi done fi # Set defaults if defined. for _var in $rcvar $rcvars; do eval _defval=\$${_var}_defval if [ -n "$_defval" ]; then eval : \${$_var:=\$${_var}_defval} fi done # check obsolete rc.conf variables for _var in $rcvars_obsolete; do eval _v=\$$_var eval _msg=\$${_var}_obsolete_msg eval _new=\$${_var}_newvar case $_v in "") ;; *) if [ -z "$_new" ]; then _msg="Ignored." else eval $_new=\"\$$_var\" if [ -z "$_msg" ]; then _msg="Use \$$_new instead." fi fi warn "\$$_var is obsolete. $_msg" ;; esac done } # # load_rc_config_var name var # Read the rc.conf(5) var for name and set in the # current shell, using load_rc_config in a subshell to prevent # unwanted side effects from other variable assignments. # load_rc_config_var() { if [ $# -ne 2 ]; then err 3 'USAGE: load_rc_config_var name var' fi eval $(eval '( load_rc_config '$1' >/dev/null; if [ -n "${'$2'}" -o "${'$2'-UNSET}" != "UNSET" ]; then echo '$2'=\'\''${'$2'}\'\''; fi )' ) } # # rc_usage commands # Print a usage string for $0, with `commands' being a list of # valid commands. # rc_usage() { echo -n 1>&2 "Usage: $0 [fast|force|one|quiet](" _sep= for _elem; do echo -n 1>&2 "$_sep$_elem" _sep="|" done echo 1>&2 ")" exit 1 } # # err exitval message # Display message to stderr and log to the syslog, and exit with exitval. # err() { exitval=$1 shift if [ -x /usr/bin/logger ]; then logger "$0: ERROR: $*" fi echo 1>&2 "$0: ERROR: $*" exit $exitval } # # warn message # Display message to stderr and log to the syslog. # warn() { if [ -x /usr/bin/logger ]; then logger "$0: WARNING: $*" fi echo 1>&2 "$0: WARNING: $*" } # # info message # Display informational message to stdout and log to syslog. # info() { case ${rc_info} in [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) if [ -x /usr/bin/logger ]; then logger "$0: INFO: $*" fi echo "$0: INFO: $*" ;; esac } # # debug message # If debugging is enabled in rc.conf output message to stderr. # BEWARE that you don't call any subroutine that itself calls this # function. # debug() { case ${rc_debug} in [Yy][Ee][Ss]|[Tt][Rr][Uu][Ee]|[Oo][Nn]|1) if [ -x /usr/bin/logger ]; then logger "$0: DEBUG: $*" fi echo 1>&2 "$0: DEBUG: $*" ;; esac } # # backup_file action file cur backup # Make a backup copy of `file' into `cur', and save the previous # version of `cur' as `backup'. # # The `action' keyword can be one of the following: # # add `file' is now being backed up (and is possibly # being reentered into the backups system). `cur' # is created. # # update `file' has changed and needs to be backed up. -# If `cur' exists, it is copied to to `back' +# If `cur' exists, it is copied to `back' # and then `file' is copied to `cur'. # # remove `file' is no longer being tracked by the backups # system. `cur' is moved `back'. # # backup_file() { _action=$1 _file=$2 _cur=$3 _back=$4 case $_action in add|update) if [ -f $_cur ]; then cp -p $_cur $_back fi cp -p $_file $_cur chown root:wheel $_cur ;; remove) mv -f $_cur $_back ;; esac } # make_symlink src link # Make a symbolic link 'link' to src from basedir. If the # directory in which link is to be created does not exist # a warning will be displayed and an error will be returned. # Returns 0 on success, 1 otherwise. # make_symlink() { local src link linkdir _me src="$1" link="$2" linkdir="`dirname $link`" _me="make_symlink()" if [ -z "$src" -o -z "$link" ]; then warn "$_me: requires two arguments." return 1 fi if [ ! -d "$linkdir" ]; then warn "$_me: the directory $linkdir does not exist." return 1 fi if ! ln -sf $src $link; then warn "$_me: unable to make a symbolic link from $link to $src" return 1 fi return 0 } # devfs_rulesets_from_file file # Reads a set of devfs commands from file, and creates # the specified rulesets with their rules. Returns non-zero # if there was an error. # devfs_rulesets_from_file() { local file _err _me _opts file="$1" _me="devfs_rulesets_from_file" _err=0 if [ -z "$file" ]; then warn "$_me: you must specify a file" return 1 fi if [ ! -e "$file" ]; then debug "$_me: no such file ($file)" return 0 fi # Disable globbing so that the rule patterns are not expanded # by accident with matching filesystem entries. _opts=$-; set -f debug "reading rulesets from file ($file)" { while read line do case $line in \#*) continue ;; \[*\]*) rulenum=`expr "$line" : "\[.*=\([0-9]*\)\]"` if [ -z "$rulenum" ]; then warn "$_me: cannot extract rule number ($line)" _err=1 break fi rulename=`expr "$line" : "\[\(.*\)=[0-9]*\]"` if [ -z "$rulename" ]; then warn "$_me: cannot extract rule name ($line)" _err=1 break; fi eval $rulename=\$rulenum debug "found ruleset: $rulename=$rulenum" if ! /sbin/devfs rule -s $rulenum delset; then _err=1 break fi ;; *) rulecmd="${line%%"\#*"}" # evaluate the command incase it includes # other rules if [ -n "$rulecmd" ]; then debug "adding rule ($rulecmd)" if ! eval /sbin/devfs rule -s $rulenum $rulecmd then _err=1 break fi fi ;; esac if [ $_err -ne 0 ]; then debug "error in $_me" break fi done } < $file case $_opts in *f*) ;; *) set +f ;; esac return $_err } # devfs_init_rulesets # Initializes rulesets from configuration files. Returns # non-zero if there was an error. # devfs_init_rulesets() { local file _me _me="devfs_init_rulesets" # Go through this only once if [ -n "$devfs_rulesets_init" ]; then debug "$_me: devfs rulesets already initialized" return fi for file in $devfs_rulesets; do if ! devfs_rulesets_from_file $file; then warn "$_me: could not read rules from $file" return 1 fi done devfs_rulesets_init=1 debug "$_me: devfs rulesets initialized" return 0 } # devfs_set_ruleset ruleset [dir] # Sets the default ruleset of dir to ruleset. The ruleset argument # must be a ruleset name as specified in devfs.rules(5) file. # Returns non-zero if it could not set it successfully. # devfs_set_ruleset() { local devdir rs _me [ -n "$1" ] && eval rs=\$$1 || rs= [ -n "$2" ] && devdir="-m "$2"" || devdir= _me="devfs_set_ruleset" if [ -z "$rs" ]; then warn "$_me: you must specify a ruleset number" return 1 fi debug "$_me: setting ruleset ($rs) on mount-point (${devdir#-m })" if ! /sbin/devfs $devdir ruleset $rs; then warn "$_me: unable to set ruleset $rs to ${devdir#-m }" return 1 fi return 0 } # devfs_apply_ruleset ruleset [dir] # Apply ruleset number $ruleset to the devfs mountpoint $dir. # The ruleset argument must be a ruleset name as specified # in a devfs.rules(5) file. Returns 0 on success or non-zero # if it could not apply the ruleset. # devfs_apply_ruleset() { local devdir rs _me [ -n "$1" ] && eval rs=\$$1 || rs= [ -n "$2" ] && devdir="-m "$2"" || devdir= _me="devfs_apply_ruleset" if [ -z "$rs" ]; then warn "$_me: you must specify a ruleset" return 1 fi debug "$_me: applying ruleset ($rs) to mount-point (${devdir#-m })" if ! /sbin/devfs $devdir rule -s $rs applyset; then warn "$_me: unable to apply ruleset $rs to ${devdir#-m }" return 1 fi return 0 } # devfs_domount dir [ruleset] # Mount devfs on dir. If ruleset is specified it is set # on the mount-point. It must also be a ruleset name as specified # in a devfs.rules(5) file. Returns 0 on success. # devfs_domount() { local devdir rs _me devdir="$1" [ -n "$2" ] && rs=$2 || rs= _me="devfs_domount()" if [ -z "$devdir" ]; then warn "$_me: you must specify a mount-point" return 1 fi debug "$_me: mount-point is ($devdir), ruleset is ($rs)" if ! mount -t devfs dev "$devdir"; then warn "$_me: Unable to mount devfs on $devdir" return 1 fi if [ -n "$rs" ]; then devfs_init_rulesets devfs_set_ruleset $rs $devdir devfs -m $devdir rule applyset fi return 0 } # Provide a function for normalizing the mounting of memory # filesystems. This should allow the rest of the code here to remain # as close as possible between 5-current and 4-stable. # $1 = size # $2 = mount point # $3 = (optional) extra mdmfs flags mount_md() { if [ -n "$3" ]; then flags="$3" fi /sbin/mdmfs $flags -s $1 ${mfs_type} $2 } # Code common to scripts that need to load a kernel module # if it isn't in the kernel yet. Syntax: # load_kld [-e regex] [-m module] file # where -e or -m chooses the way to check if the module # is already loaded: # regex is egrep'd in the output from `kldstat -v', # module is passed to `kldstat -m'. # The default way is as though `-m file' were specified. load_kld() { local _loaded _mod _opt _re while getopts "e:m:" _opt; do case "$_opt" in e) _re="$OPTARG" ;; m) _mod="$OPTARG" ;; *) err 3 'USAGE: load_kld [-e regex] [-m module] file' ;; esac done shift $(($OPTIND - 1)) if [ $# -ne 1 ]; then err 3 'USAGE: load_kld [-e regex] [-m module] file' fi _mod=${_mod:-$1} _loaded=false if [ -n "$_re" ]; then if kldstat -v | egrep -q -e "$_re"; then _loaded=true fi else if kldstat -q -m "$_mod"; then _loaded=true fi fi if ! $_loaded; then if ! kldload "$1"; then warn "Unable to load kernel module $1" return 1 else info "$1 kernel module loaded." fi else debug "load_kld: $1 kernel module already loaded." fi return 0 } # ltr str src dst [var] # Change every $src in $str to $dst. # Useful when /usr is not yet mounted and we cannot use tr(1), sed(1) nor # awk(1). If var is non-NULL, set it to the result. ltr() { local _str _src _dst _out _com _var _str="$1" _src="$2" _dst="$3" _var="$4" _out="" local IFS="${_src}" for _com in ${_str}; do if [ -z "${_out}" ]; then _out="${_com}" else _out="${_out}${_dst}${_com}" fi done if [ -n "${_var}" ]; then setvar "${_var}" "${_out}" else echo "${_out}" fi } # Creates a list of providers for GELI encryption. geli_make_list() { local devices devices2 local provider mountpoint type options rest # Create list of GELI providers from fstab. while read provider mountpoint type options rest ; do case ":${options}" in :*noauto*) noauto=yes ;; *) noauto=no ;; esac case ":${provider}" in :#*) continue ;; *.eli) # Skip swap devices. if [ "${type}" = "swap" -o "${options}" = "sw" -o "${noauto}" = "yes" ]; then continue fi devices="${devices} ${provider}" ;; esac done < /etc/fstab # Append providers from geli_devices. devices="${devices} ${geli_devices}" for provider in ${devices}; do provider=${provider%.eli} provider=${provider#/dev/} devices2="${devices2} ${provider}" done echo ${devices2} } # Originally, root mount hold had to be released before mounting # the root filesystem. This delayed the boot, so it was changed # to only wait if the root device isn't readily available. This # can result in rc scripts executing before all the devices - such # as graid(8), or USB disks - can be accessed. This function can # be used to explicitly wait for root mount holds to be released. root_hold_wait() { local wait waited holders waited=0 while true; do holders="$(sysctl -n vfs.root_mount_hold)" if [ -z "${holders}" ]; then break; fi if [ ${waited} -eq 0 ]; then echo -n "Waiting ${root_hold_delay}s" \ "for the root mount holders: ${holders}" else echo -n . fi if [ ${waited} -ge ${root_hold_delay} ]; then echo break fi sleep 1 waited=$(($waited + 1)) done } # Find scripts in local_startup directories that use the old syntax # find_local_scripts_old() { zlist='' slist='' for dir in ${local_startup}; do if [ -d "${dir}" ]; then for file in ${dir}/[0-9]*.sh; do grep '^# PROVIDE:' $file >/dev/null 2>&1 && continue zlist="$zlist $file" done for file in ${dir}/[!0-9]*.sh; do grep '^# PROVIDE:' $file >/dev/null 2>&1 && continue slist="$slist $file" done fi done } find_local_scripts_new() { local_rc='' for dir in ${local_startup}; do if [ -d "${dir}" ]; then for file in `grep -l '^# PROVIDE:' ${dir}/* 2>/dev/null`; do case "$file" in *.sample) ;; *) if [ -x "$file" ]; then local_rc="${local_rc} ${file}" fi ;; esac done fi done } # check_required_{before|after} command # Check for things required by the command before and after its precmd, # respectively. The two separate functions are needed because some # conditions should prevent precmd from being run while other things # depend on precmd having already been run. # check_required_before() { local _f case "$1" in start) for _f in $required_vars; do if ! checkyesno $_f; then warn "\$${_f} is not enabled." if [ -z "$rc_force" ]; then return 1 fi fi done for _f in $required_dirs; do if [ ! -d "${_f}/." ]; then warn "${_f} is not a directory." if [ -z "$rc_force" ]; then return 1 fi fi done for _f in $required_files; do if [ ! -r "${_f}" ]; then warn "${_f} is not readable." if [ -z "$rc_force" ]; then return 1 fi fi done ;; esac return 0 } check_required_after() { local _f _args case "$1" in start) for _f in $required_modules; do case "${_f}" in *~*) _args="-e ${_f#*~} ${_f%%~*}" ;; *:*) _args="-m ${_f#*:} ${_f%%:*}" ;; *) _args="${_f}" ;; esac if ! load_kld ${_args}; then if [ -z "$rc_force" ]; then return 1 fi fi done ;; esac return 0 } # check_jail mib # Return true if security.jail.$mib exists and set to 1. check_jail() { local _mib _v _mib=$1 if _v=$(${SYSCTL_N} "security.jail.$_mib" 2> /dev/null); then case $_v in 1) return 0;; esac fi return 1 } # check_kern_features mib # Return existence of kern.features.* sysctl MIB as true or # false. The result will be cached in $_rc_cache_kern_features_ # namespace. "0" means the kern.features.X exists. check_kern_features() { local _v [ -n "$1" ] || return 1; eval _v=\$_rc_cache_kern_features_$1 [ -n "$_v" ] && return "$_v"; if ${SYSCTL_N} kern.features.$1 > /dev/null 2>&1; then eval _rc_cache_kern_features_$1=0 return 0 else eval _rc_cache_kern_features_$1=1 return 1 fi } # check_namevarlist var # Return "0" if ${name}_var is reserved in rc.subr. _rc_namevarlist="program chroot chdir env flags fib nice user group groups prepend" check_namevarlist() { local _v for _v in $_rc_namevarlist; do case $1 in $_v) return 0 ;; esac done return 1 } # _echoonce var msg mode # mode=0: Echo $msg if ${$var} is empty. # After doing echo, a string is set to ${$var}. # # mode=1: Echo $msg if ${$var} is a string with non-zero length. # _echoonce() { local _var _msg _mode eval _var=\$$1 _msg=$2 _mode=$3 case $_mode in 1) [ -n "$_var" ] && echo "$_msg" ;; *) [ -z "$_var" ] && echo -n "$_msg" && eval "$1=finished" ;; esac } # If the loader env variable rc.debug is set, turn on debugging. rc.conf will # still override this, but /etc/defaults/rc.conf can't unconditionally set this # since it would undo what we've done here. if kenv -q rc.debug > /dev/null ; then rc_debug=YES fi Index: head/usr.bin/localedef/ctype.c =================================================================== --- head/usr.bin/localedef/ctype.c (revision 327229) +++ head/usr.bin/localedef/ctype.c (revision 327230) @@ -1,463 +1,463 @@ /*- * Copyright 2011 Nexenta Systems, Inc. All rights reserved. * Copyright 2012 Garrett D'Amore All rights reserved. * Copyright 2015 John Marino * * This source code is derived from the illumos localedef command, and * provided under BSD-style license terms by Nexenta Systems, Inc. * * 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. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS 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 COPYRIGHT OWNER 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. */ /* * LC_CTYPE database generation routines for localedef. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include "localedef.h" #include "parser.h" #include "runefile.h" /* Needed for bootstrapping, _CTYPE_N */ #ifndef _CTYPE_N #define _CTYPE_N 0x00400000L #endif #define _ISUPPER _CTYPE_U #define _ISLOWER _CTYPE_L #define _ISDIGIT _CTYPE_D #define _ISXDIGIT _CTYPE_X #define _ISSPACE _CTYPE_S #define _ISBLANK _CTYPE_B #define _ISALPHA _CTYPE_A #define _ISPUNCT _CTYPE_P #define _ISGRAPH _CTYPE_G #define _ISPRINT _CTYPE_R #define _ISCNTRL _CTYPE_C #define _E1 _CTYPE_Q #define _E2 _CTYPE_I #define _E3 0 #define _E4 _CTYPE_N #define _E5 _CTYPE_T static wchar_t last_ctype; static int ctype_compare(const void *n1, const void *n2); typedef struct ctype_node { wchar_t wc; int32_t ctype; int32_t toupper; int32_t tolower; RB_ENTRY(ctype_node) entry; } ctype_node_t; static RB_HEAD(ctypes, ctype_node) ctypes; RB_GENERATE_STATIC(ctypes, ctype_node, entry, ctype_compare); static int ctype_compare(const void *n1, const void *n2) { const ctype_node_t *c1 = n1; const ctype_node_t *c2 = n2; return (c1->wc < c2->wc ? -1 : c1->wc > c2->wc ? 1 : 0); } void init_ctype(void) { RB_INIT(&ctypes); } static void add_ctype_impl(ctype_node_t *ctn) { switch (last_kw) { case T_ISUPPER: ctn->ctype |= (_ISUPPER | _ISALPHA | _ISGRAPH | _ISPRINT); break; case T_ISLOWER: ctn->ctype |= (_ISLOWER | _ISALPHA | _ISGRAPH | _ISPRINT); break; case T_ISALPHA: ctn->ctype |= (_ISALPHA | _ISGRAPH | _ISPRINT); break; case T_ISDIGIT: ctn->ctype |= (_ISDIGIT | _ISGRAPH | _ISPRINT | _ISXDIGIT | _E4); break; case T_ISSPACE: ctn->ctype |= _ISSPACE; break; case T_ISCNTRL: ctn->ctype |= _ISCNTRL; break; case T_ISGRAPH: ctn->ctype |= (_ISGRAPH | _ISPRINT); break; case T_ISPRINT: ctn->ctype |= _ISPRINT; break; case T_ISPUNCT: ctn->ctype |= (_ISPUNCT | _ISGRAPH | _ISPRINT); break; case T_ISXDIGIT: ctn->ctype |= (_ISXDIGIT | _ISPRINT); break; case T_ISBLANK: ctn->ctype |= (_ISBLANK | _ISSPACE); break; case T_ISPHONOGRAM: ctn->ctype |= (_E1 | _ISPRINT | _ISGRAPH); break; case T_ISIDEOGRAM: ctn->ctype |= (_E2 | _ISPRINT | _ISGRAPH); break; case T_ISENGLISH: ctn->ctype |= (_E3 | _ISPRINT | _ISGRAPH); break; case T_ISNUMBER: ctn->ctype |= (_E4 | _ISPRINT | _ISGRAPH); break; case T_ISSPECIAL: ctn->ctype |= (_E5 | _ISPRINT | _ISGRAPH); break; case T_ISALNUM: /* * We can't do anything with this. The character * should already be specified as a digit or alpha. */ break; default: errf("not a valid character class"); } } static ctype_node_t * get_ctype(wchar_t wc) { ctype_node_t srch; ctype_node_t *ctn; srch.wc = wc; if ((ctn = RB_FIND(ctypes, &ctypes, &srch)) == NULL) { if ((ctn = calloc(1, sizeof (*ctn))) == NULL) { errf("out of memory"); return (NULL); } ctn->wc = wc; RB_INSERT(ctypes, &ctypes, ctn); } return (ctn); } void add_ctype(int val) { ctype_node_t *ctn; if ((ctn = get_ctype(val)) == NULL) { INTERR; return; } add_ctype_impl(ctn); last_ctype = ctn->wc; } void add_ctype_range(wchar_t end) { ctype_node_t *ctn; wchar_t cur; if (end < last_ctype) { errf("malformed character range (%u ... %u))", last_ctype, end); return; } for (cur = last_ctype + 1; cur <= end; cur++) { if ((ctn = get_ctype(cur)) == NULL) { INTERR; return; } add_ctype_impl(ctn); } last_ctype = end; } /* * A word about widths: if the width mask is specified, then libc * unconditionally honors it. Otherwise, it assumes printable * characters have width 1, and non-printable characters have width - * -1 (except for NULL which is special with with 0). Hence, we have + * -1 (except for NULL which is special with width 0). Hence, we have * no need to inject defaults here -- the "default" unset value of 0 * indicates that libc should use its own logic in wcwidth as described. */ void add_width(int wc, int width) { ctype_node_t *ctn; if ((ctn = get_ctype(wc)) == NULL) { INTERR; return; } ctn->ctype &= ~(_CTYPE_SWM); switch (width) { case 0: ctn->ctype |= _CTYPE_SW0; break; case 1: ctn->ctype |= _CTYPE_SW1; break; case 2: ctn->ctype |= _CTYPE_SW2; break; case 3: ctn->ctype |= _CTYPE_SW3; break; } } void add_width_range(int start, int end, int width) { for (; start <= end; start++) { add_width(start, width); } } void add_caseconv(int val, int wc) { ctype_node_t *ctn; ctn = get_ctype(val); if (ctn == NULL) { INTERR; return; } switch (last_kw) { case T_TOUPPER: ctn->toupper = wc; break; case T_TOLOWER: ctn->tolower = wc; break; default: INTERR; break; } } void dump_ctype(void) { FILE *f; _FileRuneLocale rl; ctype_node_t *ctn, *last_ct, *last_lo, *last_up; _FileRuneEntry *ct = NULL; _FileRuneEntry *lo = NULL; _FileRuneEntry *up = NULL; wchar_t wc; (void) memset(&rl, 0, sizeof (rl)); last_ct = NULL; last_lo = NULL; last_up = NULL; if ((f = open_category()) == NULL) return; (void) memcpy(rl.magic, _FILE_RUNE_MAGIC_1, 8); (void) strlcpy(rl.encoding, get_wide_encoding(), sizeof (rl.encoding)); /* * Initialize the identity map. */ for (wc = 0; (unsigned)wc < _CACHED_RUNES; wc++) { rl.maplower[wc] = wc; rl.mapupper[wc] = wc; } RB_FOREACH(ctn, ctypes, &ctypes) { int conflict = 0; wc = ctn->wc; /* * POSIX requires certain portable characters have * certain types. Add them if they are missing. */ if ((wc >= 1) && (wc <= 127)) { if ((wc >= 'A') && (wc <= 'Z')) ctn->ctype |= _ISUPPER; if ((wc >= 'a') && (wc <= 'z')) ctn->ctype |= _ISLOWER; if ((wc >= '0') && (wc <= '9')) ctn->ctype |= _ISDIGIT; if (wc == ' ') ctn->ctype |= _ISPRINT; if (strchr(" \f\n\r\t\v", (char)wc) != NULL) ctn->ctype |= _ISSPACE; if (strchr("0123456789ABCDEFabcdef", (char)wc) != NULL) ctn->ctype |= _ISXDIGIT; if (strchr(" \t", (char)wc)) ctn->ctype |= _ISBLANK; /* * Technically these settings are only * required for the C locale. However, it * turns out that because of the historical * version of isprint(), we need them for all * locales as well. Note that these are not * necessarily valid punctation characters in * the current language, but ispunct() needs * to return TRUE for them. */ if (strchr("!\"'#$%&()*+,-./:;<=>?@[\\]^_`{|}~", (char)wc)) ctn->ctype |= _ISPUNCT; } /* * POSIX also requires that certain types imply * others. Add any inferred types here. */ if (ctn->ctype & (_ISUPPER |_ISLOWER)) ctn->ctype |= _ISALPHA; if (ctn->ctype & _ISDIGIT) ctn->ctype |= _ISXDIGIT; if (ctn->ctype & _ISBLANK) ctn->ctype |= _ISSPACE; if (ctn->ctype & (_ISALPHA|_ISDIGIT|_ISXDIGIT)) ctn->ctype |= _ISGRAPH; if (ctn->ctype & _ISGRAPH) ctn->ctype |= _ISPRINT; /* * Finally, POSIX requires that certain combinations * are invalid. We don't flag this as a fatal error, * but we will warn about. */ if ((ctn->ctype & _ISALPHA) && (ctn->ctype & (_ISPUNCT|_ISDIGIT))) conflict++; if ((ctn->ctype & _ISPUNCT) && (ctn->ctype & (_ISDIGIT|_ISALPHA|_ISXDIGIT))) conflict++; if ((ctn->ctype & _ISSPACE) && (ctn->ctype & _ISGRAPH)) conflict++; if ((ctn->ctype & _ISCNTRL) && (ctn->ctype & _ISPRINT)) conflict++; if ((wc == ' ') && (ctn->ctype & (_ISPUNCT|_ISGRAPH))) conflict++; if (conflict) { warn("conflicting classes for character 0x%x (%x)", wc, ctn->ctype); } /* * Handle the lower 256 characters using the simple * optimization. Note that if we have not defined the * upper/lower case, then we identity map it. */ if ((unsigned)wc < _CACHED_RUNES) { rl.runetype[wc] = ctn->ctype; if (ctn->tolower) rl.maplower[wc] = ctn->tolower; if (ctn->toupper) rl.mapupper[wc] = ctn->toupper; continue; } if ((last_ct != NULL) && (last_ct->ctype == ctn->ctype) && (last_ct->wc + 1 == wc)) { ct[rl.runetype_ext_nranges-1].max = wc; } else { rl.runetype_ext_nranges++; ct = realloc(ct, sizeof (*ct) * rl.runetype_ext_nranges); ct[rl.runetype_ext_nranges - 1].min = wc; ct[rl.runetype_ext_nranges - 1].max = wc; ct[rl.runetype_ext_nranges - 1].map = ctn->ctype; } last_ct = ctn; if (ctn->tolower == 0) { last_lo = NULL; } else if ((last_lo != NULL) && (last_lo->tolower + 1 == ctn->tolower)) { lo[rl.maplower_ext_nranges-1].max = wc; last_lo = ctn; } else { rl.maplower_ext_nranges++; lo = realloc(lo, sizeof (*lo) * rl.maplower_ext_nranges); lo[rl.maplower_ext_nranges - 1].min = wc; lo[rl.maplower_ext_nranges - 1].max = wc; lo[rl.maplower_ext_nranges - 1].map = ctn->tolower; last_lo = ctn; } if (ctn->toupper == 0) { last_up = NULL; } else if ((last_up != NULL) && (last_up->toupper + 1 == ctn->toupper)) { up[rl.mapupper_ext_nranges-1].max = wc; last_up = ctn; } else { rl.mapupper_ext_nranges++; up = realloc(up, sizeof (*up) * rl.mapupper_ext_nranges); up[rl.mapupper_ext_nranges - 1].min = wc; up[rl.mapupper_ext_nranges - 1].max = wc; up[rl.mapupper_ext_nranges - 1].map = ctn->toupper; last_up = ctn; } } if ((wr_category(&rl, sizeof (rl), f) < 0) || (wr_category(ct, sizeof (*ct) * rl.runetype_ext_nranges, f) < 0) || (wr_category(lo, sizeof (*lo) * rl.maplower_ext_nranges, f) < 0) || (wr_category(up, sizeof (*up) * rl.mapupper_ext_nranges, f) < 0)) { return; } close_category(f); } Index: head/usr.bin/mail/cmd3.c =================================================================== --- head/usr.bin/mail/cmd3.c (revision 327229) +++ head/usr.bin/mail/cmd3.c (revision 327230) @@ -1,727 +1,727 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1980, 1993 * The Regents of the University of California. All rights reserved. * * 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. 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 #if 0 static char sccsid[] = "@(#)cmd3.c 8.2 (Berkeley) 4/20/95"; #endif #endif /* not lint */ #include __FBSDID("$FreeBSD$"); #include "rcv.h" #include "extern.h" /* * Mail -- a mail program * * Still more user commands. */ /* * Process a shell escape by saving signals, ignoring signals, * and forking a sh -c */ int shell(char *str) { sig_t sigint = signal(SIGINT, SIG_IGN); char *sh; char cmd[BUFSIZ]; if (strlcpy(cmd, str, sizeof(cmd)) >= sizeof(cmd)) return (1); if (bangexp(cmd, sizeof(cmd)) < 0) return (1); if ((sh = value("SHELL")) == NULL) sh = _PATH_CSHELL; (void)run_command(sh, 0, -1, -1, "-c", cmd, NULL); (void)signal(SIGINT, sigint); printf("!\n"); return (0); } /* * Fork an interactive shell. */ /*ARGSUSED*/ int dosh(char *str __unused) { sig_t sigint = signal(SIGINT, SIG_IGN); char *sh; if ((sh = value("SHELL")) == NULL) sh = _PATH_CSHELL; (void)run_command(sh, 0, -1, -1, NULL); (void)signal(SIGINT, sigint); printf("\n"); return (0); } /* * Expand the shell escape by expanding unescaped !'s into the * last issued command where possible. */ int bangexp(char *str, size_t strsize) { char bangbuf[BUFSIZ]; static char lastbang[BUFSIZ]; char *cp, *cp2; int n, changed = 0; cp = str; cp2 = bangbuf; n = sizeof(bangbuf); while (*cp != '\0') { if (*cp == '!') { if (n < (int)strlen(lastbang)) { overf: printf("Command buffer overflow\n"); return (-1); } changed++; if (strlcpy(cp2, lastbang, sizeof(bangbuf) - (cp2 - bangbuf)) >= sizeof(bangbuf) - (cp2 - bangbuf)) goto overf; cp2 += strlen(lastbang); n -= strlen(lastbang); cp++; continue; } if (*cp == '\\' && cp[1] == '!') { if (--n <= 1) goto overf; *cp2++ = '!'; cp += 2; changed++; } if (--n <= 1) goto overf; *cp2++ = *cp++; } *cp2 = 0; if (changed) { printf("!%s\n", bangbuf); (void)fflush(stdout); } if (strlcpy(str, bangbuf, strsize) >= strsize) goto overf; if (strlcpy(lastbang, bangbuf, sizeof(lastbang)) >= sizeof(lastbang)) goto overf; return (0); } /* * Print out a nice help message from some file or another. */ int help(void) { int c; FILE *f; if ((f = Fopen(_PATH_HELP, "r")) == NULL) { warn("%s", _PATH_HELP); return (1); } while ((c = getc(f)) != EOF) putchar(c); (void)Fclose(f); return (0); } /* * Change user's working directory. */ int schdir(void *v) { char **arglist = v; char *cp; if (*arglist == NULL) { if (homedir == NULL) return (1); cp = homedir; } else if ((cp = expand(*arglist)) == NULL) return (1); if (chdir(cp) < 0) { warn("%s", cp); return (1); } return (0); } int respond(void *v) { int *msgvec = v; if (value("Replyall") == NULL && value("flipr") == NULL) return (dorespond(msgvec)); else return (doRespond(msgvec)); } /* * Reply to a list of messages. Extract each name from the * message header and send them off to mail1() */ int dorespond(int *msgvec) { struct message *mp; char *cp, *rcv, *replyto; char **ap; struct name *np; struct header head; if (msgvec[1] != 0) { printf("Sorry, can't reply to multiple messages at once\n"); return (1); } mp = &message[msgvec[0] - 1]; touch(mp); dot = mp; if ((rcv = skin(hfield("from", mp))) == NULL) rcv = skin(nameof(mp, 1)); if ((replyto = skin(hfield("reply-to", mp))) != NULL) np = extract(replyto, GTO); else if ((cp = skin(hfield("to", mp))) != NULL) np = extract(cp, GTO); else np = NULL; np = elide(np); /* * Delete my name from the reply list, * and with it, all my alternate names. */ np = delname(np, myname); if (altnames) for (ap = altnames; *ap != NULL; ap++) np = delname(np, *ap); if (np != NULL && replyto == NULL) np = cat(np, extract(rcv, GTO)); else if (np == NULL) { if (replyto != NULL) printf("Empty reply-to field -- replying to author\n"); np = extract(rcv, GTO); } head.h_to = np; if ((head.h_subject = hfield("subject", mp)) == NULL) head.h_subject = hfield("subj", mp); head.h_subject = reedit(head.h_subject); if (replyto == NULL && (cp = skin(hfield("cc", mp))) != NULL) { np = elide(extract(cp, GCC)); np = delname(np, myname); if (altnames != 0) for (ap = altnames; *ap != NULL; ap++) np = delname(np, *ap); head.h_cc = np; } else head.h_cc = NULL; head.h_bcc = NULL; head.h_smopts = NULL; head.h_replyto = value("REPLYTO"); head.h_inreplyto = skin(hfield("message-id", mp)); mail1(&head, 1); return (0); } /* - * Modify the subject we are replying to to begin with Re: if + * Modify the message subject to begin with "Re:" if * it does not already. */ char * reedit(char *subj) { char *newsubj; if (subj == NULL) return (NULL); if ((subj[0] == 'r' || subj[0] == 'R') && (subj[1] == 'e' || subj[1] == 'E') && subj[2] == ':') return (subj); newsubj = salloc(strlen(subj) + 5); sprintf(newsubj, "Re: %s", subj); return (newsubj); } /* * Preserve the named messages, so that they will be sent * back to the system mailbox. */ int preserve(void *v) { int *msgvec = v; int *ip, mesg; struct message *mp; if (edit) { printf("Cannot \"preserve\" in edit mode\n"); return (1); } for (ip = msgvec; *ip != 0; ip++) { mesg = *ip; mp = &message[mesg-1]; mp->m_flag |= MPRESERVE; mp->m_flag &= ~MBOX; dot = mp; } return (0); } /* * Mark all given messages as unread. */ int unread(void *v) { int *msgvec = v; int *ip; for (ip = msgvec; *ip != 0; ip++) { dot = &message[*ip-1]; dot->m_flag &= ~(MREAD|MTOUCH); dot->m_flag |= MSTATUS; } return (0); } /* * Print the size of each message. */ int messize(void *v) { int *msgvec = v; struct message *mp; int *ip, mesg; for (ip = msgvec; *ip != 0; ip++) { mesg = *ip; mp = &message[mesg-1]; printf("%d: %ld/%ld\n", mesg, mp->m_lines, mp->m_size); } return (0); } /* * Quit quickly. If we are sourcing, just pop the input level * by returning an error. */ int rexit(void *v) { if (sourcing) return (1); exit(0); /*NOTREACHED*/ } /* * Set or display a variable value. Syntax is similar to that * of csh. */ int set(void *v) { char **arglist = v; struct var *vp; char *cp, *cp2; char varbuf[BUFSIZ], **ap, **p; int errs, h, s; if (*arglist == NULL) { for (h = 0, s = 1; h < HSHSIZE; h++) for (vp = variables[h]; vp != NULL; vp = vp->v_link) s++; ap = (char **)salloc(s * sizeof(*ap)); for (h = 0, p = ap; h < HSHSIZE; h++) for (vp = variables[h]; vp != NULL; vp = vp->v_link) *p++ = vp->v_name; *p = NULL; sort(ap); for (p = ap; *p != NULL; p++) printf("%s\t%s\n", *p, value(*p)); return (0); } errs = 0; for (ap = arglist; *ap != NULL; ap++) { cp = *ap; cp2 = varbuf; while (cp2 < varbuf + sizeof(varbuf) - 1 && *cp != '=' && *cp != '\0') *cp2++ = *cp++; *cp2 = '\0'; if (*cp == '\0') cp = ""; else cp++; if (equal(varbuf, "")) { printf("Non-null variable name required\n"); errs++; continue; } assign(varbuf, cp); } return (errs); } /* * Unset a bunch of variable values. */ int unset(void *v) { char **arglist = v; struct var *vp, *vp2; int errs, h; char **ap; errs = 0; for (ap = arglist; *ap != NULL; ap++) { if ((vp2 = lookup(*ap)) == NULL) { if (getenv(*ap)) unsetenv(*ap); else if (!sourcing) { printf("\"%s\": undefined variable\n", *ap); errs++; } continue; } h = hash(*ap); if (vp2 == variables[h]) { variables[h] = variables[h]->v_link; vfree(vp2->v_name); vfree(vp2->v_value); (void)free(vp2); continue; } for (vp = variables[h]; vp->v_link != vp2; vp = vp->v_link) ; vp->v_link = vp2->v_link; vfree(vp2->v_name); vfree(vp2->v_value); (void)free(vp2); } return (errs); } /* * Put add users to a group. */ int group(void *v) { char **argv = v; struct grouphead *gh; struct group *gp; char **ap, *gname, **p; int h, s; if (*argv == NULL) { for (h = 0, s = 1; h < HSHSIZE; h++) for (gh = groups[h]; gh != NULL; gh = gh->g_link) s++; ap = (char **)salloc(s * sizeof(*ap)); for (h = 0, p = ap; h < HSHSIZE; h++) for (gh = groups[h]; gh != NULL; gh = gh->g_link) *p++ = gh->g_name; *p = NULL; sort(ap); for (p = ap; *p != NULL; p++) printgroup(*p); return (0); } if (argv[1] == NULL) { printgroup(*argv); return (0); } gname = *argv; h = hash(gname); if ((gh = findgroup(gname)) == NULL) { if ((gh = calloc(1, sizeof(*gh))) == NULL) err(1, "Out of memory"); gh->g_name = vcopy(gname); gh->g_list = NULL; gh->g_link = groups[h]; groups[h] = gh; } /* * Insert names from the command list into the group. * Who cares if there are duplicates? They get tossed * later anyway. */ for (ap = argv+1; *ap != NULL; ap++) { if ((gp = calloc(1, sizeof(*gp))) == NULL) err(1, "Out of memory"); gp->ge_name = vcopy(*ap); gp->ge_link = gh->g_list; gh->g_list = gp; } return (0); } /* * Sort the passed string vecotor into ascending dictionary * order. */ void sort(char **list) { char **ap; for (ap = list; *ap != NULL; ap++) ; if (ap-list < 2) return; qsort(list, ap-list, sizeof(*list), diction); } /* * Do a dictionary order comparison of the arguments from * qsort. */ int diction(const void *a, const void *b) { return (strcmp(*(const char **)a, *(const char **)b)); } /* * The do nothing command for comments. */ /*ARGSUSED*/ int null(int e __unused) { return (0); } /* * Change to another file. With no argument, print information about * the current file. */ int file(char **argv) { if (argv[0] == NULL) { newfileinfo(0); return (0); } if (setfile(*argv) < 0) return (1); announce(); return (0); } /* * Expand file names like echo */ int echo(char **argv) { char **ap, *cp; for (ap = argv; *ap != NULL; ap++) { cp = *ap; if ((cp = expand(cp)) != NULL) { if (ap != argv) printf(" "); printf("%s", cp); } } printf("\n"); return (0); } int Respond(int *msgvec) { if (value("Replyall") == NULL && value("flipr") == NULL) return (doRespond(msgvec)); else return (dorespond(msgvec)); } /* * Reply to a series of messages by simply mailing to the senders * and not messing around with the To: and Cc: lists as in normal * reply. */ int doRespond(int msgvec[]) { struct header head; struct message *mp; int *ap; char *cp, *mid; head.h_to = NULL; for (ap = msgvec; *ap != 0; ap++) { mp = &message[*ap - 1]; touch(mp); dot = mp; if ((cp = skin(hfield("from", mp))) == NULL) cp = skin(nameof(mp, 2)); head.h_to = cat(head.h_to, extract(cp, GTO)); mid = skin(hfield("message-id", mp)); } if (head.h_to == NULL) return (0); mp = &message[msgvec[0] - 1]; if ((head.h_subject = hfield("subject", mp)) == NULL) head.h_subject = hfield("subj", mp); head.h_subject = reedit(head.h_subject); head.h_cc = NULL; head.h_bcc = NULL; head.h_smopts = NULL; head.h_replyto = value("REPLYTO"); head.h_inreplyto = mid; mail1(&head, 1); return (0); } /* * Conditional commands. These allow one to parameterize one's * .mailrc and do some things if sending, others if receiving. */ int ifcmd(char **argv) { char *cp; if (cond != CANY) { printf("Illegal nested \"if\"\n"); return (1); } cond = CANY; cp = argv[0]; switch (*cp) { case 'r': case 'R': cond = CRCV; break; case 's': case 'S': cond = CSEND; break; default: printf("Unrecognized if-keyword: \"%s\"\n", cp); return (1); } return (0); } /* * Implement 'else'. This is pretty simple -- we just * flip over the conditional flag. */ int elsecmd(void) { switch (cond) { case CANY: printf("\"Else\" without matching \"if\"\n"); return (1); case CSEND: cond = CRCV; break; case CRCV: cond = CSEND; break; default: printf("Mail's idea of conditions is screwed up\n"); cond = CANY; break; } return (0); } /* * End of if statement. Just set cond back to anything. */ int endifcmd(void) { if (cond == CANY) { printf("\"Endif\" without matching \"if\"\n"); return (1); } cond = CANY; return (0); } /* * Set the list of alternate names. */ int alternates(char **namelist) { int c; char **ap, **ap2, *cp; c = argcount(namelist) + 1; if (c == 1) { if (altnames == 0) return (0); for (ap = altnames; *ap != NULL; ap++) printf("%s ", *ap); printf("\n"); return (0); } if (altnames != 0) (void)free(altnames); if ((altnames = calloc((unsigned)c, sizeof(char *))) == NULL) err(1, "Out of memory"); for (ap = namelist, ap2 = altnames; *ap != NULL; ap++, ap2++) { cp = calloc((unsigned)strlen(*ap) + 1, sizeof(char)); strcpy(cp, *ap); *ap2 = cp; } *ap2 = 0; return (0); } Index: head/usr.bin/xargs/strnsubst.c =================================================================== --- head/usr.bin/xargs/strnsubst.c (revision 327229) +++ head/usr.bin/xargs/strnsubst.c (revision 327230) @@ -1,109 +1,109 @@ /* $xMach: strnsubst.c,v 1.3 2002/02/23 02:10:24 jmallett Exp $ */ /* * Copyright (c) 2002 J. Mallett. All rights reserved. * You may do whatever you want with this file as long as * the above copyright and this notice remain intact, along * with the following statement: * For the man who taught me vi, and who got too old, too young. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include void strnsubst(char **, const char *, const char *, size_t); /* * Replaces str with a string consisting of str with match replaced with * replstr as many times as can be done before the constructed string is * maxsize bytes large. It does not free the string pointed to by str, it * is up to the calling program to be sure that the original contents of * str as well as the new contents are handled in an appropriate manner. * If replstr is NULL, then that internally is changed to a nil-string, so * that we can still pretend to do somewhat meaningful substitution. * No value is returned. */ void strnsubst(char **str, const char *match, const char *replstr, size_t maxsize) { char *s1, *s2, *this; s1 = *str; if (s1 == NULL) return; /* - * If maxsize is 0 then set it to to the length of s1, because we have + * If maxsize is 0 then set it to the length of s1, because we have * to duplicate s1. XXX we maybe should double-check whether the match * appears in s1. If it doesn't, then we also have to set the length * to the length of s1, to avoid modifying the argument. It may make * sense to check if maxsize is <= strlen(s1), because in that case we * want to return the unmodified string, too. */ if (maxsize == 0) { match = NULL; maxsize = strlen(s1) + 1; } s2 = calloc(1, maxsize); if (s2 == NULL) err(1, "calloc"); if (replstr == NULL) replstr = ""; if (match == NULL || replstr == NULL || maxsize == strlen(s1)) { strlcpy(s2, s1, maxsize); goto done; } for (;;) { this = strstr(s1, match); if (this == NULL) break; if ((strlen(s2) + strlen(s1) + strlen(replstr) - strlen(match) + 1) > maxsize) { strlcat(s2, s1, maxsize); goto done; } strncat(s2, s1, (uintptr_t)this - (uintptr_t)s1); strcat(s2, replstr); s1 = this + strlen(match); } strcat(s2, s1); done: *str = s2; return; } #ifdef TEST #include int main(void) { char *x, *y, *z, *za; x = "{}%$"; strnsubst(&x, "%$", "{} enpury!", 255); y = x; strnsubst(&y, "}{}", "ybir", 255); z = y; strnsubst(&z, "{", "v ", 255); za = z; strnsubst(&z, NULL, za, 255); if (strcmp(z, "v ybir enpury!") == 0) printf("strnsubst() seems to work!\n"); else printf("strnsubst() is broken.\n"); printf("%s\n", z); free(x); free(y); free(z); free(za); return 0; } #endif Index: head/usr.sbin/ctld/login.c =================================================================== --- head/usr.sbin/ctld/login.c (revision 327229) +++ head/usr.sbin/ctld/login.c (revision 327230) @@ -1,1059 +1,1059 @@ /*- * SPDX-License-Identifier: BSD-2-Clause-FreeBSD * * Copyright (c) 2012 The FreeBSD Foundation * All rights reserved. * * This software was developed by Edward Tomasz Napierala under sponsorship * from the FreeBSD Foundation. * * 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. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include "ctld.h" #include "iscsi_proto.h" static void login_send_error(struct pdu *request, char class, char detail); static void login_set_nsg(struct pdu *response, int nsg) { struct iscsi_bhs_login_response *bhslr; assert(nsg == BHSLR_STAGE_SECURITY_NEGOTIATION || nsg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || nsg == BHSLR_STAGE_FULL_FEATURE_PHASE); bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; bhslr->bhslr_flags &= 0xFC; bhslr->bhslr_flags |= nsg; bhslr->bhslr_flags |= BHSLR_FLAGS_TRANSIT; } static int login_csg(const struct pdu *request) { struct iscsi_bhs_login_request *bhslr; bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; return ((bhslr->bhslr_flags & 0x0C) >> 2); } static void login_set_csg(struct pdu *response, int csg) { struct iscsi_bhs_login_response *bhslr; assert(csg == BHSLR_STAGE_SECURITY_NEGOTIATION || csg == BHSLR_STAGE_OPERATIONAL_NEGOTIATION || csg == BHSLR_STAGE_FULL_FEATURE_PHASE); bhslr = (struct iscsi_bhs_login_response *)response->pdu_bhs; bhslr->bhslr_flags &= 0xF3; bhslr->bhslr_flags |= csg << 2; } static struct pdu * login_receive(struct connection *conn, bool initial) { struct pdu *request; struct iscsi_bhs_login_request *bhslr; request = pdu_new(conn); pdu_receive(request); if ((request->pdu_bhs->bhs_opcode & ~ISCSI_BHS_OPCODE_IMMEDIATE) != ISCSI_BHS_OPCODE_LOGIN_REQUEST) { /* * The first PDU in session is special - if we receive any PDU * different than login request, we have to drop the connection * without sending response ("A target receiving any PDU * except a Login request before the Login Phase is started MUST * immediately terminate the connection on which the PDU * was received.") */ if (initial == false) login_send_error(request, 0x02, 0x0b); log_errx(1, "protocol error: received invalid opcode 0x%x", request->pdu_bhs->bhs_opcode); } bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; /* * XXX: Implement the C flag some day. */ if ((bhslr->bhslr_flags & BHSLR_FLAGS_CONTINUE) != 0) { login_send_error(request, 0x03, 0x00); log_errx(1, "received Login PDU with unsupported \"C\" flag"); } if (bhslr->bhslr_version_max != 0x00) { login_send_error(request, 0x02, 0x05); log_errx(1, "received Login PDU with unsupported " "Version-max 0x%x", bhslr->bhslr_version_max); } if (bhslr->bhslr_version_min != 0x00) { login_send_error(request, 0x02, 0x05); log_errx(1, "received Login PDU with unsupported " "Version-min 0x%x", bhslr->bhslr_version_min); } if (initial == false && ISCSI_SNLT(ntohl(bhslr->bhslr_cmdsn), conn->conn_cmdsn)) { login_send_error(request, 0x02, 0x00); log_errx(1, "received Login PDU with decreasing CmdSN: " "was %u, is %u", conn->conn_cmdsn, ntohl(bhslr->bhslr_cmdsn)); } if (initial == false && ntohl(bhslr->bhslr_expstatsn) != conn->conn_statsn) { login_send_error(request, 0x02, 0x00); log_errx(1, "received Login PDU with wrong ExpStatSN: " "is %u, should be %u", ntohl(bhslr->bhslr_expstatsn), conn->conn_statsn); } conn->conn_cmdsn = ntohl(bhslr->bhslr_cmdsn); return (request); } static struct pdu * login_new_response(struct pdu *request) { struct pdu *response; struct connection *conn; struct iscsi_bhs_login_request *bhslr; struct iscsi_bhs_login_response *bhslr2; bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; conn = request->pdu_connection; response = pdu_new_response(request); bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; bhslr2->bhslr_opcode = ISCSI_BHS_OPCODE_LOGIN_RESPONSE; login_set_csg(response, BHSLR_STAGE_SECURITY_NEGOTIATION); memcpy(bhslr2->bhslr_isid, bhslr->bhslr_isid, sizeof(bhslr2->bhslr_isid)); bhslr2->bhslr_initiator_task_tag = bhslr->bhslr_initiator_task_tag; bhslr2->bhslr_statsn = htonl(conn->conn_statsn++); bhslr2->bhslr_expcmdsn = htonl(conn->conn_cmdsn); bhslr2->bhslr_maxcmdsn = htonl(conn->conn_cmdsn); return (response); } static void login_send_error(struct pdu *request, char class, char detail) { struct pdu *response; struct iscsi_bhs_login_response *bhslr2; log_debugx("sending Login Response PDU with failure class 0x%x/0x%x; " "see next line for reason", class, detail); response = login_new_response(request); bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; bhslr2->bhslr_status_class = class; bhslr2->bhslr_status_detail = detail; pdu_send(response); pdu_delete(response); } static int login_list_contains(const char *list, const char *what) { char *tofree, *str, *token; tofree = str = checked_strdup(list); while ((token = strsep(&str, ",")) != NULL) { if (strcmp(token, what) == 0) { free(tofree); return (1); } } free(tofree); return (0); } static int login_list_prefers(const char *list, const char *choice1, const char *choice2) { char *tofree, *str, *token; tofree = str = checked_strdup(list); while ((token = strsep(&str, ",")) != NULL) { if (strcmp(token, choice1) == 0) { free(tofree); return (1); } if (strcmp(token, choice2) == 0) { free(tofree); return (2); } } free(tofree); return (-1); } static struct pdu * login_receive_chap_a(struct connection *conn) { struct pdu *request; struct keys *request_keys; const char *chap_a; request = login_receive(conn, false); request_keys = keys_new(); keys_load(request_keys, request); chap_a = keys_find(request_keys, "CHAP_A"); if (chap_a == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "received CHAP Login PDU without CHAP_A"); } if (login_list_contains(chap_a, "5") == 0) { login_send_error(request, 0x02, 0x01); log_errx(1, "received CHAP Login PDU with unsupported CHAP_A " "\"%s\"", chap_a); } keys_delete(request_keys); return (request); } static void login_send_chap_c(struct pdu *request, struct chap *chap) { struct pdu *response; struct keys *response_keys; char *chap_c, *chap_i; chap_c = chap_get_challenge(chap); chap_i = chap_get_id(chap); response = login_new_response(request); response_keys = keys_new(); keys_add(response_keys, "CHAP_A", "5"); keys_add(response_keys, "CHAP_I", chap_i); keys_add(response_keys, "CHAP_C", chap_c); free(chap_i); free(chap_c); keys_save(response_keys, response); pdu_send(response); pdu_delete(response); keys_delete(response_keys); } static struct pdu * login_receive_chap_r(struct connection *conn, struct auth_group *ag, struct chap *chap, const struct auth **authp) { struct pdu *request; struct keys *request_keys; const char *chap_n, *chap_r; const struct auth *auth; int error; request = login_receive(conn, false); request_keys = keys_new(); keys_load(request_keys, request); chap_n = keys_find(request_keys, "CHAP_N"); if (chap_n == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "received CHAP Login PDU without CHAP_N"); } chap_r = keys_find(request_keys, "CHAP_R"); if (chap_r == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "received CHAP Login PDU without CHAP_R"); } error = chap_receive(chap, chap_r); if (error != 0) { login_send_error(request, 0x02, 0x07); log_errx(1, "received CHAP Login PDU with malformed CHAP_R"); } /* * Verify the response. */ assert(ag->ag_type == AG_TYPE_CHAP || ag->ag_type == AG_TYPE_CHAP_MUTUAL); auth = auth_find(ag, chap_n); if (auth == NULL) { login_send_error(request, 0x02, 0x01); log_errx(1, "received CHAP Login with invalid user \"%s\"", chap_n); } assert(auth->a_secret != NULL); assert(strlen(auth->a_secret) > 0); error = chap_authenticate(chap, auth->a_secret); if (error != 0) { login_send_error(request, 0x02, 0x01); log_errx(1, "CHAP authentication failed for user \"%s\"", auth->a_user); } keys_delete(request_keys); *authp = auth; return (request); } static void login_send_chap_success(struct pdu *request, const struct auth *auth) { struct pdu *response; struct keys *request_keys, *response_keys; struct rchap *rchap; const char *chap_i, *chap_c; char *chap_r; int error; response = login_new_response(request); login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); /* * Actually, one more thing: mutual authentication. */ request_keys = keys_new(); keys_load(request_keys, request); chap_i = keys_find(request_keys, "CHAP_I"); chap_c = keys_find(request_keys, "CHAP_C"); if (chap_i != NULL || chap_c != NULL) { if (chap_i == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "initiator requested target " "authentication, but didn't send CHAP_I"); } if (chap_c == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "initiator requested target " "authentication, but didn't send CHAP_C"); } if (auth->a_auth_group->ag_type != AG_TYPE_CHAP_MUTUAL) { login_send_error(request, 0x02, 0x01); log_errx(1, "initiator requests target authentication " "for user \"%s\", but mutual user/secret " "is not set", auth->a_user); } log_debugx("performing mutual authentication as user \"%s\"", auth->a_mutual_user); rchap = rchap_new(auth->a_mutual_secret); error = rchap_receive(rchap, chap_i, chap_c); if (error != 0) { login_send_error(request, 0x02, 0x07); log_errx(1, "received CHAP Login PDU with malformed " "CHAP_I or CHAP_C"); } chap_r = rchap_get_response(rchap); rchap_delete(rchap); response_keys = keys_new(); keys_add(response_keys, "CHAP_N", auth->a_mutual_user); keys_add(response_keys, "CHAP_R", chap_r); free(chap_r); keys_save(response_keys, response); keys_delete(response_keys); } else { log_debugx("initiator did not request target authentication"); } keys_delete(request_keys); pdu_send(response); pdu_delete(response); } static void login_chap(struct connection *conn, struct auth_group *ag) { const struct auth *auth; struct chap *chap; struct pdu *request; /* * Receive CHAP_A PDU. */ log_debugx("beginning CHAP authentication; waiting for CHAP_A"); request = login_receive_chap_a(conn); /* * Generate the challenge. */ chap = chap_new(); /* * Send the challenge. */ log_debugx("sending CHAP_C, binary challenge size is %zd bytes", sizeof(chap->chap_challenge)); login_send_chap_c(request, chap); pdu_delete(request); /* * Receive CHAP_N/CHAP_R PDU and authenticate. */ log_debugx("waiting for CHAP_N/CHAP_R"); request = login_receive_chap_r(conn, ag, chap, &auth); /* * Yay, authentication succeeded! */ log_debugx("authentication succeeded for user \"%s\"; " "transitioning to operational parameter negotiation", auth->a_user); login_send_chap_success(request, auth); pdu_delete(request); /* * Leave username and CHAP information for discovery(). */ conn->conn_user = auth->a_user; conn->conn_chap = chap; } static void login_negotiate_key(struct pdu *request, const char *name, const char *value, bool skipped_security, struct keys *response_keys) { int which; size_t tmp; struct connection *conn; conn = request->pdu_connection; if (strcmp(name, "InitiatorName") == 0) { if (!skipped_security) log_errx(1, "initiator resent InitiatorName"); } else if (strcmp(name, "SessionType") == 0) { if (!skipped_security) log_errx(1, "initiator resent SessionType"); } else if (strcmp(name, "TargetName") == 0) { if (!skipped_security) log_errx(1, "initiator resent TargetName"); } else if (strcmp(name, "InitiatorAlias") == 0) { if (conn->conn_initiator_alias != NULL) free(conn->conn_initiator_alias); conn->conn_initiator_alias = checked_strdup(value); } else if (strcmp(value, "Irrelevant") == 0) { /* Ignore. */ } else if (strcmp(name, "HeaderDigest") == 0) { /* * We don't handle digests for discovery sessions. */ if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) { log_debugx("discovery session; digests disabled"); keys_add(response_keys, name, "None"); return; } which = login_list_prefers(value, "CRC32C", "None"); switch (which) { case 1: log_debugx("initiator prefers CRC32C " "for header digest; we'll use it"); conn->conn_header_digest = CONN_DIGEST_CRC32C; keys_add(response_keys, name, "CRC32C"); break; case 2: log_debugx("initiator prefers not to do " "header digest; we'll comply"); keys_add(response_keys, name, "None"); break; default: log_warnx("initiator sent unrecognized " "HeaderDigest value \"%s\"; will use None", value); keys_add(response_keys, name, "None"); break; } } else if (strcmp(name, "DataDigest") == 0) { if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) { log_debugx("discovery session; digests disabled"); keys_add(response_keys, name, "None"); return; } which = login_list_prefers(value, "CRC32C", "None"); switch (which) { case 1: log_debugx("initiator prefers CRC32C " "for data digest; we'll use it"); conn->conn_data_digest = CONN_DIGEST_CRC32C; keys_add(response_keys, name, "CRC32C"); break; case 2: log_debugx("initiator prefers not to do " "data digest; we'll comply"); keys_add(response_keys, name, "None"); break; default: log_warnx("initiator sent unrecognized " "DataDigest value \"%s\"; will use None", value); keys_add(response_keys, name, "None"); break; } } else if (strcmp(name, "MaxConnections") == 0) { keys_add(response_keys, name, "1"); } else if (strcmp(name, "InitialR2T") == 0) { keys_add(response_keys, name, "Yes"); } else if (strcmp(name, "ImmediateData") == 0) { if (conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY) { log_debugx("discovery session; ImmediateData irrelevant"); keys_add(response_keys, name, "Irrelevant"); } else { if (strcmp(value, "Yes") == 0) { conn->conn_immediate_data = true; keys_add(response_keys, name, "Yes"); } else { conn->conn_immediate_data = false; keys_add(response_keys, name, "No"); } } } else if (strcmp(name, "MaxRecvDataSegmentLength") == 0) { tmp = strtoul(value, NULL, 10); if (tmp <= 0) { login_send_error(request, 0x02, 0x00); log_errx(1, "received invalid " "MaxRecvDataSegmentLength"); } /* * MaxRecvDataSegmentLength is a direction-specific parameter. * We'll limit our _send_ to what the initiator can handle but * our MaxRecvDataSegmentLength is not influenced by the * initiator in any way. */ if ((int)tmp > conn->conn_max_send_data_segment_limit) { log_debugx("capping MaxRecvDataSegmentLength " "from %zd to %d", tmp, conn->conn_max_send_data_segment_limit); tmp = conn->conn_max_send_data_segment_limit; } conn->conn_max_send_data_segment_length = tmp; conn->conn_max_recv_data_segment_length = conn->conn_max_recv_data_segment_limit; keys_add_int(response_keys, name, conn->conn_max_recv_data_segment_length); } else if (strcmp(name, "MaxBurstLength") == 0) { tmp = strtoul(value, NULL, 10); if (tmp <= 0) { login_send_error(request, 0x02, 0x00); log_errx(1, "received invalid MaxBurstLength"); } if ((int)tmp > conn->conn_max_burst_limit) { log_debugx("capping MaxBurstLength from %zd to %d", tmp, conn->conn_max_burst_limit); tmp = conn->conn_max_burst_limit; } conn->conn_max_burst_length = tmp; keys_add_int(response_keys, name, tmp); } else if (strcmp(name, "FirstBurstLength") == 0) { tmp = strtoul(value, NULL, 10); if (tmp <= 0) { login_send_error(request, 0x02, 0x00); log_errx(1, "received invalid FirstBurstLength"); } if ((int)tmp > conn->conn_first_burst_limit) { log_debugx("capping FirstBurstLength from %zd to %d", tmp, conn->conn_first_burst_limit); tmp = conn->conn_first_burst_limit; } conn->conn_first_burst_length = tmp; keys_add_int(response_keys, name, tmp); } else if (strcmp(name, "DefaultTime2Wait") == 0) { keys_add(response_keys, name, value); } else if (strcmp(name, "DefaultTime2Retain") == 0) { keys_add(response_keys, name, "0"); } else if (strcmp(name, "MaxOutstandingR2T") == 0) { keys_add(response_keys, name, "1"); } else if (strcmp(name, "DataPDUInOrder") == 0) { keys_add(response_keys, name, "Yes"); } else if (strcmp(name, "DataSequenceInOrder") == 0) { keys_add(response_keys, name, "Yes"); } else if (strcmp(name, "ErrorRecoveryLevel") == 0) { keys_add(response_keys, name, "0"); } else if (strcmp(name, "OFMarker") == 0) { keys_add(response_keys, name, "No"); } else if (strcmp(name, "IFMarker") == 0) { keys_add(response_keys, name, "No"); } else if (strcmp(name, "iSCSIProtocolLevel") == 0) { tmp = strtoul(value, NULL, 10); if (tmp > 2) tmp = 2; keys_add_int(response_keys, name, tmp); } else { log_debugx("unknown key \"%s\"; responding " "with NotUnderstood", name); keys_add(response_keys, name, "NotUnderstood"); } } static void login_redirect(struct pdu *request, const char *target_address) { struct pdu *response; struct iscsi_bhs_login_response *bhslr2; struct keys *response_keys; response = login_new_response(request); login_set_csg(response, login_csg(request)); bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; bhslr2->bhslr_status_class = 0x01; bhslr2->bhslr_status_detail = 0x01; response_keys = keys_new(); keys_add(response_keys, "TargetAddress", target_address); keys_save(response_keys, response); pdu_send(response); pdu_delete(response); keys_delete(response_keys); } static bool login_portal_redirect(struct connection *conn, struct pdu *request) { const struct portal_group *pg; pg = conn->conn_portal->p_portal_group; if (pg->pg_redirection == NULL) return (false); log_debugx("portal-group \"%s\" configured to redirect to %s", pg->pg_name, pg->pg_redirection); login_redirect(request, pg->pg_redirection); return (true); } static bool login_target_redirect(struct connection *conn, struct pdu *request) { const char *target_address; assert(conn->conn_portal->p_portal_group->pg_redirection == NULL); if (conn->conn_target == NULL) return (false); target_address = conn->conn_target->t_redirection; if (target_address == NULL) return (false); log_debugx("target \"%s\" configured to redirect to %s", conn->conn_target->t_name, target_address); login_redirect(request, target_address); return (true); } static void login_negotiate(struct connection *conn, struct pdu *request) { struct pdu *response; struct iscsi_bhs_login_response *bhslr2; struct keys *request_keys, *response_keys; int i; bool redirected, skipped_security; if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { /* * Query the kernel for various size limits. In case of * offload, it depends on hardware capabilities. */ assert(conn->conn_target != NULL); conn->conn_max_recv_data_segment_limit = (1 << 24) - 1; conn->conn_max_send_data_segment_limit = (1 << 24) - 1; conn->conn_max_burst_limit = (1 << 24) - 1; conn->conn_first_burst_limit = (1 << 24) - 1; kernel_limits(conn->conn_portal->p_portal_group->pg_offload, &conn->conn_max_recv_data_segment_limit, &conn->conn_max_send_data_segment_limit, &conn->conn_max_burst_limit, &conn->conn_first_burst_limit); /* We expect legal, usable values at this point. */ assert(conn->conn_max_recv_data_segment_limit >= 512); assert(conn->conn_max_recv_data_segment_limit < (1 << 24)); assert(conn->conn_max_send_data_segment_limit >= 512); assert(conn->conn_max_send_data_segment_limit < (1 << 24)); assert(conn->conn_max_burst_limit >= 512); assert(conn->conn_max_burst_limit < (1 << 24)); assert(conn->conn_first_burst_limit >= 512); assert(conn->conn_first_burst_limit < (1 << 24)); assert(conn->conn_first_burst_limit <= conn->conn_max_burst_limit); /* * Limit default send length in case it won't be negotiated. * We can't do it for other limits, since they may affect both * sender and receiver operation, and we must obey defaults. */ if (conn->conn_max_send_data_segment_limit < conn->conn_max_send_data_segment_length) { conn->conn_max_send_data_segment_length = conn->conn_max_send_data_segment_limit; } } else { conn->conn_max_recv_data_segment_limit = MAX_DATA_SEGMENT_LENGTH; conn->conn_max_send_data_segment_limit = MAX_DATA_SEGMENT_LENGTH; } if (request == NULL) { log_debugx("beginning operational parameter negotiation; " "waiting for Login PDU"); request = login_receive(conn, false); skipped_security = false; } else skipped_security = true; /* * RFC 3720, 10.13.5. Status-Class and Status-Detail, says * the redirection SHOULD be accepted by the initiator before - * authentication, but MUST be be accepted afterwards; that's + * authentication, but MUST be accepted afterwards; that's * why we're doing it here and not earlier. */ redirected = login_target_redirect(conn, request); if (redirected) { log_debugx("initiator redirected; exiting"); exit(0); } request_keys = keys_new(); keys_load(request_keys, request); response = login_new_response(request); bhslr2 = (struct iscsi_bhs_login_response *)response->pdu_bhs; bhslr2->bhslr_tsih = htons(0xbadd); login_set_csg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); login_set_nsg(response, BHSLR_STAGE_FULL_FEATURE_PHASE); response_keys = keys_new(); if (skipped_security && conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { if (conn->conn_target->t_alias != NULL) keys_add(response_keys, "TargetAlias", conn->conn_target->t_alias); keys_add_int(response_keys, "TargetPortalGroupTag", conn->conn_portal->p_portal_group->pg_tag); } for (i = 0; i < KEYS_MAX; i++) { if (request_keys->keys_names[i] == NULL) break; login_negotiate_key(request, request_keys->keys_names[i], request_keys->keys_values[i], skipped_security, response_keys); } /* * We'd started with usable values at our end. But a bad initiator * could have presented a large FirstBurstLength and then a smaller * MaxBurstLength (in that order) and because we process the key/value * pairs in the order they are in the request we might have ended up * with illegal values here. */ if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL && conn->conn_first_burst_length > conn->conn_max_burst_length) { log_errx(1, "initiator sent FirstBurstLength > MaxBurstLength"); } log_debugx("operational parameter negotiation done; " "transitioning to Full Feature Phase"); keys_save(response_keys, response); pdu_send(response); pdu_delete(response); keys_delete(response_keys); pdu_delete(request); keys_delete(request_keys); } static void login_wait_transition(struct connection *conn) { struct pdu *request, *response; struct iscsi_bhs_login_request *bhslr; log_debugx("waiting for state transition request"); request = login_receive(conn, false); bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; if ((bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) == 0) { login_send_error(request, 0x02, 0x00); log_errx(1, "got no \"T\" flag after answering AuthMethod"); } log_debugx("got state transition request"); response = login_new_response(request); pdu_delete(request); login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); pdu_send(response); pdu_delete(response); login_negotiate(conn, NULL); } void login(struct connection *conn) { struct pdu *request, *response; struct iscsi_bhs_login_request *bhslr; struct keys *request_keys, *response_keys; struct auth_group *ag; struct portal_group *pg; const char *initiator_name, *initiator_alias, *session_type, *target_name, *auth_method; bool redirected, fail, trans; /* * Handle the initial Login Request - figure out required authentication * method and either transition to the next phase, if no authentication * is required, or call appropriate authentication code. */ log_debugx("beginning Login Phase; waiting for Login PDU"); request = login_receive(conn, true); bhslr = (struct iscsi_bhs_login_request *)request->pdu_bhs; if (bhslr->bhslr_tsih != 0) { login_send_error(request, 0x02, 0x0a); log_errx(1, "received Login PDU with non-zero TSIH"); } pg = conn->conn_portal->p_portal_group; memcpy(conn->conn_initiator_isid, bhslr->bhslr_isid, sizeof(conn->conn_initiator_isid)); /* * XXX: Implement the C flag some day. */ request_keys = keys_new(); keys_load(request_keys, request); assert(conn->conn_initiator_name == NULL); initiator_name = keys_find(request_keys, "InitiatorName"); if (initiator_name == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "received Login PDU without InitiatorName"); } if (valid_iscsi_name(initiator_name) == false) { login_send_error(request, 0x02, 0x00); log_errx(1, "received Login PDU with invalid InitiatorName"); } conn->conn_initiator_name = checked_strdup(initiator_name); log_set_peer_name(conn->conn_initiator_name); setproctitle("%s (%s)", conn->conn_initiator_addr, conn->conn_initiator_name); redirected = login_portal_redirect(conn, request); if (redirected) { log_debugx("initiator redirected; exiting"); exit(0); } initiator_alias = keys_find(request_keys, "InitiatorAlias"); if (initiator_alias != NULL) conn->conn_initiator_alias = checked_strdup(initiator_alias); assert(conn->conn_session_type == CONN_SESSION_TYPE_NONE); session_type = keys_find(request_keys, "SessionType"); if (session_type != NULL) { if (strcmp(session_type, "Normal") == 0) { conn->conn_session_type = CONN_SESSION_TYPE_NORMAL; } else if (strcmp(session_type, "Discovery") == 0) { conn->conn_session_type = CONN_SESSION_TYPE_DISCOVERY; } else { login_send_error(request, 0x02, 0x00); log_errx(1, "received Login PDU with invalid " "SessionType \"%s\"", session_type); } } else conn->conn_session_type = CONN_SESSION_TYPE_NORMAL; assert(conn->conn_target == NULL); if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { target_name = keys_find(request_keys, "TargetName"); if (target_name == NULL) { login_send_error(request, 0x02, 0x07); log_errx(1, "received Login PDU without TargetName"); } conn->conn_port = port_find_in_pg(pg, target_name); if (conn->conn_port == NULL) { login_send_error(request, 0x02, 0x03); log_errx(1, "requested target \"%s\" not found", target_name); } conn->conn_target = conn->conn_port->p_target; } /* * At this point we know what kind of authentication we need. */ if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { ag = conn->conn_port->p_auth_group; if (ag == NULL) ag = conn->conn_target->t_auth_group; if (ag->ag_name != NULL) { log_debugx("initiator requests to connect " "to target \"%s\"; auth-group \"%s\"", conn->conn_target->t_name, ag->ag_name); } else { log_debugx("initiator requests to connect " "to target \"%s\"", conn->conn_target->t_name); } } else { assert(conn->conn_session_type == CONN_SESSION_TYPE_DISCOVERY); ag = pg->pg_discovery_auth_group; if (ag->ag_name != NULL) { log_debugx("initiator requests " "discovery session; auth-group \"%s\"", ag->ag_name); } else { log_debugx("initiator requests discovery session"); } } if (ag->ag_type == AG_TYPE_DENY) { login_send_error(request, 0x02, 0x01); log_errx(1, "auth-type is \"deny\""); } if (ag->ag_type == AG_TYPE_UNKNOWN) { /* * This can happen with empty auth-group. */ login_send_error(request, 0x02, 0x01); log_errx(1, "auth-type not set, denying access"); } /* * Enforce initiator-name and initiator-portal. */ if (auth_name_check(ag, initiator_name) != 0) { login_send_error(request, 0x02, 0x02); log_errx(1, "initiator does not match allowed initiator names"); } if (auth_portal_check(ag, &conn->conn_initiator_sa) != 0) { login_send_error(request, 0x02, 0x02); log_errx(1, "initiator does not match allowed " "initiator portals"); } /* * Let's see if the initiator intends to do any kind of authentication * at all. */ if (login_csg(request) == BHSLR_STAGE_OPERATIONAL_NEGOTIATION) { if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) { login_send_error(request, 0x02, 0x01); log_errx(1, "initiator skipped the authentication, " "but authentication is required"); } keys_delete(request_keys); log_debugx("initiator skipped the authentication, " "and we don't need it; proceeding with negotiation"); login_negotiate(conn, request); return; } fail = false; response = login_new_response(request); response_keys = keys_new(); trans = (bhslr->bhslr_flags & BHSLR_FLAGS_TRANSIT) != 0; auth_method = keys_find(request_keys, "AuthMethod"); if (ag->ag_type == AG_TYPE_NO_AUTHENTICATION) { log_debugx("authentication not required"); if (auth_method == NULL || login_list_contains(auth_method, "None")) { keys_add(response_keys, "AuthMethod", "None"); } else { log_warnx("initiator requests " "AuthMethod \"%s\" instead of \"None\"", auth_method); keys_add(response_keys, "AuthMethod", "Reject"); } if (trans) login_set_nsg(response, BHSLR_STAGE_OPERATIONAL_NEGOTIATION); } else { log_debugx("CHAP authentication required"); if (auth_method == NULL || login_list_contains(auth_method, "CHAP")) { keys_add(response_keys, "AuthMethod", "CHAP"); } else { log_warnx("initiator requests unsupported " "AuthMethod \"%s\" instead of \"CHAP\"", auth_method); keys_add(response_keys, "AuthMethod", "Reject"); fail = true; } } if (conn->conn_session_type == CONN_SESSION_TYPE_NORMAL) { if (conn->conn_target->t_alias != NULL) keys_add(response_keys, "TargetAlias", conn->conn_target->t_alias); keys_add_int(response_keys, "TargetPortalGroupTag", pg->pg_tag); } keys_save(response_keys, response); pdu_send(response); pdu_delete(response); keys_delete(response_keys); pdu_delete(request); keys_delete(request_keys); if (fail) { log_debugx("sent reject for AuthMethod; exiting"); exit(1); } if (ag->ag_type != AG_TYPE_NO_AUTHENTICATION) { login_chap(conn, ag); login_negotiate(conn, NULL); } else if (trans) { login_negotiate(conn, NULL); } else { login_wait_transition(conn); } } Index: head/usr.sbin/makefs/cd9660.c =================================================================== --- head/usr.sbin/makefs/cd9660.c (revision 327229) +++ head/usr.sbin/makefs/cd9660.c (revision 327230) @@ -1,2131 +1,2131 @@ /* $NetBSD: cd9660.c,v 1.32 2011/08/23 17:09:11 christos Exp $ */ /*- * SPDX-License-Identifier: BSD-2-Clause-NetBSD AND BSD-4-Clause * * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan * Perez-Rathke and Ram Vedam. All rights reserved. * * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys, * Alan Perez-Rathke and Ram Vedam. * * 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. * * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``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 DANIEL WATT, WALTER DEIGNAN, RYAN * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM 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. */ /* * Copyright (c) 2001 Wasabi Systems, Inc. * All rights reserved. * * Written by Luke Mewburn for Wasabi Systems, Inc. * * 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 for the NetBSD Project by * Wasabi Systems, Inc. * 4. The name of Wasabi Systems, Inc. may not be used to endorse * or promote products derived from this software without specific prior * written permission. * * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``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 WASABI SYSTEMS, INC * 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. */ /* * Copyright (c) 1982, 1986, 1989, 1993 * The Regents of the University of California. All rights reserved. * * 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. 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. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include "makefs.h" #include "cd9660.h" #include "cd9660/iso9660_rrip.h" #include "cd9660/cd9660_archimedes.h" static void cd9660_finalize_PVD(iso9660_disk *); static cd9660node *cd9660_allocate_cd9660node(void); static void cd9660_set_defaults(iso9660_disk *); static int cd9660_arguments_set_string(const char *, const char *, int, char, char *); static void cd9660_populate_iso_dir_record( struct _iso_directory_record_cd9660 *, u_char, u_char, u_char, const char *); static void cd9660_setup_root_node(iso9660_disk *); static int cd9660_setup_volume_descriptors(iso9660_disk *); #if 0 static int cd9660_fill_extended_attribute_record(cd9660node *); #endif static void cd9660_sort_nodes(cd9660node *); static int cd9660_translate_node_common(iso9660_disk *, cd9660node *); static int cd9660_translate_node(iso9660_disk *, fsnode *, cd9660node *); static int cd9660_compare_filename(const char *, const char *); static void cd9660_sorted_child_insert(cd9660node *, cd9660node *); static int cd9660_handle_collisions(iso9660_disk *, cd9660node *, int); static cd9660node *cd9660_rename_filename(iso9660_disk *, cd9660node *, int, int); static void cd9660_copy_filenames(iso9660_disk *, cd9660node *); static void cd9660_sorting_nodes(cd9660node *); static int cd9660_count_collisions(cd9660node *); static cd9660node *cd9660_rrip_move_directory(iso9660_disk *, cd9660node *); static int cd9660_add_dot_records(iso9660_disk *, cd9660node *); static void cd9660_convert_structure(iso9660_disk *, fsnode *, cd9660node *, int, int *, int *); static void cd9660_free_structure(cd9660node *); static int cd9660_generate_path_table(iso9660_disk *); static int cd9660_level1_convert_filename(iso9660_disk *, const char *, char *, int); static int cd9660_level2_convert_filename(iso9660_disk *, const char *, char *, int); #if 0 static int cd9660_joliet_convert_filename(iso9660_disk *, const char *, char *, int); #endif static int cd9660_convert_filename(iso9660_disk *, const char *, char *, int); static void cd9660_populate_dot_records(iso9660_disk *, cd9660node *); static int64_t cd9660_compute_offsets(iso9660_disk *, cd9660node *, int64_t); #if 0 static int cd9660_copy_stat_info(cd9660node *, cd9660node *, int); #endif static cd9660node *cd9660_create_virtual_entry(iso9660_disk *, const char *, cd9660node *, int, int); static cd9660node *cd9660_create_file(iso9660_disk *, const char *, cd9660node *, cd9660node *); static cd9660node *cd9660_create_directory(iso9660_disk *, const char *, cd9660node *, cd9660node *); static cd9660node *cd9660_create_special_directory(iso9660_disk *, u_char, cd9660node *); static int cd9660_add_generic_bootimage(iso9660_disk *, const char *); /* * Allocate and initialize a cd9660node * @returns struct cd9660node * Pointer to new node, or NULL on error */ static cd9660node * cd9660_allocate_cd9660node(void) { cd9660node *temp = ecalloc(1, sizeof(*temp)); TAILQ_INIT(&temp->cn_children); temp->parent = temp->dot_record = temp->dot_dot_record = NULL; temp->ptnext = temp->ptprev = temp->ptlast = NULL; temp->node = NULL; temp->isoDirRecord = NULL; temp->isoExtAttributes = NULL; temp->rr_real_parent = temp->rr_relocated = NULL; temp->su_tail_data = NULL; return temp; } /** * Set default values for cd9660 extension to makefs */ static void cd9660_set_defaults(iso9660_disk *diskStructure) { /*Fix the sector size for now, though the spec allows for other sizes*/ diskStructure->sectorSize = 2048; /* Set up defaults in our own structure */ diskStructure->verbose_level = 0; diskStructure->keep_bad_images = 0; diskStructure->follow_sym_links = 0; diskStructure->isoLevel = 2; diskStructure->rock_ridge_enabled = 0; diskStructure->rock_ridge_renamed_dir_name = 0; diskStructure->rock_ridge_move_count = 0; diskStructure->rr_moved_dir = 0; diskStructure->archimedes_enabled = 0; diskStructure->chrp_boot = 0; diskStructure->include_padding_areas = 1; /* Spec breaking functionality */ diskStructure->allow_deep_trees = diskStructure->allow_start_dot = diskStructure->allow_max_name = diskStructure->allow_illegal_chars = diskStructure->allow_lowercase = diskStructure->allow_multidot = diskStructure->omit_trailing_period = 0; /* Make sure the PVD is clear */ memset(&diskStructure->primaryDescriptor, 0, 2048); memset(diskStructure->primaryDescriptor.publisher_id, 0x20,128); memset(diskStructure->primaryDescriptor.preparer_id, 0x20,128); memset(diskStructure->primaryDescriptor.application_id, 0x20,128); memset(diskStructure->primaryDescriptor.copyright_file_id, 0x20,37); memset(diskStructure->primaryDescriptor.abstract_file_id, 0x20,37); memset(diskStructure->primaryDescriptor.bibliographic_file_id, 0x20,37); strcpy(diskStructure->primaryDescriptor.system_id, "FreeBSD"); /* Boot support: Initially disabled */ diskStructure->has_generic_bootimage = 0; diskStructure->generic_bootimage = NULL; diskStructure->boot_image_directory = 0; /*memset(diskStructure->boot_descriptor, 0, 2048);*/ diskStructure->is_bootable = 0; TAILQ_INIT(&diskStructure->boot_images); LIST_INIT(&diskStructure->boot_entries); } void cd9660_prep_opts(fsinfo_t *fsopts) { iso9660_disk *diskStructure = ecalloc(1, sizeof(*diskStructure)); #define OPT_STR(letter, name, desc) \ { letter, name, NULL, OPT_STRBUF, 0, 0, desc } #define OPT_NUM(letter, name, field, min, max, desc) \ { letter, name, &diskStructure->field, \ sizeof(diskStructure->field) == 8 ? OPT_INT64 : \ (sizeof(diskStructure->field) == 4 ? OPT_INT32 : \ (sizeof(diskStructure->field) == 2 ? OPT_INT16 : OPT_INT8)), \ min, max, desc } #define OPT_BOOL(letter, name, field, desc) \ OPT_NUM(letter, name, field, 0, 1, desc) const option_t cd9660_options[] = { OPT_NUM('l', "isolevel", isoLevel, 1, 2, "ISO Level"), OPT_NUM('v', "verbose", verbose_level, 0, 2, "Turns on verbose output"), OPT_BOOL('h', "help", displayHelp, "Show help message"), OPT_BOOL('S', "follow-symlinks", follow_sym_links, "Resolve symlinks in pathnames"), OPT_BOOL('R', "rockridge", rock_ridge_enabled, "Enable Rock-Ridge extensions"), OPT_BOOL('C', "chrp-boot", chrp_boot, "Enable CHRP boot"), OPT_BOOL('K', "keep-bad-images", keep_bad_images, "Keep bad images"), OPT_BOOL('D', "allow-deep-trees", allow_deep_trees, "Allow trees more than 8 levels"), OPT_BOOL('a', "allow-max-name", allow_max_name, "Allow 37 char filenames (unimplemented)"), OPT_BOOL('i', "allow-illegal-chars", allow_illegal_chars, "Allow illegal characters in filenames"), OPT_BOOL('m', "allow-multidot", allow_multidot, "Allow multiple periods in filenames"), OPT_BOOL('o', "omit-trailing-period", omit_trailing_period, "Omit trailing periods in filenames"), OPT_BOOL('\0', "allow-lowercase", allow_lowercase, "Allow lowercase characters in filenames"), OPT_BOOL('\0', "archimedes", archimedes_enabled, "Enable Archimedes structure"), OPT_BOOL('\0', "no-trailing-padding", include_padding_areas, "Include padding areas"), OPT_STR('A', "applicationid", "Application Identifier"), OPT_STR('P', "publisher", "Publisher Identifier"), OPT_STR('p', "preparer", "Preparer Identifier"), OPT_STR('L', "label", "Disk Label"), OPT_STR('V', "volumeid", "Volume Set Identifier"), OPT_STR('B', "bootimage", "Boot image parameter"), OPT_STR('G', "generic-bootimage", "Generic boot image param"), OPT_STR('\0', "bootimagedir", "Boot image directory"), OPT_STR('\0', "no-emul-boot", "No boot emulation"), OPT_STR('\0', "no-boot", "No boot support"), OPT_STR('\0', "hard-disk-boot", "Boot from hard disk"), OPT_STR('\0', "boot-load-segment", "Boot load segment"), { .name = NULL } }; fsopts->fs_specific = diskStructure; fsopts->fs_options = copy_opts(cd9660_options); cd9660_set_defaults(diskStructure); } void cd9660_cleanup_opts(fsinfo_t *fsopts) { free(fsopts->fs_specific); free(fsopts->fs_options); } static int cd9660_arguments_set_string(const char *val, const char *fieldtitle, int length, char testmode, char * dest) { int len, test; if (val == NULL) warnx("error: The %s requires a string argument", fieldtitle); else if ((len = strlen(val)) <= length) { if (testmode == 'd') test = cd9660_valid_d_chars(val); else test = cd9660_valid_a_chars(val); if (test) { memcpy(dest, val, len); if (test == 2) cd9660_uppercase_characters(dest, len); return 1; } else warnx("error: The %s must be composed of " "%c-characters", fieldtitle, testmode); } else warnx("error: The %s must be at most 32 characters long", fieldtitle); return 0; } /* * Command-line parsing function */ int cd9660_parse_opts(const char *option, fsinfo_t *fsopts) { int rv, i; iso9660_disk *diskStructure = fsopts->fs_specific; option_t *cd9660_options = fsopts->fs_options; char buf[1024]; const char *name, *desc; assert(option != NULL); if (debug & DEBUG_FS_PARSE_OPTS) printf("%s: got `%s'\n", __func__, option); i = set_option(cd9660_options, option, buf, sizeof(buf)); if (i == -1) return 0; if (cd9660_options[i].name == NULL) abort(); name = cd9660_options[i].name; desc = cd9660_options[i].desc; switch (cd9660_options[i].letter) { case 'h': case 'S': rv = 0; /* this is not handled yet */ break; case 'L': rv = cd9660_arguments_set_string(buf, desc, 32, 'd', diskStructure->primaryDescriptor.volume_id); break; case 'A': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.application_id); break; case 'P': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.publisher_id); break; case 'p': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.preparer_id); break; case 'V': rv = cd9660_arguments_set_string(buf, desc, 128, 'a', diskStructure->primaryDescriptor.volume_set_id); break; /* Boot options */ case 'B': if (buf[0] == '\0') { warnx("The Boot Image parameter requires a valid boot" "information string"); rv = 0; } else rv = cd9660_add_boot_disk(diskStructure, buf); break; case 'G': if (buf[0] == '\0') { warnx("The Generic Boot Image parameter requires a" " valid boot information string"); rv = 0; } else rv = cd9660_add_generic_bootimage(diskStructure, buf); break; default: if (strcmp(name, "bootimagedir") == 0) { /* * XXXfvdl this is unused. */ if (buf[0] == '\0') { warnx("The Boot Image Directory parameter" " requires a directory name"); rv = 0; } else { diskStructure->boot_image_directory = emalloc(strlen(buf) + 1); /* BIG TODO: Add the max length function here */ rv = cd9660_arguments_set_string(buf, desc, 12, 'd', diskStructure->boot_image_directory); } } else if (strcmp(name, "no-emul-boot") == 0 || strcmp(name, "no-boot") == 0 || strcmp(name, "hard-disk-boot") == 0) { /* RRIP */ cd9660_eltorito_add_boot_option(diskStructure, name, 0); rv = 1; } else if (strcmp(name, "boot-load-segment") == 0) { if (buf[0] == '\0') { warnx("Option `%s' doesn't contain a value", name); rv = 0; } else { cd9660_eltorito_add_boot_option(diskStructure, name, buf); rv = 1; } } else rv = 1; } return rv; } /* * Main function for cd9660_makefs * Builds the ISO image file * @param const char *image The image filename to create * @param const char *dir The directory that is being read * @param struct fsnode *root The root node of the filesystem tree * @param struct fsinfo_t *fsopts Any options */ void cd9660_makefs(const char *image, const char *dir, fsnode *root, fsinfo_t *fsopts) { int64_t startoffset; int numDirectories; uint64_t pathTableSectors; int64_t firstAvailableSector; int64_t totalSpace; int error; cd9660node *real_root; iso9660_disk *diskStructure = fsopts->fs_specific; if (diskStructure->verbose_level > 0) printf("%s: ISO level is %i\n", __func__, diskStructure->isoLevel); if (diskStructure->isoLevel < 2 && diskStructure->allow_multidot) errx(EXIT_FAILURE, "allow-multidot requires iso level of 2"); assert(image != NULL); assert(dir != NULL); assert(root != NULL); if (diskStructure->displayHelp) { /* * Display help here - probably want to put it in * a separate function */ return; } if (diskStructure->verbose_level > 0) printf("%s: image %s directory %s root %p\n", __func__, image, dir, root); /* Set up some constants. Later, these will be defined with options */ /* Counter needed for path tables */ numDirectories = 0; /* Convert tree to our own format */ /* Actually, we now need to add the REAL root node, at level 0 */ real_root = cd9660_allocate_cd9660node(); real_root->isoDirRecord = emalloc(sizeof(*real_root->isoDirRecord)); /* Leave filename blank for root */ memset(real_root->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); real_root->level = 0; diskStructure->rootNode = real_root; real_root->type = CD9660_TYPE_DIR; error = 0; real_root->node = root; cd9660_convert_structure(diskStructure, root, real_root, 1, &numDirectories, &error); if (TAILQ_EMPTY(&real_root->cn_children)) { errx(EXIT_FAILURE, "%s: converted directory is empty. " "Tree conversion failed", __func__); } else if (error != 0) { errx(EXIT_FAILURE, "%s: tree conversion failed", __func__); } else { if (diskStructure->verbose_level > 0) printf("%s: tree converted\n", __func__); } /* Add the dot and dot dot records */ cd9660_add_dot_records(diskStructure, real_root); cd9660_setup_root_node(diskStructure); if (diskStructure->verbose_level > 0) printf("%s: done converting tree\n", __func__); /* non-SUSP extensions */ if (diskStructure->archimedes_enabled) archimedes_convert_tree(diskStructure->rootNode); /* Rock ridge / SUSP init pass */ if (diskStructure->rock_ridge_enabled) { cd9660_susp_initialize(diskStructure, diskStructure->rootNode, diskStructure->rootNode, NULL); } /* Build path table structure */ diskStructure->pathTableLength = cd9660_generate_path_table( diskStructure); pathTableSectors = CD9660_BLOCKS(diskStructure->sectorSize, diskStructure->pathTableLength); firstAvailableSector = cd9660_setup_volume_descriptors(diskStructure); if (diskStructure->is_bootable) { firstAvailableSector = cd9660_setup_boot(diskStructure, firstAvailableSector); if (firstAvailableSector < 0) errx(EXIT_FAILURE, "setup_boot failed"); } /* LE first, then BE */ diskStructure->primaryLittleEndianTableSector = firstAvailableSector; diskStructure->primaryBigEndianTableSector = diskStructure->primaryLittleEndianTableSector + pathTableSectors; /* Set the secondary ones to -1, not going to use them for now */ diskStructure->secondaryBigEndianTableSector = -1; diskStructure->secondaryLittleEndianTableSector = -1; diskStructure->dataFirstSector = diskStructure->primaryBigEndianTableSector + pathTableSectors; if (diskStructure->verbose_level > 0) printf("%s: Path table conversion complete. " "Each table is %i bytes, or %" PRIu64 " sectors.\n", __func__, diskStructure->pathTableLength, pathTableSectors); startoffset = diskStructure->sectorSize*diskStructure->dataFirstSector; totalSpace = cd9660_compute_offsets(diskStructure, real_root, startoffset); diskStructure->totalSectors = diskStructure->dataFirstSector + CD9660_BLOCKS(diskStructure->sectorSize, totalSpace); /* Disabled until pass 1 is done */ if (diskStructure->rock_ridge_enabled) { diskStructure->susp_continuation_area_start_sector = diskStructure->totalSectors; diskStructure->totalSectors += CD9660_BLOCKS(diskStructure->sectorSize, diskStructure->susp_continuation_area_size); cd9660_susp_finalize(diskStructure, diskStructure->rootNode); } cd9660_finalize_PVD(diskStructure); /* Add padding sectors, just for testing purposes right now */ /* diskStructure->totalSectors+=150; */ /* Debugging output */ if (diskStructure->verbose_level > 0) { printf("%s: Sectors 0-15 reserved\n", __func__); printf("%s: Primary path tables starts in sector %" PRId64 "\n", __func__, diskStructure->primaryLittleEndianTableSector); printf("%s: File data starts in sector %" PRId64 "\n", __func__, diskStructure->dataFirstSector); printf("%s: Total sectors: %" PRId64 "\n", __func__, diskStructure->totalSectors); } /* * Add padding sectors at the end * TODO: Clean this up and separate padding */ if (diskStructure->include_padding_areas) diskStructure->totalSectors += 150; cd9660_write_image(diskStructure, image); if (diskStructure->verbose_level > 1) { debug_print_volume_descriptor_information(diskStructure); debug_print_tree(diskStructure, real_root, 0); debug_print_path_tree(real_root); } /* Clean up data structures */ cd9660_free_structure(real_root); if (diskStructure->verbose_level > 0) printf("%s: done\n", __func__); } /* Generic function pointer - implement later */ typedef int (*cd9660node_func)(cd9660node *); static void cd9660_finalize_PVD(iso9660_disk *diskStructure) { time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL); /* root should be a fixed size of 34 bytes since it has no name */ memcpy(diskStructure->primaryDescriptor.root_directory_record, diskStructure->rootNode->dot_record->isoDirRecord, 34); /* In RRIP, this might be longer than 34 */ diskStructure->primaryDescriptor.root_directory_record[0] = 34; /* Set up all the important numbers in the PVD */ cd9660_bothendian_dword(diskStructure->totalSectors, (unsigned char *)diskStructure->primaryDescriptor.volume_space_size); cd9660_bothendian_word(1, (unsigned char *)diskStructure->primaryDescriptor.volume_set_size); cd9660_bothendian_word(1, (unsigned char *) diskStructure->primaryDescriptor.volume_sequence_number); cd9660_bothendian_word(diskStructure->sectorSize, (unsigned char *) diskStructure->primaryDescriptor.logical_block_size); cd9660_bothendian_dword(diskStructure->pathTableLength, (unsigned char *)diskStructure->primaryDescriptor.path_table_size); cd9660_731(diskStructure->primaryLittleEndianTableSector, (u_char *)diskStructure->primaryDescriptor.type_l_path_table); cd9660_732(diskStructure->primaryBigEndianTableSector, (u_char *)diskStructure->primaryDescriptor.type_m_path_table); diskStructure->primaryDescriptor.file_structure_version[0] = 1; /* Pad all strings with spaces instead of nulls */ cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_id, 32); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.system_id, 32); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.volume_set_id, 128); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.publisher_id, 128); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.preparer_id, 128); cd9660_pad_string_spaces(diskStructure->primaryDescriptor.application_id, 128); cd9660_pad_string_spaces( diskStructure->primaryDescriptor.copyright_file_id, 37); cd9660_pad_string_spaces( diskStructure->primaryDescriptor.abstract_file_id, 37); cd9660_pad_string_spaces( diskStructure->primaryDescriptor.bibliographic_file_id, 37); /* Setup dates */ cd9660_time_8426( (unsigned char *)diskStructure->primaryDescriptor.creation_date, tstamp); cd9660_time_8426( (unsigned char *)diskStructure->primaryDescriptor.modification_date, tstamp); #if 0 cd9660_set_date(diskStructure->primaryDescriptor.expiration_date, tstamp); #endif memset(diskStructure->primaryDescriptor.expiration_date, '0', 16); diskStructure->primaryDescriptor.expiration_date[16] = 0; cd9660_time_8426( (unsigned char *)diskStructure->primaryDescriptor.effective_date, tstamp); /* make this sane */ cd9660_time_915(diskStructure->rootNode->dot_record->isoDirRecord->date, tstamp); } static void cd9660_populate_iso_dir_record(struct _iso_directory_record_cd9660 *record, u_char ext_attr_length, u_char flags, u_char name_len, const char * name) { record->ext_attr_length[0] = ext_attr_length; record->flags[0] = ISO_FLAG_CLEAR | flags; record->file_unit_size[0] = 0; record->interleave[0] = 0; cd9660_bothendian_word(1, record->volume_sequence_number); record->name_len[0] = name_len; memset(record->name, '\0', sizeof (record->name)); memcpy(record->name, name, name_len); record->length[0] = 33 + name_len; /* Todo : better rounding */ record->length[0] += (record->length[0] & 1) ? 1 : 0; } static void cd9660_setup_root_node(iso9660_disk *diskStructure) { cd9660_populate_iso_dir_record(diskStructure->rootNode->isoDirRecord, 0, ISO_FLAG_DIRECTORY, 1, "\0"); } /*********** SUPPORT FUNCTIONS ***********/ static int cd9660_setup_volume_descriptors(iso9660_disk *diskStructure) { /* Boot volume descriptor should come second */ int sector = 16; /* For now, a fixed 2 : PVD and terminator */ volume_descriptor *temp, *t; /* Set up the PVD */ temp = emalloc(sizeof(*temp)); temp->volumeDescriptorData = (unsigned char *)&diskStructure->primaryDescriptor; temp->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_PVD; temp->volumeDescriptorData[6] = 1; temp->sector = sector; memcpy(temp->volumeDescriptorData + 1, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); diskStructure->firstVolumeDescriptor = temp; sector++; /* Set up boot support if enabled. BVD must reside in sector 17 */ if (diskStructure->is_bootable) { t = emalloc(sizeof(*t)); t->volumeDescriptorData = ecalloc(1, 2048); temp->next = t; temp = t; t->sector = 17; if (diskStructure->verbose_level > 0) printf("Setting up boot volume descriptor\n"); cd9660_setup_boot_volume_descriptor(diskStructure, t); sector++; } /* Set up the terminator */ t = emalloc(sizeof(*t)); t->volumeDescriptorData = ecalloc(1, 2048); temp->next = t; t->volumeDescriptorData[0] = ISO_VOLUME_DESCRIPTOR_TERMINATOR; t->next = NULL; t->volumeDescriptorData[6] = 1; t->sector = sector; memcpy(t->volumeDescriptorData + 1, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5); sector++; return sector; } #if 0 /* * Populate EAR at some point. Not required, but is used by NetBSD's * cd9660 support */ static int cd9660_fill_extended_attribute_record(cd9660node *node) { node->isoExtAttributes = emalloc(sizeof(*node->isoExtAttributes)); return 1; } #endif static int cd9660_translate_node_common(iso9660_disk *diskStructure, cd9660node *newnode) { time_t tstamp = stampst.st_ino ? stampst.st_mtime : time(NULL); u_char flag; char temp[ISO_FILENAME_MAXLENGTH_WITH_PADDING]; /* Now populate the isoDirRecord structure */ memset(temp, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); (void)cd9660_convert_filename(diskStructure, newnode->node->name, temp, !(S_ISDIR(newnode->node->type))); flag = ISO_FLAG_CLEAR; if (S_ISDIR(newnode->node->type)) flag |= ISO_FLAG_DIRECTORY; cd9660_populate_iso_dir_record(newnode->isoDirRecord, 0, flag, strlen(temp), temp); /* Set the various dates */ /* If we want to use the current date and time */ cd9660_time_915(newnode->isoDirRecord->date, tstamp); cd9660_bothendian_dword(newnode->fileDataLength, newnode->isoDirRecord->size); /* If the file is a link, we want to set the size to 0 */ if (S_ISLNK(newnode->node->type)) newnode->fileDataLength = 0; return 1; } /* * Translate fsnode to cd9660node * Translate filenames and other metadata, including dates, sizes, * permissions, etc * @param struct fsnode * The node generated by makefs * @param struct cd9660node * The intermediate node to be written to * @returns int 0 on failure, 1 on success */ static int cd9660_translate_node(iso9660_disk *diskStructure, fsnode *node, cd9660node *newnode) { if (node == NULL) { if (diskStructure->verbose_level > 0) printf("%s: NULL node passed, returning\n", __func__); return 0; } newnode->isoDirRecord = emalloc(sizeof(*newnode->isoDirRecord)); /* Set the node pointer */ newnode->node = node; /* Set the size */ if (!(S_ISDIR(node->type))) newnode->fileDataLength = node->inode->st.st_size; if (cd9660_translate_node_common(diskStructure, newnode) == 0) return 0; /* Finally, overwrite some of the values that are set by default */ cd9660_time_915(newnode->isoDirRecord->date, stampst.st_ino ? stampst.st_mtime : node->inode->st.st_mtime); return 1; } /* * Compares two ISO filenames * @param const char * The first file name * @param const char * The second file name * @returns : -1 if first is less than second, 0 if they are the same, 1 if * the second is greater than the first */ static int cd9660_compare_filename(const char *first, const char *second) { /* * This can be made more optimal once it has been tested * (the extra character, for example, is for testing) */ int p1 = 0; int p2 = 0; char c1, c2; /* First, on the filename */ while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION-1) { c1 = first[p1]; c2 = second[p2]; if (c1 == '.' && c2 =='.') break; else if (c1 == '.') { p2++; c1 = ' '; } else if (c2 == '.') { p1++; c2 = ' '; } else { p1++; p2++; } if (c1 < c2) return -1; else if (c1 > c2) { return 1; } } if (first[p1] == '.' && second[p2] == '.') { p1++; p2++; while (p1 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1 && p2 < ISO_FILENAME_MAXLENGTH_BEFORE_VERSION - 1) { c1 = first[p1]; c2 = second[p2]; if (c1 == ';' && c2 == ';') break; else if (c1 == ';') { p2++; c1 = ' '; } else if (c2 == ';') { p1++; c2 = ' '; } else { p1++; p2++; } if (c1 < c2) return -1; else if (c1 > c2) return 1; } } return 0; } /* * Insert a node into list with ISO sorting rules * @param cd9660node * The head node of the list * @param cd9660node * The node to be inserted */ static void cd9660_sorted_child_insert(cd9660node *parent, cd9660node *cn_new) { int compare; cd9660node *cn; struct cd9660_children_head *head = &parent->cn_children; /* TODO: Optimize? */ cn_new->parent = parent; /* * first will either be 0, the . or the .. * if . or .., this means no other entry may be written before first * if 0, the new node may be inserted at the head */ TAILQ_FOREACH(cn, head, cn_next_child) { /* * Dont insert a node twice - * that would cause an infinite loop */ if (cn_new == cn) return; compare = cd9660_compare_filename(cn_new->isoDirRecord->name, cn->isoDirRecord->name); if (compare == 0) compare = cd9660_compare_filename(cn_new->node->name, cn->node->name); if (compare < 0) break; } if (cn == NULL) TAILQ_INSERT_TAIL(head, cn_new, cn_next_child); else TAILQ_INSERT_BEFORE(cn, cn_new, cn_next_child); } /* * Called After cd9660_sorted_child_insert * handles file collisions by suffixing each filname with ~n * where n represents the files respective place in the ordering */ static int cd9660_handle_collisions(iso9660_disk *diskStructure, cd9660node *colliding, int past) { cd9660node *iter, *next, *prev; int skip; int delete_chars = 0; int temp_past = past; int temp_skip; int flag = 0; cd9660node *end_of_range; for (iter = TAILQ_FIRST(&colliding->cn_children); iter != NULL && (next = TAILQ_NEXT(iter, cn_next_child)) != NULL;) { if (strcmp(iter->isoDirRecord->name, next->isoDirRecord->name) != 0) { iter = TAILQ_NEXT(iter, cn_next_child); continue; } flag = 1; temp_skip = skip = cd9660_count_collisions(iter); end_of_range = iter; while (temp_skip > 0) { temp_skip--; end_of_range = TAILQ_NEXT(end_of_range, cn_next_child); } temp_past = past; while (temp_past > 0) { if ((next = TAILQ_NEXT(end_of_range, cn_next_child)) != NULL) end_of_range = next; else if ((prev = TAILQ_PREV(iter, cd9660_children_head, cn_next_child)) != NULL) iter = prev; else delete_chars++; temp_past--; } skip += past; iter = cd9660_rename_filename(diskStructure, iter, skip, delete_chars); } return flag; } static cd9660node * cd9660_rename_filename(iso9660_disk *diskStructure, cd9660node *iter, int num, int delete_chars) { int i = 0; int numbts, digit, digits, temp, powers, count; char *naming; int maxlength; char *tmp; if (diskStructure->verbose_level > 0) printf("Rename_filename called\n"); assert(1 <= diskStructure->isoLevel && diskStructure->isoLevel <= 2); /* TODO : A LOT of chanes regarding 8.3 filenames */ if (diskStructure->isoLevel == 1) maxlength = 8; else if (diskStructure->isoLevel == 2) maxlength = 31; else maxlength = ISO_FILENAME_MAXLENGTH_BEFORE_VERSION; tmp = emalloc(ISO_FILENAME_MAXLENGTH_WITH_PADDING); while (i < num && iter) { powers = 1; count = 0; digits = 1; while (((int)(i / powers) ) >= 10) { digits++; powers = powers * 10; } naming = iter->o_name; /* while ((*naming != '.') && (*naming != ';')) { naming++; count++; } */ while (count < maxlength) { if (*naming == ';') break; naming++; count++; } if ((count + digits) < maxlength) numbts = count; else numbts = maxlength - (digits); numbts -= delete_chars; /* 8.3 rules - keep the extension, add before the dot */ /* * This code makes a bunch of assumptions. * See if you can spot them all :) */ #if 0 if (diskStructure->isoLevel == 1) { numbts = 8 - digits - delete_chars; if (dot < 0) { } else { if (dot < 8) { memmove(&tmp[numbts],&tmp[dot],4); } } } #endif /* (copying just the filename before the '.' */ memcpy(tmp, (iter->o_name), numbts); /* adding the appropriate number following the name */ temp = i; while (digits > 0) { digit = (int)(temp / powers); temp = temp - digit * powers; sprintf(&tmp[numbts] , "%d", digit); digits--; numbts++; powers = powers / 10; } while ((*naming != ';') && (numbts < maxlength)) { tmp[numbts] = (*naming); naming++; numbts++; } tmp[numbts] = ';'; tmp[numbts+1] = '1'; tmp[numbts+2] = '\0'; /* * now tmp has exactly the identifier * we want so we'll copy it back to record */ memcpy((iter->isoDirRecord->name), tmp, numbts + 3); iter = TAILQ_NEXT(iter, cn_next_child); i++; } free(tmp); return iter; } /* Todo: Figure out why these functions are nec. */ static void cd9660_copy_filenames(iso9660_disk *diskStructure, cd9660node *node) { cd9660node *cn; if (TAILQ_EMPTY(&node->cn_children)) return; if (TAILQ_FIRST(&node->cn_children)->isoDirRecord == NULL) { debug_print_tree(diskStructure, diskStructure->rootNode, 0); exit(1); } TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { cd9660_copy_filenames(diskStructure, cn); memcpy(cn->o_name, cn->isoDirRecord->name, ISO_FILENAME_MAXLENGTH_WITH_PADDING); } } static void cd9660_sorting_nodes(cd9660node *node) { cd9660node *cn; TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) cd9660_sorting_nodes(cn); cd9660_sort_nodes(node); } /* XXX Bubble sort. */ static void cd9660_sort_nodes(cd9660node *node) { cd9660node *cn, *next; do { TAILQ_FOREACH(cn, &node->cn_children, cn_next_child) { if ((next = TAILQ_NEXT(cn, cn_next_child)) == NULL) return; else if (strcmp(next->isoDirRecord->name, cn->isoDirRecord->name) >= 0) continue; TAILQ_REMOVE(&node->cn_children, next, cn_next_child); TAILQ_INSERT_BEFORE(cn, next, cn_next_child); break; } } while (cn != NULL); } static int cd9660_count_collisions(cd9660node *copy) { int count = 0; cd9660node *iter, *next; for (iter = copy; (next = TAILQ_NEXT(iter, cn_next_child)) != NULL; iter = next) { if (cd9660_compare_filename(iter->isoDirRecord->name, next->isoDirRecord->name) == 0) count++; else return count; } #if 0 if ((next = TAILQ_NEXT(iter, cn_next_child)) != NULL) { printf("%s: count is %i\n", __func__, count); compare = cd9660_compare_filename(iter->isoDirRecord->name, next->isoDirRecord->name); if (compare == 0) { count++; return cd9660_recurse_on_collision(next, count); } else return count; } #endif return count; } static cd9660node * cd9660_rrip_move_directory(iso9660_disk *diskStructure, cd9660node *dir) { char newname[9]; cd9660node *tfile; /* * This function needs to: * 1) Create an empty virtual file in place of the old directory * 2) Point the virtual file to the new directory * 3) Point the relocated directory to its old parent * 4) Move the directory specified by dir into rr_moved_dir, * and rename it to "diskStructure->rock_ridge_move_count" (as a string) */ /* First see if the moved directory even exists */ if (diskStructure->rr_moved_dir == NULL) { diskStructure->rr_moved_dir = cd9660_create_directory( diskStructure, ISO_RRIP_DEFAULT_MOVE_DIR_NAME, diskStructure->rootNode, dir); if (diskStructure->rr_moved_dir == NULL) return 0; cd9660_time_915(diskStructure->rr_moved_dir->isoDirRecord->date, stampst.st_ino ? stampst.st_mtime : start_time.tv_sec); } /* Create a file with the same ORIGINAL name */ tfile = cd9660_create_file(diskStructure, dir->node->name, dir->parent, dir); if (tfile == NULL) return NULL; diskStructure->rock_ridge_move_count++; snprintf(newname, sizeof(newname), "%08i", diskStructure->rock_ridge_move_count); /* Point to old parent */ dir->rr_real_parent = dir->parent; /* Place the placeholder file */ if (TAILQ_EMPTY(&dir->rr_real_parent->cn_children)) { TAILQ_INSERT_HEAD(&dir->rr_real_parent->cn_children, tfile, cn_next_child); } else { cd9660_sorted_child_insert(dir->rr_real_parent, tfile); } /* Point to new parent */ dir->parent = diskStructure->rr_moved_dir; /* Point the file to the moved directory */ tfile->rr_relocated = dir; /* Actually move the directory */ cd9660_sorted_child_insert(diskStructure->rr_moved_dir, dir); /* TODO: Inherit permissions / ownership (basically the entire inode) */ /* Set the new name */ memset(dir->isoDirRecord->name, 0, ISO_FILENAME_MAXLENGTH_WITH_PADDING); strncpy(dir->isoDirRecord->name, newname, 8); dir->isoDirRecord->length[0] = 34 + 8; dir->isoDirRecord->name_len[0] = 8; return dir; } static int cd9660_add_dot_records(iso9660_disk *diskStructure, cd9660node *root) { struct cd9660_children_head *head = &root->cn_children; cd9660node *cn; TAILQ_FOREACH(cn, head, cn_next_child) { if ((cn->type & CD9660_TYPE_DIR) == 0) continue; /* Recursion first */ cd9660_add_dot_records(diskStructure, cn); } cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOT, root); cd9660_create_special_directory(diskStructure, CD9660_TYPE_DOTDOT, root); return 1; } /* * Convert node to cd9660 structure * This function is designed to be called recursively on the root node of * the filesystem * Lots of recursion going on here, want to make sure it is efficient * @param struct fsnode * The root node to be converted * @param struct cd9660* The parent node (should not be NULL) * @param int Current directory depth * @param int* Running count of the number of directories that are being created */ static void cd9660_convert_structure(iso9660_disk *diskStructure, fsnode *root, cd9660node *parent_node, int level, int *numDirectories, int *error) { fsnode *iterator = root; cd9660node *this_node; int working_level; int add; int flag = 0; int counter = 0; /* * Newer, more efficient method, reduces recursion depth */ if (root == NULL) { warnx("%s: root is null", __func__); return; } /* Test for an empty directory - makefs still gives us the . record */ if ((S_ISDIR(root->type)) && (root->name[0] == '.') && (root->name[1] == '\0')) { root = root->next; if (root == NULL) return; } if ((this_node = cd9660_allocate_cd9660node()) == NULL) { CD9660_MEM_ALLOC_ERROR(__func__); } /* * To reduce the number of recursive calls, we will iterate over * the next pointers to the right. */ while (iterator != NULL) { add = 1; /* * Increment the directory count if this is a directory * Ignore "." entries. We will generate them later */ if (!S_ISDIR(iterator->type) || strcmp(iterator->name, ".") != 0) { /* Translate the node, including its filename */ this_node->parent = parent_node; cd9660_translate_node(diskStructure, iterator, this_node); this_node->level = level; if (S_ISDIR(iterator->type)) { (*numDirectories)++; this_node->type = CD9660_TYPE_DIR; working_level = level + 1; /* * If at level 8, directory would be at 8 * and have children at 9 which is not * allowed as per ISO spec */ if (level == 8) { if ((!diskStructure->allow_deep_trees) && (!diskStructure->rock_ridge_enabled)) { warnx("error: found entry " "with depth greater " "than 8."); (*error) = 1; return; } else if (diskStructure-> rock_ridge_enabled) { working_level = 3; /* * Moved directory is actually * at level 2. */ this_node->level = working_level - 1; if (cd9660_rrip_move_directory( diskStructure, this_node) == NULL) { warnx("Failure in " "cd9660_rrip_" "move_directory" ); (*error) = 1; return; } add = 0; } } /* Do the recursive call on the children */ if (iterator->child != NULL) { cd9660_convert_structure(diskStructure, iterator->child, this_node, working_level, numDirectories, error); if ((*error) == 1) { warnx("%s: Error on recursive " "call", __func__); return; } } } else { /* Only directories should have children */ assert(iterator->child == NULL); this_node->type = CD9660_TYPE_FILE; } /* * Finally, do a sorted insert */ if (add) { cd9660_sorted_child_insert( parent_node, this_node); } /*Allocate new temp_node */ if (iterator->next != NULL) { this_node = cd9660_allocate_cd9660node(); if (this_node == NULL) CD9660_MEM_ALLOC_ERROR(__func__); } } iterator = iterator->next; } /* cd9660_handle_collisions(first_node); */ /* TODO: need cleanup */ cd9660_copy_filenames(diskStructure, parent_node); do { flag = cd9660_handle_collisions(diskStructure, parent_node, counter); counter++; cd9660_sorting_nodes(parent_node); } while ((flag == 1) && (counter < 100)); } /* * Clean up the cd9660node tree * This is designed to be called recursively on the root node * @param struct cd9660node *root The node to free * @returns void */ static void cd9660_free_structure(cd9660node *root) { cd9660node *cn; while ((cn = TAILQ_FIRST(&root->cn_children)) != NULL) { TAILQ_REMOVE(&root->cn_children, cn, cn_next_child); cd9660_free_structure(cn); } free(root); } /* * Be a little more memory conservative: * instead of having the TAILQ_ENTRY as part of the cd9660node, * just create a temporary structure */ static struct ptq_entry { TAILQ_ENTRY(ptq_entry) ptq; cd9660node *node; } *n; #define PTQUEUE_NEW(n,s,r,t){\ n = emalloc(sizeof(struct s)); \ n->node = t;\ } /* * Generate the path tables * The specific implementation of this function is left as an exercise to the * programmer. It could be done recursively. Make sure you read how the path * table has to be laid out, it has levels. * @param struct iso9660_disk *disk The disk image * @returns int The number of built path tables (between 1 and 4), 0 on failure */ static int cd9660_generate_path_table(iso9660_disk *diskStructure) { cd9660node *cn, *dirNode = diskStructure->rootNode; cd9660node *last = dirNode; int pathTableSize = 0; /* computed as we go */ int counter = 1; /* root gets a count of 0 */ TAILQ_HEAD(cd9660_pt_head, ptq_entry) pt_head; TAILQ_INIT(&pt_head); PTQUEUE_NEW(n, ptq_entry, -1, diskStructure->rootNode); /* Push the root node */ TAILQ_INSERT_HEAD(&pt_head, n, ptq); /* Breadth-first traversal of file structure */ while (pt_head.tqh_first != 0) { n = pt_head.tqh_first; dirNode = n->node; TAILQ_REMOVE(&pt_head, pt_head.tqh_first, ptq); free(n); /* Update the size */ pathTableSize += ISO_PATHTABLE_ENTRY_BASESIZE + dirNode->isoDirRecord->name_len[0]+ (dirNode->isoDirRecord->name_len[0] % 2 == 0 ? 0 : 1); /* includes the padding bit */ dirNode->ptnumber=counter; if (dirNode != last) { last->ptnext = dirNode; dirNode->ptprev = last; } last = dirNode; /* Push children onto queue */ TAILQ_FOREACH(cn, &dirNode->cn_children, cn_next_child) { /* * Dont add the DOT and DOTDOT types to the path * table. */ if ((cn->type != CD9660_TYPE_DOT) && (cn->type != CD9660_TYPE_DOTDOT)) { if (S_ISDIR(cn->node->type)) { PTQUEUE_NEW(n, ptq_entry, -1, cn); TAILQ_INSERT_TAIL(&pt_head, n, ptq); } } } counter++; } return pathTableSize; } void cd9660_compute_full_filename(cd9660node *node, char *buf) { int len; len = CD9660MAXPATH + 1; len = snprintf(buf, len, "%s/%s/%s", node->node->root, node->node->path, node->node->name); if (len > CD9660MAXPATH) errx(EXIT_FAILURE, "Pathname too long."); } /* NEW filename conversion method */ typedef int(*cd9660_filename_conversion_functor)(iso9660_disk *, const char *, char *, int); /* * TODO: These two functions are almost identical. * Some code cleanup is possible here * * XXX bounds checking! */ static int cd9660_level1_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { /* * ISO 9660 : 10.1 * File Name shall not contain more than 8 d or d1 characters * File Name Extension shall not contain more than 3 d or d1 characters * Directory Identifier shall not contain more than 8 d or d1 characters */ int namelen = 0; int extlen = 0; int found_ext = 0; while (*oldname != '\0' && extlen < 3) { /* Handle period first, as it is special */ if (*oldname == '.') { if (found_ext) { *newname++ = '_'; extlen ++; } else { *newname++ = '.'; found_ext = 1; } } else { /* cut RISC OS file type off ISO name */ if (diskStructure->archimedes_enabled && *oldname == ',' && strlen(oldname) == 4) break; /* Enforce 12.3 / 8 */ if (namelen == 8 && !found_ext) break; if (islower((unsigned char)*oldname)) *newname++ = toupper((unsigned char)*oldname); else if (isupper((unsigned char)*oldname) || isdigit((unsigned char)*oldname)) *newname++ = *oldname; else *newname++ = '_'; if (found_ext) extlen++; else namelen++; } oldname++; } if (is_file) { if (!found_ext && !diskStructure->omit_trailing_period) *newname++ = '.'; /* Add version */ sprintf(newname, ";%i", 1); } return namelen + extlen + found_ext; } /* XXX bounds checking! */ static int cd9660_level2_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { /* * ISO 9660 : 7.5.1 * File name : 0+ d or d1 characters * separator 1 (.) * File name extension : 0+ d or d1 characters * separator 2 (;) * File version number (5 characters, 1-32767) * 1 <= Sum of File name and File name extension <= 30 */ int namelen = 0; int extlen = 0; int found_ext = 0; while (*oldname != '\0' && namelen + extlen < 30) { /* Handle period first, as it is special */ if (*oldname == '.') { if (found_ext) { if (diskStructure->allow_multidot) { *newname++ = '.'; } else { *newname++ = '_'; } extlen ++; } else { *newname++ = '.'; found_ext = 1; } } else { /* cut RISC OS file type off ISO name */ if (diskStructure->archimedes_enabled && *oldname == ',' && strlen(oldname) == 4) break; if (islower((unsigned char)*oldname)) *newname++ = toupper((unsigned char)*oldname); else if (isupper((unsigned char)*oldname) || isdigit((unsigned char)*oldname)) *newname++ = *oldname; else if (diskStructure->allow_multidot && *oldname == '.') { *newname++ = '.'; } else { *newname++ = '_'; } if (found_ext) extlen++; else namelen++; } oldname ++; } if (is_file) { if (!found_ext && !diskStructure->omit_trailing_period) *newname++ = '.'; /* Add version */ sprintf(newname, ";%i", 1); } return namelen + extlen + found_ext; } #if 0 static int cd9660_joliet_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { /* TODO: implement later, move to cd9660_joliet.c ?? */ } #endif /* * Convert a file name to ISO compliant file name * @param char * oldname The original filename * @param char ** newname The new file name, in the appropriate character * set and of appropriate length * @param int 1 if file, 0 if directory * @returns int The length of the new string */ static int cd9660_convert_filename(iso9660_disk *diskStructure, const char *oldname, char *newname, int is_file) { assert(1 <= diskStructure->isoLevel && diskStructure->isoLevel <= 2); /* NEW */ cd9660_filename_conversion_functor conversion_function = NULL; if (diskStructure->isoLevel == 1) conversion_function = &cd9660_level1_convert_filename; else if (diskStructure->isoLevel == 2) conversion_function = &cd9660_level2_convert_filename; return (*conversion_function)(diskStructure, oldname, newname, is_file); } int cd9660_compute_record_size(iso9660_disk *diskStructure, cd9660node *node) { int size = node->isoDirRecord->length[0]; if (diskStructure->rock_ridge_enabled) size += node->susp_entry_size; size += node->su_tail_size; size += size & 1; /* Ensure length of record is even. */ assert(size <= 254); return size; } static void cd9660_populate_dot_records(iso9660_disk *diskStructure, cd9660node *node) { node->dot_record->fileDataSector = node->fileDataSector; memcpy(node->dot_record->isoDirRecord,node->isoDirRecord, 34); node->dot_record->isoDirRecord->name_len[0] = 1; node->dot_record->isoDirRecord->name[0] = 0; node->dot_record->isoDirRecord->name[1] = 0; node->dot_record->isoDirRecord->length[0] = 34; node->dot_record->fileRecordSize = cd9660_compute_record_size(diskStructure, node->dot_record); if (node == diskStructure->rootNode) { node->dot_dot_record->fileDataSector = node->fileDataSector; memcpy(node->dot_dot_record->isoDirRecord,node->isoDirRecord, 34); } else { node->dot_dot_record->fileDataSector = node->parent->fileDataSector; memcpy(node->dot_dot_record->isoDirRecord, node->parent->isoDirRecord,34); } node->dot_dot_record->isoDirRecord->name_len[0] = 1; node->dot_dot_record->isoDirRecord->name[0] = 1; node->dot_dot_record->isoDirRecord->name[1] = 0; node->dot_dot_record->isoDirRecord->length[0] = 34; node->dot_dot_record->fileRecordSize = cd9660_compute_record_size(diskStructure, node->dot_dot_record); } /* * @param struct cd9660node *node The node * @param int The offset (in bytes) - SHOULD align to the beginning of a sector * @returns int The total size of files and directory entries (should be * a multiple of sector size) */ static int64_t cd9660_compute_offsets(iso9660_disk *diskStructure, cd9660node *node, int64_t startOffset) { /* * This function needs to compute the size of directory records and * runs, file lengths, and set the appropriate variables both in * cd9660node and isoDirEntry */ int64_t used_bytes = 0; int64_t current_sector_usage = 0; cd9660node *child; fsinode *inode; int64_t r; assert(node != NULL); /* * NOTE : There needs to be some special case detection for * the "real root" node, since for it, node->node is undefined */ node->fileDataSector = -1; if (node->type & CD9660_TYPE_DIR) { node->fileRecordSize = cd9660_compute_record_size( diskStructure, node); /*Set what sector this directory starts in*/ node->fileDataSector = CD9660_BLOCKS(diskStructure->sectorSize,startOffset); cd9660_bothendian_dword(node->fileDataSector, node->isoDirRecord->extent); /* * First loop over children, need to know the size of * their directory records */ node->fileSectorsUsed = 1; TAILQ_FOREACH(child, &node->cn_children, cn_next_child) { node->fileDataLength += cd9660_compute_record_size(diskStructure, child); if ((cd9660_compute_record_size(diskStructure, child) + current_sector_usage) >= diskStructure->sectorSize) { current_sector_usage = 0; node->fileSectorsUsed++; } current_sector_usage += cd9660_compute_record_size(diskStructure, child); } cd9660_bothendian_dword(node->fileSectorsUsed * diskStructure->sectorSize,node->isoDirRecord->size); /* * This should point to the sector after the directory * record (or, the first byte in that sector) */ used_bytes += node->fileSectorsUsed * diskStructure->sectorSize; for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child); child != NULL; child = TAILQ_NEXT(child, cn_next_child)) { /* Directories need recursive call */ if (S_ISDIR(child->node->type)) { r = cd9660_compute_offsets(diskStructure, child, used_bytes + startOffset); if (r != -1) used_bytes += r; else return -1; } } /* Explicitly set the . and .. records */ cd9660_populate_dot_records(diskStructure, node); /* Finally, do another iteration to write the file data*/ for (child = TAILQ_NEXT(node->dot_dot_record, cn_next_child); child != NULL; child = TAILQ_NEXT(child, cn_next_child)) { /* Files need extent set */ if (S_ISDIR(child->node->type)) continue; child->fileRecordSize = cd9660_compute_record_size(diskStructure, child); child->fileSectorsUsed = CD9660_BLOCKS(diskStructure->sectorSize, child->fileDataLength); inode = child->node->inode; if ((inode->flags & FI_ALLOCATED) == 0) { inode->ino = CD9660_BLOCKS(diskStructure->sectorSize, used_bytes + startOffset); inode->flags |= FI_ALLOCATED; used_bytes += child->fileSectorsUsed * diskStructure->sectorSize; } else { INODE_WARNX(("%s: already allocated inode %d " "data sectors at %" PRIu32, __func__, (int)inode->st.st_ino, inode->ino)); } child->fileDataSector = inode->ino; cd9660_bothendian_dword(child->fileDataSector, child->isoDirRecord->extent); } } return used_bytes; } #if 0 /* Might get rid of this func */ static int cd9660_copy_stat_info(cd9660node *from, cd9660node *to, int file) { to->node->inode->st.st_dev = 0; to->node->inode->st.st_ino = 0; to->node->inode->st.st_size = 0; to->node->inode->st.st_blksize = from->node->inode->st.st_blksize; to->node->inode->st.st_atime = from->node->inode->st.st_atime; to->node->inode->st.st_mtime = from->node->inode->st.st_mtime; to->node->inode->st.st_ctime = from->node->inode->st.st_ctime; to->node->inode->st.st_uid = from->node->inode->st.st_uid; to->node->inode->st.st_gid = from->node->inode->st.st_gid; to->node->inode->st.st_mode = from->node->inode->st.st_mode; /* Clear out type */ to->node->inode->st.st_mode = to->node->inode->st.st_mode & ~(S_IFMT); if (file) to->node->inode->st.st_mode |= S_IFREG; else to->node->inode->st.st_mode |= S_IFDIR; return 1; } #endif static cd9660node * cd9660_create_virtual_entry(iso9660_disk *diskStructure, const char *name, cd9660node *parent, int file, int insert) { cd9660node *temp; fsnode * tfsnode; assert(parent != NULL); temp = cd9660_allocate_cd9660node(); if (temp == NULL) return NULL; tfsnode = emalloc(sizeof(*tfsnode)); tfsnode->name = estrdup(name); temp->isoDirRecord = emalloc(sizeof(*temp->isoDirRecord)); cd9660_convert_filename(diskStructure, tfsnode->name, temp->isoDirRecord->name, file); temp->node = tfsnode; temp->parent = parent; if (insert) { if (temp->parent != NULL) { temp->level = temp->parent->level + 1; if (!TAILQ_EMPTY(&temp->parent->cn_children)) cd9660_sorted_child_insert(temp->parent, temp); else TAILQ_INSERT_HEAD(&temp->parent->cn_children, temp, cn_next_child); } } if (parent->node != NULL) { tfsnode->type = parent->node->type; } /* Clear out file type bits */ tfsnode->type &= ~(S_IFMT); if (file) tfsnode->type |= S_IFREG; else tfsnode->type |= S_IFDIR; /* Indicate that there is no spec entry (inode) */ tfsnode->flags &= ~(FSNODE_F_HASSPEC); #if 0 cd9660_copy_stat_info(parent, temp, file); #endif return temp; } static cd9660node * cd9660_create_file(iso9660_disk *diskStructure, const char *name, cd9660node *parent, cd9660node *me) { cd9660node *temp; temp = cd9660_create_virtual_entry(diskStructure, name, parent, 1, 1); if (temp == NULL) return NULL; temp->fileDataLength = 0; temp->type = CD9660_TYPE_FILE | CD9660_TYPE_VIRTUAL; temp->node->inode = ecalloc(1, sizeof(*temp->node->inode)); *temp->node->inode = *me->node->inode; if (cd9660_translate_node_common(diskStructure, temp) == 0) return NULL; return temp; } /* * Create a new directory which does not exist on disk * @param const char * name The name to assign to the directory * @param const char * parent Pointer to the parent directory * @returns cd9660node * Pointer to the new directory */ static cd9660node * cd9660_create_directory(iso9660_disk *diskStructure, const char *name, cd9660node *parent, cd9660node *me) { cd9660node *temp; temp = cd9660_create_virtual_entry(diskStructure, name, parent, 0, 1); if (temp == NULL) return NULL; temp->node->type |= S_IFDIR; temp->type = CD9660_TYPE_DIR | CD9660_TYPE_VIRTUAL; temp->node->inode = ecalloc(1, sizeof(*temp->node->inode)); *temp->node->inode = *me->node->inode; if (cd9660_translate_node_common(diskStructure, temp) == 0) return NULL; return temp; } static cd9660node * cd9660_create_special_directory(iso9660_disk *diskStructure, u_char type, cd9660node *parent) { cd9660node *temp, *first; char na[2]; assert(parent != NULL); if (type == CD9660_TYPE_DOT) na[0] = 0; else if (type == CD9660_TYPE_DOTDOT) na[0] = 1; else return 0; na[1] = 0; if ((temp = cd9660_create_virtual_entry(diskStructure, na, parent, 0, 0)) == NULL) return NULL; temp->parent = parent; temp->type = type; temp->isoDirRecord->length[0] = 34; /* Dot record is always first */ if (type == CD9660_TYPE_DOT) { parent->dot_record = temp; TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child); /* DotDot should be second */ } else if (type == CD9660_TYPE_DOTDOT) { parent->dot_dot_record = temp; /* * If the first child is the dot record, insert * this second. Otherwise, insert it at the head. */ if ((first = TAILQ_FIRST(&parent->cn_children)) == NULL || (first->type & CD9660_TYPE_DOT) == 0) { TAILQ_INSERT_HEAD(&parent->cn_children, temp, cn_next_child); } else { TAILQ_INSERT_AFTER(&parent->cn_children, first, temp, cn_next_child); } } return temp; } static int cd9660_add_generic_bootimage(iso9660_disk *diskStructure, const char *bootimage) { struct stat stbuf; assert(bootimage != NULL); if (*bootimage == '\0') { warnx("Error: Boot image must be a filename"); return 0; } diskStructure->generic_bootimage = estrdup(bootimage); /* Get information about the file */ if (lstat(diskStructure->generic_bootimage, &stbuf) == -1) err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__, diskStructure->generic_bootimage); if (stbuf.st_size > 32768) { warnx("Error: Boot image must be no greater than 32768 bytes"); return 0; } if (diskStructure->verbose_level > 0) { - printf("Generic boot image image has size %lld\n", + printf("Generic boot image has size %lld\n", (long long)stbuf.st_size); } diskStructure->has_generic_bootimage = 1; return 1; } Index: head/usr.sbin/nfsd/nfsd.c =================================================================== --- head/usr.sbin/nfsd/nfsd.c (revision 327229) +++ head/usr.sbin/nfsd/nfsd.c (revision 327230) @@ -1,1143 +1,1143 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1989, 1993, 1994 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Rick Macklem at The University of Guelph. * * 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. 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 static const char copyright[] = "@(#) Copyright (c) 1989, 1993, 1994\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)nfsd.c 8.9 (Berkeley) 3/29/95"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static int debug = 0; #define NFSD_STABLERESTART "/var/db/nfs-stablerestart" #define NFSD_STABLEBACKUP "/var/db/nfs-stablerestart.bak" #define MAXNFSDCNT 256 #define DEFNFSDCNT 4 #define NFS_VER2 2 #define NFS_VER3 3 #define NFS_VER4 4 static pid_t children[MAXNFSDCNT]; /* PIDs of children */ static int nfsdcnt; /* number of children */ static int nfsdcnt_set; static int minthreads; static int maxthreads; static int nfssvc_nfsd; /* Set to correct NFSSVC_xxx flag */ static int stablefd = -1; /* Fd for the stable restart file */ static int backupfd; /* Fd for the backup stable restart file */ static const char *getopt_shortopts; static const char *getopt_usage; static int minthreads_set; static int maxthreads_set; static struct option longopts[] = { { "debug", no_argument, &debug, 1 }, { "minthreads", required_argument, &minthreads_set, 1 }, { "maxthreads", required_argument, &maxthreads_set, 1 }, { NULL, 0, NULL, 0} }; static void cleanup(int); static void child_cleanup(int); static void killchildren(void); static void nfsd_exit(int); static void nonfs(int); static void reapchild(int); static int setbindhost(struct addrinfo **ia, const char *bindhost, struct addrinfo hints); static void start_server(int); static void unregistration(void); static void usage(void); static void open_stable(int *, int *); static void copy_stable(int, int); static void backup_stable(int); static void set_nfsdcnt(int); /* * Nfs server daemon mostly just a user context for nfssvc() * * 1 - do file descriptor and signal cleanup * 2 - fork the nfsd(s) * 3 - create server socket(s) * 4 - register socket with rpcbind * * For connectionless protocols, just pass the socket into the kernel via. * nfssvc(). * For connection based sockets, loop doing accepts. When you get a new * socket from accept, pass the msgsock into the kernel via. nfssvc(). * The arguments are: * -r - reregister with rpcbind * -d - unregister with rpcbind * -t - support tcp nfs clients * -u - support udp nfs clients * -e - forces it to run a server that supports nfsv4 * followed by "n" which is the number of nfsds' to fork off */ int main(int argc, char **argv) { struct nfsd_addsock_args addsockargs; struct addrinfo *ai_udp, *ai_tcp, *ai_udp6, *ai_tcp6, hints; struct netconfig *nconf_udp, *nconf_tcp, *nconf_udp6, *nconf_tcp6; struct netbuf nb_udp, nb_tcp, nb_udp6, nb_tcp6; struct sockaddr_in inetpeer; struct sockaddr_in6 inet6peer; fd_set ready, sockbits; fd_set v4bits, v6bits; int ch, connect_type_cnt, i, maxsock, msgsock; socklen_t len; int on = 1, unregister, reregister, sock; int tcp6sock, ip6flag, tcpflag, tcpsock; int udpflag, ecode, error, s; int bindhostc, bindanyflag, rpcbreg, rpcbregcnt; int nfssvc_addsock; int longindex = 0; int nfs_minvers = NFS_VER2; size_t nfs_minvers_size; const char *lopt; char **bindhost = NULL; pid_t pid; nfsdcnt = DEFNFSDCNT; unregister = reregister = tcpflag = maxsock = 0; bindanyflag = udpflag = connect_type_cnt = bindhostc = 0; getopt_shortopts = "ah:n:rdtue"; getopt_usage = "usage:\n" " nfsd [-ardtue] [-h bindip]\n" " [-n numservers] [--minthreads #] [--maxthreads #]\n"; while ((ch = getopt_long(argc, argv, getopt_shortopts, longopts, &longindex)) != -1) switch (ch) { case 'a': bindanyflag = 1; break; case 'n': set_nfsdcnt(atoi(optarg)); break; case 'h': bindhostc++; bindhost = realloc(bindhost,sizeof(char *)*bindhostc); if (bindhost == NULL) errx(1, "Out of memory"); bindhost[bindhostc-1] = strdup(optarg); if (bindhost[bindhostc-1] == NULL) errx(1, "Out of memory"); break; case 'r': reregister = 1; break; case 'd': unregister = 1; break; case 't': tcpflag = 1; break; case 'u': udpflag = 1; break; case 'e': /* now a no-op, since this is the default */ break; case 0: lopt = longopts[longindex].name; if (!strcmp(lopt, "minthreads")) { minthreads = atoi(optarg); } else if (!strcmp(lopt, "maxthreads")) { maxthreads = atoi(optarg); } break; default: case '?': usage(); } if (!tcpflag && !udpflag) udpflag = 1; argv += optind; argc -= optind; if (minthreads_set && maxthreads_set && minthreads > maxthreads) errx(EX_USAGE, "error: minthreads(%d) can't be greater than " "maxthreads(%d)", minthreads, maxthreads); /* * XXX * Backward compatibility, trailing number is the count of daemons. */ if (argc > 1) usage(); if (argc == 1) set_nfsdcnt(atoi(argv[0])); /* * Unless the "-o" option was specified, try and run "nfsd". * If "-o" was specified, try and run "nfsserver". */ if (modfind("nfsd") < 0) { /* Not present in kernel, try loading it */ if (kldload("nfsd") < 0 || modfind("nfsd") < 0) errx(1, "NFS server is not available"); } ip6flag = 1; s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (s == -1) { if (errno != EPROTONOSUPPORT && errno != EAFNOSUPPORT) err(1, "socket"); ip6flag = 0; } else if (getnetconfigent("udp6") == NULL || getnetconfigent("tcp6") == NULL) { ip6flag = 0; } if (s != -1) close(s); if (bindhostc == 0 || bindanyflag) { bindhostc++; bindhost = realloc(bindhost,sizeof(char *)*bindhostc); if (bindhost == NULL) errx(1, "Out of memory"); bindhost[bindhostc-1] = strdup("*"); if (bindhost[bindhostc-1] == NULL) errx(1, "Out of memory"); } nfs_minvers_size = sizeof(nfs_minvers); error = sysctlbyname("vfs.nfsd.server_min_nfsvers", &nfs_minvers, &nfs_minvers_size, NULL, 0); if (error != 0 || nfs_minvers < NFS_VER2 || nfs_minvers > NFS_VER4) { warnx("sysctlbyname(vfs.nfsd.server_min_nfsvers) failed," " defaulting to NFSv2"); nfs_minvers = NFS_VER2; } if (unregister) { unregistration(); exit (0); } if (reregister) { if (udpflag) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp); if (ecode != 0) err(1, "getaddrinfo udp: %s", gai_strerror(ecode)); nconf_udp = getnetconfigent("udp"); if (nconf_udp == NULL) err(1, "getnetconfigent udp failed"); nb_udp.buf = ai_udp->ai_addr; nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) err(1, "rpcb_set udp failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp)) err(1, "rpcb_set udp failed"); freeaddrinfo(ai_udp); } if (udpflag && ip6flag) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6); if (ecode != 0) err(1, "getaddrinfo udp6: %s", gai_strerror(ecode)); nconf_udp6 = getnetconfigent("udp6"); if (nconf_udp6 == NULL) err(1, "getnetconfigent udp6 failed"); nb_udp6.buf = ai_udp6->ai_addr; nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) err(1, "rpcb_set udp6 failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6)) err(1, "rpcb_set udp6 failed"); freeaddrinfo(ai_udp6); } if (tcpflag) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp); if (ecode != 0) err(1, "getaddrinfo tcp: %s", gai_strerror(ecode)); nconf_tcp = getnetconfigent("tcp"); if (nconf_tcp == NULL) err(1, "getnetconfigent tcp failed"); nb_tcp.buf = ai_tcp->ai_addr; nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, &nb_tcp)) err(1, "rpcb_set tcp failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, &nb_tcp)) err(1, "rpcb_set tcp failed"); freeaddrinfo(ai_tcp); } if (tcpflag && ip6flag) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6); if (ecode != 0) err(1, "getaddrinfo tcp6: %s", gai_strerror(ecode)); nconf_tcp6 = getnetconfigent("tcp6"); if (nconf_tcp6 == NULL) err(1, "getnetconfigent tcp6 failed"); nb_tcp6.buf = ai_tcp6->ai_addr; nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) err(1, "rpcb_set tcp6 failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6)) err(1, "rpcb_set tcp6 failed"); freeaddrinfo(ai_tcp6); } exit (0); } if (debug == 0) { daemon(0, 0); (void)signal(SIGHUP, SIG_IGN); (void)signal(SIGINT, SIG_IGN); /* * nfsd sits in the kernel most of the time. It needs * to ignore SIGTERM/SIGQUIT in order to stay alive as long * as possible during a shutdown, otherwise loopback * mounts will not be able to unmount. */ (void)signal(SIGTERM, SIG_IGN); (void)signal(SIGQUIT, SIG_IGN); } (void)signal(SIGSYS, nonfs); (void)signal(SIGCHLD, reapchild); (void)signal(SIGUSR2, backup_stable); openlog("nfsd", LOG_PID | (debug ? LOG_PERROR : 0), LOG_DAEMON); /* * For V4, we open the stablerestart file and call nfssvc() * to get it loaded. This is done before the daemons do the * regular nfssvc() call to service NFS requests. * (This way the file remains open until the last nfsd is killed * off.) * It and the backup copy will be created as empty files * the first time this nfsd is started and should never be * deleted/replaced if at all possible. It should live on a * local, non-volatile storage device that does not do hardware * level write-back caching. (See SCSI doc for more information * on how to prevent write-back caching on SCSI disks.) */ open_stable(&stablefd, &backupfd); if (stablefd < 0) { syslog(LOG_ERR, "Can't open %s: %m\n", NFSD_STABLERESTART); exit(1); } /* This system call will fail for old kernels, but that's ok. */ nfssvc(NFSSVC_BACKUPSTABLE, NULL); if (nfssvc(NFSSVC_STABLERESTART, (caddr_t)&stablefd) < 0) { syslog(LOG_ERR, "Can't read stable storage file: %m\n"); exit(1); } nfssvc_addsock = NFSSVC_NFSDADDSOCK; nfssvc_nfsd = NFSSVC_NFSDNFSD; if (tcpflag) { /* * For TCP mode, we fork once to start the first * kernel nfsd thread. The kernel will add more * threads as needed. */ pid = fork(); if (pid == -1) { syslog(LOG_ERR, "fork: %m"); nfsd_exit(1); } if (pid) { children[0] = pid; } else { (void)signal(SIGUSR1, child_cleanup); setproctitle("server"); start_server(0); } } (void)signal(SIGUSR1, cleanup); FD_ZERO(&v4bits); FD_ZERO(&v6bits); FD_ZERO(&sockbits); rpcbregcnt = 0; /* Set up the socket for udp and rpcb register it. */ if (udpflag) { rpcbreg = 0; for (i = 0; i < bindhostc; i++) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; if (setbindhost(&ai_udp, bindhost[i], hints) == 0) { rpcbreg = 1; rpcbregcnt++; if ((sock = socket(ai_udp->ai_family, ai_udp->ai_socktype, ai_udp->ai_protocol)) < 0) { syslog(LOG_ERR, "can't create udp socket"); nfsd_exit(1); } if (bind(sock, ai_udp->ai_addr, ai_udp->ai_addrlen) < 0) { syslog(LOG_ERR, "can't bind udp addr %s: %m", bindhost[i]); nfsd_exit(1); } freeaddrinfo(ai_udp); addsockargs.sock = sock; addsockargs.name = NULL; addsockargs.namelen = 0; if (nfssvc(nfssvc_addsock, &addsockargs) < 0) { syslog(LOG_ERR, "can't Add UDP socket"); nfsd_exit(1); } (void)close(sock); } } if (rpcbreg == 1) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp); if (ecode != 0) { syslog(LOG_ERR, "getaddrinfo udp: %s", gai_strerror(ecode)); nfsd_exit(1); } nconf_udp = getnetconfigent("udp"); if (nconf_udp == NULL) err(1, "getnetconfigent udp failed"); nb_udp.buf = ai_udp->ai_addr; nb_udp.len = nb_udp.maxlen = ai_udp->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp, &nb_udp)) err(1, "rpcb_set udp failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp, &nb_udp)) err(1, "rpcb_set udp failed"); freeaddrinfo(ai_udp); } } /* Set up the socket for udp6 and rpcb register it. */ if (udpflag && ip6flag) { rpcbreg = 0; for (i = 0; i < bindhostc; i++) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; if (setbindhost(&ai_udp6, bindhost[i], hints) == 0) { rpcbreg = 1; rpcbregcnt++; if ((sock = socket(ai_udp6->ai_family, ai_udp6->ai_socktype, ai_udp6->ai_protocol)) < 0) { syslog(LOG_ERR, "can't create udp6 socket"); nfsd_exit(1); } if (setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof on) < 0) { syslog(LOG_ERR, "can't set v6-only binding for " "udp6 socket: %m"); nfsd_exit(1); } if (bind(sock, ai_udp6->ai_addr, ai_udp6->ai_addrlen) < 0) { syslog(LOG_ERR, "can't bind udp6 addr %s: %m", bindhost[i]); nfsd_exit(1); } freeaddrinfo(ai_udp6); addsockargs.sock = sock; addsockargs.name = NULL; addsockargs.namelen = 0; if (nfssvc(nfssvc_addsock, &addsockargs) < 0) { syslog(LOG_ERR, "can't add UDP6 socket"); nfsd_exit(1); } (void)close(sock); } } if (rpcbreg == 1) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; hints.ai_protocol = IPPROTO_UDP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_udp6); if (ecode != 0) { syslog(LOG_ERR, "getaddrinfo udp6: %s", gai_strerror(ecode)); nfsd_exit(1); } nconf_udp6 = getnetconfigent("udp6"); if (nconf_udp6 == NULL) err(1, "getnetconfigent udp6 failed"); nb_udp6.buf = ai_udp6->ai_addr; nb_udp6.len = nb_udp6.maxlen = ai_udp6->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_udp6, &nb_udp6)) err(1, "rpcb_set udp6 failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_udp6, &nb_udp6)) err(1, "rpcb_set udp6 failed"); freeaddrinfo(ai_udp6); } } /* Set up the socket for tcp and rpcb register it. */ if (tcpflag) { rpcbreg = 0; for (i = 0; i < bindhostc; i++) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; if (setbindhost(&ai_tcp, bindhost[i], hints) == 0) { rpcbreg = 1; rpcbregcnt++; if ((tcpsock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { syslog(LOG_ERR, "can't create tcp socket"); nfsd_exit(1); } if (setsockopt(tcpsock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m"); if (bind(tcpsock, ai_tcp->ai_addr, ai_tcp->ai_addrlen) < 0) { syslog(LOG_ERR, "can't bind tcp addr %s: %m", bindhost[i]); nfsd_exit(1); } if (listen(tcpsock, -1) < 0) { syslog(LOG_ERR, "listen failed"); nfsd_exit(1); } freeaddrinfo(ai_tcp); FD_SET(tcpsock, &sockbits); FD_SET(tcpsock, &v4bits); maxsock = tcpsock; connect_type_cnt++; } } if (rpcbreg == 1) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp); if (ecode != 0) { syslog(LOG_ERR, "getaddrinfo tcp: %s", gai_strerror(ecode)); nfsd_exit(1); } nconf_tcp = getnetconfigent("tcp"); if (nconf_tcp == NULL) err(1, "getnetconfigent tcp failed"); nb_tcp.buf = ai_tcp->ai_addr; nb_tcp.len = nb_tcp.maxlen = ai_tcp->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp, &nb_tcp)) err(1, "rpcb_set tcp failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp, &nb_tcp)) err(1, "rpcb_set tcp failed"); freeaddrinfo(ai_tcp); } } /* Set up the socket for tcp6 and rpcb register it. */ if (tcpflag && ip6flag) { rpcbreg = 0; for (i = 0; i < bindhostc; i++) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; if (setbindhost(&ai_tcp6, bindhost[i], hints) == 0) { rpcbreg = 1; rpcbregcnt++; if ((tcp6sock = socket(ai_tcp6->ai_family, ai_tcp6->ai_socktype, ai_tcp6->ai_protocol)) < 0) { syslog(LOG_ERR, "can't create tcp6 socket"); nfsd_exit(1); } if (setsockopt(tcp6sock, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) syslog(LOG_ERR, "setsockopt SO_REUSEADDR: %m"); if (setsockopt(tcp6sock, IPPROTO_IPV6, IPV6_V6ONLY, &on, sizeof on) < 0) { syslog(LOG_ERR, "can't set v6-only binding for tcp6 " "socket: %m"); nfsd_exit(1); } if (bind(tcp6sock, ai_tcp6->ai_addr, ai_tcp6->ai_addrlen) < 0) { syslog(LOG_ERR, "can't bind tcp6 addr %s: %m", bindhost[i]); nfsd_exit(1); } if (listen(tcp6sock, -1) < 0) { syslog(LOG_ERR, "listen failed"); nfsd_exit(1); } freeaddrinfo(ai_tcp6); FD_SET(tcp6sock, &sockbits); FD_SET(tcp6sock, &v6bits); if (maxsock < tcp6sock) maxsock = tcp6sock; connect_type_cnt++; } } if (rpcbreg == 1) { memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_protocol = IPPROTO_TCP; ecode = getaddrinfo(NULL, "nfs", &hints, &ai_tcp6); if (ecode != 0) { syslog(LOG_ERR, "getaddrinfo tcp6: %s", gai_strerror(ecode)); nfsd_exit(1); } nconf_tcp6 = getnetconfigent("tcp6"); if (nconf_tcp6 == NULL) err(1, "getnetconfigent tcp6 failed"); nb_tcp6.buf = ai_tcp6->ai_addr; nb_tcp6.len = nb_tcp6.maxlen = ai_tcp6->ai_addrlen; if (nfs_minvers == NFS_VER2) if (!rpcb_set(NFS_PROGRAM, 2, nconf_tcp6, &nb_tcp6)) err(1, "rpcb_set tcp6 failed"); if (nfs_minvers <= NFS_VER3) if (!rpcb_set(NFS_PROGRAM, 3, nconf_tcp6, &nb_tcp6)) err(1, "rpcb_set tcp6 failed"); freeaddrinfo(ai_tcp6); } } if (rpcbregcnt == 0) { syslog(LOG_ERR, "rpcb_set() failed, nothing to do: %m"); nfsd_exit(1); } if (tcpflag && connect_type_cnt == 0) { syslog(LOG_ERR, "tcp connects == 0, nothing to do: %m"); nfsd_exit(1); } setproctitle("master"); /* - * We always want a master to have a clean way to to shut nfsd down + * We always want a master to have a clean way to shut nfsd down * (with unregistration): if the master is killed, it unregisters and * kills all children. If we run for UDP only (and so do not have to - * loop waiting waiting for accept), we instead make the parent + * loop waiting for accept), we instead make the parent * a "server" too. start_server will not return. */ if (!tcpflag) start_server(1); /* * Loop forever accepting connections and passing the sockets * into the kernel for the mounts. */ for (;;) { ready = sockbits; if (connect_type_cnt > 1) { if (select(maxsock + 1, &ready, NULL, NULL, NULL) < 1) { error = errno; if (error == EINTR) continue; syslog(LOG_ERR, "select failed: %m"); nfsd_exit(1); } } for (tcpsock = 0; tcpsock <= maxsock; tcpsock++) { if (FD_ISSET(tcpsock, &ready)) { if (FD_ISSET(tcpsock, &v4bits)) { len = sizeof(inetpeer); if ((msgsock = accept(tcpsock, (struct sockaddr *)&inetpeer, &len)) < 0) { error = errno; syslog(LOG_ERR, "accept failed: %m"); if (error == ECONNABORTED || error == EINTR) continue; nfsd_exit(1); } memset(inetpeer.sin_zero, 0, sizeof(inetpeer.sin_zero)); if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) syslog(LOG_ERR, "setsockopt SO_KEEPALIVE: %m"); addsockargs.sock = msgsock; addsockargs.name = (caddr_t)&inetpeer; addsockargs.namelen = len; nfssvc(nfssvc_addsock, &addsockargs); (void)close(msgsock); } else if (FD_ISSET(tcpsock, &v6bits)) { len = sizeof(inet6peer); if ((msgsock = accept(tcpsock, (struct sockaddr *)&inet6peer, &len)) < 0) { error = errno; syslog(LOG_ERR, "accept failed: %m"); if (error == ECONNABORTED || error == EINTR) continue; nfsd_exit(1); } if (setsockopt(msgsock, SOL_SOCKET, SO_KEEPALIVE, (char *)&on, sizeof(on)) < 0) syslog(LOG_ERR, "setsockopt " "SO_KEEPALIVE: %m"); addsockargs.sock = msgsock; addsockargs.name = (caddr_t)&inet6peer; addsockargs.namelen = len; nfssvc(nfssvc_addsock, &addsockargs); (void)close(msgsock); } } } } } static int setbindhost(struct addrinfo **ai, const char *bindhost, struct addrinfo hints) { int ecode; u_int32_t host_addr[4]; /* IPv4 or IPv6 */ const char *hostptr; if (bindhost == NULL || strcmp("*", bindhost) == 0) hostptr = NULL; else hostptr = bindhost; if (hostptr != NULL) { switch (hints.ai_family) { case AF_INET: if (inet_pton(AF_INET, hostptr, host_addr) == 1) { hints.ai_flags = AI_NUMERICHOST; } else { if (inet_pton(AF_INET6, hostptr, host_addr) == 1) return (1); } break; case AF_INET6: if (inet_pton(AF_INET6, hostptr, host_addr) == 1) { hints.ai_flags = AI_NUMERICHOST; } else { if (inet_pton(AF_INET, hostptr, host_addr) == 1) return (1); } break; default: break; } } ecode = getaddrinfo(hostptr, "nfs", &hints, ai); if (ecode != 0) { syslog(LOG_ERR, "getaddrinfo %s: %s", bindhost, gai_strerror(ecode)); return (1); } return (0); } static void set_nfsdcnt(int proposed) { if (proposed < 1) { warnx("nfsd count too low %d; reset to %d", proposed, DEFNFSDCNT); nfsdcnt = DEFNFSDCNT; } else if (proposed > MAXNFSDCNT) { warnx("nfsd count too high %d; truncated to %d", proposed, MAXNFSDCNT); nfsdcnt = MAXNFSDCNT; } else nfsdcnt = proposed; nfsdcnt_set = 1; } static void usage(void) { (void)fprintf(stderr, "%s", getopt_usage); exit(1); } static void nonfs(__unused int signo) { syslog(LOG_ERR, "missing system call: NFS not available"); } static void reapchild(__unused int signo) { pid_t pid; int i; while ((pid = wait3(NULL, WNOHANG, NULL)) > 0) { for (i = 0; i < nfsdcnt; i++) if (pid == children[i]) children[i] = -1; } } static void unregistration(void) { if ((!rpcb_unset(NFS_PROGRAM, 2, NULL)) || (!rpcb_unset(NFS_PROGRAM, 3, NULL))) syslog(LOG_ERR, "rpcb_unset failed"); } static void killchildren(void) { int i; for (i = 0; i < nfsdcnt; i++) { if (children[i] > 0) kill(children[i], SIGKILL); } } /* * Cleanup master after SIGUSR1. */ static void cleanup(__unused int signo) { nfsd_exit(0); } /* * Cleanup child after SIGUSR1. */ static void child_cleanup(__unused int signo) { exit(0); } static void nfsd_exit(int status) { killchildren(); unregistration(); exit(status); } static int get_tuned_nfsdcount(void) { int ncpu, error, tuned_nfsdcnt; size_t ncpu_size; ncpu_size = sizeof(ncpu); error = sysctlbyname("hw.ncpu", &ncpu, &ncpu_size, NULL, 0); if (error) { warnx("sysctlbyname(hw.ncpu) failed defaulting to %d nfs servers", DEFNFSDCNT); tuned_nfsdcnt = DEFNFSDCNT; } else { tuned_nfsdcnt = ncpu * 8; } return tuned_nfsdcnt; } static void start_server(int master) { char principal[MAXHOSTNAMELEN + 5]; struct nfsd_nfsd_args nfsdargs; int status, error; char hostname[MAXHOSTNAMELEN + 1], *cp; struct addrinfo *aip, hints; status = 0; gethostname(hostname, sizeof (hostname)); snprintf(principal, sizeof (principal), "nfs@%s", hostname); if ((cp = strchr(hostname, '.')) == NULL || *(cp + 1) == '\0') { /* If not fully qualified, try getaddrinfo() */ memset((void *)&hints, 0, sizeof (hints)); hints.ai_flags = AI_CANONNAME; error = getaddrinfo(hostname, NULL, &hints, &aip); if (error == 0) { if (aip->ai_canonname != NULL && (cp = strchr(aip->ai_canonname, '.')) != NULL && *(cp + 1) != '\0') snprintf(principal, sizeof (principal), "nfs@%s", aip->ai_canonname); freeaddrinfo(aip); } } nfsdargs.principal = principal; if (nfsdcnt_set) nfsdargs.minthreads = nfsdargs.maxthreads = nfsdcnt; else { nfsdargs.minthreads = minthreads_set ? minthreads : get_tuned_nfsdcount(); nfsdargs.maxthreads = maxthreads_set ? maxthreads : nfsdargs.minthreads; if (nfsdargs.maxthreads < nfsdargs.minthreads) nfsdargs.maxthreads = nfsdargs.minthreads; } error = nfssvc(nfssvc_nfsd, &nfsdargs); if (error < 0 && errno == EAUTH) { /* * This indicates that it could not register the * rpcsec_gss credentials, usually because the * gssd daemon isn't running. * (only the experimental server with nfsv4) */ syslog(LOG_ERR, "No gssd, using AUTH_SYS only"); principal[0] = '\0'; error = nfssvc(nfssvc_nfsd, &nfsdargs); } if (error < 0) { syslog(LOG_ERR, "nfssvc: %m"); status = 1; } if (master) nfsd_exit(status); else exit(status); } /* * Open the stable restart file and return the file descriptor for it. */ static void open_stable(int *stable_fdp, int *backup_fdp) { int stable_fd, backup_fd = -1, ret; struct stat st, backup_st; /* Open and stat the stable restart file. */ stable_fd = open(NFSD_STABLERESTART, O_RDWR, 0); if (stable_fd < 0) stable_fd = open(NFSD_STABLERESTART, O_RDWR | O_CREAT, 0600); if (stable_fd >= 0) { ret = fstat(stable_fd, &st); if (ret < 0) { close(stable_fd); stable_fd = -1; } } /* Open and stat the backup stable restart file. */ if (stable_fd >= 0) { backup_fd = open(NFSD_STABLEBACKUP, O_RDWR, 0); if (backup_fd < 0) backup_fd = open(NFSD_STABLEBACKUP, O_RDWR | O_CREAT, 0600); if (backup_fd >= 0) { ret = fstat(backup_fd, &backup_st); if (ret < 0) { close(backup_fd); backup_fd = -1; } } if (backup_fd < 0) { close(stable_fd); stable_fd = -1; } } *stable_fdp = stable_fd; *backup_fdp = backup_fd; if (stable_fd < 0) return; /* Sync up the 2 files, as required. */ if (st.st_size > 0) copy_stable(stable_fd, backup_fd); else if (backup_st.st_size > 0) copy_stable(backup_fd, stable_fd); } /* * Copy the stable restart file to the backup or vice versa. */ static void copy_stable(int from_fd, int to_fd) { int cnt, ret; static char buf[1024]; ret = lseek(from_fd, (off_t)0, SEEK_SET); if (ret >= 0) ret = lseek(to_fd, (off_t)0, SEEK_SET); if (ret >= 0) ret = ftruncate(to_fd, (off_t)0); if (ret >= 0) do { cnt = read(from_fd, buf, 1024); if (cnt > 0) ret = write(to_fd, buf, cnt); else if (cnt < 0) ret = cnt; } while (cnt > 0 && ret >= 0); if (ret >= 0) ret = fsync(to_fd); if (ret < 0) syslog(LOG_ERR, "stable restart copy failure: %m"); } /* * Back up the stable restart file when indicated by the kernel. */ static void backup_stable(__unused int signo) { if (stablefd >= 0) copy_stable(stablefd, backupfd); } Index: head/usr.sbin/rpc.lockd/lockd_lock.c =================================================================== --- head/usr.sbin/rpc.lockd/lockd_lock.c (revision 327229) +++ head/usr.sbin/rpc.lockd/lockd_lock.c (revision 327230) @@ -1,2306 +1,2306 @@ /* $NetBSD: lockd_lock.c,v 1.5 2000/11/21 03:47:41 enami Exp $ */ /*- * SPDX-License-Identifier: BSD-4-Clause * * Copyright (c) 2001 Andrew P. Lentvorski, Jr. * Copyright (c) 2000 Manuel Bouyer. * * 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. * */ #include __FBSDID("$FreeBSD$"); #define LOCKD_DEBUG #include #ifdef LOCKD_DEBUG #include #endif #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "lockd_lock.h" #include "lockd.h" #define MAXOBJECTSIZE 64 #define MAXBUFFERSIZE 1024 /* * A set of utilities for managing file locking * * XXX: All locks are in a linked list, a better structure should be used * to improve search/access efficiency. */ /* struct describing a lock */ struct file_lock { LIST_ENTRY(file_lock) nfslocklist; fhandle_t filehandle; /* NFS filehandle */ struct sockaddr *addr; struct nlm4_holder client; /* lock holder */ /* XXX: client_cookie used *only* in send_granted */ netobj client_cookie; /* cookie sent by the client */ int nsm_status; /* status from the remote lock manager */ int status; /* lock status, see below */ int flags; /* lock flags, see lockd_lock.h */ int blocking; /* blocking lock or not */ char client_name[SM_MAXSTRLEN]; /* client_name is really variable length and must be last! */ }; LIST_HEAD(nfslocklist_head, file_lock); struct nfslocklist_head nfslocklist_head = LIST_HEAD_INITIALIZER(nfslocklist_head); LIST_HEAD(blockedlocklist_head, file_lock); struct blockedlocklist_head blockedlocklist_head = LIST_HEAD_INITIALIZER(blockedlocklist_head); /* lock status */ #define LKST_LOCKED 1 /* lock is locked */ /* XXX: Is this flag file specific or lock specific? */ #define LKST_WAITING 2 /* file is already locked by another host */ #define LKST_PROCESSING 3 /* child is trying to acquire the lock */ #define LKST_DYING 4 /* must dies when we get news from the child */ /* struct describing a monitored host */ struct host { LIST_ENTRY(host) hostlst; int refcnt; char name[SM_MAXSTRLEN]; /* name is really variable length and must be last! */ }; /* list of hosts we monitor */ LIST_HEAD(hostlst_head, host); struct hostlst_head hostlst_head = LIST_HEAD_INITIALIZER(hostlst_head); /* * File monitoring handlers * XXX: These might be able to be removed when kevent support * is placed into the hardware lock/unlock routines. (ie. * let the kernel do all the file monitoring) */ /* Struct describing a monitored file */ struct monfile { LIST_ENTRY(monfile) monfilelist; fhandle_t filehandle; /* Local access filehandle */ int fd; /* file descriptor: remains open until unlock! */ int refcount; int exclusive; }; /* List of files we monitor */ LIST_HEAD(monfilelist_head, monfile); struct monfilelist_head monfilelist_head = LIST_HEAD_INITIALIZER(monfilelist_head); static int debugdelay = 0; enum nfslock_status { NFS_GRANTED = 0, NFS_GRANTED_DUPLICATE, NFS_DENIED, NFS_DENIED_NOLOCK, NFS_RESERR }; enum hwlock_status { HW_GRANTED = 0, HW_GRANTED_DUPLICATE, HW_DENIED, HW_DENIED_NOLOCK, HW_STALEFH, HW_READONLY, HW_RESERR }; enum partialfilelock_status { PFL_GRANTED=0, PFL_GRANTED_DUPLICATE, PFL_DENIED, PFL_NFSDENIED, PFL_NFSBLOCKED, PFL_NFSDENIED_NOLOCK, PFL_NFSRESERR, PFL_HWDENIED, PFL_HWBLOCKED, PFL_HWDENIED_NOLOCK, PFL_HWRESERR}; enum LFLAGS {LEDGE_LEFT, LEDGE_LBOUNDARY, LEDGE_INSIDE, LEDGE_RBOUNDARY, LEDGE_RIGHT}; enum RFLAGS {REDGE_LEFT, REDGE_LBOUNDARY, REDGE_INSIDE, REDGE_RBOUNDARY, REDGE_RIGHT}; /* XXX: WARNING! I HAVE OVERLOADED THIS STATUS ENUM! SPLIT IT APART INTO TWO */ enum split_status {SPL_DISJOINT=0, SPL_LOCK1=1, SPL_LOCK2=2, SPL_CONTAINED=4, SPL_RESERR=8}; enum partialfilelock_status lock_partialfilelock(struct file_lock *fl); void send_granted(struct file_lock *fl, int opcode); void siglock(void); void sigunlock(void); void monitor_lock_host(const char *hostname); void unmonitor_lock_host(char *hostname); void copy_nlm4_lock_to_nlm4_holder(const struct nlm4_lock *src, const bool_t exclusive, struct nlm4_holder *dest); struct file_lock * allocate_file_lock(const netobj *lockowner, const netobj *matchcookie, const struct sockaddr *addr, const char *caller_name); void deallocate_file_lock(struct file_lock *fl); void fill_file_lock(struct file_lock *fl, const fhandle_t *fh, const bool_t exclusive, const int32_t svid, const u_int64_t offset, const u_int64_t len, const int state, const int status, const int flags, const int blocking); int regions_overlap(const u_int64_t start1, const u_int64_t len1, const u_int64_t start2, const u_int64_t len2); enum split_status region_compare(const u_int64_t starte, const u_int64_t lene, const u_int64_t startu, const u_int64_t lenu, u_int64_t *start1, u_int64_t *len1, u_int64_t *start2, u_int64_t *len2); int same_netobj(const netobj *n0, const netobj *n1); int same_filelock_identity(const struct file_lock *fl0, const struct file_lock *fl2); static void debuglog(char const *fmt, ...); void dump_static_object(const unsigned char* object, const int sizeof_object, unsigned char* hbuff, const int sizeof_hbuff, unsigned char* cbuff, const int sizeof_cbuff); void dump_netobj(const struct netobj *nobj); void dump_filelock(const struct file_lock *fl); struct file_lock * get_lock_matching_unlock(const struct file_lock *fl); enum nfslock_status test_nfslock(const struct file_lock *fl, struct file_lock **conflicting_fl); enum nfslock_status lock_nfslock(struct file_lock *fl); enum nfslock_status delete_nfslock(struct file_lock *fl); enum nfslock_status unlock_nfslock(const struct file_lock *fl, struct file_lock **released_lock, struct file_lock **left_lock, struct file_lock **right_lock); enum hwlock_status lock_hwlock(struct file_lock *fl); enum split_status split_nfslock(const struct file_lock *exist_lock, const struct file_lock *unlock_lock, struct file_lock **left_lock, struct file_lock **right_lock); int duplicate_block(struct file_lock *fl); void add_blockingfilelock(struct file_lock *fl); enum hwlock_status unlock_hwlock(const struct file_lock *fl); enum hwlock_status test_hwlock(const struct file_lock *fl, struct file_lock **conflicting_fl); void remove_blockingfilelock(struct file_lock *fl); void clear_blockingfilelock(const char *hostname); void retry_blockingfilelocklist(void); enum partialfilelock_status unlock_partialfilelock( const struct file_lock *fl); void clear_partialfilelock(const char *hostname); enum partialfilelock_status test_partialfilelock( const struct file_lock *fl, struct file_lock **conflicting_fl); enum nlm_stats do_test(struct file_lock *fl, struct file_lock **conflicting_fl); enum nlm_stats do_unlock(struct file_lock *fl); enum nlm_stats do_lock(struct file_lock *fl); void do_clear(const char *hostname); size_t strnlen(const char *, size_t); void debuglog(char const *fmt, ...) { va_list ap; if (debug_level < 1) { return; } sleep(debugdelay); va_start(ap, fmt); vsyslog(LOG_DEBUG, fmt, ap); va_end(ap); } void dump_static_object(object, size_object, hbuff, size_hbuff, cbuff, size_cbuff) const unsigned char *object; const int size_object; unsigned char *hbuff; const int size_hbuff; unsigned char *cbuff; const int size_cbuff; { int i, objectsize; if (debug_level < 2) { return; } objectsize = size_object; if (objectsize == 0) { debuglog("object is size 0\n"); } else { if (objectsize > MAXOBJECTSIZE) { debuglog("Object of size %d being clamped" "to size %d\n", objectsize, MAXOBJECTSIZE); objectsize = MAXOBJECTSIZE; } if (hbuff != NULL) { if (size_hbuff < objectsize*2+1) { debuglog("Hbuff not large enough." " Increase size\n"); } else { for(i=0;i= 32 && *(object+i) <= 127) { *(cbuff+i) = *(object+i); } else { *(cbuff+i) = '.'; } } *(cbuff+i) = '\0'; } } } void dump_netobj(const struct netobj *nobj) { char hbuff[MAXBUFFERSIZE*2]; char cbuff[MAXBUFFERSIZE]; if (debug_level < 2) { return; } if (nobj == NULL) { debuglog("Null netobj pointer\n"); } else if (nobj->n_len == 0) { debuglog("Size zero netobj\n"); } else { dump_static_object(nobj->n_bytes, nobj->n_len, hbuff, sizeof(hbuff), cbuff, sizeof(cbuff)); debuglog("netobj: len: %d data: %s ::: %s\n", nobj->n_len, hbuff, cbuff); } } /* #define DUMP_FILELOCK_VERBOSE */ void dump_filelock(const struct file_lock *fl) { #ifdef DUMP_FILELOCK_VERBOSE char hbuff[MAXBUFFERSIZE*2]; char cbuff[MAXBUFFERSIZE]; #endif if (debug_level < 2) { return; } if (fl != NULL) { debuglog("Dumping file lock structure @ %p\n", fl); #ifdef DUMP_FILELOCK_VERBOSE dump_static_object((unsigned char *)&fl->filehandle, sizeof(fl->filehandle), hbuff, sizeof(hbuff), cbuff, sizeof(cbuff)); debuglog("Filehandle: %8s ::: %8s\n", hbuff, cbuff); #endif debuglog("Dumping nlm4_holder:\n" "exc: %x svid: %x offset:len %llx:%llx\n", fl->client.exclusive, fl->client.svid, fl->client.l_offset, fl->client.l_len); #ifdef DUMP_FILELOCK_VERBOSE debuglog("Dumping client identity:\n"); dump_netobj(&fl->client.oh); debuglog("Dumping client cookie:\n"); dump_netobj(&fl->client_cookie); debuglog("nsm: %d status: %d flags: %d svid: %x" " client_name: %s\n", fl->nsm_status, fl->status, fl->flags, fl->client.svid, fl->client_name); #endif } else { debuglog("NULL file lock structure\n"); } } void copy_nlm4_lock_to_nlm4_holder(src, exclusive, dest) const struct nlm4_lock *src; const bool_t exclusive; struct nlm4_holder *dest; { dest->exclusive = exclusive; dest->oh.n_len = src->oh.n_len; dest->oh.n_bytes = src->oh.n_bytes; dest->svid = src->svid; dest->l_offset = src->l_offset; dest->l_len = src->l_len; } size_t strnlen(const char *s, size_t len) { size_t n; for (n = 0; s[n] != 0 && n < len; n++) ; return n; } /* * allocate_file_lock: Create a lock with the given parameters */ struct file_lock * allocate_file_lock(const netobj *lockowner, const netobj *matchcookie, const struct sockaddr *addr, const char *caller_name) { struct file_lock *newfl; size_t n; /* Beware of rubbish input! */ n = strnlen(caller_name, SM_MAXSTRLEN); if (n == SM_MAXSTRLEN) { return NULL; } newfl = malloc(sizeof(*newfl) - sizeof(newfl->client_name) + n + 1); if (newfl == NULL) { return NULL; } bzero(newfl, sizeof(*newfl) - sizeof(newfl->client_name)); memcpy(newfl->client_name, caller_name, n); newfl->client_name[n] = 0; newfl->client.oh.n_bytes = malloc(lockowner->n_len); if (newfl->client.oh.n_bytes == NULL) { free(newfl); return NULL; } newfl->client.oh.n_len = lockowner->n_len; bcopy(lockowner->n_bytes, newfl->client.oh.n_bytes, lockowner->n_len); newfl->client_cookie.n_bytes = malloc(matchcookie->n_len); if (newfl->client_cookie.n_bytes == NULL) { free(newfl->client.oh.n_bytes); free(newfl); return NULL; } newfl->client_cookie.n_len = matchcookie->n_len; bcopy(matchcookie->n_bytes, newfl->client_cookie.n_bytes, matchcookie->n_len); newfl->addr = malloc(addr->sa_len); if (newfl->addr == NULL) { free(newfl->client_cookie.n_bytes); free(newfl->client.oh.n_bytes); free(newfl); return NULL; } memcpy(newfl->addr, addr, addr->sa_len); return newfl; } /* * file_file_lock: Force creation of a valid file lock */ void fill_file_lock(struct file_lock *fl, const fhandle_t *fh, const bool_t exclusive, const int32_t svid, const u_int64_t offset, const u_int64_t len, const int state, const int status, const int flags, const int blocking) { bcopy(fh, &fl->filehandle, sizeof(fhandle_t)); fl->client.exclusive = exclusive; fl->client.svid = svid; fl->client.l_offset = offset; fl->client.l_len = len; fl->nsm_status = state; fl->status = status; fl->flags = flags; fl->blocking = blocking; } /* * deallocate_file_lock: Free all storage associated with a file lock */ void deallocate_file_lock(struct file_lock *fl) { free(fl->addr); free(fl->client.oh.n_bytes); free(fl->client_cookie.n_bytes); free(fl); } /* * regions_overlap(): This function examines the two provided regions for * overlap. */ int regions_overlap(start1, len1, start2, len2) const u_int64_t start1, len1, start2, len2; { u_int64_t d1,d2,d3,d4; enum split_status result; debuglog("Entering region overlap with vals: %llu:%llu--%llu:%llu\n", start1, len1, start2, len2); result = region_compare(start1, len1, start2, len2, &d1, &d2, &d3, &d4); debuglog("Exiting region overlap with val: %d\n",result); if (result == SPL_DISJOINT) { return 0; } else { return 1; } } /* * region_compare(): Examine lock regions and split appropriately * * XXX: Fix 64 bit overflow problems * XXX: Check to make sure I got *ALL* the cases. * XXX: This DESPERATELY needs a regression test. */ enum split_status region_compare(starte, lene, startu, lenu, start1, len1, start2, len2) const u_int64_t starte, lene, startu, lenu; u_int64_t *start1, *len1, *start2, *len2; { /* * Please pay attention to the sequential exclusions * of the if statements!!! */ enum LFLAGS lflags; enum RFLAGS rflags; enum split_status retval; retval = SPL_DISJOINT; if (lene == 0 && lenu == 0) { /* Examine left edge of locker */ lflags = LEDGE_INSIDE; if (startu < starte) { lflags = LEDGE_LEFT; } else if (startu == starte) { lflags = LEDGE_LBOUNDARY; } rflags = REDGE_RBOUNDARY; /* Both are infiinite */ if (lflags == LEDGE_INSIDE) { *start1 = starte; *len1 = startu - starte; } if (lflags == LEDGE_LEFT || lflags == LEDGE_LBOUNDARY) { retval = SPL_CONTAINED; } else { retval = SPL_LOCK1; } } else if (lene == 0 && lenu != 0) { /* Established lock is infinite */ /* Examine left edge of unlocker */ lflags = LEDGE_INSIDE; if (startu < starte) { lflags = LEDGE_LEFT; } else if (startu == starte) { lflags = LEDGE_LBOUNDARY; } /* Examine right edge of unlocker */ if (startu + lenu < starte) { /* Right edge of unlocker left of established lock */ rflags = REDGE_LEFT; return SPL_DISJOINT; } else if (startu + lenu == starte) { /* Right edge of unlocker on start of established lock */ rflags = REDGE_LBOUNDARY; return SPL_DISJOINT; } else { /* Infinifty is right of finity */ /* Right edge of unlocker inside established lock */ rflags = REDGE_INSIDE; } if (lflags == LEDGE_INSIDE) { *start1 = starte; *len1 = startu - starte; retval |= SPL_LOCK1; } if (rflags == REDGE_INSIDE) { /* Create right lock */ *start2 = startu+lenu; *len2 = 0; retval |= SPL_LOCK2; } } else if (lene != 0 && lenu == 0) { /* Unlocker is infinite */ /* Examine left edge of unlocker */ lflags = LEDGE_RIGHT; if (startu < starte) { lflags = LEDGE_LEFT; retval = SPL_CONTAINED; return retval; } else if (startu == starte) { lflags = LEDGE_LBOUNDARY; retval = SPL_CONTAINED; return retval; } else if ((startu > starte) && (startu < starte + lene - 1)) { lflags = LEDGE_INSIDE; } else if (startu == starte + lene - 1) { lflags = LEDGE_RBOUNDARY; } else { /* startu > starte + lene -1 */ lflags = LEDGE_RIGHT; return SPL_DISJOINT; } rflags = REDGE_RIGHT; /* Infinity is right of finity */ if (lflags == LEDGE_INSIDE || lflags == LEDGE_RBOUNDARY) { *start1 = starte; *len1 = startu - starte; retval |= SPL_LOCK1; return retval; } } else { /* Both locks are finite */ /* Examine left edge of unlocker */ lflags = LEDGE_RIGHT; if (startu < starte) { lflags = LEDGE_LEFT; } else if (startu == starte) { lflags = LEDGE_LBOUNDARY; } else if ((startu > starte) && (startu < starte + lene - 1)) { lflags = LEDGE_INSIDE; } else if (startu == starte + lene - 1) { lflags = LEDGE_RBOUNDARY; } else { /* startu > starte + lene -1 */ lflags = LEDGE_RIGHT; return SPL_DISJOINT; } /* Examine right edge of unlocker */ if (startu + lenu < starte) { /* Right edge of unlocker left of established lock */ rflags = REDGE_LEFT; return SPL_DISJOINT; } else if (startu + lenu == starte) { /* Right edge of unlocker on start of established lock */ rflags = REDGE_LBOUNDARY; return SPL_DISJOINT; } else if (startu + lenu < starte + lene) { /* Right edge of unlocker inside established lock */ rflags = REDGE_INSIDE; } else if (startu + lenu == starte + lene) { /* Right edge of unlocker on right edge of established lock */ rflags = REDGE_RBOUNDARY; } else { /* startu + lenu > starte + lene */ /* Right edge of unlocker is right of established lock */ rflags = REDGE_RIGHT; } if (lflags == LEDGE_INSIDE || lflags == LEDGE_RBOUNDARY) { /* Create left lock */ *start1 = starte; *len1 = (startu - starte); retval |= SPL_LOCK1; } if (rflags == REDGE_INSIDE) { /* Create right lock */ *start2 = startu+lenu; *len2 = starte+lene-(startu+lenu); retval |= SPL_LOCK2; } if ((lflags == LEDGE_LEFT || lflags == LEDGE_LBOUNDARY) && (rflags == REDGE_RBOUNDARY || rflags == REDGE_RIGHT)) { retval = SPL_CONTAINED; } } return retval; } /* * same_netobj: Compares the apprpriate bits of a netobj for identity */ int same_netobj(const netobj *n0, const netobj *n1) { int retval; retval = 0; debuglog("Entering netobj identity check\n"); if (n0->n_len == n1->n_len) { debuglog("Preliminary length check passed\n"); retval = !bcmp(n0->n_bytes, n1->n_bytes, n0->n_len); debuglog("netobj %smatch\n", retval ? "" : "mis"); } return (retval); } /* * same_filelock_identity: Compares the appropriate bits of a file_lock */ int same_filelock_identity(fl0, fl1) const struct file_lock *fl0, *fl1; { int retval; retval = 0; debuglog("Checking filelock identity\n"); /* * Check process ids and host information. */ retval = (fl0->client.svid == fl1->client.svid && same_netobj(&(fl0->client.oh), &(fl1->client.oh))); debuglog("Exiting checking filelock identity: retval: %d\n",retval); return (retval); } /* * Below here are routines associated with manipulating the NFS * lock list. */ /* * get_lock_matching_unlock: Return a lock which matches the given unlock lock * or NULL otehrwise * XXX: It is a shame that this duplicates so much code from test_nfslock. */ struct file_lock * get_lock_matching_unlock(const struct file_lock *fl) { struct file_lock *ifl; /* Iterator */ debuglog("Entering get_lock_matching_unlock\n"); debuglog("********Dump of fl*****************\n"); dump_filelock(fl); LIST_FOREACH(ifl, &nfslocklist_head, nfslocklist) { debuglog("Pointer to file lock: %p\n",ifl); debuglog("****Dump of ifl****\n"); dump_filelock(ifl); debuglog("*******************\n"); /* * XXX: It is conceivable that someone could use the NLM RPC * system to directly access filehandles. This may be a * security hazard as the filehandle code may bypass normal * file access controls */ if (bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t))) continue; debuglog("get_lock_matching_unlock: Filehandles match, " "checking regions\n"); /* Filehandles match, check for region overlap */ if (!regions_overlap(fl->client.l_offset, fl->client.l_len, ifl->client.l_offset, ifl->client.l_len)) continue; debuglog("get_lock_matching_unlock: Region overlap" " found %llu : %llu -- %llu : %llu\n", fl->client.l_offset,fl->client.l_len, ifl->client.l_offset,ifl->client.l_len); /* Regions overlap, check the identity */ if (!same_filelock_identity(fl,ifl)) continue; debuglog("get_lock_matching_unlock: Duplicate lock id. Granting\n"); return (ifl); } debuglog("Exiting bet_lock_matching_unlock\n"); return (NULL); } /* * test_nfslock: check for NFS lock in lock list * * This routine makes the following assumptions: * 1) Nothing will adjust the lock list during a lookup * * This routine has an intersting quirk which bit me hard. * The conflicting_fl is the pointer to the conflicting lock. * However, to modify the "*pointer* to the conflicting lock" rather * that the "conflicting lock itself" one must pass in a "pointer to * the pointer of the conflicting lock". Gross. */ enum nfslock_status test_nfslock(const struct file_lock *fl, struct file_lock **conflicting_fl) { struct file_lock *ifl; /* Iterator */ enum nfslock_status retval; debuglog("Entering test_nfslock\n"); retval = NFS_GRANTED; (*conflicting_fl) = NULL; debuglog("Entering lock search loop\n"); debuglog("***********************************\n"); debuglog("Dumping match filelock\n"); debuglog("***********************************\n"); dump_filelock(fl); debuglog("***********************************\n"); LIST_FOREACH(ifl, &nfslocklist_head, nfslocklist) { if (retval == NFS_DENIED) break; debuglog("Top of lock loop\n"); debuglog("Pointer to file lock: %p\n",ifl); debuglog("***********************************\n"); debuglog("Dumping test filelock\n"); debuglog("***********************************\n"); dump_filelock(ifl); debuglog("***********************************\n"); /* * XXX: It is conceivable that someone could use the NLM RPC * system to directly access filehandles. This may be a * security hazard as the filehandle code may bypass normal * file access controls */ if (bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t))) continue; debuglog("test_nfslock: filehandle match found\n"); /* Filehandles match, check for region overlap */ if (!regions_overlap(fl->client.l_offset, fl->client.l_len, ifl->client.l_offset, ifl->client.l_len)) continue; debuglog("test_nfslock: Region overlap found" " %llu : %llu -- %llu : %llu\n", fl->client.l_offset,fl->client.l_len, ifl->client.l_offset,ifl->client.l_len); /* Regions overlap, check the exclusivity */ if (!(fl->client.exclusive || ifl->client.exclusive)) continue; debuglog("test_nfslock: Exclusivity failure: %d %d\n", fl->client.exclusive, ifl->client.exclusive); if (same_filelock_identity(fl,ifl)) { debuglog("test_nfslock: Duplicate id. Granting\n"); (*conflicting_fl) = ifl; retval = NFS_GRANTED_DUPLICATE; } else { /* locking attempt fails */ debuglog("test_nfslock: Lock attempt failed\n"); debuglog("Desired lock\n"); dump_filelock(fl); debuglog("Conflicting lock\n"); dump_filelock(ifl); (*conflicting_fl) = ifl; retval = NFS_DENIED; } } debuglog("Dumping file locks\n"); debuglog("Exiting test_nfslock\n"); return (retval); } /* * lock_nfslock: attempt to create a lock in the NFS lock list * * This routine tests whether the lock will be granted and then adds * the entry to the lock list if so. * * Argument fl gets modified as its list housekeeping entries get modified * upon insertion into the NFS lock list * * This routine makes several assumptions: * 1) It is perfectly happy to grant a duplicate lock from the same pid. * While this seems to be intuitively wrong, it is required for proper * Posix semantics during unlock. It is absolutely imperative to not * unlock the main lock before the two child locks are established. Thus, - * one has be be able to create duplicate locks over an existing lock + * one has to be able to create duplicate locks over an existing lock * 2) It currently accepts duplicate locks from the same id,pid */ enum nfslock_status lock_nfslock(struct file_lock *fl) { enum nfslock_status retval; struct file_lock *dummy_fl; dummy_fl = NULL; debuglog("Entering lock_nfslock...\n"); retval = test_nfslock(fl,&dummy_fl); if (retval == NFS_GRANTED || retval == NFS_GRANTED_DUPLICATE) { debuglog("Inserting lock...\n"); dump_filelock(fl); LIST_INSERT_HEAD(&nfslocklist_head, fl, nfslocklist); } debuglog("Exiting lock_nfslock...\n"); return (retval); } /* * delete_nfslock: delete an NFS lock list entry * * This routine is used to delete a lock out of the NFS lock list * without regard to status, underlying locks, regions or anything else * * Note that this routine *does not deallocate memory* of the lock. * It just disconnects it from the list. The lock can then be used * by other routines without fear of trashing the list. */ enum nfslock_status delete_nfslock(struct file_lock *fl) { LIST_REMOVE(fl, nfslocklist); return (NFS_GRANTED); } enum split_status split_nfslock(exist_lock, unlock_lock, left_lock, right_lock) const struct file_lock *exist_lock, *unlock_lock; struct file_lock **left_lock, **right_lock; { u_int64_t start1, len1, start2, len2; enum split_status spstatus; spstatus = region_compare(exist_lock->client.l_offset, exist_lock->client.l_len, unlock_lock->client.l_offset, unlock_lock->client.l_len, &start1, &len1, &start2, &len2); if ((spstatus & SPL_LOCK1) != 0) { *left_lock = allocate_file_lock(&exist_lock->client.oh, &exist_lock->client_cookie, exist_lock->addr, exist_lock->client_name); if (*left_lock == NULL) { debuglog("Unable to allocate resource for split 1\n"); return SPL_RESERR; } fill_file_lock(*left_lock, &exist_lock->filehandle, exist_lock->client.exclusive, exist_lock->client.svid, start1, len1, exist_lock->nsm_status, exist_lock->status, exist_lock->flags, exist_lock->blocking); } if ((spstatus & SPL_LOCK2) != 0) { *right_lock = allocate_file_lock(&exist_lock->client.oh, &exist_lock->client_cookie, exist_lock->addr, exist_lock->client_name); if (*right_lock == NULL) { debuglog("Unable to allocate resource for split 1\n"); if (*left_lock != NULL) { deallocate_file_lock(*left_lock); } return SPL_RESERR; } fill_file_lock(*right_lock, &exist_lock->filehandle, exist_lock->client.exclusive, exist_lock->client.svid, start2, len2, exist_lock->nsm_status, exist_lock->status, exist_lock->flags, exist_lock->blocking); } return spstatus; } enum nfslock_status unlock_nfslock(fl, released_lock, left_lock, right_lock) const struct file_lock *fl; struct file_lock **released_lock; struct file_lock **left_lock; struct file_lock **right_lock; { struct file_lock *mfl; /* Matching file lock */ enum nfslock_status retval; enum split_status spstatus; debuglog("Entering unlock_nfslock\n"); *released_lock = NULL; *left_lock = NULL; *right_lock = NULL; retval = NFS_DENIED_NOLOCK; debuglog("Attempting to match lock...\n"); mfl = get_lock_matching_unlock(fl); if (mfl != NULL) { debuglog("Unlock matched. Querying for split\n"); spstatus = split_nfslock(mfl, fl, left_lock, right_lock); debuglog("Split returned %d %p %p %p %p\n",spstatus,mfl,fl,*left_lock,*right_lock); debuglog("********Split dumps********"); dump_filelock(mfl); dump_filelock(fl); dump_filelock(*left_lock); dump_filelock(*right_lock); debuglog("********End Split dumps********"); if (spstatus == SPL_RESERR) { if (*left_lock != NULL) { deallocate_file_lock(*left_lock); *left_lock = NULL; } if (*right_lock != NULL) { deallocate_file_lock(*right_lock); *right_lock = NULL; } return NFS_RESERR; } /* Insert new locks from split if required */ if (*left_lock != NULL) { debuglog("Split left activated\n"); LIST_INSERT_HEAD(&nfslocklist_head, *left_lock, nfslocklist); } if (*right_lock != NULL) { debuglog("Split right activated\n"); LIST_INSERT_HEAD(&nfslocklist_head, *right_lock, nfslocklist); } /* Unlock the lock since it matches identity */ LIST_REMOVE(mfl, nfslocklist); *released_lock = mfl; retval = NFS_GRANTED; } debuglog("Exiting unlock_nfslock\n"); return retval; } /* * Below here are the routines for manipulating the file lock directly * on the disk hardware itself */ enum hwlock_status lock_hwlock(struct file_lock *fl) { struct monfile *imf,*nmf; int lflags, flerror; /* Scan to see if filehandle already present */ LIST_FOREACH(imf, &monfilelist_head, monfilelist) { if (bcmp(&fl->filehandle, &imf->filehandle, sizeof(fl->filehandle)) == 0) { /* imf is the correct filehandle */ break; } } /* * Filehandle already exists (we control the file) * *AND* NFS has already cleared the lock for availability * Grant it and bump the refcount. */ if (imf != NULL) { ++(imf->refcount); return (HW_GRANTED); } /* No filehandle found, create and go */ nmf = malloc(sizeof(struct monfile)); if (nmf == NULL) { debuglog("hwlock resource allocation failure\n"); return (HW_RESERR); } /* XXX: Is O_RDWR always the correct mode? */ nmf->fd = fhopen(&fl->filehandle, O_RDWR); if (nmf->fd < 0) { debuglog("fhopen failed (from %16s): %32s\n", fl->client_name, strerror(errno)); free(nmf); switch (errno) { case ESTALE: return (HW_STALEFH); case EROFS: return (HW_READONLY); default: return (HW_RESERR); } } /* File opened correctly, fill the monitor struct */ bcopy(&fl->filehandle, &nmf->filehandle, sizeof(fl->filehandle)); nmf->refcount = 1; nmf->exclusive = fl->client.exclusive; lflags = (nmf->exclusive == 1) ? (LOCK_EX | LOCK_NB) : (LOCK_SH | LOCK_NB); flerror = flock(nmf->fd, lflags); if (flerror != 0) { debuglog("flock failed (from %16s): %32s\n", fl->client_name, strerror(errno)); close(nmf->fd); free(nmf); switch (errno) { case EAGAIN: return (HW_DENIED); case ESTALE: return (HW_STALEFH); case EROFS: return (HW_READONLY); default: return (HW_RESERR); break; } } /* File opened and locked */ LIST_INSERT_HEAD(&monfilelist_head, nmf, monfilelist); debuglog("flock succeeded (from %16s)\n", fl->client_name); return (HW_GRANTED); } enum hwlock_status unlock_hwlock(const struct file_lock *fl) { struct monfile *imf; debuglog("Entering unlock_hwlock\n"); debuglog("Entering loop interation\n"); /* Scan to see if filehandle already present */ LIST_FOREACH(imf, &monfilelist_head, monfilelist) { if (bcmp(&fl->filehandle, &imf->filehandle, sizeof(fl->filehandle)) == 0) { /* imf is the correct filehandle */ break; } } debuglog("Completed iteration. Proceeding\n"); if (imf == NULL) { /* No lock found */ debuglog("Exiting unlock_hwlock (HW_DENIED_NOLOCK)\n"); return (HW_DENIED_NOLOCK); } /* Lock found */ --imf->refcount; if (imf->refcount < 0) { debuglog("Negative hardware reference count\n"); } if (imf->refcount <= 0) { close(imf->fd); LIST_REMOVE(imf, monfilelist); free(imf); } debuglog("Exiting unlock_hwlock (HW_GRANTED)\n"); return (HW_GRANTED); } enum hwlock_status test_hwlock(fl, conflicting_fl) const struct file_lock *fl __unused; struct file_lock **conflicting_fl __unused; { /* * XXX: lock tests on hardware are not required until * true partial file testing is done on the underlying file */ return (HW_RESERR); } /* * Below here are routines for manipulating blocked lock requests * They should only be called from the XXX_partialfilelock routines * if at all possible */ int duplicate_block(struct file_lock *fl) { struct file_lock *ifl; int retval = 0; debuglog("Entering duplicate_block"); /* * Is this lock request already on the blocking list? * Consider it a dupe if the file handles, offset, length, * exclusivity and client match. */ LIST_FOREACH(ifl, &blockedlocklist_head, nfslocklist) { if (!bcmp(&fl->filehandle, &ifl->filehandle, sizeof(fhandle_t)) && fl->client.exclusive == ifl->client.exclusive && fl->client.l_offset == ifl->client.l_offset && fl->client.l_len == ifl->client.l_len && same_filelock_identity(fl, ifl)) { retval = 1; break; } } debuglog("Exiting duplicate_block: %s\n", retval ? "already blocked" : "not already blocked"); return retval; } void add_blockingfilelock(struct file_lock *fl) { debuglog("Entering add_blockingfilelock\n"); /* * A blocking lock request _should_ never be duplicated as a client * that is already blocked shouldn't be able to request another * lock. Alas, there are some buggy clients that do request the same * lock repeatedly. Make sure only unique locks are on the blocked * lock list. */ if (duplicate_block(fl)) { debuglog("Exiting add_blockingfilelock: already blocked\n"); return; } /* * Clear the blocking flag so that it can be reused without * adding it to the blocking queue a second time */ fl->blocking = 0; LIST_INSERT_HEAD(&blockedlocklist_head, fl, nfslocklist); debuglog("Exiting add_blockingfilelock: added blocked lock\n"); } void remove_blockingfilelock(struct file_lock *fl) { debuglog("Entering remove_blockingfilelock\n"); LIST_REMOVE(fl, nfslocklist); debuglog("Exiting remove_blockingfilelock\n"); } void clear_blockingfilelock(const char *hostname) { struct file_lock *ifl,*nfl; /* * Normally, LIST_FOREACH is called for, but since * the current element *is* the iterator, deleting it * would mess up the iteration. Thus, a next element * must be used explicitly */ ifl = LIST_FIRST(&blockedlocklist_head); while (ifl != NULL) { nfl = LIST_NEXT(ifl, nfslocklist); if (strncmp(hostname, ifl->client_name, SM_MAXSTRLEN) == 0) { remove_blockingfilelock(ifl); deallocate_file_lock(ifl); } ifl = nfl; } } void retry_blockingfilelocklist(void) { /* Retry all locks in the blocked list */ struct file_lock *ifl, *nfl; /* Iterator */ enum partialfilelock_status pflstatus; debuglog("Entering retry_blockingfilelocklist\n"); LIST_FOREACH_SAFE(ifl, &blockedlocklist_head, nfslocklist, nfl) { debuglog("Iterator choice %p\n",ifl); debuglog("Next iterator choice %p\n",nfl); /* * SUBTLE BUG: The file_lock must be removed from the * old list so that it's list pointers get disconnected * before being allowed to participate in the new list * which will automatically add it in if necessary. */ LIST_REMOVE(ifl, nfslocklist); pflstatus = lock_partialfilelock(ifl); if (pflstatus == PFL_GRANTED || pflstatus == PFL_GRANTED_DUPLICATE) { debuglog("Granted blocked lock\n"); /* lock granted and is now being used */ send_granted(ifl,0); } else { /* Reinsert lock back into blocked list */ debuglog("Replacing blocked lock\n"); LIST_INSERT_HEAD(&blockedlocklist_head, ifl, nfslocklist); } } debuglog("Exiting retry_blockingfilelocklist\n"); } /* * Below here are routines associated with manipulating all * aspects of the partial file locking system (list, hardware, etc.) */ /* * Please note that lock monitoring must be done at this level which * keeps track of *individual* lock requests on lock and unlock * * XXX: Split unlocking is going to make the unlock code miserable */ /* * lock_partialfilelock: * * Argument fl gets modified as its list housekeeping entries get modified * upon insertion into the NFS lock list * * This routine makes several assumptions: * 1) It (will) pass locks through to flock to lock the entire underlying file * and then parcel out NFS locks if it gets control of the file. * This matches the old rpc.lockd file semantics (except where it * is now more correct). It is the safe solution, but will cause * overly restrictive blocking if someone is trying to use the * underlying files without using NFS. This appears to be an * acceptable tradeoff since most people use standalone NFS servers. * XXX: The right solution is probably kevent combined with fcntl * * 2) Nothing modifies the lock lists between testing and granting * I have no idea whether this is a useful assumption or not */ enum partialfilelock_status lock_partialfilelock(struct file_lock *fl) { enum partialfilelock_status retval; enum nfslock_status lnlstatus; enum hwlock_status hwstatus; debuglog("Entering lock_partialfilelock\n"); retval = PFL_DENIED; /* * Execute the NFS lock first, if possible, as it is significantly * easier and less expensive to undo than the filesystem lock */ lnlstatus = lock_nfslock(fl); switch (lnlstatus) { case NFS_GRANTED: case NFS_GRANTED_DUPLICATE: /* * At this point, the NFS lock is allocated and active. * Remember to clean it up if the hardware lock fails */ hwstatus = lock_hwlock(fl); switch (hwstatus) { case HW_GRANTED: case HW_GRANTED_DUPLICATE: debuglog("HW GRANTED\n"); /* * XXX: Fixme: Check hwstatus for duplicate when * true partial file locking and accounting is * done on the hardware. */ if (lnlstatus == NFS_GRANTED_DUPLICATE) { retval = PFL_GRANTED_DUPLICATE; } else { retval = PFL_GRANTED; } monitor_lock_host(fl->client_name); break; case HW_RESERR: debuglog("HW RESERR\n"); retval = PFL_HWRESERR; break; case HW_DENIED: debuglog("HW DENIED\n"); retval = PFL_HWDENIED; break; default: debuglog("Unmatched hwstatus %d\n",hwstatus); break; } if (retval != PFL_GRANTED && retval != PFL_GRANTED_DUPLICATE) { /* Clean up the NFS lock */ debuglog("Deleting trial NFS lock\n"); delete_nfslock(fl); } break; case NFS_DENIED: retval = PFL_NFSDENIED; break; case NFS_RESERR: retval = PFL_NFSRESERR; break; default: debuglog("Unmatched lnlstatus %d\n"); retval = PFL_NFSDENIED_NOLOCK; break; } /* * By the time fl reaches here, it is completely free again on * failure. The NFS lock done before attempting the * hardware lock has been backed out */ if (retval == PFL_NFSDENIED || retval == PFL_HWDENIED) { /* Once last chance to check the lock */ if (fl->blocking == 1) { if (retval == PFL_NFSDENIED) { /* Queue the lock */ debuglog("BLOCKING LOCK RECEIVED\n"); retval = PFL_NFSBLOCKED; add_blockingfilelock(fl); dump_filelock(fl); } else { /* retval is okay as PFL_HWDENIED */ debuglog("BLOCKING LOCK DENIED IN HARDWARE\n"); dump_filelock(fl); } } else { /* Leave retval alone, it's already correct */ debuglog("Lock denied. Non-blocking failure\n"); dump_filelock(fl); } } debuglog("Exiting lock_partialfilelock\n"); return retval; } /* * unlock_partialfilelock: * * Given a file_lock, unlock all locks which match. * * Note that a given lock might have to unlock ITSELF! See * clear_partialfilelock for example. */ enum partialfilelock_status unlock_partialfilelock(const struct file_lock *fl) { struct file_lock *lfl,*rfl,*releasedfl,*selffl; enum partialfilelock_status retval; enum nfslock_status unlstatus; enum hwlock_status unlhwstatus, lhwstatus; debuglog("Entering unlock_partialfilelock\n"); selffl = NULL; lfl = NULL; rfl = NULL; releasedfl = NULL; retval = PFL_DENIED; /* * There are significant overlap and atomicity issues * with partially releasing a lock. For example, releasing * part of an NFS shared lock does *not* always release the * corresponding part of the file since there is only one * rpc.lockd UID but multiple users could be requesting it * from NFS. Also, an unlock request should never allow * another process to gain a lock on the remaining parts. * ie. Always apply the new locks before releasing the * old one */ /* * Loop is required since multiple little locks * can be allocated and then deallocated with one * big unlock. * * The loop is required to be here so that the nfs & * hw subsystems do not need to communicate with one * one another */ do { debuglog("Value of releasedfl: %p\n",releasedfl); /* lfl&rfl are created *AND* placed into the NFS lock list if required */ unlstatus = unlock_nfslock(fl, &releasedfl, &lfl, &rfl); debuglog("Value of releasedfl: %p\n",releasedfl); /* XXX: This is grungy. It should be refactored to be cleaner */ if (lfl != NULL) { lhwstatus = lock_hwlock(lfl); if (lhwstatus != HW_GRANTED && lhwstatus != HW_GRANTED_DUPLICATE) { debuglog("HW duplicate lock failure for left split\n"); } monitor_lock_host(lfl->client_name); } if (rfl != NULL) { lhwstatus = lock_hwlock(rfl); if (lhwstatus != HW_GRANTED && lhwstatus != HW_GRANTED_DUPLICATE) { debuglog("HW duplicate lock failure for right split\n"); } monitor_lock_host(rfl->client_name); } switch (unlstatus) { case NFS_GRANTED: /* Attempt to unlock on the hardware */ debuglog("NFS unlock granted. Attempting hardware unlock\n"); /* This call *MUST NOT* unlock the two newly allocated locks */ unlhwstatus = unlock_hwlock(fl); debuglog("HW unlock returned with code %d\n",unlhwstatus); switch (unlhwstatus) { case HW_GRANTED: debuglog("HW unlock granted\n"); unmonitor_lock_host(releasedfl->client_name); retval = PFL_GRANTED; break; case HW_DENIED_NOLOCK: /* Huh?!?! This shouldn't happen */ debuglog("HW unlock denied no lock\n"); retval = PFL_HWRESERR; /* Break out of do-while */ unlstatus = NFS_RESERR; break; default: debuglog("HW unlock failed\n"); retval = PFL_HWRESERR; /* Break out of do-while */ unlstatus = NFS_RESERR; break; } debuglog("Exiting with status retval: %d\n",retval); retry_blockingfilelocklist(); break; case NFS_DENIED_NOLOCK: retval = PFL_GRANTED; debuglog("All locks cleaned out\n"); break; default: retval = PFL_NFSRESERR; debuglog("NFS unlock failure\n"); dump_filelock(fl); break; } if (releasedfl != NULL) { if (fl == releasedfl) { /* * XXX: YECHHH!!! Attempt to unlock self succeeded * but we can't deallocate the space yet. This is what * happens when you don't write malloc and free together */ debuglog("Attempt to unlock self\n"); selffl = releasedfl; } else { /* * XXX: this deallocation *still* needs to migrate closer * to the allocation code way up in get_lock or the allocation * code needs to migrate down (violation of "When you write * malloc you must write free") */ deallocate_file_lock(releasedfl); releasedfl = NULL; } } } while (unlstatus == NFS_GRANTED); if (selffl != NULL) { /* * This statement wipes out the incoming file lock (fl) * in spite of the fact that it is declared const */ debuglog("WARNING! Destroying incoming lock pointer\n"); deallocate_file_lock(selffl); } debuglog("Exiting unlock_partialfilelock\n"); return retval; } /* * clear_partialfilelock * * Normally called in response to statd state number change. * Wipe out all locks held by a host. As a bonus, the act of * doing so should automatically clear their statd entries and * unmonitor the host. */ void clear_partialfilelock(const char *hostname) { struct file_lock *ifl, *nfl; /* Clear blocking file lock list */ clear_blockingfilelock(hostname); /* do all required unlocks */ /* Note that unlock can smash the current pointer to a lock */ /* * Normally, LIST_FOREACH is called for, but since * the current element *is* the iterator, deleting it * would mess up the iteration. Thus, a next element * must be used explicitly */ ifl = LIST_FIRST(&nfslocklist_head); while (ifl != NULL) { nfl = LIST_NEXT(ifl, nfslocklist); if (strncmp(hostname, ifl->client_name, SM_MAXSTRLEN) == 0) { /* Unlock destroys ifl out from underneath */ unlock_partialfilelock(ifl); /* ifl is NO LONGER VALID AT THIS POINT */ } ifl = nfl; } } /* * test_partialfilelock: */ enum partialfilelock_status test_partialfilelock(const struct file_lock *fl, struct file_lock **conflicting_fl) { enum partialfilelock_status retval; enum nfslock_status teststatus; debuglog("Entering testpartialfilelock...\n"); retval = PFL_DENIED; teststatus = test_nfslock(fl, conflicting_fl); debuglog("test_partialfilelock: teststatus %d\n",teststatus); if (teststatus == NFS_GRANTED || teststatus == NFS_GRANTED_DUPLICATE) { /* XXX: Add the underlying filesystem locking code */ retval = (teststatus == NFS_GRANTED) ? PFL_GRANTED : PFL_GRANTED_DUPLICATE; debuglog("Dumping locks...\n"); dump_filelock(fl); dump_filelock(*conflicting_fl); debuglog("Done dumping locks...\n"); } else { retval = PFL_NFSDENIED; debuglog("NFS test denied.\n"); dump_filelock(fl); debuglog("Conflicting.\n"); dump_filelock(*conflicting_fl); } debuglog("Exiting testpartialfilelock...\n"); return retval; } /* * Below here are routines associated with translating the partial file locking * codes into useful codes to send back to the NFS RPC messaging system */ /* * These routines translate the (relatively) useful return codes back onto * the few return codes which the nlm subsystems wishes to trasmit */ enum nlm_stats do_test(struct file_lock *fl, struct file_lock **conflicting_fl) { enum partialfilelock_status pfsret; enum nlm_stats retval; debuglog("Entering do_test...\n"); pfsret = test_partialfilelock(fl,conflicting_fl); switch (pfsret) { case PFL_GRANTED: debuglog("PFL test lock granted\n"); dump_filelock(fl); dump_filelock(*conflicting_fl); retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; break; case PFL_GRANTED_DUPLICATE: debuglog("PFL test lock granted--duplicate id detected\n"); dump_filelock(fl); dump_filelock(*conflicting_fl); debuglog("Clearing conflicting_fl for call semantics\n"); *conflicting_fl = NULL; retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; break; case PFL_NFSDENIED: case PFL_HWDENIED: debuglog("PFL test lock denied\n"); dump_filelock(fl); dump_filelock(*conflicting_fl); retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied; break; case PFL_NFSRESERR: case PFL_HWRESERR: debuglog("PFL test lock resource fail\n"); dump_filelock(fl); dump_filelock(*conflicting_fl); retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; break; default: debuglog("PFL test lock *FAILED*\n"); dump_filelock(fl); dump_filelock(*conflicting_fl); retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied; break; } debuglog("Exiting do_test...\n"); return retval; } /* * do_lock: Try to acquire a lock * * This routine makes a distinction between NLM versions. I am pretty * convinced that this should be abstracted out and bounced up a level */ enum nlm_stats do_lock(struct file_lock *fl) { enum partialfilelock_status pfsret; enum nlm_stats retval; debuglog("Entering do_lock...\n"); pfsret = lock_partialfilelock(fl); switch (pfsret) { case PFL_GRANTED: debuglog("PFL lock granted"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; break; case PFL_GRANTED_DUPLICATE: debuglog("PFL lock granted--duplicate id detected"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; break; case PFL_NFSDENIED: case PFL_HWDENIED: debuglog("PFL_NFS lock denied"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied; break; case PFL_NFSBLOCKED: case PFL_HWBLOCKED: debuglog("PFL_NFS blocking lock denied. Queued.\n"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_blocked : nlm_blocked; break; case PFL_NFSRESERR: case PFL_HWRESERR: debuglog("PFL lock resource alocation fail\n"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; break; default: debuglog("PFL lock *FAILED*"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied; break; } debuglog("Exiting do_lock...\n"); return retval; } enum nlm_stats do_unlock(struct file_lock *fl) { enum partialfilelock_status pfsret; enum nlm_stats retval; debuglog("Entering do_unlock...\n"); pfsret = unlock_partialfilelock(fl); switch (pfsret) { case PFL_GRANTED: debuglog("PFL unlock granted"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; break; case PFL_NFSDENIED: case PFL_HWDENIED: debuglog("PFL_NFS unlock denied"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_denied : nlm_denied; break; case PFL_NFSDENIED_NOLOCK: case PFL_HWDENIED_NOLOCK: debuglog("PFL_NFS no lock found\n"); retval = (fl->flags & LOCK_V4) ? nlm4_granted : nlm_granted; break; case PFL_NFSRESERR: case PFL_HWRESERR: debuglog("PFL unlock resource failure"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; break; default: debuglog("PFL unlock *FAILED*"); dump_filelock(fl); retval = (fl->flags & LOCK_V4) ? nlm4_failed : nlm_denied; break; } debuglog("Exiting do_unlock...\n"); return retval; } /* * do_clear * * This routine is non-existent because it doesn't have a return code. * It is here for completeness in case someone *does* need to do return * codes later. A decent compiler should optimize this away. */ void do_clear(const char *hostname) { clear_partialfilelock(hostname); } /* * The following routines are all called from the code which the * RPC layer invokes */ /* * testlock(): inform the caller if the requested lock would be granted * * returns NULL if lock would granted * returns pointer to a conflicting nlm4_holder if not */ struct nlm4_holder * testlock(struct nlm4_lock *lock, bool_t exclusive, int flags __unused) { struct file_lock test_fl, *conflicting_fl; bzero(&test_fl, sizeof(test_fl)); bcopy(lock->fh.n_bytes, &(test_fl.filehandle), sizeof(fhandle_t)); copy_nlm4_lock_to_nlm4_holder(lock, exclusive, &test_fl.client); siglock(); do_test(&test_fl, &conflicting_fl); if (conflicting_fl == NULL) { debuglog("No conflicting lock found\n"); sigunlock(); return NULL; } else { debuglog("Found conflicting lock\n"); dump_filelock(conflicting_fl); sigunlock(); return (&conflicting_fl->client); } } /* * getlock: try to acquire the lock. * If file is already locked and we can sleep, put the lock in the list with * status LKST_WAITING; it'll be processed later. * Otherwise try to lock. If we're allowed to block, fork a child which * will do the blocking lock. */ enum nlm_stats getlock(nlm4_lockargs *lckarg, struct svc_req *rqstp, const int flags) { struct file_lock *newfl; enum nlm_stats retval; debuglog("Entering getlock...\n"); if (grace_expired == 0 && lckarg->reclaim == 0) return (flags & LOCK_V4) ? nlm4_denied_grace_period : nlm_denied_grace_period; /* allocate new file_lock for this request */ newfl = allocate_file_lock(&lckarg->alock.oh, &lckarg->cookie, (struct sockaddr *)svc_getrpccaller(rqstp->rq_xprt)->buf, lckarg->alock.caller_name); if (newfl == NULL) { syslog(LOG_NOTICE, "lock allocate failed: %s", strerror(errno)); /* failed */ return (flags & LOCK_V4) ? nlm4_denied_nolocks : nlm_denied_nolocks; } if (lckarg->alock.fh.n_len != sizeof(fhandle_t)) { debuglog("received fhandle size %d, local size %d", lckarg->alock.fh.n_len, (int)sizeof(fhandle_t)); } fill_file_lock(newfl, (fhandle_t *)lckarg->alock.fh.n_bytes, lckarg->exclusive, lckarg->alock.svid, lckarg->alock.l_offset, lckarg->alock.l_len, lckarg->state, 0, flags, lckarg->block); /* * newfl is now fully constructed and deallocate_file_lock * can now be used to delete it */ siglock(); debuglog("Pointer to new lock is %p\n",newfl); retval = do_lock(newfl); debuglog("Pointer to new lock is %p\n",newfl); sigunlock(); switch (retval) { case nlm4_granted: /* case nlm_granted: is the same as nlm4_granted */ /* do_mon(lckarg->alock.caller_name); */ break; case nlm4_blocked: /* case nlm_blocked: is the same as nlm4_blocked */ /* do_mon(lckarg->alock.caller_name); */ break; default: deallocate_file_lock(newfl); break; } debuglog("Exiting getlock...\n"); return retval; } /* unlock a filehandle */ enum nlm_stats unlock(nlm4_lock *lock, const int flags __unused) { struct file_lock fl; enum nlm_stats err; siglock(); debuglog("Entering unlock...\n"); bzero(&fl,sizeof(struct file_lock)); bcopy(lock->fh.n_bytes, &fl.filehandle, sizeof(fhandle_t)); copy_nlm4_lock_to_nlm4_holder(lock, 0, &fl.client); err = do_unlock(&fl); sigunlock(); debuglog("Exiting unlock...\n"); return err; } /* * XXX: The following monitor/unmonitor routines * have not been extensively tested (ie. no regression * script exists like for the locking sections */ /* * monitor_lock_host: monitor lock hosts locally with a ref count and * inform statd */ void monitor_lock_host(const char *hostname) { struct host *ihp, *nhp; struct mon smon; struct sm_stat_res sres; int rpcret, statflag; size_t n; rpcret = 0; statflag = 0; LIST_FOREACH(ihp, &hostlst_head, hostlst) { if (strncmp(hostname, ihp->name, SM_MAXSTRLEN) == 0) { /* Host is already monitored, bump refcount */ ++ihp->refcnt; /* Host should only be in the monitor list once */ return; } } /* Host is not yet monitored, add it */ n = strnlen(hostname, SM_MAXSTRLEN); if (n == SM_MAXSTRLEN) { return; } nhp = malloc(sizeof(*nhp) - sizeof(nhp->name) + n + 1); if (nhp == NULL) { debuglog("Unable to allocate entry for statd mon\n"); return; } /* Allocated new host entry, now fill the fields */ memcpy(nhp->name, hostname, n); nhp->name[n] = 0; nhp->refcnt = 1; debuglog("Locally Monitoring host %16s\n",hostname); debuglog("Attempting to tell statd\n"); bzero(&smon,sizeof(smon)); smon.mon_id.mon_name = nhp->name; smon.mon_id.my_id.my_name = "localhost"; smon.mon_id.my_id.my_prog = NLM_PROG; smon.mon_id.my_id.my_vers = NLM_SM; smon.mon_id.my_id.my_proc = NLM_SM_NOTIFY; rpcret = callrpc("localhost", SM_PROG, SM_VERS, SM_MON, (xdrproc_t)xdr_mon, &smon, (xdrproc_t)xdr_sm_stat_res, &sres); if (rpcret == 0) { if (sres.res_stat == stat_fail) { debuglog("Statd call failed\n"); statflag = 0; } else { statflag = 1; } } else { debuglog("Rpc call to statd failed with return value: %d\n", rpcret); statflag = 0; } if (statflag == 1) { LIST_INSERT_HEAD(&hostlst_head, nhp, hostlst); } else { free(nhp); } } /* * unmonitor_lock_host: clear monitor ref counts and inform statd when gone */ void unmonitor_lock_host(char *hostname) { struct host *ihp; struct mon_id smon_id; struct sm_stat smstat; int rpcret; rpcret = 0; for( ihp=LIST_FIRST(&hostlst_head); ihp != NULL; ihp=LIST_NEXT(ihp, hostlst)) { if (strncmp(hostname, ihp->name, SM_MAXSTRLEN) == 0) { /* Host is monitored, bump refcount */ --ihp->refcnt; /* Host should only be in the monitor list once */ break; } } if (ihp == NULL) { debuglog("Could not find host %16s in mon list\n", hostname); return; } if (ihp->refcnt > 0) return; if (ihp->refcnt < 0) { debuglog("Negative refcount!: %d\n", ihp->refcnt); } debuglog("Attempting to unmonitor host %16s\n", hostname); bzero(&smon_id,sizeof(smon_id)); smon_id.mon_name = hostname; smon_id.my_id.my_name = "localhost"; smon_id.my_id.my_prog = NLM_PROG; smon_id.my_id.my_vers = NLM_SM; smon_id.my_id.my_proc = NLM_SM_NOTIFY; rpcret = callrpc("localhost", SM_PROG, SM_VERS, SM_UNMON, (xdrproc_t)xdr_mon_id, &smon_id, (xdrproc_t)xdr_sm_stat, &smstat); if (rpcret != 0) { debuglog("Rpc call to unmonitor statd failed with " " return value: %d\n", rpcret); } LIST_REMOVE(ihp, hostlst); free(ihp); } /* * notify: Clear all locks from a host if statd complains * * XXX: This routine has not been thoroughly tested. However, neither * had the old one been. It used to compare the statd crash state counter * to the current lock state. The upshot of this was that it basically * cleared all locks from the specified host 99% of the time (with the * other 1% being a bug). Consequently, the assumption is that clearing * all locks from a host when notified by statd is acceptable. * * Please note that this routine skips the usual level of redirection * through a do_* type routine. This introduces a possible level of * error and might better be written as do_notify and take this one out. */ void notify(const char *hostname, const int state) { debuglog("notify from %s, new state %d", hostname, state); siglock(); do_clear(hostname); sigunlock(); debuglog("Leaving notify\n"); } void send_granted(fl, opcode) struct file_lock *fl; int opcode __unused; { CLIENT *cli; static char dummy; struct timeval timeo; int success; static struct nlm_res retval; static struct nlm4_res retval4; debuglog("About to send granted on blocked lock\n"); cli = get_client(fl->addr, (fl->flags & LOCK_V4) ? NLM_VERS4 : NLM_VERS); if (cli == NULL) { syslog(LOG_NOTICE, "failed to get CLIENT for %s", fl->client_name); /* * We fail to notify remote that the lock has been granted. * The client will timeout and retry, the lock will be * granted at this time. */ return; } timeo.tv_sec = 0; timeo.tv_usec = (fl->flags & LOCK_ASYNC) ? 0 : 500000; /* 0.5s */ if (fl->flags & LOCK_V4) { static nlm4_testargs res; res.cookie = fl->client_cookie; res.exclusive = fl->client.exclusive; res.alock.caller_name = fl->client_name; res.alock.fh.n_len = sizeof(fhandle_t); res.alock.fh.n_bytes = (char*)&fl->filehandle; res.alock.oh = fl->client.oh; res.alock.svid = fl->client.svid; res.alock.l_offset = fl->client.l_offset; res.alock.l_len = fl->client.l_len; debuglog("sending v4 reply%s", (fl->flags & LOCK_ASYNC) ? " (async)":""); if (fl->flags & LOCK_ASYNC) { success = clnt_call(cli, NLM4_GRANTED_MSG, (xdrproc_t)xdr_nlm4_testargs, &res, (xdrproc_t)xdr_void, &dummy, timeo); } else { success = clnt_call(cli, NLM4_GRANTED, (xdrproc_t)xdr_nlm4_testargs, &res, (xdrproc_t)xdr_nlm4_res, &retval4, timeo); } } else { static nlm_testargs res; res.cookie = fl->client_cookie; res.exclusive = fl->client.exclusive; res.alock.caller_name = fl->client_name; res.alock.fh.n_len = sizeof(fhandle_t); res.alock.fh.n_bytes = (char*)&fl->filehandle; res.alock.oh = fl->client.oh; res.alock.svid = fl->client.svid; res.alock.l_offset = fl->client.l_offset; res.alock.l_len = fl->client.l_len; debuglog("sending v1 reply%s", (fl->flags & LOCK_ASYNC) ? " (async)":""); if (fl->flags & LOCK_ASYNC) { success = clnt_call(cli, NLM_GRANTED_MSG, (xdrproc_t)xdr_nlm_testargs, &res, (xdrproc_t)xdr_void, &dummy, timeo); } else { success = clnt_call(cli, NLM_GRANTED, (xdrproc_t)xdr_nlm_testargs, &res, (xdrproc_t)xdr_nlm_res, &retval, timeo); } } if (debug_level > 2) debuglog("clnt_call returns %d(%s) for granted", success, clnt_sperrno(success)); } /* * Routines below here have not been modified in the overhaul */ /* * Are these two routines still required since lockd is not spawning off * children to service locks anymore? Presumably they were originally * put in place to prevent a one child from changing the lock list out * from under another one. */ void siglock(void) { sigset_t block; sigemptyset(&block); sigaddset(&block, SIGCHLD); if (sigprocmask(SIG_BLOCK, &block, NULL) < 0) { syslog(LOG_WARNING, "siglock failed: %s", strerror(errno)); } } void sigunlock(void) { sigset_t block; sigemptyset(&block); sigaddset(&block, SIGCHLD); if (sigprocmask(SIG_UNBLOCK, &block, NULL) < 0) { syslog(LOG_WARNING, "sigunlock failed: %s", strerror(errno)); } }