Index: head/sbin/dump/optr.c =================================================================== --- head/sbin/dump/optr.c (revision 298088) +++ head/sbin/dump/optr.c (revision 298089) @@ -1,429 +1,429 @@ /*- * Copyright (c) 1980, 1988, 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint #if 0 static char sccsid[] = "@(#)optr.c 8.2 (Berkeley) 1/6/94"; #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 "dump.h" #include "pathnames.h" void alarmcatch(int); int datesort(const void *, const void *); /* * Query the operator; This previously-fascist piece of code * no longer requires an exact response. * It is intended to protect dump aborting by inquisitive * people banging on the console terminal to see what is * happening which might cause dump to croak, destroying * a large number of hours of work. * * Every 2 minutes we reprint the message, alerting others * that dump needs attention. */ static int timeout; static const char *attnmessage; /* attention message */ int query(const char *question) { char replybuffer[64]; int back, errcount; FILE *mytty; if ((mytty = fopen(_PATH_TTY, "r")) == NULL) quit("fopen on %s fails: %s\n", _PATH_TTY, strerror(errno)); attnmessage = question; timeout = 0; alarmcatch(0); back = -1; errcount = 0; do { if (fgets(replybuffer, 63, mytty) == NULL) { clearerr(mytty); if (++errcount > 30) /* XXX ugly */ quit("excessive operator query failures\n"); } else if (replybuffer[0] == 'y' || replybuffer[0] == 'Y') { back = 1; } else if (replybuffer[0] == 'n' || replybuffer[0] == 'N') { back = 0; } else { (void) fprintf(stderr, " DUMP: \"Yes\" or \"No\"?\n"); (void) fprintf(stderr, " DUMP: %s: (\"yes\" or \"no\") ", question); } } while (back < 0); /* * Turn off the alarm, and reset the signal to trap out.. */ (void) alarm(0); if (signal(SIGALRM, sig) == SIG_IGN) signal(SIGALRM, SIG_IGN); (void) fclose(mytty); return(back); } char lastmsg[BUFSIZ]; /* * Alert the console operator, and enable the alarm clock to * sleep for 2 minutes in case nobody comes to satisfy dump */ void alarmcatch(int sig __unused) { if (notify == 0) { if (timeout == 0) (void) fprintf(stderr, " DUMP: %s: (\"yes\" or \"no\") ", attnmessage); else msgtail("\a\a"); } else { if (timeout) { msgtail("\n"); broadcast(""); /* just print last msg */ } (void) fprintf(stderr," DUMP: %s: (\"yes\" or \"no\") ", attnmessage); } signal(SIGALRM, alarmcatch); (void) alarm(120); timeout = 1; } /* * Here if an inquisitive operator interrupts the dump program */ void interrupt(int signo __unused) { msg("Interrupt received.\n"); if (query("Do you want to abort dump?")) dumpabort(0); } /* * We now use wall(1) to do the actual broadcasting. */ void broadcast(const char *message) { FILE *fp; char buf[sizeof(_PATH_WALL) + sizeof(OPGRENT) + 3]; if (!notify) return; snprintf(buf, sizeof(buf), "%s -g %s", _PATH_WALL, OPGRENT); if ((fp = popen(buf, "w")) == NULL) return; (void) fputs("\a\a\aMessage from the dump program to all operators\n\nDUMP: NEEDS ATTENTION: ", fp); if (lastmsg[0]) (void) fputs(lastmsg, fp); if (message[0]) (void) fputs(message, fp); (void) pclose(fp); } /* * Print out an estimate of the amount of time left to do the dump */ time_t tschedule = 0; void timeest(void) { double percent; time_t tnow, tdone; char *tdone_str; int deltat, hours, mins; (void)time(&tnow); if (blockswritten > tapesize) { setproctitle("%s: 99.99%% done, finished soon", disk); if (tnow >= tschedule) { tschedule = tnow + 300; msg("99.99%% done, finished soon\n"); } } else { deltat = (blockswritten == 0) ? 0 : tstart_writing - tnow + (double)(tnow - tstart_writing) / blockswritten * tapesize; tdone = tnow + deltat; percent = (blockswritten * 100.0) / tapesize; hours = deltat / 3600; mins = (deltat % 3600) / 60; tdone_str = ctime(&tdone); tdone_str[strlen(tdone_str) - 1] = '\0'; setproctitle( "%s: pass %d: %3.2f%% done, finished in %d:%02d at %s", disk, passno, percent, hours, mins, tdone_str); if (tnow >= tschedule) { tschedule = tnow + 300; if (blockswritten < 500) return; msg("%3.2f%% done, finished in %d:%02d at %s\n", percent, hours, mins, tdone_str); } } } /* * Schedule a printout of the estimate in the next call to timeest(). */ void infosch(int signal __unused) { tschedule = 0; } void msg(const char *fmt, ...) { va_list ap; (void) fprintf(stderr," DUMP: "); #ifdef TDEBUG (void) fprintf(stderr, "pid=%d ", getpid()); #endif va_start(ap, fmt); (void) vfprintf(stderr, fmt, ap); va_end(ap); (void) fflush(stdout); (void) fflush(stderr); va_start(ap, fmt); (void) vsnprintf(lastmsg, sizeof(lastmsg), fmt, ap); va_end(ap); } void msgtail(const char *fmt, ...) { va_list ap; va_start(ap, fmt); (void) vfprintf(stderr, fmt, ap); va_end(ap); } void quit(const char *fmt, ...) { va_list ap; (void) fprintf(stderr," DUMP: "); #ifdef TDEBUG (void) fprintf(stderr, "pid=%d ", getpid()); #endif va_start(ap, fmt); (void) vfprintf(stderr, fmt, ap); va_end(ap); (void) fflush(stdout); (void) fflush(stderr); dumpabort(0); } /* * Tell the operator what has to be done; * we don't actually do it */ struct fstab * allocfsent(const struct fstab *fs) { struct fstab *new; new = (struct fstab *)malloc(sizeof (*fs)); if (new == NULL || (new->fs_file = strdup(fs->fs_file)) == NULL || (new->fs_type = strdup(fs->fs_type)) == NULL || (new->fs_spec = strdup(fs->fs_spec)) == NULL) quit("%s\n", strerror(errno)); new->fs_passno = fs->fs_passno; new->fs_freq = fs->fs_freq; return (new); } struct pfstab { SLIST_ENTRY(pfstab) pf_list; struct fstab *pf_fstab; }; static SLIST_HEAD(, pfstab) table; void dump_getfstab(void) { struct fstab *fs; struct pfstab *pf; if (setfsent() == 0) { msg("Can't open %s for dump table information: %s\n", _PATH_FSTAB, strerror(errno)); return; } while ((fs = getfsent()) != NULL) { if ((strcmp(fs->fs_type, FSTAB_RW) && strcmp(fs->fs_type, FSTAB_RO) && strcmp(fs->fs_type, FSTAB_RQ)) || strcmp(fs->fs_vfstype, "ufs")) continue; fs = allocfsent(fs); if ((pf = (struct pfstab *)malloc(sizeof (*pf))) == NULL) quit("%s\n", strerror(errno)); pf->pf_fstab = fs; SLIST_INSERT_HEAD(&table, pf, pf_list); } (void) endfsent(); } /* * Search in the fstab for a file name. * This file name can be either the special or the path file name. * * The file name can omit the leading '/'. */ struct fstab * fstabsearch(const char *key) { struct pfstab *pf; struct fstab *fs; char *rn; SLIST_FOREACH(pf, &table, pf_list) { fs = pf->pf_fstab; if (strcmp(fs->fs_file, key) == 0 || strcmp(fs->fs_spec, key) == 0) return (fs); rn = rawname(fs->fs_spec); if (rn != NULL && strcmp(rn, key) == 0) return (fs); if (key[0] != '/') { if (*fs->fs_spec == '/' && strcmp(fs->fs_spec + 1, key) == 0) return (fs); if (*fs->fs_file == '/' && strcmp(fs->fs_file + 1, key) == 0) return (fs); } } return (NULL); } /* * Tell the operator what to do */ void lastdump(int arg) /* w ==> just what to do; W ==> most recent dumps */ { int i; struct fstab *dt; struct dumpdates *dtwalk; char *lastname, *date; int dumpme; time_t tnow; struct tm *tlast; (void) time(&tnow); dump_getfstab(); /* /etc/fstab input */ initdumptimes(); /* /etc/dumpdates input */ qsort((char *) ddatev, nddates, sizeof(struct dumpdates *), datesort); if (arg == 'w') (void) printf("Dump these file systems:\n"); else (void) printf("Last dump(s) done (Dump '>' file systems):\n"); lastname = "??"; ITITERATE(i, dtwalk) { if (strncmp(lastname, dtwalk->dd_name, sizeof(dtwalk->dd_name)) == 0) continue; date = (char *)ctime(&dtwalk->dd_ddate); date[16] = '\0'; /* blast away seconds and year */ lastname = dtwalk->dd_name; dt = fstabsearch(dtwalk->dd_name); dumpme = (dt != NULL && dt->fs_freq != 0); if (dumpme) { tlast = localtime(&dtwalk->dd_ddate); dumpme = tnow > (dtwalk->dd_ddate - (tlast->tm_hour * 3600) - (tlast->tm_min * 60) - tlast->tm_sec + (dt->fs_freq * 86400)); - }; + } if (arg != 'w' || dumpme) (void) printf( "%c %8s\t(%6s) Last dump: Level %d, Date %s\n", dumpme && (arg != 'w') ? '>' : ' ', dtwalk->dd_name, dt ? dt->fs_file : "", dtwalk->dd_level, date); } } int datesort(const void *a1, const void *a2) { struct dumpdates *d1 = *(struct dumpdates **)a1; struct dumpdates *d2 = *(struct dumpdates **)a2; int diff; diff = strncmp(d1->dd_name, d2->dd_name, sizeof(d1->dd_name)); if (diff == 0) return (d2->dd_ddate - d1->dd_ddate); return (diff); } Index: head/sbin/fsck_ffs/globs.c =================================================================== --- head/sbin/fsck_ffs/globs.c (revision 298088) +++ head/sbin/fsck_ffs/globs.c (revision 298089) @@ -1,165 +1,165 @@ /* * Copyright (c) 1980, 1986, 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. * 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. */ #if 0 #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1980, 1986, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 5/14/95"; #endif /* not lint */ #endif #include __FBSDID("$FreeBSD$"); #include #include #include #include #include "fsck.h" long readcnt[BT_NUMBUFTYPES]; long totalreadcnt[BT_NUMBUFTYPES]; struct timespec readtime[BT_NUMBUFTYPES]; struct timespec totalreadtime[BT_NUMBUFTYPES]; struct timespec startprog; struct bufarea sblk; /* file system superblock */ struct bufarea *pdirbp; /* current directory contents */ struct bufarea *pbp; /* current inode block */ ino_t cursnapshot; long numdirs, dirhash, listmax, inplast; long countdirs; /* number of directories we actually found */ int adjrefcnt[MIBSIZE]; /* MIB command to adjust inode reference cnt */ int adjblkcnt[MIBSIZE]; /* MIB command to adjust inode block count */ int adjndir[MIBSIZE]; /* MIB command to adjust number of directories */ int adjnbfree[MIBSIZE]; /* MIB command to adjust number of free blocks */ int adjnifree[MIBSIZE]; /* MIB command to adjust number of free inodes */ int adjnffree[MIBSIZE]; /* MIB command to adjust number of free frags */ int adjnumclusters[MIBSIZE]; /* MIB command to adjust number of free clusters */ int freefiles[MIBSIZE]; /* MIB command to free a set of files */ int freedirs[MIBSIZE]; /* MIB command to free a set of directories */ int freeblks[MIBSIZE]; /* MIB command to free a set of data blocks */ struct fsck_cmd cmd; /* sysctl file system update commands */ char snapname[BUFSIZ]; /* when doing snapshots, the name of the file */ char *cdevname; /* name of device being checked */ long dev_bsize; /* computed value of DEV_BSIZE */ long secsize; /* actual disk sector size */ u_int real_dev_bsize; /* actual disk sector size, not overridden */ char nflag; /* assume a no response */ char yflag; /* assume a yes response */ int bkgrdflag; /* use a snapshot to run on an active system */ int bflag; /* location of alternate super block */ int debug; /* output debugging info */ int Eflag; /* delete empty data blocks */ int Zflag; /* zero empty data blocks */ int inoopt; /* trim out unused inodes */ char ckclean; /* only do work if not cleanly unmounted */ int cvtlevel; /* convert to newer file system format */ int bkgrdcheck; /* determine if background check is possible */ int bkgrdsumadj; /* whether the kernel have ability to adjust superblock summary */ char usedsoftdep; /* just fix soft dependency inconsistencies */ char preen; /* just fix normal inconsistencies */ char rerun; /* rerun fsck. Only used in non-preen mode */ int returntosingle; /* 1 => return to single user mode on exit */ char resolved; /* cleared if unresolved changes => not clean */ char havesb; /* superblock has been read */ char skipclean; /* skip clean file systems if preening */ int fsmodified; /* 1 => write done to file system */ int fsreadfd; /* file descriptor for reading file system */ int fswritefd; /* file descriptor for writing file system */ int surrender; /* Give up if reads fail */ int wantrestart; /* Restart fsck on early termination */ ufs2_daddr_t maxfsblock; /* number of blocks in the file system */ char *blockmap; /* ptr to primary blk allocation map */ ino_t maxino; /* number of inodes in file system */ ino_t lfdir; /* lost & found directory inode number */ const char *lfname; /* lost & found directory name */ int lfmode; /* lost & found directory creation mode */ ufs2_daddr_t n_blks; /* number of blocks in use */ ino_t n_files; /* number of files in use */ volatile sig_atomic_t got_siginfo; /* received a SIGINFO */ volatile sig_atomic_t got_sigalarm; /* received a SIGALRM */ struct ufs1_dinode ufs1_zino; struct ufs2_dinode ufs2_zino; void fsckinit(void) { bzero(readcnt, sizeof(long) * BT_NUMBUFTYPES); bzero(totalreadcnt, sizeof(long) * BT_NUMBUFTYPES); bzero(readtime, sizeof(struct timespec) * BT_NUMBUFTYPES); bzero(totalreadtime, sizeof(struct timespec) * BT_NUMBUFTYPES); - bzero(&startprog, sizeof(struct timespec));; + bzero(&startprog, sizeof(struct timespec)); bzero(&sblk, sizeof(struct bufarea)); pdirbp = NULL; pbp = NULL; cursnapshot = 0; numdirs = dirhash = listmax = inplast = 0; countdirs = 0; bzero(adjrefcnt, sizeof(int) * MIBSIZE); bzero(adjblkcnt, sizeof(int) * MIBSIZE); bzero(adjndir, sizeof(int) * MIBSIZE); bzero(adjnbfree, sizeof(int) * MIBSIZE); bzero(adjnifree, sizeof(int) * MIBSIZE); bzero(adjnffree, sizeof(int) * MIBSIZE); bzero(adjnumclusters, sizeof(int) * MIBSIZE); bzero(freefiles, sizeof(int) * MIBSIZE); bzero(freedirs, sizeof(int) * MIBSIZE); bzero(freeblks, sizeof(int) * MIBSIZE); bzero(&cmd, sizeof(struct fsck_cmd)); bzero(snapname, sizeof(char) * BUFSIZ); cdevname = NULL; dev_bsize = 0; secsize = 0; real_dev_bsize = 0; bkgrdsumadj = 0; usedsoftdep = 0; rerun = 0; returntosingle = 0; resolved = 0; havesb = 0; fsmodified = 0; fsreadfd = 0; fswritefd = 0; maxfsblock = 0; blockmap = NULL; maxino = 0; lfdir = 0; lfname = "lost+found"; lfmode = 0700; n_blks = 0; n_files = 0; got_siginfo = 0; got_sigalarm = 0; bzero(&ufs1_zino, sizeof(struct ufs1_dinode)); bzero(&ufs2_zino, sizeof(struct ufs2_dinode)); } Index: head/sbin/ifconfig/sfp.c =================================================================== --- head/sbin/ifconfig/sfp.c (revision 298088) +++ head/sbin/ifconfig/sfp.c (revision 298089) @@ -1,916 +1,916 @@ /*- * Copyright (c) 2014 Alexander V. Chernikov. 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. */ #ifndef lint static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "ifconfig.h" struct i2c_info { int fd; /* fd to issue SIOCGI2C */ int error; /* Store first error */ int qsfp; /* True if transceiver is QSFP */ int do_diag; /* True if we need to request DDM */ struct ifreq *ifr; /* Pointer to pre-filled ifreq */ }; static int read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len, uint8_t *buf); static void dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len); struct _nv { int v; const char *n; }; const char *find_value(struct _nv *x, int value); const char *find_zero_bit(struct _nv *x, int value, int sz); /* SFF-8472 Rev. 11.4 table 3.4: Connector values */ static struct _nv conn[] = { { 0x00, "Unknown" }, { 0x01, "SC" }, { 0x02, "Fibre Channel Style 1 copper" }, { 0x03, "Fibre Channel Style 2 copper" }, { 0x04, "BNC/TNC" }, { 0x05, "Fibre Channel coaxial" }, { 0x06, "FiberJack" }, { 0x07, "LC" }, { 0x08, "MT-RJ" }, { 0x09, "MU" }, { 0x0A, "SG" }, { 0x0B, "Optical pigtail" }, { 0x0C, "MPO Parallel Optic" }, { 0x20, "HSSDC II" }, { 0x21, "Copper pigtail" }, { 0x22, "RJ45" }, { 0x23, "No separate connector" }, /* SFF-8436 */ { 0, NULL } }; /* SFF-8472 Rev. 11.4 table 3.5: Transceiver codes */ /* 10G Ethernet/IB compliance codes, byte 3 */ static struct _nv eth_10g[] = { { 0x80, "10G Base-ER" }, { 0x40, "10G Base-LRM" }, { 0x20, "10G Base-LR" }, { 0x10, "10G Base-SR" }, { 0x08, "1X SX" }, { 0x04, "1X LX" }, { 0x02, "1X Copper Active" }, { 0x01, "1X Copper Passive" }, { 0, NULL } }; /* Ethernet compliance codes, byte 6 */ static struct _nv eth_compat[] = { { 0x80, "BASE-PX" }, { 0x40, "BASE-BX10" }, { 0x20, "100BASE-FX" }, { 0x10, "100BASE-LX/LX10" }, { 0x08, "1000BASE-T" }, { 0x04, "1000BASE-CX" }, { 0x02, "1000BASE-LX" }, { 0x01, "1000BASE-SX" }, { 0, NULL } }; /* FC link length, byte 7 */ static struct _nv fc_len[] = { { 0x80, "very long distance" }, { 0x40, "short distance" }, { 0x20, "intermediate distance" }, { 0x10, "long distance" }, { 0x08, "medium distance" }, { 0, NULL } }; /* Channel/Cable technology, byte 7-8 */ static struct _nv cab_tech[] = { { 0x0400, "Shortwave laser (SA)" }, { 0x0200, "Longwave laser (LC)" }, { 0x0100, "Electrical inter-enclosure (EL)" }, { 0x80, "Electrical intra-enclosure (EL)" }, { 0x40, "Shortwave laser (SN)" }, { 0x20, "Shortwave laser (SL)" }, { 0x10, "Longwave laser (LL)" }, { 0x08, "Active Cable" }, { 0x04, "Passive Cable" }, { 0, NULL } }; /* FC Transmission media, byte 9 */ static struct _nv fc_media[] = { { 0x80, "Twin Axial Pair" }, { 0x40, "Twisted Pair" }, { 0x20, "Miniature Coax" }, { 0x10, "Viao Coax" }, { 0x08, "Miltimode, 62.5um" }, { 0x04, "Multimode, 50um" }, { 0x02, "" }, { 0x01, "Single Mode" }, { 0, NULL } }; /* FC Speed, byte 10 */ static struct _nv fc_speed[] = { { 0x80, "1200 MBytes/sec" }, { 0x40, "800 MBytes/sec" }, { 0x20, "1600 MBytes/sec" }, { 0x10, "400 MBytes/sec" }, { 0x08, "3200 MBytes/sec" }, { 0x04, "200 MBytes/sec" }, { 0x01, "100 MBytes/sec" }, { 0, NULL } }; /* SFF-8436 Rev. 4.8 table 33: Specification compliance */ /* 10/40G Ethernet compliance codes, byte 128 + 3 */ static struct _nv eth_1040g[] = { { 0x80, "Extended" }, { 0x40, "10GBASE-LRM" }, { 0x20, "10GBASE-LR" }, { 0x10, "10GBASE-SR" }, { 0x08, "40GBASE-CR4" }, { 0x04, "40GBASE-SR4" }, { 0x02, "40GBASE-LR4" }, { 0x01, "40G Active Cable" }, { 0, NULL } }; #define SFF_8636_EXT_COMPLIANCE 0x80 /* SFF-8024 Rev. 3.4 table 4.4: Extended Specification Compliance */ static struct _nv eth_extended_comp[] = { { 0xFF, "Reserved" }, { 0x1A, "2 lambda DWDM 100G" }, { 0x19, "100G ACC or 25GAUI C2M ACC" }, { 0x18, "100G AOC or 25GAUI C2M AOC" }, { 0x17, "100G CLR4" }, { 0x16, "10GBASE-T with SFI electrical interface" }, { 0x15, "G959.1 profile P1L1-2D2" }, { 0x14, "G959.1 profile P1S1-2D2" }, { 0x13, "G959.1 profile P1I1-2D1" }, { 0x12, "40G PSM4 Parallel SMF" }, { 0x11, "4 x 10GBASE-SR" }, { 0x10, "40GBASE-ER4" }, { 0x0F, "Reserved" }, { 0x0D, "25GBASE-CR CA-N" }, { 0x0C, "25GBASE-CR CA-S" }, { 0x0B, "100GBASE-CR4 or 25GBASE-CR CA-L" }, { 0x0A, "Reserved" }, { 0x09, "100G CWDM4 MSA without FEC" }, { 0x08, "100G ACC (Active Copper Cable)" }, { 0x07, "100G PSM4 Parallel SMF" }, { 0x06, "100G CWDM4 MSA with FEC" }, { 0x05, "100GBASE-SR10" }, { 0x04, "100GBASE-ER4" }, { 0x03, "100GBASE-LR4" }, { 0x02, "100GBASE-SR4" }, { 0x01, "100G AOC (Active Optical Cable) or 25GAUI C2M ACC" }, { 0x00, "Unspecified" } }; /* SFF-8636 Rev. 2.5 table 6.3: Revision compliance */ static struct _nv rev_compl[] = { { 0x1, "SFF-8436 rev <=4.8" }, { 0x2, "SFF-8436 rev <=4.8" }, { 0x3, "SFF-8636 rev <=1.3" }, { 0x4, "SFF-8636 rev <=1.4" }, { 0x5, "SFF-8636 rev <=1.5" }, { 0x6, "SFF-8636 rev <=2.0" }, { 0x7, "SFF-8636 rev <=2.5" }, { 0x0, "Unspecified" } }; const char * find_value(struct _nv *x, int value) { for (; x->n != NULL; x++) if (x->v == value) return (x->n); return (NULL); } const char * find_zero_bit(struct _nv *x, int value, int sz) { int v, m; const char *s; v = 1; for (v = 1, m = 1 << (8 * sz); v < m; v *= 2) { if ((value & v) == 0) continue; if ((s = find_value(x, value & v)) != NULL) { value &= ~v; return (s); } } return (NULL); } static void convert_sff_identifier(char *buf, size_t size, uint8_t value) { const char *x; x = NULL; if (value <= SFF_8024_ID_LAST) x = sff_8024_id[value]; else { if (value > 0x80) x = "Vendor specific"; else x = "Reserved"; } snprintf(buf, size, "%s", x); } static void convert_sff_connector(char *buf, size_t size, uint8_t value) { const char *x; if ((x = find_value(conn, value)) == NULL) { if (value >= 0x0D && value <= 0x1F) x = "Unallocated"; else if (value >= 0x24 && value <= 0x7F) x = "Unallocated"; else x = "Vendor specific"; } snprintf(buf, size, "%s", x); } static void convert_sff_rev_compliance(char *buf, size_t size, uint8_t value) { const char *x; if (value > 0x07) x = "Unallocated"; else x = find_value(rev_compl, value); snprintf(buf, size, "%s", x); } static void get_sfp_identifier(struct i2c_info *ii, char *buf, size_t size) { uint8_t data; read_i2c(ii, SFF_8472_BASE, SFF_8472_ID, 1, &data); convert_sff_identifier(buf, size, data); } static void get_sfp_connector(struct i2c_info *ii, char *buf, size_t size) { uint8_t data; read_i2c(ii, SFF_8472_BASE, SFF_8472_CONNECTOR, 1, &data); convert_sff_connector(buf, size, data); } static void get_qsfp_identifier(struct i2c_info *ii, char *buf, size_t size) { uint8_t data; read_i2c(ii, SFF_8436_BASE, SFF_8436_ID, 1, &data); convert_sff_identifier(buf, size, data); } static void get_qsfp_connector(struct i2c_info *ii, char *buf, size_t size) { uint8_t data; read_i2c(ii, SFF_8436_BASE, SFF_8436_CONNECTOR, 1, &data); convert_sff_connector(buf, size, data); } static void printf_sfp_transceiver_descr(struct i2c_info *ii, char *buf, size_t size) { char xbuf[12]; const char *tech_class, *tech_len, *tech_tech, *tech_media, *tech_speed; tech_class = NULL; tech_len = NULL; tech_tech = NULL; tech_media = NULL; tech_speed = NULL; /* Read bytes 3-10 at once */ read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, &xbuf[3]); /* Check 10G ethernet first */ tech_class = find_zero_bit(eth_10g, xbuf[3], 1); if (tech_class == NULL) { /* No match. Try 1G */ tech_class = find_zero_bit(eth_compat, xbuf[6], 1); } tech_len = find_zero_bit(fc_len, xbuf[7], 1); tech_tech = find_zero_bit(cab_tech, xbuf[7] << 8 | xbuf[8], 2); tech_media = find_zero_bit(fc_media, xbuf[9], 1); tech_speed = find_zero_bit(fc_speed, xbuf[10], 1); printf("Class: %s\n", tech_class); printf("Length: %s\n", tech_len); printf("Tech: %s\n", tech_tech); printf("Media: %s\n", tech_media); printf("Speed: %s\n", tech_speed); } static void get_sfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size) { const char *tech_class; uint8_t code; unsigned char qbuf[8]; read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 8, (uint8_t *)qbuf); /* Check 10G Ethernet/IB first */ read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START, 1, &code); tech_class = find_zero_bit(eth_10g, code, 1); if (tech_class == NULL) { /* No match. Try Ethernet 1G */ read_i2c(ii, SFF_8472_BASE, SFF_8472_TRANS_START + 3, 1, (caddr_t)&code); tech_class = find_zero_bit(eth_compat, code, 1); } if (tech_class == NULL) tech_class = "Unknown"; snprintf(buf, size, "%s", tech_class); } static void get_qsfp_transceiver_class(struct i2c_info *ii, char *buf, size_t size) { const char *tech_class; uint8_t code; read_i2c(ii, SFF_8436_BASE, SFF_8436_CODE_E1040100G, 1, &code); /* Check for extended specification compliance */ if (code & SFF_8636_EXT_COMPLIANCE) { read_i2c(ii, SFF_8436_BASE, SFF_8436_OPTIONS_START, 1, &code); tech_class = find_value(eth_extended_comp, code); } else /* Check 10/40G Ethernet class only */ tech_class = find_zero_bit(eth_1040g, code, 1); if (tech_class == NULL) tech_class = "Unknown"; snprintf(buf, size, "%s", tech_class); } /* * Print SFF-8472/SFF-8436 string to supplied buffer. * All (vendor-specific) strings are padded right with '0x20'. */ static void convert_sff_name(char *buf, size_t size, char *xbuf) { char *p; for (p = &xbuf[16]; *(p - 1) == 0x20; p--) ; *p = '\0'; snprintf(buf, size, "%s", xbuf); } static void convert_sff_date(char *buf, size_t size, char *xbuf) { snprintf(buf, size, "20%c%c-%c%c-%c%c", xbuf[0], xbuf[1], xbuf[2], xbuf[3], xbuf[4], xbuf[5]); } static void get_sfp_vendor_name(struct i2c_info *ii, char *buf, size_t size) { char xbuf[17]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_BASE, SFF_8472_VENDOR_START, 16, (uint8_t *)xbuf); convert_sff_name(buf, size, xbuf); } static void get_sfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size) { char xbuf[17]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_BASE, SFF_8472_PN_START, 16, (uint8_t *)xbuf); convert_sff_name(buf, size, xbuf); } static void get_sfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size) { char xbuf[17]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_BASE, SFF_8472_SN_START, 16, (uint8_t *)xbuf); convert_sff_name(buf, size, xbuf); } static void get_sfp_vendor_date(struct i2c_info *ii, char *buf, size_t size) { char xbuf[6]; memset(xbuf, 0, sizeof(xbuf)); /* Date code, see Table 3.8 for description */ read_i2c(ii, SFF_8472_BASE, SFF_8472_DATE_START, 6, (uint8_t *)xbuf); convert_sff_date(buf, size, xbuf); } static void get_qsfp_vendor_name(struct i2c_info *ii, char *buf, size_t size) { char xbuf[17]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_VENDOR_START, 16, (uint8_t *)xbuf); convert_sff_name(buf, size, xbuf); } static void get_qsfp_vendor_pn(struct i2c_info *ii, char *buf, size_t size) { char xbuf[17]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_PN_START, 16, (uint8_t *)xbuf); convert_sff_name(buf, size, xbuf); } static void get_qsfp_vendor_sn(struct i2c_info *ii, char *buf, size_t size) { char xbuf[17]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_SN_START, 16, (uint8_t *)xbuf); convert_sff_name(buf, size, xbuf); } static void get_qsfp_vendor_date(struct i2c_info *ii, char *buf, size_t size) { char xbuf[6]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_DATE_START, 6, (uint8_t *)xbuf); convert_sff_date(buf, size, xbuf); } static void print_sfp_vendor(struct i2c_info *ii, char *buf, size_t size) { char xbuf[80]; memset(xbuf, 0, sizeof(xbuf)); if (ii->qsfp != 0) { get_qsfp_vendor_name(ii, xbuf, 20); get_qsfp_vendor_pn(ii, &xbuf[20], 20); get_qsfp_vendor_sn(ii, &xbuf[40], 20); get_qsfp_vendor_date(ii, &xbuf[60], 20); } else { get_sfp_vendor_name(ii, xbuf, 20); get_sfp_vendor_pn(ii, &xbuf[20], 20); get_sfp_vendor_sn(ii, &xbuf[40], 20); get_sfp_vendor_date(ii, &xbuf[60], 20); } snprintf(buf, size, "vendor: %s PN: %s SN: %s DATE: %s", xbuf, &xbuf[20], &xbuf[40], &xbuf[60]); } /* * Converts internal templerature (SFF-8472, SFF-8436) * 16-bit unsigned value to human-readable representation: * * Internally measured Module temperature are represented * as a 16-bit signed twos complement value in increments of * 1/256 degrees Celsius, yielding a total range of –128C to +128C * that is considered valid between –40 and +125C. * */ static void convert_sff_temp(char *buf, size_t size, uint8_t *xbuf) { double d; d = (double)xbuf[0]; d += (double)xbuf[1] / 256; snprintf(buf, size, "%.2f C", d); } /* * Retrieves supplied voltage (SFF-8472, SFF-8436). * 16-bit usigned value, treated as range 0..+6.55 Volts */ static void convert_sff_voltage(char *buf, size_t size, uint8_t *xbuf) { double d; d = (double)((xbuf[0] << 8) | xbuf[1]); snprintf(buf, size, "%.2f Volts", d / 10000); } /* * Converts value in @xbuf to both milliwats and dBm * human representation. */ static void convert_sff_power(struct i2c_info *ii, char *buf, size_t size, uint8_t *xbuf) { uint16_t mW; double dbm; mW = (xbuf[0] << 8) + xbuf[1]; /* Convert mw to dbm */ dbm = 10.0 * log10(1.0 * mW / 10000); /* * Assume internally-calibrated data. * This is always true for SFF-8346, and explicitly * checked for SFF-8472. */ /* Table 3.9, bit 5 is set, internally calibrated */ snprintf(buf, size, "%d.%02d mW (%.2f dBm)", mW / 10000, (mW % 10000) / 100, dbm); } static void get_sfp_temp(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_DIAG, SFF_8472_TEMP, 2, xbuf); convert_sff_temp(buf, size, xbuf); } static void get_sfp_voltage(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_DIAG, SFF_8472_VCC, 2, xbuf); convert_sff_voltage(buf, size, xbuf); } static int get_qsfp_temp(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_TEMP, 2, xbuf); if ((xbuf[0] == 0xFF && xbuf[1] == 0xFF) || (xbuf[0] == 0 && xbuf[1] == 0)) return (-1); convert_sff_temp(buf, size, xbuf); return (0); } static void get_qsfp_voltage(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_VCC, 2, xbuf); convert_sff_voltage(buf, size, xbuf); } static void get_sfp_rx_power(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_DIAG, SFF_8472_RX_POWER, 2, xbuf); convert_sff_power(ii, buf, size, xbuf); } static void get_sfp_tx_power(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8472_DIAG, SFF_8472_TX_POWER, 2, xbuf); convert_sff_power(ii, buf, size, xbuf); } static void get_qsfp_rx_power(struct i2c_info *ii, char *buf, size_t size, int chan) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_RX_CH1_MSB + (chan-1)*2, 2, xbuf); convert_sff_power(ii, buf, size, xbuf); } static void get_qsfp_tx_power(struct i2c_info *ii, char *buf, size_t size, int chan) { uint8_t xbuf[2]; memset(xbuf, 0, sizeof(xbuf)); read_i2c(ii, SFF_8436_BASE, SFF_8436_TX_CH1_MSB + (chan-1)*2, 2, xbuf); convert_sff_power(ii, buf, size, xbuf); } static void get_qsfp_rev_compliance(struct i2c_info *ii, char *buf, size_t size) { uint8_t xbuf; xbuf = 0; read_i2c(ii, SFF_8436_BASE, SFF_8436_STATUS, 1, &xbuf); convert_sff_rev_compliance(buf, size, xbuf); } static uint32_t get_qsfp_br(struct i2c_info *ii) { uint8_t xbuf; uint32_t rate; xbuf = 0; read_i2c(ii, SFF_8436_BASE, SFF_8436_BITRATE, 1, &xbuf); rate = xbuf * 100; if (xbuf == 0xFF) { read_i2c(ii, SFF_8436_BASE, SFF_8636_BITRATE, 1, &xbuf); rate = xbuf * 250; } return (rate); } /* * Reads i2c data from opened kernel socket. */ static int read_i2c(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len, uint8_t *buf) { struct ifi2creq req; int i, l; if (ii->error != 0) return (ii->error); ii->ifr->ifr_data = (caddr_t)&req; i = 0; l = 0; memset(&req, 0, sizeof(req)); req.dev_addr = addr; req.offset = off; req.len = len; while (len > 0) { l = (len > sizeof(req.data)) ? sizeof(req.data) : len; req.len = l; if (ioctl(ii->fd, SIOCGI2C, ii->ifr) != 0) { ii->error = errno; return (errno); } memcpy(&buf[i], req.data, l); len -= l; i += l; req.offset += l; } return (0); } static void dump_i2c_data(struct i2c_info *ii, uint8_t addr, uint8_t off, uint8_t len) { unsigned char buf[16]; int i, read; while (len > 0) { memset(buf, 0, sizeof(buf)); read = (len > sizeof(buf)) ? sizeof(buf) : len; read_i2c(ii, addr, off, read, buf); if (ii->error != 0) { fprintf(stderr, "Error reading i2c info\n"); return; } printf("\t"); for (i = 0; i < read; i++) printf("%02X ", buf[i]); printf("\n"); len -= read; off += read; } } static void print_qsfp_status(struct i2c_info *ii, int verbose) { char buf[80], buf2[40], buf3[40]; uint32_t bitrate; int i; ii->qsfp = 1; /* Transceiver type */ get_qsfp_identifier(ii, buf, sizeof(buf)); get_qsfp_transceiver_class(ii, buf2, sizeof(buf2)); get_qsfp_connector(ii, buf3, sizeof(buf3)); if (ii->error == 0) printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3); print_sfp_vendor(ii, buf, sizeof(buf)); if (ii->error == 0) printf("\t%s\n", buf); if (verbose > 1) { get_qsfp_rev_compliance(ii, buf, sizeof(buf)); if (ii->error == 0) printf("\tcompliance level: %s\n", buf); bitrate = get_qsfp_br(ii); if (ii->error == 0 && bitrate > 0) printf("\tnominal bitrate: %u Mbps\n", bitrate); } /* * The standards in this area are not clear when the * additional measurements are present or not. Use a valid * temperature reading as an indicator for the presence of * voltage and TX/RX power measurements. */ if (get_qsfp_temp(ii, buf, sizeof(buf)) == 0) { get_qsfp_voltage(ii, buf2, sizeof(buf2)); printf("\tmodule temperature: %s voltage: %s\n", buf, buf2); for (i = 1; i <= 4; i++) { get_qsfp_rx_power(ii, buf, sizeof(buf), i); get_qsfp_tx_power(ii, buf2, sizeof(buf2), i); printf("\tlane %d: RX: %s TX: %s\n", i, buf, buf2); } } if (verbose > 2) { printf("\n\tSFF8436 DUMP (0xA0 128..255 range):\n"); dump_i2c_data(ii, SFF_8436_BASE, 128, 128); printf("\n\tSFF8436 DUMP (0xA0 0..81 range):\n"); dump_i2c_data(ii, SFF_8436_BASE, 0, 82); } } static void print_sfp_status(struct i2c_info *ii, int verbose) { char buf[80], buf2[40], buf3[40]; uint8_t diag_type, flags; /* Read diagnostic monitoring type */ read_i2c(ii, SFF_8472_BASE, SFF_8472_DIAG_TYPE, 1, (caddr_t)&diag_type); if (ii->error != 0) return; /* * Read monitoring data IFF it is supplied AND is * internally calibrated */ flags = SFF_8472_DDM_DONE | SFF_8472_DDM_INTERNAL; if ((diag_type & flags) == flags) ii->do_diag = 1; /* Transceiver type */ get_sfp_identifier(ii, buf, sizeof(buf)); get_sfp_transceiver_class(ii, buf2, sizeof(buf2)); get_sfp_connector(ii, buf3, sizeof(buf3)); if (ii->error == 0) printf("\tplugged: %s %s (%s)\n", buf, buf2, buf3); print_sfp_vendor(ii, buf, sizeof(buf)); if (ii->error == 0) printf("\t%s\n", buf); if (verbose > 5) printf_sfp_transceiver_descr(ii, buf, sizeof(buf)); /* * Request current measurements iff they are provided: */ if (ii->do_diag != 0) { get_sfp_temp(ii, buf, sizeof(buf)); get_sfp_voltage(ii, buf2, sizeof(buf2)); printf("\tmodule temperature: %s Voltage: %s\n", buf, buf2); get_sfp_rx_power(ii, buf, sizeof(buf)); get_sfp_tx_power(ii, buf2, sizeof(buf2)); printf("\tRX: %s TX: %s\n", buf, buf2); } if (verbose > 2) { printf("\n\tSFF8472 DUMP (0xA0 0..127 range):\n"); dump_i2c_data(ii, SFF_8472_BASE, 0, 128); } } void sfp_status(int s, struct ifreq *ifr, int verbose) { struct i2c_info ii; uint8_t id_byte; /* Prepare necessary into pass to i2c reader */ memset(&ii, 0, sizeof(ii)); ii.fd = s; ii.ifr = ifr; /* * Try to read byte 0 from i2c: * Both SFF-8472 and SFF-8436 use it as * 'identification byte'. * Stop reading status on zero as value - * this might happen in case of empty transceiver slot. */ id_byte = 0; read_i2c(&ii, SFF_8472_BASE, SFF_8472_ID, 1, (caddr_t)&id_byte); if (ii.error != 0 || id_byte == 0) return; switch (id_byte) { case SFF_8024_ID_QSFP: case SFF_8024_ID_QSFPPLUS: case SFF_8024_ID_QSFP28: print_qsfp_status(&ii, verbose); break; default: print_sfp_status(&ii, verbose); - }; + } } Index: head/sbin/mount_nfs/mount_nfs.c =================================================================== --- head/sbin/mount_nfs/mount_nfs.c (revision 298088) +++ head/sbin/mount_nfs/mount_nfs.c (revision 298089) @@ -1,1043 +1,1043 @@ /* * Copyright (c) 1992, 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. * 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. */ #if 0 #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1992, 1993, 1994\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint static char sccsid[] = "@(#)mount_nfs.c 8.11 (Berkeley) 5/4/95"; #endif /* not lint */ #endif #include __FBSDID("$FreeBSD$"); #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 "mntopts.h" #include "mounttab.h" /* Table for af,sotype -> netid conversions. */ static struct nc_protos { const char *netid; int af; int sotype; } nc_protos[] = { {"udp", AF_INET, SOCK_DGRAM}, {"tcp", AF_INET, SOCK_STREAM}, {"udp6", AF_INET6, SOCK_DGRAM}, {"tcp6", AF_INET6, SOCK_STREAM}, {NULL, 0, 0} }; struct nfhret { u_long stat; long vers; long auth; long fhsize; u_char nfh[NFS3_FHSIZE]; }; #define BGRND 1 #define ISBGRND 2 #define OF_NOINET4 4 #define OF_NOINET6 8 static int retrycnt = -1; static int opflags = 0; static int nfsproto = IPPROTO_TCP; static int mnttcp_ok = 1; static int noconn = 0; /* The 'portspec' is the server nfs port; NULL means look up via rpcbind. */ static const char *portspec = NULL; static struct sockaddr *addr; static int addrlen = 0; static u_char *fh = NULL; static int fhsize = 0; static int secflavor = -1; static int got_principal = 0; static enum mountmode { ANY, V2, V3, V4 } mountmode = ANY; /* Return codes for nfs_tryproto. */ enum tryret { TRYRET_SUCCESS, TRYRET_TIMEOUT, /* No response received. */ TRYRET_REMOTEERR, /* Error received from remote server. */ TRYRET_LOCALERR /* Local failure. */ }; static int sec_name_to_num(const char *sec); static const char *sec_num_to_name(int num); static int getnfsargs(char *, struct iovec **iov, int *iovlen); /* void set_rpc_maxgrouplist(int); */ static struct netconfig *getnetconf_cached(const char *netid); static const char *netidbytype(int af, int sotype); static void usage(void) __dead2; static int xdr_dir(XDR *, char *); static int xdr_fh(XDR *, struct nfhret *); static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr, struct iovec **iov, int *iovlen); static enum tryret returncode(enum clnt_stat stat, struct rpc_err *rpcerr); int main(int argc, char *argv[]) { int c; struct iovec *iov; int num, iovlen; char *mntname, *p, *spec, *tmp; char mntpath[MAXPATHLEN], errmsg[255]; char hostname[MAXHOSTNAMELEN + 1], gssn[MAXHOSTNAMELEN + 50]; const char *fstype, *gssname; iov = NULL; iovlen = 0; memset(errmsg, 0, sizeof(errmsg)); gssname = NULL; fstype = strrchr(argv[0], '_'); if (fstype == NULL) errx(EX_USAGE, "argv[0] must end in _fstype"); ++fstype; while ((c = getopt(argc, argv, "23a:bcdD:g:I:iLlNo:PR:r:sTt:w:x:U")) != -1) switch (c) { case '2': mountmode = V2; break; case '3': mountmode = V3; break; case 'a': printf("-a deprecated, use -o readahead=\n"); build_iovec(&iov, &iovlen, "readahead", optarg, (size_t)-1); break; case 'b': opflags |= BGRND; break; case 'c': printf("-c deprecated, use -o noconn\n"); build_iovec(&iov, &iovlen, "noconn", NULL, 0); noconn = 1; break; case 'D': printf("-D deprecated, use -o deadthresh=\n"); build_iovec(&iov, &iovlen, "deadthresh", optarg, (size_t)-1); break; case 'd': printf("-d deprecated, use -o dumbtimer"); build_iovec(&iov, &iovlen, "dumbtimer", NULL, 0); break; case 'g': printf("-g deprecated, use -o maxgroups"); num = strtol(optarg, &p, 10); if (*p || num <= 0) errx(1, "illegal -g value -- %s", optarg); //set_rpc_maxgrouplist(num); build_iovec(&iov, &iovlen, "maxgroups", optarg, (size_t)-1); break; case 'I': printf("-I deprecated, use -o readdirsize=\n"); build_iovec(&iov, &iovlen, "readdirsize", optarg, (size_t)-1); break; case 'i': printf("-i deprecated, use -o intr\n"); build_iovec(&iov, &iovlen, "intr", NULL, 0); break; case 'L': printf("-L deprecated, use -o nolockd\n"); build_iovec(&iov, &iovlen, "nolockd", NULL, 0); break; case 'l': printf("-l deprecated, -o rdirplus\n"); build_iovec(&iov, &iovlen, "rdirplus", NULL, 0); break; case 'N': printf("-N deprecated, do not specify -o resvport\n"); break; case 'o': { int pass_flag_to_nmount; char *opt = optarg; while (opt) { char *pval = NULL; char *pnextopt = NULL; const char *val = ""; pass_flag_to_nmount = 1; pnextopt = strchr(opt, ','); if (pnextopt != NULL) { *pnextopt = '\0'; pnextopt++; } pval = strchr(opt, '='); if (pval != NULL) { *pval = '\0'; val = pval + 1; } if (strcmp(opt, "bg") == 0) { opflags |= BGRND; pass_flag_to_nmount=0; } else if (strcmp(opt, "fg") == 0) { /* same as not specifying -o bg */ pass_flag_to_nmount=0; } else if (strcmp(opt, "gssname") == 0) { pass_flag_to_nmount = 0; gssname = val; } else if (strcmp(opt, "mntudp") == 0) { mnttcp_ok = 0; nfsproto = IPPROTO_UDP; } else if (strcmp(opt, "udp") == 0) { nfsproto = IPPROTO_UDP; } else if (strcmp(opt, "tcp") == 0) { nfsproto = IPPROTO_TCP; } else if (strcmp(opt, "noinet4") == 0) { pass_flag_to_nmount=0; opflags |= OF_NOINET4; } else if (strcmp(opt, "noinet6") == 0) { pass_flag_to_nmount=0; opflags |= OF_NOINET6; } else if (strcmp(opt, "noconn") == 0) { noconn = 1; } else if (strcmp(opt, "nfsv2") == 0) { pass_flag_to_nmount=0; mountmode = V2; } else if (strcmp(opt, "nfsv3") == 0) { mountmode = V3; } else if (strcmp(opt, "nfsv4") == 0) { pass_flag_to_nmount=0; mountmode = V4; fstype = "nfs"; nfsproto = IPPROTO_TCP; if (portspec == NULL) portspec = "2049"; } else if (strcmp(opt, "port") == 0) { pass_flag_to_nmount=0; asprintf(&tmp, "%d", atoi(val)); if (tmp == NULL) err(1, "asprintf"); portspec = tmp; } else if (strcmp(opt, "principal") == 0) { got_principal = 1; } else if (strcmp(opt, "proto") == 0) { pass_flag_to_nmount=0; if (strcmp(val, "tcp") == 0) { nfsproto = IPPROTO_TCP; opflags |= OF_NOINET6; build_iovec(&iov, &iovlen, "tcp", NULL, 0); } else if (strcmp(val, "udp") == 0) { mnttcp_ok = 0; nfsproto = IPPROTO_UDP; opflags |= OF_NOINET6; build_iovec(&iov, &iovlen, "udp", NULL, 0); } else if (strcmp(val, "tcp6") == 0) { nfsproto = IPPROTO_TCP; opflags |= OF_NOINET4; build_iovec(&iov, &iovlen, "tcp", NULL, 0); } else if (strcmp(val, "udp6") == 0) { mnttcp_ok = 0; nfsproto = IPPROTO_UDP; opflags |= OF_NOINET4; build_iovec(&iov, &iovlen, "udp", NULL, 0); } else { errx(1, "illegal proto value -- %s", val); } } else if (strcmp(opt, "sec") == 0) { /* * Don't add this option to * the iovec yet - we will * negotiate which sec flavor * to use with the remote * mountd. */ pass_flag_to_nmount=0; secflavor = sec_name_to_num(val); if (secflavor < 0) { errx(1, "illegal sec value -- %s", val); } } else if (strcmp(opt, "retrycnt") == 0) { pass_flag_to_nmount=0; num = strtol(val, &p, 10); if (*p || num < 0) errx(1, "illegal retrycnt value -- %s", val); retrycnt = num; } else if (strcmp(opt, "maxgroups") == 0) { num = strtol(val, &p, 10); if (*p || num <= 0) errx(1, "illegal maxgroups value -- %s", val); //set_rpc_maxgrouplist(num); } else if (strcmp(opt, "vers") == 0) { num = strtol(val, &p, 10); if (*p || num <= 0) errx(1, "illegal vers value -- " "%s", val); switch (num) { case 2: mountmode = V2; break; case 3: mountmode = V3; build_iovec(&iov, &iovlen, "nfsv3", NULL, 0); break; case 4: mountmode = V4; fstype = "nfs"; nfsproto = IPPROTO_TCP; if (portspec == NULL) portspec = "2049"; break; default: errx(1, "illegal nfs version " "value -- %s", val); } pass_flag_to_nmount=0; } if (pass_flag_to_nmount) { build_iovec(&iov, &iovlen, opt, __DECONST(void *, val), strlen(val) + 1); } opt = pnextopt; } } break; case 'P': /* obsolete for -o noresvport now default */ printf("-P deprecated, use -o noresvport\n"); build_iovec(&iov, &iovlen, "noresvport", NULL, 0); break; case 'R': printf("-R deprecated, use -o retrycnt=\n"); num = strtol(optarg, &p, 10); if (*p || num < 0) errx(1, "illegal -R value -- %s", optarg); retrycnt = num; break; case 'r': printf("-r deprecated, use -o rsize=\n"); build_iovec(&iov, &iovlen, "rsize", optarg, (size_t)-1); break; case 's': printf("-s deprecated, use -o soft\n"); build_iovec(&iov, &iovlen, "soft", NULL, 0); break; case 'T': nfsproto = IPPROTO_TCP; printf("-T deprecated, use -o tcp\n"); break; case 't': printf("-t deprecated, use -o timeout=\n"); build_iovec(&iov, &iovlen, "timeout", optarg, (size_t)-1); break; case 'w': printf("-w deprecated, use -o wsize=\n"); build_iovec(&iov, &iovlen, "wsize", optarg, (size_t)-1); break; case 'x': printf("-x deprecated, use -o retrans=\n"); build_iovec(&iov, &iovlen, "retrans", optarg, (size_t)-1); break; case 'U': printf("-U deprecated, use -o mntudp\n"); mnttcp_ok = 0; nfsproto = IPPROTO_UDP; build_iovec(&iov, &iovlen, "mntudp", NULL, 0); break; default: usage(); break; } argc -= optind; argv += optind; if (argc != 2) { usage(); /* NOTREACHED */ } spec = *argv++; mntname = *argv; if (retrycnt == -1) /* The default is to keep retrying forever. */ retrycnt = 0; /* * If the fstye is "oldnfs", run the old NFS client unless the * "nfsv4" option was specified. */ if (strcmp(fstype, "nfs") == 0) { if (modfind("nfscl") < 0) { /* Not present in kernel, try loading it */ if (kldload("nfscl") < 0 || modfind("nfscl") < 0) errx(1, "nfscl is not available"); } } /* * Add the fqdn to the gssname, as required. */ if (gssname != NULL) { if (strchr(gssname, '@') == NULL && gethostname(hostname, MAXHOSTNAMELEN) == 0) { snprintf(gssn, sizeof (gssn), "%s@%s", gssname, hostname); gssname = gssn; } build_iovec(&iov, &iovlen, "gssname", __DECONST(void *, gssname), strlen(gssname) + 1); } if (!getnfsargs(spec, &iov, &iovlen)) exit(1); /* resolve the mountpoint with realpath(3) */ if (checkpath(mntname, mntpath) != 0) err(1, "%s", mntpath); build_iovec(&iov, &iovlen, "fstype", __DECONST(void *, fstype), (size_t)-1); build_iovec(&iov, &iovlen, "fspath", mntpath, (size_t)-1); build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); if (nmount(iov, iovlen, 0)) err(1, "nmount: %s%s%s", mntpath, errmsg[0] ? ", " : "", errmsg); exit(0); } static int sec_name_to_num(const char *sec) { if (!strcmp(sec, "krb5")) return (RPCSEC_GSS_KRB5); if (!strcmp(sec, "krb5i")) return (RPCSEC_GSS_KRB5I); if (!strcmp(sec, "krb5p")) return (RPCSEC_GSS_KRB5P); if (!strcmp(sec, "sys")) return (AUTH_SYS); return (-1); } static const char * sec_num_to_name(int flavor) { switch (flavor) { case RPCSEC_GSS_KRB5: return ("krb5"); case RPCSEC_GSS_KRB5I: return ("krb5i"); case RPCSEC_GSS_KRB5P: return ("krb5p"); case AUTH_SYS: return ("sys"); } return (NULL); } static int getnfsargs(char *spec, struct iovec **iov, int *iovlen) { struct addrinfo hints, *ai_nfs, *ai; enum tryret ret; int ecode, speclen, remoteerr, offset, have_bracket = 0; char *hostp, *delimp, *errstr; size_t len; static char nam[MNAMELEN + 1], pname[MAXHOSTNAMELEN + 5]; if (*spec == '[' && (delimp = strchr(spec + 1, ']')) != NULL && *(delimp + 1) == ':') { hostp = spec + 1; spec = delimp + 2; have_bracket = 1; } else if ((delimp = strrchr(spec, ':')) != NULL) { hostp = spec; spec = delimp + 1; } else if ((delimp = strrchr(spec, '@')) != NULL) { warnx("path@server syntax is deprecated, use server:path"); hostp = delimp + 1; } else { warnx("no : nfs-name"); return (0); } *delimp = '\0'; /* * If there has been a trailing slash at mounttime it seems * that some mountd implementations fail to remove the mount * entries from their mountlist while unmounting. */ for (speclen = strlen(spec); speclen > 1 && spec[speclen - 1] == '/'; speclen--) spec[speclen - 1] = '\0'; if (strlen(hostp) + strlen(spec) + 1 > MNAMELEN) { warnx("%s:%s: %s", hostp, spec, strerror(ENAMETOOLONG)); return (0); } /* Make both '@' and ':' notations equal */ if (*hostp != '\0') { len = strlen(hostp); offset = 0; if (have_bracket) nam[offset++] = '['; memmove(nam + offset, hostp, len); if (have_bracket) nam[len + offset++] = ']'; nam[len + offset++] = ':'; memmove(nam + len + offset, spec, speclen); nam[len + speclen + offset] = '\0'; } /* * Handle an internet host address. */ memset(&hints, 0, sizeof hints); hints.ai_flags = AI_NUMERICHOST; if (nfsproto == IPPROTO_TCP) hints.ai_socktype = SOCK_STREAM; else if (nfsproto == IPPROTO_UDP) hints.ai_socktype = SOCK_DGRAM; if (getaddrinfo(hostp, portspec, &hints, &ai_nfs) != 0) { hints.ai_flags = AI_CANONNAME; if ((ecode = getaddrinfo(hostp, portspec, &hints, &ai_nfs)) != 0) { if (portspec == NULL) errx(1, "%s: %s", hostp, gai_strerror(ecode)); else errx(1, "%s:%s: %s", hostp, portspec, gai_strerror(ecode)); return (0); } /* * For a Kerberized nfs mount where the "principal" * argument has not been set, add it here. */ if (got_principal == 0 && secflavor != AUTH_SYS && ai_nfs->ai_canonname != NULL) { snprintf(pname, sizeof (pname), "nfs@%s", ai_nfs->ai_canonname); build_iovec(iov, iovlen, "principal", pname, strlen(pname) + 1); } } ret = TRYRET_LOCALERR; for (;;) { /* * Try each entry returned by getaddrinfo(). Note the * occurrence of remote errors by setting `remoteerr'. */ remoteerr = 0; for (ai = ai_nfs; ai != NULL; ai = ai->ai_next) { if ((ai->ai_family == AF_INET6) && (opflags & OF_NOINET6)) continue; if ((ai->ai_family == AF_INET) && (opflags & OF_NOINET4)) continue; ret = nfs_tryproto(ai, hostp, spec, &errstr, iov, iovlen); if (ret == TRYRET_SUCCESS) break; if (ret != TRYRET_LOCALERR) remoteerr = 1; if ((opflags & ISBGRND) == 0) fprintf(stderr, "%s\n", errstr); } if (ret == TRYRET_SUCCESS) break; /* Exit if all errors were local. */ if (!remoteerr) exit(1); /* * If retrycnt == 0, we are to keep retrying forever. * Otherwise decrement it, and exit if it hits zero. */ if (retrycnt != 0 && --retrycnt == 0) exit(1); if ((opflags & (BGRND | ISBGRND)) == BGRND) { warnx("Cannot immediately mount %s:%s, backgrounding", hostp, spec); opflags |= ISBGRND; if (daemon(0, 0) != 0) err(1, "daemon"); } sleep(60); } freeaddrinfo(ai_nfs); build_iovec(iov, iovlen, "hostname", nam, (size_t)-1); /* Add mounted file system to PATH_MOUNTTAB */ if (!add_mtab(hostp, spec)) warnx("can't update %s for %s:%s", PATH_MOUNTTAB, hostp, spec); return (1); } /* * Try to set up the NFS arguments according to the address * family, protocol (and possibly port) specified in `ai'. * * Returns TRYRET_SUCCESS if successful, or: * TRYRET_TIMEOUT The server did not respond. * TRYRET_REMOTEERR The server reported an error. * TRYRET_LOCALERR Local failure. * * In all error cases, *errstr will be set to a statically-allocated string * describing the error. */ static enum tryret nfs_tryproto(struct addrinfo *ai, char *hostp, char *spec, char **errstr, struct iovec **iov, int *iovlen) { static char errbuf[256]; struct sockaddr_storage nfs_ss; struct netbuf nfs_nb; struct nfhret nfhret; struct timeval try; struct rpc_err rpcerr; CLIENT *clp; struct netconfig *nconf, *nconf_mnt; const char *netid, *netid_mnt, *secname; int doconnect, nfsvers, mntvers, sotype; enum clnt_stat clntstat; enum mountmode trymntmode; sotype = 0; trymntmode = mountmode; errbuf[0] = '\0'; *errstr = errbuf; if (nfsproto == IPPROTO_TCP) sotype = SOCK_STREAM; else if (nfsproto == IPPROTO_UDP) sotype = SOCK_DGRAM; if ((netid = netidbytype(ai->ai_family, sotype)) == NULL) { snprintf(errbuf, sizeof errbuf, "af %d sotype %d not supported", ai->ai_family, sotype); return (TRYRET_LOCALERR); } if ((nconf = getnetconf_cached(netid)) == NULL) { snprintf(errbuf, sizeof errbuf, "%s: %s", netid, nc_sperror()); return (TRYRET_LOCALERR); } /* The RPCPROG_MNT netid may be different. */ if (mnttcp_ok) { netid_mnt = netid; nconf_mnt = nconf; } else { if ((netid_mnt = netidbytype(ai->ai_family, SOCK_DGRAM)) == NULL) { snprintf(errbuf, sizeof errbuf, "af %d sotype SOCK_DGRAM not supported", ai->ai_family); return (TRYRET_LOCALERR); } if ((nconf_mnt = getnetconf_cached(netid_mnt)) == NULL) { snprintf(errbuf, sizeof errbuf, "%s: %s", netid_mnt, nc_sperror()); return (TRYRET_LOCALERR); } } tryagain: if (trymntmode == V4) { nfsvers = 4; mntvers = 3; /* Workaround for GCC. */ } else if (trymntmode == V2) { nfsvers = 2; mntvers = 1; } else { nfsvers = 3; mntvers = 3; } if (portspec != NULL) { /* `ai' contains the complete nfsd sockaddr. */ nfs_nb.buf = ai->ai_addr; nfs_nb.len = nfs_nb.maxlen = ai->ai_addrlen; } else { /* Ask the remote rpcbind. */ nfs_nb.buf = &nfs_ss; nfs_nb.len = nfs_nb.maxlen = sizeof nfs_ss; if (!rpcb_getaddr(NFS_PROGRAM, nfsvers, nconf, &nfs_nb, hostp)) { if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { trymntmode = V2; goto tryagain; } snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, hostp, spec, clnt_spcreateerror("RPCPROG_NFS")); return (returncode(rpc_createerr.cf_stat, &rpc_createerr.cf_error)); } } /* Check that the server (nfsd) responds on the port we have chosen. */ clp = clnt_tli_create(RPC_ANYFD, nconf, &nfs_nb, NFS_PROGRAM, nfsvers, 0, 0); if (clp == NULL) { snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, hostp, spec, clnt_spcreateerror("nfsd: RPCPROG_NFS")); return (returncode(rpc_createerr.cf_stat, &rpc_createerr.cf_error)); } if (sotype == SOCK_DGRAM && noconn == 0) { /* * Use connect(), to match what the kernel does. This * catches cases where the server responds from the * wrong source address. */ doconnect = 1; if (!clnt_control(clp, CLSET_CONNECT, (char *)&doconnect)) { clnt_destroy(clp); snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: CLSET_CONNECT failed", netid, hostp, spec); return (TRYRET_LOCALERR); } } try.tv_sec = 10; try.tv_usec = 0; clntstat = clnt_call(clp, NFSPROC_NULL, (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_void, NULL, try); if (clntstat != RPC_SUCCESS) { if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { clnt_destroy(clp); trymntmode = V2; goto tryagain; } clnt_geterr(clp, &rpcerr); snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid, hostp, spec, clnt_sperror(clp, "NFSPROC_NULL")); clnt_destroy(clp); return (returncode(clntstat, &rpcerr)); } clnt_destroy(clp); /* * For NFSv4, there is no mount protocol. */ if (trymntmode == V4) { /* * Store the server address in nfsargsp, making * sure to copy any locally allocated structures. */ addrlen = nfs_nb.len; addr = malloc(addrlen); if (addr == NULL) err(1, "malloc"); bcopy(nfs_nb.buf, addr, addrlen); build_iovec(iov, iovlen, "addr", addr, addrlen); secname = sec_num_to_name(secflavor); if (secname != NULL) { build_iovec(iov, iovlen, "sec", __DECONST(void *, secname), (size_t)-1); } build_iovec(iov, iovlen, "nfsv4", NULL, 0); build_iovec(iov, iovlen, "dirpath", spec, (size_t)-1); return (TRYRET_SUCCESS); } /* Send the MOUNTPROC_MNT RPC to get the root filehandle. */ try.tv_sec = 10; try.tv_usec = 0; clp = clnt_tp_create(hostp, MOUNTPROG, mntvers, nconf_mnt); if (clp == NULL) { snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, hostp, spec, clnt_spcreateerror("RPCMNT: clnt_create")); return (returncode(rpc_createerr.cf_stat, &rpc_createerr.cf_error)); } clp->cl_auth = authsys_create_default(); nfhret.auth = secflavor; nfhret.vers = mntvers; clntstat = clnt_call(clp, MOUNTPROC_MNT, (xdrproc_t)xdr_dir, spec, (xdrproc_t)xdr_fh, &nfhret, try); auth_destroy(clp->cl_auth); if (clntstat != RPC_SUCCESS) { if (clntstat == RPC_PROGVERSMISMATCH && trymntmode == ANY) { clnt_destroy(clp); trymntmode = V2; goto tryagain; } clnt_geterr(clp, &rpcerr); snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, hostp, spec, clnt_sperror(clp, "RPCPROG_MNT")); clnt_destroy(clp); return (returncode(clntstat, &rpcerr)); } clnt_destroy(clp); if (nfhret.stat != 0) { snprintf(errbuf, sizeof errbuf, "[%s] %s:%s: %s", netid_mnt, hostp, spec, strerror(nfhret.stat)); return (TRYRET_REMOTEERR); } /* * Store the filehandle and server address in nfsargsp, making * sure to copy any locally allocated structures. */ addrlen = nfs_nb.len; addr = malloc(addrlen); fhsize = nfhret.fhsize; fh = malloc(fhsize); if (addr == NULL || fh == NULL) err(1, "malloc"); bcopy(nfs_nb.buf, addr, addrlen); bcopy(nfhret.nfh, fh, fhsize); build_iovec(iov, iovlen, "addr", addr, addrlen); build_iovec(iov, iovlen, "fh", fh, fhsize); secname = sec_num_to_name(nfhret.auth); if (secname) { build_iovec(iov, iovlen, "sec", __DECONST(void *, secname), (size_t)-1); } if (nfsvers == 3) build_iovec(iov, iovlen, "nfsv3", NULL, 0); return (TRYRET_SUCCESS); } /* * Catagorise a RPC return status and error into an `enum tryret' * return code. */ static enum tryret returncode(enum clnt_stat clntstat, struct rpc_err *rpcerr) { switch (clntstat) { case RPC_TIMEDOUT: return (TRYRET_TIMEOUT); case RPC_PMAPFAILURE: case RPC_PROGNOTREGISTERED: case RPC_PROGVERSMISMATCH: /* XXX, these can be local or remote. */ case RPC_CANTSEND: case RPC_CANTRECV: return (TRYRET_REMOTEERR); case RPC_SYSTEMERROR: switch (rpcerr->re_errno) { case ETIMEDOUT: return (TRYRET_TIMEOUT); case ENOMEM: break; default: return (TRYRET_REMOTEERR); } /* FALLTHROUGH */ default: break; } return (TRYRET_LOCALERR); } /* * Look up a netid based on an address family and socket type. * `af' is the address family, and `sotype' is SOCK_DGRAM or SOCK_STREAM. * * XXX there should be a library function for this. */ static const char * netidbytype(int af, int sotype) { struct nc_protos *p; for (p = nc_protos; p->netid != NULL; p++) { if (af != p->af || sotype != p->sotype) continue; return (p->netid); } return (NULL); } /* * Look up a netconfig entry based on a netid, and cache the result so * that we don't need to remember to call freenetconfigent(). * * Otherwise it behaves just like getnetconfigent(), so nc_*error() * work on failure. */ static struct netconfig * getnetconf_cached(const char *netid) { static struct nc_entry { struct netconfig *nconf; struct nc_entry *next; } *head; struct nc_entry *p; struct netconfig *nconf; for (p = head; p != NULL; p = p->next) if (strcmp(netid, p->nconf->nc_netid) == 0) return (p->nconf); if ((nconf = getnetconfigent(netid)) == NULL) return (NULL); if ((p = malloc(sizeof(*p))) == NULL) err(1, "malloc"); p->nconf = nconf; p->next = head; head = p; return (p->nconf); } /* * xdr routines for mount rpc's */ static int xdr_dir(XDR *xdrsp, char *dirp) { return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); } static int xdr_fh(XDR *xdrsp, struct nfhret *np) { int i; long auth, authcnt, authfnd = 0; if (!xdr_u_long(xdrsp, &np->stat)) return (0); if (np->stat) return (1); switch (np->vers) { case 1: np->fhsize = NFS_FHSIZE; return (xdr_opaque(xdrsp, (caddr_t)np->nfh, NFS_FHSIZE)); case 3: if (!xdr_long(xdrsp, &np->fhsize)) return (0); if (np->fhsize <= 0 || np->fhsize > NFS3_FHSIZE) return (0); if (!xdr_opaque(xdrsp, (caddr_t)np->nfh, np->fhsize)) return (0); if (!xdr_long(xdrsp, &authcnt)) return (0); for (i = 0; i < authcnt; i++) { if (!xdr_long(xdrsp, &auth)) return (0); if (np->auth == -1) { np->auth = auth; authfnd++; } else if (auth == np->auth) { authfnd++; } } /* * Some servers, such as DEC's OSF/1 return a nil authenticator * list to indicate RPCAUTH_UNIX. */ if (authcnt == 0 && np->auth == -1) np->auth = AUTH_SYS; if (!authfnd && (authcnt > 0 || np->auth != AUTH_SYS)) np->stat = EAUTH; return (1); - }; + } return (0); } static void usage(void) { (void)fprintf(stderr, "%s\n%s\n%s\n%s\n", "usage: mount_nfs [-23bcdiLlNPsTU] [-a maxreadahead] [-D deadthresh]", " [-g maxgroups] [-I readdirsize] [-o options] [-R retrycnt]", " [-r readsize] [-t timeout] [-w writesize] [-x retrans]", " rhost:path node"); exit(1); } Index: head/usr.bin/bsdiff/bsdiff/bsdiff.c =================================================================== --- head/usr.bin/bsdiff/bsdiff/bsdiff.c (revision 298088) +++ head/usr.bin/bsdiff/bsdiff/bsdiff.c (revision 298089) @@ -1,436 +1,436 @@ /*- * Copyright 2003-2005 Colin Percival * All rights reserved * * Redistribution and use in source and binary forms, with or without * modification, are permitted providing 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 ``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 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 #include #include #include #include #ifndef O_BINARY #define O_BINARY 0 #endif #define MIN(x,y) (((x)<(y)) ? (x) : (y)) static void split(off_t *I,off_t *V,off_t start,off_t len,off_t h) { off_t i,j,k,x,tmp,jj,kk; if(len<16) { for(k=start;kstart) split(I,V,start,jj-start,h); for(i=0;ikk) split(I,V,kk,start+len-kk,h); } static void qsufsort(off_t *I,off_t *V,u_char *old,off_t oldsize) { off_t buckets[256]; off_t i,h,len; for(i=0;i<256;i++) buckets[i]=0; for(i=0;i0;i--) buckets[i]=buckets[i-1]; buckets[0]=0; for(i=0;iy) { *pos=I[st]; return x; } else { *pos=I[en]; return y; } - }; + } x=st+(en-st)/2; if(memcmp(old+I[x],new,MIN(oldsize-I[x],newsize))<0) { return search(I,old,oldsize,new,newsize,x,en,pos); } else { return search(I,old,oldsize,new,newsize,st,x,pos); }; } static void offtout(off_t x,u_char *buf) { off_t y; if(x<0) y=-x; else y=x; buf[0]=y%256;y-=buf[0]; y=y/256;buf[1]=y%256;y-=buf[1]; y=y/256;buf[2]=y%256;y-=buf[2]; y=y/256;buf[3]=y%256;y-=buf[3]; y=y/256;buf[4]=y%256;y-=buf[4]; y=y/256;buf[5]=y%256;y-=buf[5]; y=y/256;buf[6]=y%256;y-=buf[6]; y=y/256;buf[7]=y%256; if(x<0) buf[7]|=0x80; } static void usage(void) { fprintf(stderr, "usage: bsdiff oldfile newfile patchfile\n"); exit(1); } int main(int argc,char *argv[]) { int fd; u_char *old,*new; off_t oldsize,newsize; off_t *I,*V; off_t scan,pos,len; off_t lastscan,lastpos,lastoffset; off_t oldscore,scsc; off_t s,Sf,lenf,Sb,lenb; off_t overlap,Ss,lens; off_t i; off_t dblen,eblen; u_char *db,*eb; u_char buf[8]; u_char header[32]; FILE * pf; BZFILE * pfbz2; int bz2err; if (argc != 4) usage(); /* Allocate oldsize+1 bytes instead of oldsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) || ((oldsize=lseek(fd,0,SEEK_END))==-1)) err(1, "%s", argv[1]); if (oldsize > SSIZE_MAX || (uintmax_t)oldsize >= SIZE_T_MAX / sizeof(off_t) || oldsize == OFF_MAX) { errno = EFBIG; err(1, "%s", argv[1]); } if (((old=malloc(oldsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || (read(fd,old,oldsize)!=oldsize) || (close(fd)==-1)) err(1,"%s",argv[1]); if(((I=malloc((oldsize+1)*sizeof(off_t)))==NULL) || ((V=malloc((oldsize+1)*sizeof(off_t)))==NULL)) err(1,NULL); qsufsort(I,V,old,oldsize); free(V); /* Allocate newsize+1 bytes instead of newsize bytes to ensure that we never try to malloc(0) and get a NULL pointer */ if(((fd=open(argv[2],O_RDONLY|O_BINARY,0))<0) || ((newsize=lseek(fd,0,SEEK_END))==-1)) err(1, "%s", argv[2]); if (newsize > SSIZE_MAX || (uintmax_t)newsize >= SIZE_T_MAX || newsize == OFF_MAX) { errno = EFBIG; err(1, "%s", argv[2]); } if (((new=malloc(newsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || (read(fd,new,newsize)!=newsize) || (close(fd)==-1)) err(1,"%s",argv[2]); if(((db=malloc(newsize+1))==NULL) || ((eb=malloc(newsize+1))==NULL)) err(1,NULL); dblen=0; eblen=0; /* Create the patch file */ if ((pf = fopen(argv[3], "wb")) == NULL) err(1, "%s", argv[3]); /* Header is 0 8 "BSDIFF40" 8 8 length of bzip2ed ctrl block 16 8 length of bzip2ed diff block 24 8 length of new file */ /* File is 0 32 Header 32 ?? Bzip2ed ctrl block ?? ?? Bzip2ed diff block ?? ?? Bzip2ed extra block */ memcpy(header,"BSDIFF40",8); offtout(0, header + 8); offtout(0, header + 16); offtout(newsize, header + 24); if (fwrite(header, 32, 1, pf) != 1) err(1, "fwrite(%s)", argv[3]); /* Compute the differences, writing ctrl as we go */ if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL) errx(1, "BZ2_bzWriteOpen, bz2err = %d", bz2err); scan=0;len=0;pos=0; lastscan=0;lastpos=0;lastoffset=0; while(scanoldscore+8)) break; if((scan+lastoffsetSf*2-lenf) { Sf=s; lenf=i; }; - }; + if(s*2-i>Sf*2-lenf) { Sf=s; lenf=i; } + } lenb=0; if(scan=lastscan+i)&&(pos>=i);i++) { if(old[pos-i]==new[scan-i]) s++; - if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; }; - }; - }; + if(s*2-i>Sb*2-lenb) { Sb=s; lenb=i; } + } + } if(lastscan+lenf>scan-lenb) { overlap=(lastscan+lenf)-(scan-lenb); s=0;Ss=0;lens=0; for(i=0;iSs) { Ss=s; lens=i+1; }; - }; + if(s>Ss) { Ss=s; lens=i+1; } + } lenf+=lens-overlap; lenb-=lens; - }; + } for(i=0;i __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #ifndef O_BINARY #define O_BINARY 0 #endif static off_t offtin(u_char *buf) { off_t y; y=buf[7]&0x7F; y=y*256;y+=buf[6]; y=y*256;y+=buf[5]; y=y*256;y+=buf[4]; y=y*256;y+=buf[3]; y=y*256;y+=buf[2]; y=y*256;y+=buf[1]; y=y*256;y+=buf[0]; if(buf[7]&0x80) y=-y; return y; } static void usage(void) { fprintf(stderr, "usage: bspatch oldfile newfile patchfile\n"); exit(1); } int main(int argc,char * argv[]) { FILE * f, * cpf, * dpf, * epf; BZFILE * cpfbz2, * dpfbz2, * epfbz2; int cbz2err, dbz2err, ebz2err; int fd; ssize_t oldsize,newsize; ssize_t bzctrllen,bzdatalen; u_char header[32],buf[8]; u_char *old, *new; off_t oldpos,newpos; off_t ctrl[3]; off_t lenread; off_t i; if (argc != 4) usage(); /* Open patch file */ if ((f = fopen(argv[3], "rb")) == NULL) err(1, "fopen(%s)", argv[3]); /* File format: 0 8 "BSDIFF40" 8 8 X 16 8 Y 24 8 sizeof(newfile) 32 X bzip2(control block) 32+X Y bzip2(diff block) 32+X+Y ??? bzip2(extra block) with control block a set of triples (x,y,z) meaning "add x bytes from oldfile to x bytes from the diff block; copy y bytes from the extra block; seek forwards in oldfile by z bytes". */ /* Read header */ if (fread(header, 1, 32, f) < 32) { if (feof(f)) errx(1, "Corrupt patch\n"); err(1, "fread(%s)", argv[3]); } /* Check for appropriate magic */ if (memcmp(header, "BSDIFF40", 8) != 0) errx(1, "Corrupt patch\n"); /* Read lengths from header */ bzctrllen=offtin(header+8); bzdatalen=offtin(header+16); newsize=offtin(header+24); if((bzctrllen<0) || (bzdatalen<0) || (newsize<0)) errx(1,"Corrupt patch\n"); /* Close patch file and re-open it via libbzip2 at the right places */ if (fclose(f)) err(1, "fclose(%s)", argv[3]); if ((cpf = fopen(argv[3], "rb")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(cpf, 32, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)32); if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err); if ((dpf = fopen(argv[3], "rb")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(dpf, 32 + bzctrllen, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)(32 + bzctrllen)); if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err); if ((epf = fopen(argv[3], "rb")) == NULL) err(1, "fopen(%s)", argv[3]); if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET)) err(1, "fseeko(%s, %lld)", argv[3], (long long)(32 + bzctrllen + bzdatalen)); if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL) errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err); if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) || ((oldsize=lseek(fd,0,SEEK_END))==-1) || ((old=malloc(oldsize+1))==NULL) || (lseek(fd,0,SEEK_SET)!=0) || (read(fd,old,oldsize)!=oldsize) || (close(fd)==-1)) err(1,"%s",argv[1]); if((new=malloc(newsize+1))==NULL) err(1,NULL); oldpos=0;newpos=0; while(newposnewsize) errx(1,"Corrupt patch\n"); /* Read diff string */ lenread = BZ2_bzRead(&dbz2err, dpfbz2, new + newpos, ctrl[0]); if ((lenread < ctrl[0]) || ((dbz2err != BZ_OK) && (dbz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); /* Add old data to diff string */ for(i=0;i=0) && (oldpos+inewsize) errx(1,"Corrupt patch\n"); /* Read extra string */ lenread = BZ2_bzRead(&ebz2err, epfbz2, new + newpos, ctrl[1]); if ((lenread < ctrl[1]) || ((ebz2err != BZ_OK) && (ebz2err != BZ_STREAM_END))) errx(1, "Corrupt patch\n"); /* Adjust pointers */ newpos+=ctrl[1]; oldpos+=ctrl[2]; - }; + } /* Clean up the bzip2 reads */ BZ2_bzReadClose(&cbz2err, cpfbz2); BZ2_bzReadClose(&dbz2err, dpfbz2); BZ2_bzReadClose(&ebz2err, epfbz2); if (fclose(cpf) || fclose(dpf) || fclose(epf)) err(1, "fclose(%s)", argv[3]); /* Write the new file */ if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))<0) || (write(fd,new,newsize)!=newsize) || (close(fd)==-1)) err(1,"%s",argv[2]); free(new); free(old); return 0; } Index: head/usr.bin/calendar/dates.c =================================================================== --- head/usr.bin/calendar/dates.c (revision 298088) +++ head/usr.bin/calendar/dates.c (revision 298089) @@ -1,451 +1,451 @@ /*- * Copyright (c) 1992-2009 Edwin Groothuis . * 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. * */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include "calendar.h" struct cal_year { int year; /* 19xx, 20xx, 21xx */ int easter; /* Julian day */ int paskha; /* Julian day */ int cny; /* Julian day */ int firstdayofweek; /* 0 .. 6 */ struct cal_month *months; struct cal_year *nextyear; }; struct cal_month { int month; /* 01 .. 12 */ int firstdayjulian; /* 000 .. 366 */ int firstdayofweek; /* 0 .. 6 */ struct cal_year *year; /* points back */ struct cal_day *days; struct cal_month *nextmonth; }; struct cal_day { int dayofmonth; /* 01 .. 31 */ int julianday; /* 000 .. 366 */ int dayofweek; /* 0 .. 6 */ struct cal_day *nextday; struct cal_month *month; /* points back */ struct cal_year *year; /* points back */ struct event *events; }; int debug_remember = 0; static struct cal_year *hyear = NULL; /* 1-based month, 0-based days, cumulative */ int cumdaytab[][14] = { {0, -1, 30, 58, 89, 119, 150, 180, 211, 242, 272, 303, 333, 364}, {0, -1, 30, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365}, }; /* 1-based month, individual */ static int *monthdays; int monthdaytab[][14] = { {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30}, {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 30}, }; static struct cal_day * find_day(int yy, int mm, int dd); static void createdate(int y, int m, int d) { struct cal_year *py, *pyp; struct cal_month *pm, *pmp; struct cal_day *pd, *pdp; int *cumday; pyp = NULL; py = hyear; while (py != NULL) { if (py->year == y + 1900) break; pyp = py; py = py->nextyear; } if (py == NULL) { struct tm td; time_t t; py = (struct cal_year *)calloc(1, sizeof(struct cal_year)); py->year = y + 1900; py->easter = easter(y); py->paskha = paskha(y); td = tm0; td.tm_year = y; td.tm_mday = 1; t = mktime(&td); localtime_r(&t, &td); py->firstdayofweek = td.tm_wday; if (pyp != NULL) pyp->nextyear = py; } if (pyp == NULL) { /* The very very very first one */ hyear = py; } pmp = NULL; pm = py->months; while (pm != NULL) { if (pm->month == m) break; pmp = pm; pm = pm->nextmonth; } if (pm == NULL) { pm = (struct cal_month *)calloc(1, sizeof(struct cal_month)); pm->year = py; pm->month = m; cumday = cumdaytab[isleap(y)]; pm->firstdayjulian = cumday[m] + 2; pm->firstdayofweek = (py->firstdayofweek + pm->firstdayjulian -1) % 7; if (pmp != NULL) pmp->nextmonth = pm; } if (pmp == NULL) py->months = pm; pdp = NULL; pd = pm->days; while (pd != NULL) { pdp = pd; pd = pd->nextday; } if (pd == NULL) { /* Always true */ pd = (struct cal_day *)calloc(1, sizeof(struct cal_day)); pd->month = pm; pd->year = py; pd->dayofmonth = d; pd->julianday = pm->firstdayjulian + d - 1; pd->dayofweek = (pm->firstdayofweek + d - 1) % 7; if (pdp != NULL) pdp->nextday = pd; } if (pdp == NULL) pm->days = pd; } void generatedates(struct tm *tp1, struct tm *tp2) { int y1, m1, d1; int y2, m2, d2; int y, m, d; y1 = tp1->tm_year; m1 = tp1->tm_mon + 1; d1 = tp1->tm_mday; y2 = tp2->tm_year; m2 = tp2->tm_mon + 1; d2 = tp2->tm_mday; if (y1 == y2) { if (m1 == m2) { /* Same year, same month. Easy! */ for (d = d1; d <= d2; d++) createdate(y1, m1, d); return; } /* * Same year, different month. * - Take the leftover days from m1 * - Take all days from * - Take the first days from m2 */ monthdays = monthdaytab[isleap(y1)]; for (d = d1; d <= monthdays[m1]; d++) createdate(y1, m1, d); for (m = m1 + 1; m < m2; m++) for (d = 1; d <= monthdays[m]; d++) createdate(y1, m, d); for (d = 1; d <= d2; d++) createdate(y1, m2, d); return; } /* * Different year, different month. * - Take the leftover days from y1-m1 * - Take all days from y1- * - Take all days from y2-[1 .. m2> * - Take the first days of y2-m2 */ monthdays = monthdaytab[isleap(y1)]; for (d = d1; d <= monthdays[m1]; d++) createdate(y1, m1, d); for (m = m1 + 1; m <= 12; m++) for (d = 1; d <= monthdays[m]; d++) createdate(y1, m, d); for (y = y1 + 1; y < y2; y++) { monthdays = monthdaytab[isleap(y)]; for (m = 1; m <= 12; m++) for (d = 1; d <= monthdays[m]; d++) createdate(y, m, d); } monthdays = monthdaytab[isleap(y2)]; for (m = 1; m < m2; m++) for (d = 1; d <= monthdays[m]; d++) createdate(y2, m, d); for (d = 1; d <= d2; d++) createdate(y2, m2, d); } void dumpdates(void) { struct cal_year *y; struct cal_month *m; struct cal_day *d; y = hyear; while (y != NULL) { printf("%-5d (wday:%d)\n", y->year, y->firstdayofweek); m = y->months; while (m != NULL) { printf("-- %-5d (julian:%d, dow:%d)\n", m->month, m->firstdayjulian, m->firstdayofweek); d = m->days; while (d != NULL) { printf(" -- %-5d (julian:%d, dow:%d)\n", d->dayofmonth, d->julianday, d->dayofweek); d = d->nextday; } m = m->nextmonth; } y = y->nextyear; } } int remember_ymd(int yy, int mm, int dd) { struct cal_year *y; struct cal_month *m; struct cal_day *d; if (debug_remember) printf("remember_ymd: %d - %d - %d\n", yy, mm, dd); y = hyear; while (y != NULL) { if (y->year != yy) { y = y->nextyear; continue; } m = y->months; while (m != NULL) { if (m->month != mm) { m = m->nextmonth; continue; } d = m->days; while (d != NULL) { if (d->dayofmonth == dd) return (1); d = d->nextday; continue; } return (0); } return (0); } return (0); } int remember_yd(int yy, int dd, int *rm, int *rd) { struct cal_year *y; struct cal_month *m; struct cal_day *d; if (debug_remember) printf("remember_yd: %d - %d\n", yy, dd); y = hyear; while (y != NULL) { if (y->year != yy) { y = y->nextyear; continue; } m = y->months; while (m != NULL) { d = m->days; while (d != NULL) { if (d->julianday == dd) { *rm = m->month; *rd = d->dayofmonth; return (1); } d = d->nextday; } m = m->nextmonth; } return (0); } return (0); } int first_dayofweek_of_year(int yy) { struct cal_year *y; y = hyear; while (y != NULL) { if (y->year == yy) return (y->firstdayofweek); y = y->nextyear; } /* Should not happen */ return (-1); } int first_dayofweek_of_month(int yy, int mm) { struct cal_year *y; struct cal_month *m; y = hyear; while (y != NULL) { if (y->year != yy) { y = y->nextyear; continue; } m = y->months; while (m != NULL) { if (m->month == mm) return (m->firstdayofweek); m = m->nextmonth; } /* No data for this month */ return (-1); } /* No data for this year. Error? */ return (-1); } int walkthrough_dates(struct event **e) { static struct cal_year *y = NULL; static struct cal_month *m = NULL; static struct cal_day *d = NULL; if (y == NULL) { y = hyear; m = y->months; d = m->days; *e = d->events; return (1); - }; + } if (d->nextday != NULL) { d = d->nextday; *e = d->events; return (1); } if (m->nextmonth != NULL) { m = m->nextmonth; d = m->days; *e = d->events; return (1); } if (y->nextyear != NULL) { y = y->nextyear; m = y->months; d = m->days; *e = d->events; return (1); } return (0); } static struct cal_day * find_day(int yy, int mm, int dd) { struct cal_year *y; struct cal_month *m; struct cal_day *d; if (debug_remember) printf("remember_ymd: %d - %d - %d\n", yy, mm, dd); y = hyear; while (y != NULL) { if (y->year != yy) { y = y->nextyear; continue; } m = y->months; while (m != NULL) { if (m->month != mm) { m = m->nextmonth; continue; } d = m->days; while (d != NULL) { if (d->dayofmonth == dd) return (d); d = d->nextday; continue; } return (NULL); } return (NULL); } return (NULL); } void addtodate(struct event *e, int year, int month, int day) { struct cal_day *d; d = find_day(year, month, day); e->next = d->events; d->events = e; } Index: head/usr.bin/calendar/io.c =================================================================== --- head/usr.bin/calendar/io.c (revision 298088) +++ head/usr.bin/calendar/io.c (revision 298089) @@ -1,503 +1,503 @@ /*- * Copyright (c) 1989, 1993, 1994 * 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1989, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif #if 0 #ifndef lint static char sccsid[] = "@(#)calendar.c 8.3 (Berkeley) 3/25/94"; #endif #endif #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #define _WITH_GETLINE #include #include #include #include #include #include "pathnames.h" #include "calendar.h" enum { T_OK = 0, T_ERR, T_PROCESS, }; const char *calendarFile = "calendar"; /* default calendar file */ static const char *calendarHomes[] = {".calendar", _PATH_INCLUDE}; /* HOME */ static const char *calendarNoMail = "nomail";/* don't sent mail if file exist */ static char path[MAXPATHLEN]; struct fixs neaster, npaskha, ncny, nfullmoon, nnewmoon; struct fixs nmarequinox, nsepequinox, njunsolstice, ndecsolstice; static int cal_parse(FILE *in, FILE *out); static StringList *definitions = NULL; static struct event *events[MAXCOUNT]; static char *extradata[MAXCOUNT]; static void trimlr(char **buf) { char *walk = *buf; char *last; while (isspace(*walk)) walk++; if (*walk != '\0') { last = walk + strlen(walk) - 1; while (last > walk && isspace(*last)) last--; *(last+1) = 0; } *buf = walk; } static FILE * cal_fopen(const char *file) { FILE *fp; char *home = getenv("HOME"); unsigned int i; if (home == NULL || *home == '\0') { warnx("Cannot get home directory"); return (NULL); } if (chdir(home) != 0) { warnx("Cannot enter home directory"); return (NULL); } for (i = 0; i < sizeof(calendarHomes)/sizeof(calendarHomes[0]) ; i++) { if (chdir(calendarHomes[i]) != 0) continue; if ((fp = fopen(file, "r")) != NULL) return (fp); } warnx("can't open calendar file \"%s\"", file); return (NULL); } static int token(char *line, FILE *out, bool *skip) { char *walk, c, a; if (strncmp(line, "endif", 5) == 0) { *skip = false; return (T_OK); } if (*skip) return (T_OK); if (strncmp(line, "include", 7) == 0) { walk = line + 7; trimlr(&walk); if (*walk == '\0') { warnx("Expecting arguments after #include"); return (T_ERR); } if (*walk != '<' && *walk != '\"') { warnx("Excecting '<' or '\"' after #include"); return (T_ERR); } a = *walk; walk++; c = walk[strlen(walk) - 1]; switch(c) { case '>': if (a != '<') { warnx("Unterminated include expecting '\"'"); return (T_ERR); } break; case '\"': if (a != '\"') { warnx("Unterminated include expecting '>'"); return (T_ERR); } break; default: warnx("Unterminated include expecting '%c'", a == '<' ? '>' : '\"' ); return (T_ERR); } walk[strlen(walk) - 1] = '\0'; if (cal_parse(cal_fopen(walk), out)) return (T_ERR); return (T_OK); } if (strncmp(line, "define", 6) == 0) { if (definitions == NULL) definitions = sl_init(); walk = line + 6; trimlr(&walk); if (*walk == '\0') { warnx("Expecting arguments after #define"); return (T_ERR); } sl_add(definitions, strdup(walk)); return (T_OK); } if (strncmp(line, "ifndef", 6) == 0) { walk = line + 6; trimlr(&walk); if (*walk == '\0') { warnx("Expecting arguments after #ifndef"); return (T_ERR); } if (definitions != NULL && sl_find(definitions, walk) != NULL) *skip = true; return (T_OK); } return (T_PROCESS); } #define REPLACE(string, slen, struct_) \ if (strncasecmp(buf, (string), (slen)) == 0 && buf[(slen)]) { \ if (struct_.name != NULL) \ free(struct_.name); \ if ((struct_.name = strdup(buf + (slen))) == NULL) \ errx(1, "cannot allocate memory"); \ struct_.len = strlen(buf + (slen)); \ continue; \ } static int cal_parse(FILE *in, FILE *out) { char *line = NULL; char *buf; size_t linecap = 0; ssize_t linelen; ssize_t l; static int d_first = -1; static int count = 0; int i; int month[MAXCOUNT]; int day[MAXCOUNT]; int year[MAXCOUNT]; bool skip = false; char dbuf[80]; char *pp, p; struct tm tm; int flags; /* Unused */ tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; tm.tm_wday = 0; if (in == NULL) return (1); while ((linelen = getline(&line, &linecap, in)) > 0) { if (*line == '#') { switch (token(line+1, out, &skip)) { case T_ERR: free(line); return (1); case T_OK: continue; case T_PROCESS: break; default: break; } } if (skip) continue; buf = line; for (l = linelen; l > 0 && isspace((unsigned char)buf[l - 1]); l--) ; buf[l] = '\0'; if (buf[0] == '\0') continue; /* Parse special definitions: LANG, Easter, Paskha etc */ if (strncmp(buf, "LANG=", 5) == 0) { (void)setlocale(LC_ALL, buf + 5); d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); setnnames(); continue; } REPLACE("Easter=", 7, neaster); REPLACE("Paskha=", 7, npaskha); REPLACE("ChineseNewYear=", 15, ncny); REPLACE("NewMoon=", 8, nnewmoon); REPLACE("FullMoon=", 9, nfullmoon); REPLACE("MarEquinox=", 11, nmarequinox); REPLACE("SepEquinox=", 11, nsepequinox); REPLACE("JunSolstice=", 12, njunsolstice); REPLACE("DecSolstice=", 12, ndecsolstice); if (strncmp(buf, "SEQUENCE=", 9) == 0) { setnsequences(buf + 9); continue; } /* * If the line starts with a tab, the data has to be * added to the previous line */ if (buf[0] == '\t') { for (i = 0; i < count; i++) event_continue(events[i], buf); continue; } /* Get rid of leading spaces (non-standard) */ while (isspace((unsigned char)buf[0])) memcpy(buf, buf + 1, strlen(buf)); /* No tab in the line, then not a valid line */ if ((pp = strchr(buf, '\t')) == NULL) continue; /* Trim spaces in front of the tab */ while (isspace((unsigned char)pp[-1])) pp--; p = *pp; *pp = '\0'; if ((count = parsedaymonth(buf, year, month, day, &flags, extradata)) == 0) continue; *pp = p; if (count < 0) { /* Show error status based on return value */ if (debug) fprintf(stderr, "Ignored: %s\n", buf); if (count == -1) continue; count = -count + 1; } /* Find the last tab */ while (pp[1] == '\t') pp++; if (d_first < 0) d_first = (*nl_langinfo(D_MD_ORDER) == 'd'); for (i = 0; i < count; i++) { tm.tm_mon = month[i] - 1; tm.tm_mday = day[i]; tm.tm_year = year[i] - 1900; (void)strftime(dbuf, sizeof(dbuf), d_first ? "%e %b" : "%b %e", &tm); if (debug) fprintf(stderr, "got %s\n", pp); events[i] = event_add(year[i], month[i], day[i], dbuf, ((flags &= F_VARIABLE) != 0) ? 1 : 0, pp, extradata[i]); } } free(line); fclose(in); return (0); } void cal(void) { FILE *fpin; FILE *fpout; int i; for (i = 0; i < MAXCOUNT; i++) extradata[i] = (char *)calloc(1, 20); if ((fpin = opencalin()) == NULL) return; if ((fpout = opencalout()) == NULL) { fclose(fpin); return; } if (cal_parse(fpin, fpout)) return; event_print_all(fpout); closecal(fpout); } FILE * opencalin(void) { struct stat sbuf; FILE *fpin; /* open up calendar file */ if ((fpin = fopen(calendarFile, "r")) == NULL) { if (doall) { if (chdir(calendarHomes[0]) != 0) return (NULL); if (stat(calendarNoMail, &sbuf) == 0) return (NULL); if ((fpin = fopen(calendarFile, "r")) == NULL) return (NULL); } else { fpin = cal_fopen(calendarFile); } } return (fpin); } FILE * opencalout(void) { int fd; /* not reading all calendar files, just set output to stdout */ if (!doall) return (stdout); /* set output to a temporary file, so if no output don't send mail */ snprintf(path, sizeof(path), "%s/_calXXXXXX", _PATH_TMP); if ((fd = mkstemp(path)) < 0) return (NULL); return (fdopen(fd, "w+")); } void closecal(FILE *fp) { uid_t uid; struct stat sbuf; int nread, pdes[2], status; char buf[1024]; if (!doall) return; rewind(fp); if (fstat(fileno(fp), &sbuf) || !sbuf.st_size) goto done; if (pipe(pdes) < 0) goto done; switch (fork()) { case -1: /* error */ (void)close(pdes[0]); (void)close(pdes[1]); goto done; case 0: /* child -- set stdin to pipe output */ if (pdes[0] != STDIN_FILENO) { (void)dup2(pdes[0], STDIN_FILENO); (void)close(pdes[0]); } (void)close(pdes[1]); uid = geteuid(); if (setuid(getuid()) < 0) { warnx("setuid failed"); _exit(1); - }; + } if (setgid(getegid()) < 0) { warnx("setgid failed"); _exit(1); } if (setuid(uid) < 0) { warnx("setuid failed"); _exit(1); } execl(_PATH_SENDMAIL, "sendmail", "-i", "-t", "-F", "\"Reminder Service\"", (char *)NULL); warn(_PATH_SENDMAIL); _exit(1); } /* parent -- write to pipe input */ (void)close(pdes[0]); write(pdes[1], "From: \"Reminder Service\" <", 26); write(pdes[1], pw->pw_name, strlen(pw->pw_name)); write(pdes[1], ">\nTo: <", 7); write(pdes[1], pw->pw_name, strlen(pw->pw_name)); write(pdes[1], ">\nSubject: ", 11); write(pdes[1], dayname, strlen(dayname)); write(pdes[1], "'s Calendar\nPrecedence: bulk\n\n", 30); while ((nread = read(fileno(fp), buf, sizeof(buf))) > 0) (void)write(pdes[1], buf, nread); (void)close(pdes[1]); done: (void)fclose(fp); (void)unlink(path); while (wait(&status) >= 0); } Index: head/usr.bin/rpcgen/rpc_cout.c =================================================================== --- head/usr.bin/rpcgen/rpc_cout.c (revision 298088) +++ head/usr.bin/rpcgen/rpc_cout.c (revision 298089) @@ -1,719 +1,719 @@ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape * media and as a part of the software program in whole or part. Users * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #if 0 #ifndef lint #ident "@(#)rpc_cout.c 1.14 93/07/05 SMI" static char sccsid[] = "@(#)rpc_cout.c 1.13 89/02/22 (C) 1987 SMI"; #endif #endif #include __FBSDID("$FreeBSD$"); /* * rpc_cout.c, XDR routine outputter for the RPC protocol compiler * Copyright (C) 1987, Sun Microsystems, Inc. */ #include #include #include #include "rpc_parse.h" #include "rpc_scan.h" #include "rpc_util.h" static void print_header( definition * ); static void print_trailer( void ); static void print_stat( int , declaration * ); static void emit_enum( definition * ); static void emit_program( definition * ); static void emit_union( definition * ); static void emit_struct( definition * ); static void emit_typedef( definition * ); static void emit_inline( int, declaration *, int ); static void emit_single_in_line( int, declaration *, int, relation ); /* * Emit the C-routine for the given definition */ void emit(definition *def) { if (def->def_kind == DEF_CONST) { return; } if (def->def_kind == DEF_PROGRAM) { emit_program(def); return; } if (def->def_kind == DEF_TYPEDEF) { /* * now we need to handle declarations like * struct typedef foo foo; * since we dont want this to be expanded into 2 calls to xdr_foo */ if (strcmp(def->def.ty.old_type, def->def_name) == 0) return; - }; + } print_header(def); switch (def->def_kind) { case DEF_UNION: emit_union(def); break; case DEF_ENUM: emit_enum(def); break; case DEF_STRUCT: emit_struct(def); break; case DEF_TYPEDEF: emit_typedef(def); break; /* DEF_CONST and DEF_PROGRAM have already been handled */ default: break; } print_trailer(); } static int findtype(definition *def, const char *type) { if (def->def_kind == DEF_PROGRAM || def->def_kind == DEF_CONST) { return (0); } else { return (streq(def->def_name, type)); } } static int undefined(const char *type) { definition *def; def = (definition *) FINDVAL(defined, type, findtype); return (def == NULL); } static void print_generic_header(const char *procname, int pointerp) { f_print(fout, "\n"); f_print(fout, "bool_t\n"); f_print(fout, "xdr_%s(", procname); f_print(fout, "XDR *xdrs, "); f_print(fout, "%s ", procname); if (pointerp) f_print(fout, "*"); f_print(fout, "objp)\n{\n\n"); } static void print_header(definition *def) { print_generic_header(def->def_name, def->def_kind != DEF_TYPEDEF || !isvectordef(def->def.ty.old_type, def->def.ty.rel)); /* Now add Inline support */ if (inline_size == 0) return; /* May cause lint to complain. but ... */ f_print(fout, "\tregister long *buf;\n\n"); } static void print_prog_header(proc_list *plist) { print_generic_header(plist->args.argname, 1); } static void print_trailer(void) { f_print(fout, "\treturn (TRUE);\n"); f_print(fout, "}\n"); } static void print_ifopen(int indent, const char *name) { tabify(fout, indent); f_print(fout, "if (!xdr_%s(xdrs", name); } static void print_ifarg(const char *arg) { f_print(fout, ", %s", arg); } static void print_ifsizeof(int indent, const char *prefix, const char *type) { if (indent) { f_print(fout, ",\n"); tabify(fout, indent); } else { f_print(fout, ", "); } if (streq(type, "bool")) { f_print(fout, "sizeof (bool_t), (xdrproc_t) xdr_bool"); } else { f_print(fout, "sizeof ("); if (undefined(type) && prefix) { f_print(fout, "%s ", prefix); } f_print(fout, "%s), (xdrproc_t) xdr_%s", type, type); } } static void print_ifclose(int indent, int brace) { f_print(fout, "))\n"); tabify(fout, indent); f_print(fout, "\treturn (FALSE);\n"); if (brace) f_print(fout, "\t}\n"); } static void print_ifstat(int indent, const char *prefix, const char *type, relation rel, const char *amax, const char *objname, const char *name) { const char *alt = NULL; int brace = 0; switch (rel) { case REL_POINTER: brace = 1; f_print(fout, "\t{\n"); f_print(fout, "\t%s **pp = %s;\n", type, objname); print_ifopen(indent, "pointer"); print_ifarg("(char **)"); f_print(fout, "pp"); print_ifsizeof(0, prefix, type); break; case REL_VECTOR: if (streq(type, "string")) { alt = "string"; } else if (streq(type, "opaque")) { alt = "opaque"; } if (alt) { print_ifopen(indent, alt); print_ifarg(objname); } else { print_ifopen(indent, "vector"); print_ifarg("(char *)"); f_print(fout, "%s", objname); } print_ifarg(amax); if (!alt) { print_ifsizeof(indent + 1, prefix, type); } break; case REL_ARRAY: if (streq(type, "string")) { alt = "string"; } else if (streq(type, "opaque")) { alt = "bytes"; } if (streq(type, "string")) { print_ifopen(indent, alt); print_ifarg(objname); } else { if (alt) { print_ifopen(indent, alt); } else { print_ifopen(indent, "array"); } print_ifarg("(char **)"); if (*objname == '&') { f_print(fout, "%s.%s_val, (u_int *) %s.%s_len", objname, name, objname, name); } else { f_print(fout, "&%s->%s_val, (u_int *) &%s->%s_len", objname, name, objname, name); } } print_ifarg(amax); if (!alt) { print_ifsizeof(indent + 1, prefix, type); } break; case REL_ALIAS: print_ifopen(indent, type); print_ifarg(objname); break; } print_ifclose(indent, brace); } /* ARGSUSED */ static void emit_enum(definition *def __unused) { print_ifopen(1, "enum"); print_ifarg("(enum_t *)objp"); print_ifclose(1, 0); } static void emit_program(definition *def) { decl_list *dl; version_list *vlist; proc_list *plist; for (vlist = def->def.pr.versions; vlist != NULL; vlist = vlist->next) for (plist = vlist->procs; plist != NULL; plist = plist->next) { if (!newstyle || plist->arg_num < 2) continue; /* old style, or single argument */ print_prog_header(plist); for (dl = plist->args.decls; dl != NULL; dl = dl->next) print_stat(1, &dl->decl); print_trailer(); } } static void emit_union(definition *def) { declaration *dflt; case_list *cl; declaration *cs; char *object; const char *vecformat = "objp->%s_u.%s"; const char *format = "&objp->%s_u.%s"; print_stat(1, &def->def.un.enum_decl); f_print(fout, "\tswitch (objp->%s) {\n", def->def.un.enum_decl.name); for (cl = def->def.un.cases; cl != NULL; cl = cl->next) { f_print(fout, "\tcase %s:\n", cl->case_name); if (cl->contflag == 1) /* a continued case statement */ continue; cs = &cl->case_decl; if (!streq(cs->type, "void")) { object = xmalloc(strlen(def->def_name) + strlen(format) + strlen(cs->name) + 1); if (isvectordef (cs->type, cs->rel)) { s_print(object, vecformat, def->def_name, cs->name); } else { s_print(object, format, def->def_name, cs->name); } print_ifstat(2, cs->prefix, cs->type, cs->rel, cs->array_max, object, cs->name); free(object); } f_print(fout, "\t\tbreak;\n"); } dflt = def->def.un.default_decl; if (dflt != NULL) { if (!streq(dflt->type, "void")) { f_print(fout, "\tdefault:\n"); object = xmalloc(strlen(def->def_name) + strlen(format) + strlen(dflt->name) + 1); if (isvectordef (dflt->type, dflt->rel)) { s_print(object, vecformat, def->def_name, dflt->name); } else { s_print(object, format, def->def_name, dflt->name); } print_ifstat(2, dflt->prefix, dflt->type, dflt->rel, dflt->array_max, object, dflt->name); free(object); f_print(fout, "\t\tbreak;\n"); } else { f_print(fout, "\tdefault:\n"); f_print(fout, "\t\tbreak;\n"); } } else { f_print(fout, "\tdefault:\n"); f_print(fout, "\t\treturn (FALSE);\n"); } f_print(fout, "\t}\n"); } static void inline_struct(definition *def, int flag) { decl_list *dl; int i, size; decl_list *cur, *psav; bas_type *ptr; char *sizestr; const char *plus; char ptemp[256]; int indent = 1; cur = NULL; if (flag == PUT) f_print(fout, "\n\tif (xdrs->x_op == XDR_ENCODE) {\n"); else f_print(fout, "\t\treturn (TRUE);\n\t} else if (xdrs->x_op == XDR_DECODE) {\n"); i = 0; size = 0; sizestr = NULL; for (dl = def->def.st.decls; dl != NULL; dl = dl->next) { /* xxx */ /* now walk down the list and check for basic types */ if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS) || (dl->decl.rel == REL_VECTOR))){ if (i == 0) cur = dl; i++; if (dl->decl.rel == REL_ALIAS) size += ptr->length; else { /* this code is required to handle arrays */ if (sizestr == NULL) plus = ""; else plus = " + "; if (ptr->length != 1) s_print(ptemp, "%s%s * %d", plus, dl->decl.array_max, ptr->length); else s_print(ptemp, "%s%s", plus, dl->decl.array_max); /* now concatenate to sizestr !!!! */ if (sizestr == NULL) { sizestr = xstrdup(ptemp); } else{ sizestr = xrealloc(sizestr, strlen(sizestr) +strlen(ptemp)+1); sizestr = strcat(sizestr, ptemp); /* build up length of array */ } } } else { if (i > 0) { if (sizestr == NULL && size < inline_size){ /* * don't expand into inline code * if size < inline_size */ while (cur != dl){ print_stat(indent + 1, &cur->decl); cur = cur->next; } } else { /* were already looking at a xdr_inlineable structure */ tabify(fout, indent + 1); if (sizestr == NULL) f_print(fout, "buf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);", size); else { if (size == 0) f_print(fout, "buf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);", sizestr); else f_print(fout, "buf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);", size, sizestr); } f_print(fout, "\n"); tabify(fout, indent + 1); f_print(fout, "if (buf == NULL) {\n"); psav = cur; while (cur != dl){ print_stat(indent + 2, &cur->decl); cur = cur->next; } f_print(fout, "\n\t\t} else {\n"); cur = psav; while (cur != dl){ emit_inline(indent + 2, &cur->decl, flag); cur = cur->next; } tabify(fout, indent + 1); f_print(fout, "}\n"); } } size = 0; i = 0; free(sizestr); sizestr = NULL; print_stat(indent + 1, &dl->decl); } } if (i > 0) { if (sizestr == NULL && size < inline_size){ /* don't expand into inline code if size < inline_size */ while (cur != dl){ print_stat(indent + 1, &cur->decl); cur = cur->next; } } else { /* were already looking at a xdr_inlineable structure */ if (sizestr == NULL) f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, %d * BYTES_PER_XDR_UNIT);", size); else if (size == 0) f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, (%s) * BYTES_PER_XDR_UNIT);", sizestr); else f_print(fout, "\t\tbuf = XDR_INLINE(xdrs, (%d + (%s)) * BYTES_PER_XDR_UNIT);", size, sizestr); f_print(fout, "\n\t\tif (buf == NULL) {\n"); psav = cur; while (cur != NULL){ print_stat(indent + 2, &cur->decl); cur = cur->next; } f_print(fout, "\t\t} else {\n"); cur = psav; while (cur != dl){ emit_inline(indent + 2, &cur->decl, flag); cur = cur->next; } f_print(fout, "\t\t}\n"); } } } static void emit_struct(definition *def) { decl_list *dl; int j, size, flag; bas_type *ptr; int can_inline; if (inline_size == 0) { /* No xdr_inlining at all */ for (dl = def->def.st.decls; dl != NULL; dl = dl->next) print_stat(1, &dl->decl); return; } for (dl = def->def.st.decls; dl != NULL; dl = dl->next) if (dl->decl.rel == REL_VECTOR){ f_print(fout, "\tint i;\n"); break; } size = 0; can_inline = 0; /* * Make a first pass and see if inling is possible. */ for (dl = def->def.st.decls; dl != NULL; dl = dl->next) if ((dl->decl.prefix == NULL) && ((ptr = find_type(dl->decl.type)) != NULL) && ((dl->decl.rel == REL_ALIAS)|| (dl->decl.rel == REL_VECTOR))){ if (dl->decl.rel == REL_ALIAS) size += ptr->length; else { can_inline = 1; break; /* can be inlined */ } } else { if (size >= inline_size){ can_inline = 1; break; /* can be inlined */ } size = 0; } if (size >= inline_size) can_inline = 1; if (can_inline == 0){ /* can not inline, drop back to old mode */ for (dl = def->def.st.decls; dl != NULL; dl = dl->next) print_stat(1, &dl->decl); return; } flag = PUT; for (j = 0; j < 2; j++){ inline_struct(def, flag); if (flag == PUT) flag = GET; } f_print(fout, "\t\treturn (TRUE);\n\t}\n\n"); /* now take care of XDR_FREE case */ for (dl = def->def.st.decls; dl != NULL; dl = dl->next) print_stat(1, &dl->decl); } static void emit_typedef(definition *def) { const char *prefix = def->def.ty.old_prefix; const char *type = def->def.ty.old_type; const char *amax = def->def.ty.array_max; relation rel = def->def.ty.rel; print_ifstat(1, prefix, type, rel, amax, "objp", def->def_name); } static void print_stat(int indent, declaration *dec) { const char *prefix = dec->prefix; const char *type = dec->type; const char *amax = dec->array_max; relation rel = dec->rel; char name[256]; if (isvectordef(type, rel)) { s_print(name, "objp->%s", dec->name); } else { s_print(name, "&objp->%s", dec->name); } print_ifstat(indent, prefix, type, rel, amax, name, dec->name); } char *upcase(const char *); static void emit_inline(int indent, declaration *decl, int flag) { switch (decl->rel) { case REL_ALIAS : emit_single_in_line(indent, decl, flag, REL_ALIAS); break; case REL_VECTOR : tabify(fout, indent); f_print(fout, "{\n"); tabify(fout, indent + 1); f_print(fout, "%s *genp;\n\n", decl->type); tabify(fout, indent + 1); f_print(fout, "for (i = 0, genp = objp->%s;\n", decl->name); tabify(fout, indent + 2); f_print(fout, "i < %s; i++) {\n", decl->array_max); emit_single_in_line(indent + 2, decl, flag, REL_VECTOR); tabify(fout, indent + 1); f_print(fout, "}\n"); tabify(fout, indent); f_print(fout, "}\n"); break; default: break; } } static void emit_single_in_line(int indent, declaration *decl, int flag, relation rel) { char *upp_case; tabify(fout, indent); if (flag == PUT) f_print(fout, "IXDR_PUT_"); else if (rel == REL_ALIAS) f_print(fout, "objp->%s = IXDR_GET_", decl->name); else f_print(fout, "*genp++ = IXDR_GET_"); upp_case = upcase(decl->type); /* hack - XX */ if (strcmp(upp_case, "INT") == 0) { free(upp_case); upp_case = strdup("LONG"); } if (strcmp(upp_case, "U_INT") == 0) { free(upp_case); upp_case = strdup("U_LONG"); } if (flag == PUT) if (rel == REL_ALIAS) f_print(fout, "%s(buf, objp->%s);\n", upp_case, decl->name); else f_print(fout, "%s(buf, *genp++);\n", upp_case); else f_print(fout, "%s(buf);\n", upp_case); free(upp_case); } char * upcase(const char *str) { char *ptr, *hptr; ptr = (char *)xmalloc(strlen(str)+1); hptr = ptr; while (*str != '\0') *ptr++ = toupper(*str++); *ptr = '\0'; return (hptr); } Index: head/usr.bin/rpcgen/rpc_main.c =================================================================== --- head/usr.bin/rpcgen/rpc_main.c (revision 298088) +++ head/usr.bin/rpcgen/rpc_main.c (revision 298089) @@ -1,1267 +1,1267 @@ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape * media and as a part of the software program in whole or part. Users * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #if 0 #ifndef lint #ident "@(#)rpc_main.c 1.21 94/04/25 SMI" static char sccsid[] = "@(#)rpc_main.c 1.30 89/03/30 (C) 1987 SMI"; #endif #endif #include __FBSDID("$FreeBSD$"); /* * rpc_main.c, Top level of the RPC protocol compiler. * Copyright (C) 1987, Sun Microsystems, Inc. */ #include #include #include #include #include #include #include #include #include #include "rpc_parse.h" #include "rpc_scan.h" #include "rpc_util.h" static void c_output(const char *, const char *, int, const char *); static void h_output(const char *, const char *, int, const char *, int); static void l_output(const char *, const char *, int, const char *); static void t_output(const char *, const char *, int, const char *); static void clnt_output(const char *, const char *, int, const char * ); static char *generate_guard(const char *); static void c_initialize(void); static void usage(void); static void options_usage(void); static int do_registers(int, const char **); static int parseargs(int, const char **, struct commandline *); static void svc_output(const char *, const char *, int, const char *); static void mkfile_output(struct commandline *); static void s_output(int, const char **, const char *, const char *, int, const char *, int, int); #define EXTEND 1 /* alias for TRUE */ #define DONT_EXTEND 0 /* alias for FALSE */ static const char *svcclosetime = "120"; static const char *CPP = NULL; static const char CPPFLAGS[] = "-C"; static char pathbuf[MAXPATHLEN + 1]; static const char *allv[] = { "rpcgen", "-s", "udp", "-s", "tcp", }; static int allc = sizeof (allv)/sizeof (allv[0]); static const char *allnv[] = { "rpcgen", "-s", "netpath", }; static int allnc = sizeof (allnv)/sizeof (allnv[0]); /* * machinations for handling expanding argument list */ static void addarg(const char *); /* add another argument to the list */ static void insarg(int, const char *); /* insert arg at specified location */ static void clear_args(void); /* clear argument list */ static void checkfiles(const char *, const char *); /* check if out file already exists */ #define ARGLISTLEN 20 #define FIXEDARGS 0 static char *arglist[ARGLISTLEN]; static int argcount = FIXEDARGS; int nonfatalerrors; /* errors */ int inetdflag = 0; /* Support for inetd is disabled by default, use -I */ int pmflag = 0; /* Support for port monitors is disabled by default */ int tirpc_socket = 1; /* TI-RPC on socket, no TLI library */ int logflag; /* Use syslog instead of fprintf for errors */ int tblflag; /* Support for dispatch table file */ int mtflag = 0; /* Support for MT */ #define INLINE 0 /* length at which to start doing an inline */ int inline_size = INLINE; /* * Length at which to start doing an inline. INLINE = default * if 0, no xdr_inline code */ int indefinitewait; /* If started by port monitors, hang till it wants */ int exitnow; /* If started by port monitors, exit after the call */ int timerflag; /* TRUE if !indefinite && !exitnow */ int newstyle; /* newstyle of passing arguments (by value) */ int CCflag = 0; /* C++ files */ static int allfiles; /* generate all files */ int tirpcflag = 1; /* generating code for tirpc, by default */ xdrfunc *xdrfunc_head = NULL; /* xdr function list */ xdrfunc *xdrfunc_tail = NULL; /* xdr function list */ pid_t childpid; int main(int argc, const char *argv[]) { struct commandline cmd; (void) memset((char *)&cmd, 0, sizeof (struct commandline)); clear_args(); if (!parseargs(argc, argv, &cmd)) usage(); /* * Only the client and server side stubs are likely to be customized, * so in that case only, check if the outfile exists, and if so, * print an error message and exit. */ if (cmd.Ssflag || cmd.Scflag || cmd.makefileflag) { checkfiles(cmd.infile, cmd.outfile); } else checkfiles(cmd.infile, NULL); if (cmd.cflag) { c_output(cmd.infile, "-DRPC_XDR", DONT_EXTEND, cmd.outfile); } else if (cmd.hflag) { h_output(cmd.infile, "-DRPC_HDR", DONT_EXTEND, cmd.outfile, cmd.hflag); } else if (cmd.lflag) { l_output(cmd.infile, "-DRPC_CLNT", DONT_EXTEND, cmd.outfile); } else if (cmd.sflag || cmd.mflag || (cmd.nflag)) { s_output(argc, argv, cmd.infile, "-DRPC_SVC", DONT_EXTEND, cmd.outfile, cmd.mflag, cmd.nflag); } else if (cmd.tflag) { t_output(cmd.infile, "-DRPC_TBL", DONT_EXTEND, cmd.outfile); } else if (cmd.Ssflag) { svc_output(cmd.infile, "-DRPC_SERVER", DONT_EXTEND, cmd.outfile); } else if (cmd.Scflag) { clnt_output(cmd.infile, "-DRPC_CLIENT", DONT_EXTEND, cmd.outfile); } else if (cmd.makefileflag) { mkfile_output(&cmd); } else { /* the rescans are required, since cpp may effect input */ c_output(cmd.infile, "-DRPC_XDR", EXTEND, "_xdr.c"); reinitialize(); h_output(cmd.infile, "-DRPC_HDR", EXTEND, ".h", cmd.hflag); reinitialize(); l_output(cmd.infile, "-DRPC_CLNT", EXTEND, "_clnt.c"); reinitialize(); if (inetdflag || !tirpcflag) s_output(allc, allv, cmd.infile, "-DRPC_SVC", EXTEND, "_svc.c", cmd.mflag, cmd.nflag); else s_output(allnc, allnv, cmd.infile, "-DRPC_SVC", EXTEND, "_svc.c", cmd.mflag, cmd.nflag); if (tblflag) { reinitialize(); t_output(cmd.infile, "-DRPC_TBL", EXTEND, "_tbl.i"); } if (allfiles) { reinitialize(); svc_output(cmd.infile, "-DRPC_SERVER", EXTEND, "_server.c"); reinitialize(); clnt_output(cmd.infile, "-DRPC_CLIENT", EXTEND, "_client.c"); } if (allfiles || (cmd.makefileflag == 1)){ reinitialize(); mkfile_output(&cmd); } } exit(nonfatalerrors); /* NOTREACHED */ } /* * add extension to filename */ static char * extendfile(const char *path, const char *ext) { char *res; const char *p; const char *file; if ((file = strrchr(path, '/')) == NULL) file = path; else file++; res = xmalloc(strlen(file) + strlen(ext) + 1); p = strrchr(file, '.'); if (p == NULL) { p = file + strlen(file); } (void) strcpy(res, file); (void) strcpy(res + (p - file), ext); return (res); } /* * Open output file with given extension */ static void open_output(const char *infile, const char *outfile) { if (outfile == NULL) { fout = stdout; return; } if (infile != NULL && streq(outfile, infile)) { warnx("%s already exists. No output generated", infile); crash(); } fout = fopen(outfile, "w"); if (fout == NULL) { warn("unable to open %s", outfile); crash(); } record_open(outfile); return; } static void add_warning(void) { f_print(fout, "/*\n"); f_print(fout, " * Please do not edit this file.\n"); f_print(fout, " * It was generated using rpcgen.\n"); f_print(fout, " */\n\n"); } /* clear list of arguments */ static void clear_args(void) { int i; for (i = FIXEDARGS; i < ARGLISTLEN; i++) arglist[i] = NULL; argcount = FIXEDARGS; } /* prepend C-preprocessor and flags before arguments */ static void prepend_cpp(void) { int idx = 1; const char *var; char *dupvar, *s, *t; if (CPP != NULL) insarg(0, CPP); else if ((var = getenv("RPCGEN_CPP")) == NULL) insarg(0, "/usr/bin/cpp"); else { /* Parse command line in a rudimentary way */ dupvar = xstrdup(var); for (s = dupvar, idx = 0; (t = strsep(&s, " \t")) != NULL; ) { if (t[0]) insarg(idx++, t); } free(dupvar); } insarg(idx, CPPFLAGS); } /* * Open input file with given define for C-preprocessor */ static void open_input(const char *infile, const char *define) { int pd[2]; infilename = (infile == NULL) ? "" : infile; (void) pipe(pd); switch (childpid = fork()) { case 0: prepend_cpp(); addarg(define); if (infile) addarg(infile); addarg((char *)NULL); (void) close(1); (void) dup2(pd[1], 1); (void) close(pd[0]); execvp(arglist[0], arglist); err(1, "execvp %s", arglist[0]); case -1: err(1, "fork"); } (void) close(pd[1]); fin = fdopen(pd[0], "r"); if (fin == NULL) { warn("%s", infilename); crash(); } } /* valid tirpc nettypes */ static const char *valid_ti_nettypes[] = { "netpath", "visible", "circuit_v", "datagram_v", "circuit_n", "datagram_n", "udp", "tcp", "raw", NULL }; /* valid inetd nettypes */ static const char *valid_i_nettypes[] = { "udp", "tcp", NULL }; static int check_nettype(const char *name, const char *list_to_check[]) { int i; for (i = 0; list_to_check[i] != NULL; i++) { if (strcmp(name, list_to_check[i]) == 0) { return (1); } } warnx("illegal nettype :\'%s\'", name); return (0); } static const char * file_name(const char *file, const char *ext) { char *temp; temp = extendfile(file, ext); if (access(temp, F_OK) != -1) return (temp); else return (" "); } static void c_output(const char *infile, const char *define, int extend, const char *outfile) { definition *def; char *include; const char *outfilename; long tell; c_initialize(); open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; open_output(infile, outfilename); add_warning(); if (infile && (include = extendfile(infile, ".h"))) { f_print(fout, "#include \"%s\"\n", include); free(include); /* .h file already contains rpc/rpc.h */ } else f_print(fout, "#include \n"); tell = ftell(fout); while ( (def = get_definition()) ) { emit(def); } if (extend && tell == ftell(fout)) { (void) unlink(outfilename); } } void c_initialize(void) { /* add all the starting basic types */ add_type(1, "int"); add_type(1, "long"); add_type(1, "short"); add_type(1, "bool"); add_type(1, "u_int"); add_type(1, "u_long"); add_type(1, "u_short"); } static const char rpcgen_table_dcl[] = "struct rpcgen_table {\n\ char *(*proc)(); \n\ xdrproc_t xdr_arg; \n\ unsigned len_arg; \n\ xdrproc_t xdr_res; \n\ unsigned len_res; \n\ }; \n"; char * generate_guard(const char *pathname) { const char *filename; char *guard, *tmp, *stopat; filename = strrchr(pathname, '/'); /* find last component */ filename = ((filename == 0) ? pathname : filename+1); guard = xstrdup(filename); stopat = strrchr(guard, '.'); /* * Convert to a valid C macro name and make it upper case. * Map macro unfriendly characterss to '_'. */ for (tmp = guard; *tmp != '\000'; ++tmp) { if (islower(*tmp)) *tmp = toupper(*tmp); else if (isupper(*tmp) || *tmp == '_') /* OK for C */; else if (tmp == guard) *tmp = '_'; else if (isdigit(*tmp)) /* OK for all but first character */; else if (tmp == stopat) { *tmp = '\0'; break; } else *tmp = '_'; } /* * Can't have a '_' in front, because it'll end up being "__". * "__" macros shoudln't be used. So, remove all of the * '_' characters from the front. */ if (*guard == '_') { for (tmp = guard; *tmp == '_'; ++tmp) ; strcpy(guard, tmp); } guard = extendfile(guard, "_H_RPCGEN"); return (guard); } /* * Compile into an XDR header file */ static void h_output(const char *infile, const char *define, int extend, const char *outfile, int headeronly) { definition *def; const char *outfilename; long tell; const char *guard; list *l; xdrfunc *xdrfuncp; open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; open_output(infile, outfilename); add_warning(); if (outfilename || infile){ guard = generate_guard(outfilename ? outfilename: infile); } else guard = "STDIN_"; f_print(fout, "#ifndef _%s\n#define _%s\n\n", guard, guard); f_print(fout, "#include \n"); if (mtflag) f_print(fout, "#include \n"); /* put the C++ support */ if (!CCflag) { f_print(fout, "\n#ifdef __cplusplus\n"); f_print(fout, "extern \"C\" {\n"); f_print(fout, "#endif\n\n"); } /* put in a typedef for quadprecision. Only with Cflag */ tell = ftell(fout); /* print data definitions */ while ( (def = get_definition()) ) { print_datadef(def, headeronly); } /* * print function declarations. * Do this after data definitions because they might be used as * arguments for functions */ for (l = defined; l != NULL; l = l->next) { print_funcdef(l->val, headeronly); } /* Now print all xdr func declarations */ if (xdrfunc_head != NULL){ f_print(fout, "\n/* the xdr functions */\n"); if (CCflag){ f_print(fout, "\n#ifdef __cplusplus\n"); f_print(fout, "extern \"C\" {\n"); f_print(fout, "#endif\n"); } xdrfuncp = xdrfunc_head; while (xdrfuncp != NULL){ print_xdr_func_def(xdrfuncp->name, xdrfuncp->pointerp); xdrfuncp = xdrfuncp->next; } } if (extend && tell == ftell(fout)) { (void) unlink(outfilename); } else if (tblflag) { f_print(fout, rpcgen_table_dcl); } f_print(fout, "\n#ifdef __cplusplus\n"); f_print(fout, "}\n"); f_print(fout, "#endif\n"); f_print(fout, "\n#endif /* !_%s */\n", guard); } /* * Compile into an RPC service */ static void s_output(int argc, const char *argv[], const char *infile, const char *define, int extend, const char *outfile, int nomain, int netflag) { char *include; definition *def; int foundprogram = 0; const char *outfilename; open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; open_output(infile, outfilename); add_warning(); if (infile && (include = extendfile(infile, ".h"))) { f_print(fout, "#include \"%s\"\n", include); free(include); } else f_print(fout, "#include \n"); f_print(fout, "#include \n"); f_print(fout, "#include /* getenv, exit */\n"); f_print (fout, "#include /* for pmap_unset */\n"); f_print (fout, "#include /* strcmp */\n"); if (tirpcflag) f_print(fout, "#include \n"); if (strcmp(svcclosetime, "-1") == 0) indefinitewait = 1; else if (strcmp(svcclosetime, "0") == 0) exitnow = 1; else if (inetdflag || pmflag) { f_print(fout, "#include \n"); timerflag = 1; } if (!tirpcflag && inetdflag) f_print(fout, "#include /* TIOCNOTTY */\n"); if (inetdflag || pmflag) { f_print(fout, "#ifdef __cplusplus\n"); f_print(fout, "#include /* getdtablesize, open */\n"); f_print(fout, "#endif /* __cplusplus */\n"); } if (tirpcflag) { f_print(fout, "#include /* open */\n"); f_print(fout, "#include /* fork / setsid */\n"); f_print(fout, "#include \n"); } f_print(fout, "#include \n"); if (inetdflag || !tirpcflag) { f_print(fout, "#include \n"); f_print(fout, "#include \n"); } if ((netflag || pmflag) && tirpcflag && !nomain) { f_print(fout, "#include \n"); } if (tirpcflag) f_print(fout, "#include /* rlimit */\n"); if (logflag || inetdflag || pmflag || tirpcflag) f_print(fout, "#include \n"); f_print(fout, "\n#ifdef DEBUG\n#define RPC_SVC_FG\n#endif\n"); if (timerflag) f_print(fout, "\n#define _RPCSVC_CLOSEDOWN %s\n", svcclosetime); while ( (def = get_definition()) ) { foundprogram |= (def->def_kind == DEF_PROGRAM); } if (extend && !foundprogram) { (void) unlink(outfilename); return; } write_most(infile, netflag, nomain); if (!nomain) { if (!do_registers(argc, argv)) { if (outfilename) (void) unlink(outfilename); usage(); } write_rest(); } } /* * generate client side stubs */ static void l_output(const char *infile, const char *define, int extend, const char *outfile) { char *include; definition *def; int foundprogram = 0; const char *outfilename; open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; open_output(infile, outfilename); add_warning(); f_print (fout, "#include /* for memset */\n"); if (infile && (include = extendfile(infile, ".h"))) { f_print(fout, "#include \"%s\"\n", include); free(include); } else f_print(fout, "#include \n"); while ( (def = get_definition()) ) { foundprogram |= (def->def_kind == DEF_PROGRAM); } if (extend && !foundprogram) { (void) unlink(outfilename); return; } write_stubs(); } /* * generate the dispatch table */ static void t_output(const char *infile, const char *define, int extend, const char *outfile) { definition *def; int foundprogram = 0; const char *outfilename; open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; open_output(infile, outfilename); add_warning(); while ( (def = get_definition()) ) { foundprogram |= (def->def_kind == DEF_PROGRAM); } if (extend && !foundprogram) { (void) unlink(outfilename); return; } write_tables(); } /* sample routine for the server template */ static void svc_output(const char *infile, const char *define, int extend, const char *outfile) { definition *def; char *include; const char *outfilename; long tell; open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; checkfiles(infile, outfilename); /* * Check if outfile already exists. * if so, print an error message and exit */ open_output(infile, outfilename); add_sample_msg(); if (infile && (include = extendfile(infile, ".h"))) { f_print(fout, "#include \"%s\"\n", include); free(include); } else f_print(fout, "#include \n"); tell = ftell(fout); while ( (def = get_definition()) ) { write_sample_svc(def); } if (extend && tell == ftell(fout)) { (void) unlink(outfilename); } } /* sample main routine for client */ static void clnt_output(const char *infile, const char *define, int extend, const char *outfile) { definition *def; char *include; const char *outfilename; long tell; int has_program = 0; open_input(infile, define); outfilename = extend ? extendfile(infile, outfile) : outfile; checkfiles(infile, outfilename); /* * Check if outfile already exists. * if so, print an error message and exit */ open_output(infile, outfilename); add_sample_msg(); if (infile && (include = extendfile(infile, ".h"))) { f_print(fout, "#include \"%s\"\n", include); free(include); } else f_print(fout, "#include \n"); f_print(fout, "#include \n"); f_print(fout, "#include \n"); tell = ftell(fout); while ( (def = get_definition()) ) { has_program += write_sample_clnt(def); } if (has_program) write_sample_clnt_main(); if (extend && tell == ftell(fout)) { (void) unlink(outfilename); } } static void mkfile_output(struct commandline *cmd) { const char *mkfilename, *clientname, *clntname, *xdrname, *hdrname; const char *servername, *svcname, *servprogname, *clntprogname; char *temp, *mkftemp; svcname = file_name(cmd->infile, "_svc.c"); clntname = file_name(cmd->infile, "_clnt.c"); xdrname = file_name(cmd->infile, "_xdr.c"); hdrname = file_name(cmd->infile, ".h"); if (allfiles){ servername = extendfile(cmd->infile, "_server.c"); clientname = extendfile(cmd->infile, "_client.c"); }else{ servername = " "; clientname = " "; } servprogname = extendfile(cmd->infile, "_server"); clntprogname = extendfile(cmd->infile, "_client"); if (allfiles){ mkftemp = xmalloc(strlen("makefile.") + strlen(cmd->infile) + 1); temp = strrchr(cmd->infile, '.'); strcpy(mkftemp, "makefile."); (void) strncat(mkftemp, cmd->infile, (temp - cmd->infile)); mkfilename = mkftemp; } else mkfilename = cmd->outfile; checkfiles(NULL, mkfilename); open_output(NULL, mkfilename); f_print(fout, "\n# This is a template makefile generated\ by rpcgen \n"); f_print(fout, "\n# Parameters \n\n"); f_print(fout, "CLIENT = %s\nSERVER = %s\n\n", clntprogname, servprogname); f_print(fout, "SOURCES_CLNT.c = \nSOURCES_CLNT.h = \n"); f_print(fout, "SOURCES_SVC.c = \nSOURCES_SVC.h = \n"); f_print(fout, "SOURCES.x = %s\n\n", cmd->infile); f_print(fout, "TARGETS_SVC.c = %s %s %s \n", svcname, servername, xdrname); f_print(fout, "TARGETS_CLNT.c = %s %s %s \n", clntname, clientname, xdrname); f_print(fout, "TARGETS = %s %s %s %s %s %s\n\n", hdrname, xdrname, clntname, svcname, clientname, servername); f_print(fout, "OBJECTS_CLNT = $(SOURCES_CLNT.c:%%.c=%%.o) \ $(TARGETS_CLNT.c:%%.c=%%.o) "); f_print(fout, "\nOBJECTS_SVC = $(SOURCES_SVC.c:%%.c=%%.o) \ $(TARGETS_SVC.c:%%.c=%%.o) "); f_print(fout, "\n# Compiler flags \n"); if (mtflag) f_print(fout, "\nCFLAGS += -D_REENTRANT -D_THEAD_SAFE \nLDLIBS += -pthread\n"); f_print(fout, "RPCGENFLAGS = \n"); f_print(fout, "\n# Targets \n\n"); f_print(fout, "all : $(CLIENT) $(SERVER)\n\n"); f_print(fout, "$(TARGETS) : $(SOURCES.x) \n"); f_print(fout, "\trpcgen $(RPCGENFLAGS) $(SOURCES.x)\n\n"); if (allfiles) { f_print(fout, "\trpcgen -Sc $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", clientname); f_print(fout, "\trpcgen -Ss $(RPCGENFLAGS) $(SOURCES.x) -o %s\n\n", servername); } f_print(fout, "$(OBJECTS_CLNT) : $(SOURCES_CLNT.c) $(SOURCES_CLNT.h) \ $(TARGETS_CLNT.c) \n\n"); f_print(fout, "$(OBJECTS_SVC) : $(SOURCES_SVC.c) $(SOURCES_SVC.h) \ $(TARGETS_SVC.c) \n\n"); f_print(fout, "$(CLIENT) : $(OBJECTS_CLNT) \n"); f_print(fout, "\t$(CC) -o $(CLIENT) $(OBJECTS_CLNT) \ $(LDLIBS) \n\n"); f_print(fout, "$(SERVER) : $(OBJECTS_SVC) \n"); f_print(fout, "\t$(CC) -o $(SERVER) $(OBJECTS_SVC) $(LDLIBS)\n\n"); f_print(fout, "clean:\n\t rm -f core $(TARGETS) $(OBJECTS_CLNT) \ $(OBJECTS_SVC) $(CLIENT) $(SERVER)\n\n"); } /* * Perform registrations for service output * Return 0 if failed; 1 otherwise. */ static int do_registers(int argc, const char *argv[]) { int i; if (inetdflag || !tirpcflag) { for (i = 1; i < argc; i++) { if (streq(argv[i], "-s")) { if (!check_nettype(argv[i + 1], valid_i_nettypes)) return (0); write_inetd_register(argv[i + 1]); i++; } } } else { for (i = 1; i < argc; i++) if (streq(argv[i], "-s")) { if (!check_nettype(argv[i + 1], valid_ti_nettypes)) return (0); write_nettype_register(argv[i + 1]); i++; } else if (streq(argv[i], "-n")) { write_netid_register(argv[i + 1]); i++; } } return (1); } /* * Add another argument to the arg list */ static void addarg(const char *cp) { if (argcount >= ARGLISTLEN) { warnx("too many defines"); crash(); /*NOTREACHED*/ } if (cp != NULL) arglist[argcount++] = xstrdup(cp); else arglist[argcount++] = NULL; } /* * Insert an argument at the specified location */ static void insarg(int place, const char *cp) { int i; if (argcount >= ARGLISTLEN) { warnx("too many defines"); crash(); /*NOTREACHED*/ } /* Move up existing arguments */ for (i = argcount - 1; i >= place; i--) arglist[i + 1] = arglist[i]; arglist[place] = xstrdup(cp); argcount++; } /* * if input file is stdin and an output file is specified then complain * if the file already exists. Otherwise the file may get overwritten * If input file does not exist, exit with an error */ static void checkfiles(const char *infile, const char *outfile) { struct stat buf; if (infile) /* infile ! = NULL */ if (stat(infile, &buf) < 0) { warn("%s", infile); crash(); - }; + } if (outfile) { if (stat(outfile, &buf) < 0) return; /* file does not exist */ else { warnx("file '%s' already exists and may be overwritten", outfile); crash(); } } } /* * Parse command line arguments */ static int parseargs(int argc, const char *argv[], struct commandline *cmd) { int i; int j; char c, ch; char flag[(1 << 8 * sizeof (char))]; int nflags; cmd->infile = cmd->outfile = NULL; if (argc < 2) { return (0); } allfiles = 0; flag['c'] = 0; flag['h'] = 0; flag['l'] = 0; flag['m'] = 0; flag['o'] = 0; flag['s'] = 0; flag['n'] = 0; flag['t'] = 0; flag['S'] = 0; flag['C'] = 0; flag['M'] = 0; for (i = 1; i < argc; i++) { if (argv[i][0] != '-') { if (cmd->infile) { warnx("cannot specify more than one input file"); return (0); } cmd->infile = argv[i]; } else { for (j = 1; argv[i][j] != 0; j++) { c = argv[i][j]; switch (c) { case 'a': allfiles = 1; break; case 'c': case 'h': case 'l': case 'm': case 't': if (flag[(int)c]) { return (0); } flag[(int)c] = 1; break; case 'S': /* * sample flag: Ss or Sc. * Ss means set flag['S']; * Sc means set flag['C']; * Sm means set flag['M']; */ ch = argv[i][++j]; /* get next char */ if (ch == 's') ch = 'S'; else if (ch == 'c') ch = 'C'; else if (ch == 'm') ch = 'M'; else return (0); if (flag[(int)ch]) { return (0); } flag[(int)ch] = 1; break; case 'C': /* ANSI C syntax */ ch = argv[i][j+1]; /* get next char */ if (ch != 'C') break; CCflag = 1; break; case 'b': /* * Turn TIRPC flag off for * generating backward compatible * code */ tirpcflag = 0; break; case 'I': inetdflag = 1; break; case 'N': newstyle = 1; break; case 'L': logflag = 1; break; case 'P': pmflag = 1; break; case 'K': if (++i == argc) { return (0); } svcclosetime = argv[i]; goto nextarg; case 'T': tblflag = 1; break; case 'M': mtflag = 1; break; case 'i' : if (++i == argc) { return (0); } inline_size = atoi(argv[i]); goto nextarg; case 'n': case 'o': case 's': if (argv[i][j - 1] != '-' || argv[i][j + 1] != 0) { return (0); } flag[(int)c] = 1; if (++i == argc) { return (0); } if (c == 'o') { if (cmd->outfile) { return (0); } cmd->outfile = argv[i]; } goto nextarg; case 'D': if (argv[i][j - 1] != '-') { return (0); } (void) addarg(argv[i]); goto nextarg; case 'Y': if (++i == argc) { return (0); } if (strlcpy(pathbuf, argv[i], sizeof(pathbuf)) >= sizeof(pathbuf) || strlcat(pathbuf, "/cpp", sizeof(pathbuf)) >= sizeof(pathbuf)) { warnx("argument too long"); return (0); } CPP = pathbuf; goto nextarg; default: return (0); } } nextarg: ; } } cmd->cflag = flag['c']; cmd->hflag = flag['h']; cmd->lflag = flag['l']; cmd->mflag = flag['m']; cmd->nflag = flag['n']; cmd->sflag = flag['s']; cmd->tflag = flag['t']; cmd->Ssflag = flag['S']; cmd->Scflag = flag['C']; cmd->makefileflag = flag['M']; if (tirpcflag) { if (inetdflag) pmflag = 0; if ((inetdflag && cmd->nflag)) { /* netid not allowed with inetdflag */ warnx("cannot use netid flag with inetd flag"); return (0); } } else { /* 4.1 mode */ pmflag = 0; /* set pmflag only in tirpcmode */ if (cmd->nflag) { /* netid needs TIRPC */ warnx("cannot use netid flag without TIRPC"); return (0); } } if (newstyle && (tblflag || cmd->tflag)) { warnx("cannot use table flags with newstyle"); return (0); } /* check no conflicts with file generation flags */ nflags = cmd->cflag + cmd->hflag + cmd->lflag + cmd->mflag + cmd->sflag + cmd->nflag + cmd->tflag + cmd->Ssflag + cmd->Scflag + cmd->makefileflag; if (nflags == 0) { if (cmd->outfile != NULL || cmd->infile == NULL) { return (0); } } else if (cmd->infile == NULL && (cmd->Ssflag || cmd->Scflag || cmd->makefileflag)) { warnx("\"infile\" is required for template generation flags"); return (0); } if (nflags > 1) { warnx("cannot have more than one file generation flag"); return (0); } return (1); } static void usage(void) { f_print(stderr, "%s\n%s\n%s\n%s\n%s\n", "usage: rpcgen infile", " rpcgen [-abCLNTM] [-Dname[=value]] [-i size]\ [-I -P [-K seconds]] [-Y path] infile", " rpcgen [-c | -h | -l | -m | -t | -Sc | -Ss | -Sm]\ [-o outfile] [infile]", " rpcgen [-s nettype]* [-o outfile] [infile]", " rpcgen [-n netid]* [-o outfile] [infile]"); options_usage(); exit(1); } static void options_usage(void) { f_print(stderr, "options:\n"); f_print(stderr, "-a\t\tgenerate all files, including samples\n"); f_print(stderr, "-b\t\tbackward compatibility mode (generates code \ for FreeBSD 4.X)\n"); f_print(stderr, "-c\t\tgenerate XDR routines\n"); f_print(stderr, "-C\t\tANSI C mode\n"); f_print(stderr, "-Dname[=value]\tdefine a symbol (same as #define)\n"); f_print(stderr, "-h\t\tgenerate header file\n"); f_print(stderr, "-i size\t\tsize at which to start generating\ inline code\n"); f_print(stderr, "-I\t\tgenerate code for inetd support in server\n"); f_print(stderr, "-K seconds\tserver exits after K seconds of\ inactivity\n"); f_print(stderr, "-l\t\tgenerate client side stubs\n"); f_print(stderr, "-L\t\tserver errors will be printed to syslog\n"); f_print(stderr, "-m\t\tgenerate server side stubs\n"); f_print(stderr, "-M\t\tgenerate MT-safe code\n"); f_print(stderr, "-n netid\tgenerate server code that supports\ named netid\n"); f_print(stderr, "-N\t\tsupports multiple arguments and\ call-by-value\n"); f_print(stderr, "-o outfile\tname of the output file\n"); f_print(stderr, "-P\t\tgenerate code for port monitoring support in server\n"); f_print(stderr, "-s nettype\tgenerate server code that supports named\ nettype\n"); f_print(stderr, "-Sc\t\tgenerate sample client code that uses remote\ procedures\n"); f_print(stderr, "-Ss\t\tgenerate sample server code that defines\ remote procedures\n"); f_print(stderr, "-Sm \t\tgenerate makefile template \n"); f_print(stderr, "-t\t\tgenerate RPC dispatch table\n"); f_print(stderr, "-T\t\tgenerate code to support RPC dispatch tables\n"); f_print(stderr, "-Y path\t\tpath where cpp is found\n"); exit(1); } Index: head/usr.bin/rpcgen/rpc_parse.c =================================================================== --- head/usr.bin/rpcgen/rpc_parse.c (revision 298088) +++ head/usr.bin/rpcgen/rpc_parse.c (revision 298089) @@ -1,639 +1,639 @@ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape * media and as a part of the software program in whole or part. Users * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #if 0 #ifndef lint #ident "@(#)rpc_parse.c 1.12 93/07/05 SMI" static char sccsid[] = "@(#)rpc_parse.c 1.8 89/02/22 (C) 1987 SMI"; #endif #endif #include __FBSDID("$FreeBSD$"); /* * rpc_parse.c, Parser for the RPC protocol compiler * Copyright (C) 1987 Sun Microsystems, Inc. */ #include #include #include "rpc/types.h" #include "rpc_parse.h" #include "rpc_scan.h" #include "rpc_util.h" #define ARGNAME "arg" static void isdefined( definition * ); static void def_struct( definition * ); static void def_program( definition * ); static void def_enum( definition * ); static void def_const( definition * ); static void def_union( definition * ); static void def_typedef( definition * ); static void get_declaration( declaration *, defkind ); static void get_prog_declaration( declaration *, defkind, int ); static void get_type(const char **, const char **, defkind); static void unsigned_dec(const char ** ); /* * return the next definition you see */ definition * get_definition(void) { definition *defp; token tok; defp = XALLOC(definition); get_token(&tok); switch (tok.kind) { case TOK_STRUCT: def_struct(defp); break; case TOK_UNION: def_union(defp); break; case TOK_TYPEDEF: def_typedef(defp); break; case TOK_ENUM: def_enum(defp); break; case TOK_PROGRAM: def_program(defp); break; case TOK_CONST: def_const(defp); break; case TOK_EOF: return (NULL); default: error("definition keyword expected"); } scan(TOK_SEMICOLON, &tok); isdefined(defp); return (defp); } static void isdefined(definition *defp) { STOREVAL(&defined, defp); } static void def_struct(definition *defp) { token tok; declaration dec; decl_list *decls; decl_list **tailp; defp->def_kind = DEF_STRUCT; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_LBRACE, &tok); tailp = &defp->def.st.decls; do { get_declaration(&dec, DEF_STRUCT); decls = XALLOC(decl_list); decls->decl = dec; *tailp = decls; tailp = &decls->next; scan(TOK_SEMICOLON, &tok); peek(&tok); } while (tok.kind != TOK_RBRACE); get_token(&tok); *tailp = NULL; } static void def_program(definition *defp) { token tok; declaration dec; decl_list *decls; decl_list **tailp; version_list *vlist; version_list **vtailp; proc_list *plist; proc_list **ptailp; int num_args; bool_t isvoid = FALSE; /* whether first argument is void */ defp->def_kind = DEF_PROGRAM; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_LBRACE, &tok); vtailp = &defp->def.pr.versions; tailp = &defp->def.st.decls; scan(TOK_VERSION, &tok); do { scan(TOK_IDENT, &tok); vlist = XALLOC(version_list); vlist->vers_name = tok.str; scan(TOK_LBRACE, &tok); ptailp = &vlist->procs; do { /* get result type */ plist = XALLOC(proc_list); get_type(&plist->res_prefix, &plist->res_type, DEF_PROGRAM); if (streq(plist->res_type, "opaque")) { error("illegal result type"); } scan(TOK_IDENT, &tok); plist->proc_name = tok.str; scan(TOK_LPAREN, &tok); /* get args - first one */ num_args = 1; isvoid = FALSE; /* * type of DEF_PROGRAM in the first * get_prog_declaration and DEF_STURCT in the next * allows void as argument if it is the only argument */ get_prog_declaration(&dec, DEF_PROGRAM, num_args); if (streq(dec.type, "void")) isvoid = TRUE; decls = XALLOC(decl_list); plist->args.decls = decls; decls->decl = dec; tailp = &decls->next; /* get args */ while (peekscan(TOK_COMMA, &tok)) { num_args++; get_prog_declaration(&dec, DEF_STRUCT, num_args); decls = XALLOC(decl_list); decls->decl = dec; *tailp = decls; if (streq(dec.type, "void")) isvoid = TRUE; tailp = &decls->next; } /* multiple arguments are only allowed in newstyle */ if (!newstyle && num_args > 1) { error("only one argument is allowed"); } if (isvoid && num_args > 1) { error("illegal use of void in program definition"); } *tailp = NULL; scan(TOK_RPAREN, &tok); scan(TOK_EQUAL, &tok); scan_num(&tok); scan(TOK_SEMICOLON, &tok); plist->proc_num = tok.str; plist->arg_num = num_args; *ptailp = plist; ptailp = &plist->next; peek(&tok); } while (tok.kind != TOK_RBRACE); *ptailp = NULL; *vtailp = vlist; vtailp = &vlist->next; scan(TOK_RBRACE, &tok); scan(TOK_EQUAL, &tok); scan_num(&tok); vlist->vers_num = tok.str; /* make the argument structure name for each arg */ for (plist = vlist->procs; plist != NULL; plist = plist->next) { plist->args.argname = make_argname(plist->proc_name, vlist->vers_num); /* free the memory ?? */ } scan(TOK_SEMICOLON, &tok); scan2(TOK_VERSION, TOK_RBRACE, &tok); } while (tok.kind == TOK_VERSION); scan(TOK_EQUAL, &tok); scan_num(&tok); defp->def.pr.prog_num = tok.str; *vtailp = NULL; } static void def_enum(definition *defp) { token tok; enumval_list *elist; enumval_list **tailp; defp->def_kind = DEF_ENUM; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_LBRACE, &tok); tailp = &defp->def.en.vals; do { scan(TOK_IDENT, &tok); elist = XALLOC(enumval_list); elist->name = tok.str; elist->assignment = NULL; scan3(TOK_COMMA, TOK_RBRACE, TOK_EQUAL, &tok); if (tok.kind == TOK_EQUAL) { scan_num(&tok); elist->assignment = tok.str; scan2(TOK_COMMA, TOK_RBRACE, &tok); } *tailp = elist; tailp = &elist->next; } while (tok.kind != TOK_RBRACE); *tailp = NULL; } static void def_const(definition *defp) { token tok; defp->def_kind = DEF_CONST; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_EQUAL, &tok); scan2(TOK_IDENT, TOK_STRCONST, &tok); defp->def.co = tok.str; } static void def_union(definition *defp) { token tok; declaration dec; case_list *cases; case_list **tailp; int flag; defp->def_kind = DEF_UNION; scan(TOK_IDENT, &tok); defp->def_name = tok.str; scan(TOK_SWITCH, &tok); scan(TOK_LPAREN, &tok); get_declaration(&dec, DEF_UNION); defp->def.un.enum_decl = dec; tailp = &defp->def.un.cases; scan(TOK_RPAREN, &tok); scan(TOK_LBRACE, &tok); scan(TOK_CASE, &tok); while (tok.kind == TOK_CASE) { scan2(TOK_IDENT, TOK_CHARCONST, &tok); cases = XALLOC(case_list); cases->case_name = tok.str; scan(TOK_COLON, &tok); /* now peek at next token */ flag = 0; if (peekscan(TOK_CASE, &tok)){ do { scan2(TOK_IDENT, TOK_CHARCONST, &tok); cases->contflag = 1; /* continued case statement */ *tailp = cases; tailp = &cases->next; cases = XALLOC(case_list); cases->case_name = tok.str; scan(TOK_COLON, &tok); } while (peekscan(TOK_CASE, &tok)); } else if (flag) { *tailp = cases; tailp = &cases->next; cases = XALLOC(case_list); - }; + } get_declaration(&dec, DEF_UNION); cases->case_decl = dec; cases->contflag = 0; /* no continued case statement */ *tailp = cases; tailp = &cases->next; scan(TOK_SEMICOLON, &tok); scan3(TOK_CASE, TOK_DEFAULT, TOK_RBRACE, &tok); } *tailp = NULL; if (tok.kind == TOK_DEFAULT) { scan(TOK_COLON, &tok); get_declaration(&dec, DEF_UNION); defp->def.un.default_decl = XALLOC(declaration); *defp->def.un.default_decl = dec; scan(TOK_SEMICOLON, &tok); scan(TOK_RBRACE, &tok); } else { defp->def.un.default_decl = NULL; } } static const char *reserved_words[] = { "array", "bytes", "destroy", "free", "getpos", "inline", "pointer", "reference", "setpos", "sizeof", "union", "vector", NULL }; static const char *reserved_types[] = { "opaque", "string", NULL }; /* * check that the given name is not one that would eventually result in * xdr routines that would conflict with internal XDR routines. */ static void check_type_name(const char *name, int new_type) { int i; char tmp[100]; for (i = 0; reserved_words[i] != NULL; i++) { if (strcmp(name, reserved_words[i]) == 0) { sprintf(tmp, "illegal (reserved) name :\'%s\' in type definition", name); error(tmp); } } if (new_type) { for (i = 0; reserved_types[i] != NULL; i++) { if (strcmp(name, reserved_types[i]) == 0) { sprintf(tmp, "illegal (reserved) name :\'%s\' in type definition", name); error(tmp); } } } } static void def_typedef(definition *defp) { declaration dec; defp->def_kind = DEF_TYPEDEF; get_declaration(&dec, DEF_TYPEDEF); defp->def_name = dec.name; check_type_name(dec.name, 1); defp->def.ty.old_prefix = dec.prefix; defp->def.ty.old_type = dec.type; defp->def.ty.rel = dec.rel; defp->def.ty.array_max = dec.array_max; } static void get_declaration(declaration *dec, defkind dkind) { token tok; get_type(&dec->prefix, &dec->type, dkind); dec->rel = REL_ALIAS; if (streq(dec->type, "void")) { return; } check_type_name(dec->type, 0); scan2(TOK_STAR, TOK_IDENT, &tok); if (tok.kind == TOK_STAR) { dec->rel = REL_POINTER; scan(TOK_IDENT, &tok); } dec->name = tok.str; if (peekscan(TOK_LBRACKET, &tok)) { if (dec->rel == REL_POINTER) { error("no array-of-pointer declarations -- use typedef"); } dec->rel = REL_VECTOR; scan_num(&tok); dec->array_max = tok.str; scan(TOK_RBRACKET, &tok); } else if (peekscan(TOK_LANGLE, &tok)) { if (dec->rel == REL_POINTER) { error("no array-of-pointer declarations -- use typedef"); } dec->rel = REL_ARRAY; if (peekscan(TOK_RANGLE, &tok)) { dec->array_max = "~0"; /* unspecified size, use max */ } else { scan_num(&tok); dec->array_max = tok.str; scan(TOK_RANGLE, &tok); } } if (streq(dec->type, "opaque")) { if (dec->rel != REL_ARRAY && dec->rel != REL_VECTOR) { error("array declaration expected"); } } else if (streq(dec->type, "string")) { if (dec->rel != REL_ARRAY) { error("variable-length array declaration expected"); } } } static void get_prog_declaration(declaration *dec, defkind dkind, int num) { token tok; char name[10]; /* argument name */ if (dkind == DEF_PROGRAM) { peek(&tok); if (tok.kind == TOK_RPAREN) { /* no arguments */ dec->rel = REL_ALIAS; dec->type = "void"; dec->prefix = NULL; dec->name = NULL; return; } } get_type(&dec->prefix, &dec->type, dkind); dec->rel = REL_ALIAS; if (peekscan(TOK_IDENT, &tok)) /* optional name of argument */ strcpy(name, tok.str); else sprintf(name, "%s%d", ARGNAME, num); /* default name of argument */ dec->name = (char *) xstrdup(name); if (streq(dec->type, "void")) { return; } if (streq(dec->type, "opaque")) { error("opaque -- illegal argument type"); } if (peekscan(TOK_STAR, &tok)) { if (streq(dec->type, "string")) { error("pointer to string not allowed in program arguments"); } dec->rel = REL_POINTER; if (peekscan(TOK_IDENT, &tok)) { /* optional name of argument */ dec->name = xstrdup(tok.str); } } if (peekscan(TOK_LANGLE, &tok)) { if (!streq(dec->type, "string")) { error("arrays cannot be declared as arguments to procedures -- use typedef"); } dec->rel = REL_ARRAY; if (peekscan(TOK_RANGLE, &tok)) { dec->array_max = "~0"; /* unspecified size, use max */ } else { scan_num(&tok); dec->array_max = tok.str; scan(TOK_RANGLE, &tok); } } if (streq(dec->type, "string")) { if (dec->rel != REL_ARRAY) { /* * .x specifies just string as * type of argument * - make it string<> */ dec->rel = REL_ARRAY; dec->array_max = "~0"; /* unspecified size, use max */ } } } static void get_type(const char **prefixp, const char **typep, defkind dkind) { token tok; *prefixp = NULL; get_token(&tok); switch (tok.kind) { case TOK_IDENT: *typep = tok.str; break; case TOK_STRUCT: case TOK_ENUM: case TOK_UNION: *prefixp = tok.str; scan(TOK_IDENT, &tok); *typep = tok.str; break; case TOK_UNSIGNED: unsigned_dec(typep); break; case TOK_SHORT: *typep = "short"; (void) peekscan(TOK_INT, &tok); break; case TOK_LONG: *typep = "long"; (void) peekscan(TOK_INT, &tok); break; case TOK_HYPER: *typep = "int64_t"; (void) peekscan(TOK_INT, &tok); break; case TOK_VOID: if (dkind != DEF_UNION && dkind != DEF_PROGRAM) { error("voids allowed only inside union and program definitions with one argument"); } *typep = tok.str; break; case TOK_STRING: case TOK_OPAQUE: case TOK_CHAR: case TOK_INT: case TOK_FLOAT: case TOK_DOUBLE: case TOK_BOOL: case TOK_QUAD: *typep = tok.str; break; default: error("expected type specifier"); } } static void unsigned_dec(const char **typep) { token tok; peek(&tok); switch (tok.kind) { case TOK_CHAR: get_token(&tok); *typep = "u_char"; break; case TOK_SHORT: get_token(&tok); *typep = "u_short"; (void) peekscan(TOK_INT, &tok); break; case TOK_LONG: get_token(&tok); *typep = "u_long"; (void) peekscan(TOK_INT, &tok); break; case TOK_HYPER: get_token(&tok); *typep = "u_int64_t"; (void) peekscan(TOK_INT, &tok); break; case TOK_INT: get_token(&tok); *typep = "u_int"; break; default: *typep = "u_int"; break; } } Index: head/usr.bin/rpcgen/rpc_svcout.c =================================================================== --- head/usr.bin/rpcgen/rpc_svcout.c (revision 298088) +++ head/usr.bin/rpcgen/rpc_svcout.c (revision 298089) @@ -1,1020 +1,1020 @@ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape * media and as a part of the software program in whole or part. Users * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #if 0 #ifndef lint #ident "@(#)rpc_svcout.c 1.4 90/04/13 SMI" static char sccsid[] = "@(#)rpc_svcout.c 1.29 89/03/30 (C) 1987 SMI"; #endif #endif #include __FBSDID("$FreeBSD$"); /* * rpc_svcout.c, Server-skeleton outputter for the RPC protocol compiler * Copyright (C) 1987, Sun Microsystems, Inc. */ #include #include #include "rpc_parse.h" #include "rpc_scan.h" #include "rpc_util.h" static char RQSTP[] = "rqstp"; static char TRANSP[] = "transp"; static char ARG[] = "argument"; static char RESULT[] = "result"; static char ROUTINE[] = "local"; static char RETVAL[] = "retval"; static char _errbuf[256]; /* For all messages */ void internal_proctype( proc_list * ); static void write_real_program( definition * ); static void write_program(definition *, const char *); static void printerr(const char *, const char *); static void printif(const char *, const char *, const char *, const char *); static void write_inetmost(const char *); static void print_return(const char *); static void print_pmapunset(const char *); static void print_err_message(const char *); static void write_timeout_func( void ); static void write_pm_most(const char *, int); static void write_rpc_svc_fg(const char *, const char *); static void open_log_file(const char *, const char *); static void write_msg_out( void ); static void p_xdrfunc(const char *rname, const char *typename) { f_print(fout, "\t\txdr_%s = (xdrproc_t) xdr_%s;\n", rname, stringfix(typename)); } void internal_proctype(proc_list *plist) { f_print(fout, "static "); ptype(plist->res_prefix, plist->res_type, 1); f_print(fout, "*"); } /* * write most of the service, that is, everything but the registrations. */ void write_most(const char *infile, int netflag, int nomain) { if (inetdflag || pmflag) { const char *var_type; var_type = (nomain? "extern" : "static"); f_print(fout, "%s int _rpcpmstart;", var_type); f_print(fout, "\t\t/* Started by a port monitor ? */\n"); if (!tirpcflag || tirpc_socket) { f_print(fout, "%s int _rpcfdtype;", var_type); f_print(fout, "\n\t\t /* Whether Stream or \ Datagram ? */\n"); } if (timerflag) { f_print(fout, " /* States a server can be in \ wrt request */\n\n"); f_print(fout, "#define\t_IDLE 0\n"); f_print(fout, "#define\t_SERVED 1\n"); f_print(fout, "#define\t_SERVING 2\n\n"); f_print(fout, "static int _rpcsvcstate = _IDLE;"); f_print(fout, "\t /* Set when a request is \ serviced */\n"); if (mtflag) { f_print(fout, "pthread_mutex_t _svcstate_lock;"); f_print(fout, "\t\t\t/* Mutex lock for variable _rpcsvcstate */\n"); } } write_svc_aux(nomain); } /* write out dispatcher and stubs */ write_programs((char *)NULL); if (nomain) return; f_print(fout, "\nint\n"); f_print(fout, "main()\n"); f_print(fout, "{\n"); if (inetdflag) { write_inetmost(infile); /* Includes call to write_rpc_svc_fg() */ } else { if (tirpcflag) { if (netflag) { f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP); f_print(fout, "\tstruct netconfig *nconf = NULL;\n"); } f_print(fout, "\tpid_t pid;\n"); f_print(fout, "\tint i;\n"); if (pmflag) { if (tirpc_socket) { f_print(fout, "\tstruct sockaddr_storage saddr;\n"); f_print(fout, "\tsocklen_t asize = sizeof (saddr);\n\n"); } else f_print(fout, "\tchar mname[FMNAMESZ + 1];\n\n"); } if (mtflag & timerflag) f_print(fout, "\tmutex_init(&_svcstate_lock, USYNC_THREAD, NULL);\n"); if (pmflag) { write_pm_most(infile, netflag); f_print(fout, "\telse {\n"); write_rpc_svc_fg(infile, "\t\t"); f_print(fout, "\t}\n"); } else write_rpc_svc_fg(infile, "\t\t"); } else { f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP); f_print(fout, "\n"); print_pmapunset("\t"); } } if (logflag && !inetdflag) { open_log_file(infile, "\t"); } } /* * write a registration for the given transport */ void write_netid_register(const char *transp) { list *l; definition *def; version_list *vp; const char *sp; char tmpbuf[32]; sp = ""; f_print(fout, "\n"); f_print(fout, "%s\tnconf = getnetconfigent(\"%s\");\n", sp, transp); f_print(fout, "%s\tif (nconf == NULL) {\n", sp); (void) sprintf(_errbuf, "cannot find %s netid.", transp); sprintf(tmpbuf, "%s\t\t", sp); print_err_message(tmpbuf); f_print(fout, "%s\t\texit(1);\n", sp); f_print(fout, "%s\t}\n", sp); if (tirpcflag) { f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, ", sp, TRANSP); f_print(fout,"nconf, 0, RPC_MAXDATASIZE, RPC_MAXDATASIZE);\n"); } else { f_print(fout, "%s\t%s = svc_tli_create(RPC_ANYFD, nconf, 0, 0, 0);\n", sp, TRANSP); } f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP); (void) sprintf(_errbuf, "cannot create %s service.", transp); print_err_message(tmpbuf); f_print(fout, "%s\t\texit(1);\n", sp); f_print(fout, "%s\t}\n", sp); for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind != DEF_PROGRAM) { continue; } for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { f_print(fout, "%s\t(void) rpcb_unset(%s, %s, nconf);\n", sp, def->def_name, vp->vers_name); f_print(fout, "%s\tif (!svc_reg(%s, %s, %s, ", sp, TRANSP, def->def_name, vp->vers_name); pvname(def->def_name, vp->vers_num); f_print(fout, ", nconf)) {\n"); (void) sprintf(_errbuf, "unable to register (%s, %s, %s).", def->def_name, vp->vers_name, transp); print_err_message(tmpbuf); f_print(fout, "%s\t\texit(1);\n", sp); f_print(fout, "%s\t}\n", sp); } } f_print(fout, "%s\tfreenetconfigent(nconf);\n", sp); } /* * write a registration for the given transport for TLI */ void write_nettype_register(const char *transp) { list *l; definition *def; version_list *vp; for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind != DEF_PROGRAM) { continue; } for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { f_print(fout, "\tif (!svc_create("); pvname(def->def_name, vp->vers_num); f_print(fout, ", %s, %s, \"%s\")) {\n", def->def_name, vp->vers_name, transp); (void) sprintf(_errbuf, "unable to create (%s, %s) for %s.", def->def_name, vp->vers_name, transp); print_err_message("\t\t"); f_print(fout, "\t\texit(1);\n"); f_print(fout, "\t}\n"); } } } /* * write the rest of the service */ void write_rest(void) { f_print(fout, "\n"); if (inetdflag) { f_print(fout, "\tif (%s == (SVCXPRT *)NULL) {\n", TRANSP); (void) sprintf(_errbuf, "could not create a handle"); print_err_message("\t\t"); f_print(fout, "\t\texit(1);\n"); f_print(fout, "\t}\n"); if (timerflag) { f_print(fout, "\tif (_rpcpmstart) {\n"); f_print(fout, "\t\t(void) signal(SIGALRM, closedown);\n"); f_print(fout, "\t\t(void) \ alarm(_RPCSVC_CLOSEDOWN/2);\n"); f_print(fout, "\t}\n"); } } f_print(fout, "\tsvc_run();\n"); (void) sprintf(_errbuf, "svc_run returned"); print_err_message("\t"); f_print(fout, "\texit(1);\n"); f_print(fout, "\t/* NOTREACHED */\n"); f_print(fout, "}\n"); } void write_programs(const char *storage) { list *l; definition *def; /* write out stubs for procedure definitions */ for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind == DEF_PROGRAM) { write_real_program(def); } } /* write out dispatcher for each program */ for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind == DEF_PROGRAM) { write_program(def, storage); } } } /* * write out definition of internal function (e.g. _printmsg_1(...)) * which calls server's definition of actual function (e.g. printmsg_1(...)). * Unpacks single user argument of printmsg_1 to call-by-value format * expected by printmsg_1. */ static void write_real_program(definition *def) { version_list *vp; proc_list *proc; decl_list *l; if (!newstyle) return; /* not needed for old style */ for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { for (proc = vp->procs; proc != NULL; proc = proc->next) { f_print(fout, "\n"); if (!mtflag) internal_proctype(proc); else f_print(fout, "int"); f_print(fout, "\n_"); pvname(proc->proc_name, vp->vers_num); f_print(fout, "("); /* arg name */ if (proc->arg_num > 1) fputs(proc->args.argname, fout); else ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 0); if (mtflag) { f_print(fout, " *argp, void *%s, struct svc_req *%s)\n", RESULT, RQSTP); } else f_print(fout, " *argp, struct svc_req *%s)\n", RQSTP); f_print(fout, "{\n"); f_print(fout, "\treturn ("); pvname_svc(proc->proc_name, vp->vers_num); f_print(fout, "("); if (proc->arg_num < 2) { /* single argument */ if (!streq(proc->args.decls->decl.type, "void")) f_print(fout, "*argp, "); /* non-void */ } else { for (l = proc->args.decls; l != NULL; l = l->next) f_print(fout, "argp->%s, ", l->decl.name); } if (mtflag) f_print(fout, "%s, ",RESULT); f_print(fout, "%s));\n}\n", RQSTP); } } } static void write_program(definition *def, const char *storage) { version_list *vp; proc_list *proc; int filled; for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { f_print(fout, "\n"); if (storage != NULL) { f_print(fout, "%s ", storage); } f_print(fout, "void\n"); pvname(def->def_name, vp->vers_num); f_print(fout, "(struct svc_req *%s, ", RQSTP); f_print(fout, "SVCXPRT *%s)\n", TRANSP); f_print(fout, "{\n"); filled = 0; f_print(fout, "\tunion {\n"); for (proc = vp->procs; proc != NULL; proc = proc->next) { if (proc->arg_num < 2) { /* single argument */ if (streq(proc->args.decls->decl.type, "void")) { continue; } filled = 1; f_print(fout, "\t\t"); ptype(proc->args.decls->decl.prefix, proc->args.decls->decl.type, 0); pvname(proc->proc_name, vp->vers_num); f_print(fout, "_arg;\n"); } else { filled = 1; f_print(fout, "\t\t%s", proc->args.argname); f_print(fout, " "); pvname(proc->proc_name, vp->vers_num); f_print(fout, "_arg;\n"); } } if (!filled) { f_print(fout, "\t\tint fill;\n"); } f_print(fout, "\t} %s;\n", ARG); if (mtflag) { f_print(fout, "\tunion {\n"); for (proc = vp->procs; proc != NULL; proc = proc->next) { if (streq(proc->res_type, "void")) { continue; } f_print(fout, "\t\t"); ptype(proc->res_prefix, proc->res_type, 0); pvname(proc->proc_name, vp->vers_num); f_print(fout, "_res;\n"); } f_print(fout, "\t} %s;\n", RESULT); f_print(fout, "\tbool_t %s;\n", RETVAL); } else f_print(fout, "\tchar *%s;\n", RESULT); f_print(fout, "\txdrproc_t xdr_%s, xdr_%s;\n", ARG, RESULT); if (mtflag) f_print(fout, "\tbool_t (*%s)(char *, void *, struct svc_req *);\n", ROUTINE); else f_print(fout, "\tchar *(*%s)(char *, struct svc_req *);\n", ROUTINE); f_print(fout, "\n"); if (timerflag) { if (mtflag) f_print(fout, "\tpthread_mutex_lock(&_svcstate_lock);\n"); f_print(fout, "\t_rpcsvcstate = _SERVING;\n"); if (mtflag) f_print(fout, "\tpthread_mutex_unlock(&_svcstate_lock);\n"); } f_print(fout, "\tswitch (%s->rq_proc) {\n", RQSTP); if (!nullproc(vp->procs)) { f_print(fout, "\tcase NULLPROC:\n"); f_print(fout, "\t\t(void) svc_sendreply(%s,\n\t\t\t" "(xdrproc_t) xdr_void, (char *)NULL);\n", TRANSP); print_return("\t\t"); f_print(fout, "\n"); } for (proc = vp->procs; proc != NULL; proc = proc->next) { f_print(fout, "\tcase %s:\n", proc->proc_name); if (proc->arg_num < 2) { /* single argument */ p_xdrfunc(ARG, proc->args.decls->decl.type); } else { p_xdrfunc(ARG, proc->args.argname); } p_xdrfunc(RESULT, proc->res_type); if (mtflag) f_print(fout, "\t\t%s = (bool_t (*) (char *, void *, struct svc_req *))", ROUTINE); else f_print(fout, "\t\t%s = (char *(*)(char *, struct svc_req *)) ", ROUTINE); if (newstyle) { /* new style: calls internal routine */ f_print(fout, "_"); } if (!newstyle) pvname_svc(proc->proc_name, vp->vers_num); else pvname(proc->proc_name, vp->vers_num); f_print(fout, ";\n"); f_print(fout, "\t\tbreak;\n\n"); } f_print(fout, "\tdefault:\n"); printerr("noproc", TRANSP); print_return("\t\t"); f_print(fout, "\t}\n"); f_print(fout, "\t(void) memset((char *)&%s, 0, sizeof (%s));\n", ARG, ARG); printif("getargs", TRANSP, "(caddr_t) &", ARG); printerr("decode", TRANSP); print_return("\t\t"); f_print(fout, "\t}\n"); if (!mtflag) f_print(fout, "\t%s = (*%s)((char *)&%s, %s);\n", RESULT, ROUTINE, ARG, RQSTP); else f_print(fout, "\t%s = (bool_t) (*%s)((char *)&%s, (void *)&%s, %s);\n", RETVAL, ROUTINE, ARG, RESULT, RQSTP); if (mtflag) f_print(fout, "\tif (%s > 0 && !svc_sendreply(%s, xdr_%s, (char *)&%s)) {\n", RETVAL, TRANSP, RESULT, RESULT); else f_print(fout, "\tif (%s != NULL && !svc_sendreply(%s, xdr_%s, %s)) {\n", RESULT, TRANSP, RESULT, RESULT); printerr("systemerr", TRANSP); f_print(fout, "\t}\n"); printif("freeargs", TRANSP, "(caddr_t) &", ARG); (void) sprintf(_errbuf, "unable to free arguments"); print_err_message("\t\t"); f_print(fout, "\t\texit(1);\n"); f_print(fout, "\t}\n"); /* print out free routine */ if (mtflag) { f_print(fout,"\tif (!"); pvname(def->def_name, vp->vers_num); f_print(fout,"_freeresult(%s, xdr_%s, (caddr_t) &%s))\n", TRANSP, RESULT, RESULT); (void) sprintf(_errbuf, "unable to free results"); print_err_message("\t\t"); f_print(fout, "\n"); - }; + } print_return("\t"); f_print(fout, "}\n"); } } static void printerr(const char *err, const char *transp) { f_print(fout, "\t\tsvcerr_%s(%s);\n", err, transp); } static void printif(const char *proc, const char *transp, const char *prefix, const char *arg) { f_print(fout, "\tif (!svc_%s(%s, xdr_%s, (char *)%s%s)) {\n", proc, transp, arg, prefix, arg); } int nullproc(proc_list *proc) { for (; proc != NULL; proc = proc->next) { if (streq(proc->proc_num, "0")) { return (1); } } return (0); } static void write_inetmost(const char *infile) { f_print(fout, "\tregister SVCXPRT *%s;\n", TRANSP); f_print(fout, "\tint sock;\n"); f_print(fout, "\tint proto;\n"); f_print(fout, "\tstruct sockaddr_in saddr;\n"); f_print(fout, "\tsocklen_t asize = sizeof (saddr);\n"); f_print(fout, "\n"); f_print(fout, "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n"); f_print(fout, "\t\tsocklen_t ssize = sizeof (int);\n\n"); f_print(fout, "\t\tif (saddr.sin_family != AF_INET)\n"); f_print(fout, "\t\t\texit(1);\n"); f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n"); f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n"); f_print(fout, "\t\t\texit(1);\n"); f_print(fout, "\t\tsock = 0;\n"); f_print(fout, "\t\t_rpcpmstart = 1;\n"); f_print(fout, "\t\tproto = 0;\n"); open_log_file(infile, "\t\t"); f_print(fout, "\t} else {\n"); write_rpc_svc_fg(infile, "\t\t"); f_print(fout, "\t\tsock = RPC_ANYSOCK;\n"); print_pmapunset("\t\t"); f_print(fout, "\t}\n"); } static void print_return(const char *space) { if (exitnow) f_print(fout, "%sexit(0);\n", space); else { if (timerflag) { if (mtflag) f_print(fout, "%spthread_mutex_lock(&_svcstate_lock);\n", space); f_print(fout, "%s_rpcsvcstate = _SERVED;\n", space); if (mtflag) f_print(fout, "%spthread_mutex_unlock(&_svcstate_lock);\n", space); } f_print(fout, "%sreturn;\n", space); } } static void print_pmapunset(const char *space) { list *l; definition *def; version_list *vp; for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind == DEF_PROGRAM) { for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { f_print(fout, "%s(void) pmap_unset(%s, %s);\n", space, def->def_name, vp->vers_name); } } } } static void print_err_message(const char *space) { if (logflag) f_print(fout, "%ssyslog(LOG_ERR, \"%s\");\n", space, _errbuf); else if (inetdflag || pmflag) f_print(fout, "%s_msgout(\"%s\");\n", space, _errbuf); else f_print(fout, "%sfprintf(stderr, \"%s\");\n", space, _errbuf); } /* * Write the server auxiliary function (_msgout, timeout) */ void write_svc_aux(int nomain) { if (!logflag) write_msg_out(); if (!nomain) write_timeout_func(); } /* * Write the _msgout function */ static void write_msg_out(void) { f_print(fout, "\n"); /* * Avoid making _msgout() static -- it's useful to have it visible * in the toplevel RPC server code. */ f_print(fout, "static\n"); f_print(fout, "void _msgout(const char* msg)\n"); f_print(fout, "{\n"); f_print(fout, "#ifdef RPC_SVC_FG\n"); if (inetdflag || pmflag) f_print(fout, "\tif (_rpcpmstart)\n"); f_print(fout, "\t\tsyslog(LOG_ERR, \"%%s\", msg);\n"); f_print(fout, "\telse\n"); f_print(fout, "\t\t(void) fprintf(stderr, \"%%s\\n\", msg);\n"); f_print(fout, "#else\n"); f_print(fout, "\tsyslog(LOG_ERR, \"%%s\", msg);\n"); f_print(fout, "#endif\n"); f_print(fout, "}\n"); } /* * Write the timeout function */ static void write_timeout_func(void) { if (!timerflag) return; f_print(fout, "\n"); f_print(fout, "static void\n"); f_print(fout, "closedown(int sig)\n"); f_print(fout, "{\n"); if (mtflag) f_print(fout, "\tpthread_mutex_lock(&_svcstate_lock);\n"); f_print(fout, "\tif (_rpcsvcstate == _IDLE) {\n"); f_print(fout, "\t\textern fd_set svc_fdset;\n"); f_print(fout, "\t\tstatic int size;\n"); f_print(fout, "\t\tint i, openfd;\n"); if (tirpcflag && pmflag) { f_print(fout, "\t\tstruct t_info tinfo;\n\n"); f_print(fout, "\t\tif (!t_getinfo(0, &tinfo) && (tinfo.servtype == T_CLTS))\n"); } else { f_print(fout, "\n\t\tif (_rpcfdtype == SOCK_DGRAM)\n"); } f_print(fout, "\t\t\texit(0);\n"); f_print(fout, "\t\tif (size == 0) {\n"); if (tirpcflag) { f_print(fout, "\t\t\tstruct rlimit rl;\n\n"); f_print(fout, "\t\t\trl.rlim_max = 0;\n"); f_print(fout, "\t\t\tgetrlimit(RLIMIT_NOFILE, &rl);\n"); f_print(fout, "\t\t\tif ((size = rl.rlim_max) == 0) {\n"); if (mtflag) f_print(fout, "\t\t\t\tpthread_mutex_unlock(&_svcstate_lock);\n"); f_print(fout, "\t\t\t\treturn;\n\t\t\t}\n"); } else { f_print(fout, "\t\t\tsize = getdtablesize();\n"); } f_print(fout, "\t\t}\n"); f_print(fout, "\t\tfor (i = 0, openfd = 0; i < size && openfd < 2; i++)\n"); f_print(fout, "\t\t\tif (FD_ISSET(i, &svc_fdset))\n"); f_print(fout, "\t\t\t\topenfd++;\n"); f_print(fout, "\t\tif (openfd <= 1)\n"); f_print(fout, "\t\t\texit(0);\n"); f_print(fout, "\t}\n"); f_print(fout, "\tif (_rpcsvcstate == _SERVED)\n"); f_print(fout, "\t\t_rpcsvcstate = _IDLE;\n\n"); if (mtflag) f_print(fout, "\tpthread_mutex_unlock(&_svcstate_lock);\n"); f_print(fout, "\t(void) signal(SIGALRM, closedown);\n"); f_print(fout, "\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n"); f_print(fout, "}\n"); } /* * Write the most of port monitor support */ static void write_pm_most(const char *infile, int netflag) { list *l; definition *def; version_list *vp; if (tirpc_socket) { f_print(fout, "\tif (getsockname(0, (struct sockaddr *)&saddr, &asize) == 0) {\n"); f_print(fout, "\t\tsocklen_t ssize = sizeof (int);\n"); } else { f_print(fout, "\tif (!ioctl(0, I_LOOK, mname) &&\n"); f_print(fout, "\t\t(!strcmp(mname, \"sockmod\") ||"); f_print(fout, " !strcmp(mname, \"timod\"))) {\n"); } f_print(fout, "\t\tchar *netid;\n"); if (!netflag) { /* Not included by -n option */ f_print(fout, "\t\tstruct netconfig *nconf = NULL;\n"); f_print(fout, "\t\tSVCXPRT *%s;\n", TRANSP); } if (timerflag) f_print(fout, "\t\tint pmclose;\n"); /* * Not necessary, defined in /usr/include/stdlib * f_print(fout, "\t\textern char *getenv();\n"); */ f_print(fout, "\n"); if (tirpc_socket) { f_print(fout, "\t\tif (saddr.ss_family != AF_INET &&\n"); f_print(fout, "\t\t saddr.ss_family != AF_INET6)\n"); f_print(fout, "\t\t\texit(1);\n"); f_print(fout, "\t\tif (getsockopt(0, SOL_SOCKET, SO_TYPE,\n"); f_print(fout, "\t\t\t\t(char *)&_rpcfdtype, &ssize) == -1)\n"); f_print(fout, "\t\t\texit(1);\n"); } f_print(fout, "\t\t_rpcpmstart = 1;\n"); open_log_file(infile, "\t\t"); f_print(fout, "\n\t\tif ((netid = \ getenv(\"NLSPROVIDER\")) == NULL) {\n"); if (timerflag) { f_print(fout, "\t\t/* started from inetd */\n"); f_print(fout, "\t\t\tpmclose = 1;\n"); } f_print(fout, "\t\t} else {\n"); f_print(fout, "\t\t\tif ((nconf = getnetconfigent(netid)) == NULL)\n"); sprintf(_errbuf, "cannot get transport info"); print_err_message("\t\t\t\t"); if (timerflag) { if (tirpc_socket) f_print(fout, "\n\t\t\tpmclose = 1;\t/* XXX */\n"); else f_print(fout, "\n\t\t\tpmclose = (t_getstate(0) != T_DATAXFER);\n"); } f_print(fout, "\t\t}\n"); /* * A kludgy support for inetd services. Inetd only works with * sockmod, and RPC works only with timod, hence all this jugglery */ if (!tirpc_socket) { f_print(fout, "\t\tif (strcmp(mname, \"sockmod\") == 0) {\n"); f_print(fout, "\t\t\tif (ioctl(0, I_POP, 0) || "); f_print(fout, "ioctl(0, I_PUSH, \"timod\")) {\n"); sprintf(_errbuf, "could not get the right module"); print_err_message("\t\t\t\t"); f_print(fout, "\t\t\t\texit(1);\n"); f_print(fout, "\t\t\t}\n"); f_print(fout, "\t\t}\n"); } if (tirpcflag) { f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, \ RPC_MAXDATASIZE, RPC_MAXDATASIZE)) \ == NULL) {\n", TRANSP); } else { f_print(fout, "\t\tif ((%s = svc_tli_create(0, nconf, NULL, 0, 0)) \ == NULL) {\n", TRANSP); } sprintf(_errbuf, "cannot create server handle"); print_err_message("\t\t\t"); f_print(fout, "\t\t\texit(1);\n"); f_print(fout, "\t\t}\n"); f_print(fout, "\t\tif (nconf)\n"); f_print(fout, "\t\t\tfreenetconfigent(nconf);\n"); for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind != DEF_PROGRAM) { continue; } for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { f_print(fout, "\t\tif (!svc_reg(%s, %s, %s, ", TRANSP, def->def_name, vp->vers_name); pvname(def->def_name, vp->vers_num); f_print(fout, ", 0)) {\n"); (void) sprintf(_errbuf, "unable to register (%s, %s).", def->def_name, vp->vers_name); print_err_message("\t\t\t"); f_print(fout, "\t\t\texit(1);\n"); f_print(fout, "\t\t}\n"); } } if (timerflag) { f_print(fout, "\t\tif (pmclose) {\n"); f_print(fout, "\t\t\t(void) signal(SIGALRM, closedown);\n"); f_print(fout, "\t\t\t(void) alarm(_RPCSVC_CLOSEDOWN/2);\n"); f_print(fout, "\t\t}\n"); } f_print(fout, "\t\tsvc_run();\n"); f_print(fout, "\t\texit(1);\n"); f_print(fout, "\t\t/* NOTREACHED */\n"); f_print(fout, "\t}"); } /* * Support for backgrounding the server if self started. */ static void write_rpc_svc_fg(const char *infile, const char *sp) { f_print(fout, "#ifndef RPC_SVC_FG\n"); f_print(fout, "%sint size;\n", sp); if (tirpcflag) f_print(fout, "%sstruct rlimit rl;\n", sp); if (inetdflag) f_print(fout, "%sint pid, i;\n\n", sp); f_print(fout, "%spid = fork();\n", sp); f_print(fout, "%sif (pid < 0) {\n", sp); f_print(fout, "%s\tperror(\"cannot fork\");\n", sp); f_print(fout, "%s\texit(1);\n", sp); f_print(fout, "%s}\n", sp); f_print(fout, "%sif (pid)\n", sp); f_print(fout, "%s\texit(0);\n", sp); /* get number of file descriptors */ if (tirpcflag) { f_print(fout, "%srl.rlim_max = 0;\n", sp); f_print(fout, "%sgetrlimit(RLIMIT_NOFILE, &rl);\n", sp); f_print(fout, "%sif ((size = rl.rlim_max) == 0)\n", sp); f_print(fout, "%s\texit(1);\n", sp); } else { f_print(fout, "%ssize = getdtablesize();\n", sp); } f_print(fout, "%sfor (i = 0; i < size; i++)\n", sp); f_print(fout, "%s\t(void) close(i);\n", sp); /* Redirect stderr and stdout to console */ f_print(fout, "%si = open(\"/dev/console\", 2);\n", sp); f_print(fout, "%s(void) dup2(i, 1);\n", sp); f_print(fout, "%s(void) dup2(i, 2);\n", sp); /* This removes control of the controlling terminal */ if (tirpcflag) f_print(fout, "%ssetsid();\n", sp); else { f_print(fout, "%si = open(\"/dev/tty\", 2);\n", sp); f_print(fout, "%sif (i >= 0) {\n", sp); f_print(fout, "%s\t(void) ioctl(i, TIOCNOTTY, (char *)NULL);\n", sp); f_print(fout, "%s\t(void) close(i);\n", sp); f_print(fout, "%s}\n", sp); } if (!logflag) open_log_file(infile, sp); f_print(fout, "#endif\n"); if (logflag) open_log_file(infile, sp); } static void open_log_file(const char *infile, const char *sp) { char *s; s = strrchr(infile, '.'); if (s) *s = '\0'; f_print(fout, "%sopenlog(\"%s\", LOG_PID, LOG_DAEMON);\n", sp, infile); if (s) *s = '.'; } /* * write a registration for the given transport for Inetd */ void write_inetd_register(const char *transp) { list *l; definition *def; version_list *vp; const char *sp; int isudp; char tmpbuf[32]; if (inetdflag) sp = "\t"; else sp = ""; if (streq(transp, "udp")) isudp = 1; else isudp = 0; f_print(fout, "\n"); if (inetdflag) { f_print(fout, "\tif ((_rpcfdtype == 0) || (_rpcfdtype == %s)) {\n", isudp ? "SOCK_DGRAM" : "SOCK_STREAM"); } f_print(fout, "%s\t%s = svc%s_create(%s", sp, TRANSP, transp, inetdflag? "sock": "RPC_ANYSOCK"); if (!isudp) f_print(fout, ", 0, 0"); f_print(fout, ");\n"); f_print(fout, "%s\tif (%s == NULL) {\n", sp, TRANSP); (void) sprintf(_errbuf, "cannot create %s service.", transp); (void) sprintf(tmpbuf, "%s\t\t", sp); print_err_message(tmpbuf); f_print(fout, "%s\t\texit(1);\n", sp); f_print(fout, "%s\t}\n", sp); if (inetdflag) { f_print(fout, "%s\tif (!_rpcpmstart)\n\t", sp); f_print(fout, "%s\tproto = IPPROTO_%s;\n", sp, isudp ? "UDP": "TCP"); } for (l = defined; l != NULL; l = l->next) { def = (definition *) l->val; if (def->def_kind != DEF_PROGRAM) { continue; } for (vp = def->def.pr.versions; vp != NULL; vp = vp->next) { f_print(fout, "%s\tif (!svc_register(%s, %s, %s, ", sp, TRANSP, def->def_name, vp->vers_name); pvname(def->def_name, vp->vers_num); if (inetdflag) f_print(fout, ", proto)) {\n"); else f_print(fout, ", IPPROTO_%s)) {\n", isudp ? "UDP": "TCP"); (void) sprintf(_errbuf, "unable to register (%s, %s, %s).", def->def_name, vp->vers_name, transp); print_err_message(tmpbuf); f_print(fout, "%s\t\texit(1);\n", sp); f_print(fout, "%s\t}\n", sp); } } if (inetdflag) f_print(fout, "\t}\n"); } Index: head/usr.bin/rpcgen/rpc_util.c =================================================================== --- head/usr.bin/rpcgen/rpc_util.c (revision 298088) +++ head/usr.bin/rpcgen/rpc_util.c (revision 298089) @@ -1,511 +1,511 @@ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape * media and as a part of the software program in whole or part. Users * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #if 0 #ifndef lint #ident "@(#)rpc_util.c 1.14 93/07/05 SMI" static char sccsid[] = "@(#)rpc_util.c 1.11 89/02/22 (C) 1987 SMI"; #endif #endif #include __FBSDID("$FreeBSD$"); /* * rpc_util.c, Utility routines for the RPC protocol compiler * Copyright (C) 1989, Sun Microsystems, Inc. */ #include #include #include #include #include #include "rpc_parse.h" #include "rpc_scan.h" #include "rpc_util.h" #define ARGEXT "argument" char curline[MAXLINESIZE]; /* current read line */ char *where = curline; /* current point in line */ int linenum = 0; /* current line number */ const char *infilename; /* input filename */ #define NFILES 7 static const char *outfiles[NFILES]; /* output file names */ static int nfiles; FILE *fout; /* file pointer of current output */ FILE *fin; /* file pointer of current input */ list *defined; /* list of defined things */ static void printwhere( void ); /* * Reinitialize the world */ void reinitialize(void) { memset(curline, 0, MAXLINESIZE); where = curline; linenum = 0; defined = NULL; } /* * string equality */ int streq(const char *a, const char *b) { return (strcmp(a, b) == 0); } /* * find a value in a list */ definition * findval(list *lst, const char *val, int (*cmp)(definition *, const char *)) { for (; lst != NULL; lst = lst->next) { if ((*cmp) (lst->val, val)) { return (lst->val); } } return (NULL); } /* * store a value in a list */ void storeval(list **lstp, definition *val) { list **l; list *lst; for (l = lstp; *l != NULL; l = (list **) & (*l)->next); lst = XALLOC(list); lst->val = val; lst->next = NULL; *l = lst; } static int findit(definition *def, const char *type) { return (streq(def->def_name, type)); } static const char * fixit(const char *type, const char *orig) { definition *def; def = (definition *) FINDVAL(defined, type, findit); if (def == NULL || def->def_kind != DEF_TYPEDEF) { return (orig); } switch (def->def.ty.rel) { case REL_VECTOR: if (streq(def->def.ty.old_type, "opaque")) return ("char"); else return (def->def.ty.old_type); case REL_ALIAS: return (fixit(def->def.ty.old_type, orig)); default: return (orig); } } const char * fixtype(const char *type) { return (fixit(type, type)); } const char * stringfix(const char *type) { if (streq(type, "string")) { return ("wrapstring"); } else { return (type); } } void ptype(const char *prefix, const char *type, int follow) { if (prefix != NULL) { if (streq(prefix, "enum")) { f_print(fout, "enum "); } else { f_print(fout, "struct "); } } if (streq(type, "bool")) { f_print(fout, "bool_t "); } else if (streq(type, "string")) { f_print(fout, "char *"); } else { f_print(fout, "%s ", follow ? fixtype(type) : type); } } static int typedefed(definition *def, const char *type) { if (def->def_kind != DEF_TYPEDEF || def->def.ty.old_prefix != NULL) { return (0); } else { return (streq(def->def_name, type)); } } int isvectordef(const char *type, relation rel) { definition *def; for (;;) { switch (rel) { case REL_VECTOR: return (!streq(type, "string")); case REL_ARRAY: return (0); case REL_POINTER: return (0); case REL_ALIAS: def = (definition *) FINDVAL(defined, type, typedefed); if (def == NULL) { return (0); } type = def->def.ty.old_type; rel = def->def.ty.rel; } } return (0); } char * locase(const char *str) { char c; static char buf[100]; char *p = buf; while ( (c = *str++) ) { *p++ = (c >= 'A' && c <= 'Z') ? (c - 'A' + 'a') : c; } *p = 0; return (buf); } void pvname_svc(const char *pname, const char *vnum) { f_print(fout, "%s_%s_svc", locase(pname), vnum); } void pvname(const char *pname, const char *vnum) { f_print(fout, "%s_%s", locase(pname), vnum); } /* * print a useful (?) error message, and then die */ void error(const char *msg) { printwhere(); warnx("%s, line %d: %s", infilename, linenum, msg); crash(); } /* * Something went wrong, unlink any files that we may have created and then * die. */ void crash(void) { int i; for (i = 0; i < nfiles; i++) { (void) unlink(outfiles[i]); } exit(1); } void record_open(const char *file) { if (nfiles < NFILES) { outfiles[nfiles++] = file; } else { warnx("too many files"); crash(); } } static char expectbuf[100]; static const char *toktostr(tok_kind kind); /* * error, token encountered was not the expected one */ void expected1(tok_kind exp1) { s_print(expectbuf, "expected '%s'", toktostr(exp1)); error(expectbuf); } /* * error, token encountered was not one of two expected ones */ void expected2(tok_kind exp1, tok_kind exp2) { s_print(expectbuf, "expected '%s' or '%s'", toktostr(exp1), toktostr(exp2)); error(expectbuf); } /* * error, token encountered was not one of 3 expected ones */ void expected3(tok_kind exp1, tok_kind exp2, tok_kind exp3) { s_print(expectbuf, "expected '%s', '%s' or '%s'", toktostr(exp1), toktostr(exp2), toktostr(exp3)); error(expectbuf); } void tabify(FILE *f, int tab) { while (tab--) { (void) fputc('\t', f); } } static token tokstrings[] = { {TOK_IDENT, "identifier"}, {TOK_CONST, "const"}, {TOK_RPAREN, ")"}, {TOK_LPAREN, "("}, {TOK_RBRACE, "}"}, {TOK_LBRACE, "{"}, {TOK_LBRACKET, "["}, {TOK_RBRACKET, "]"}, {TOK_STAR, "*"}, {TOK_COMMA, ","}, {TOK_EQUAL, "="}, {TOK_COLON, ":"}, {TOK_SEMICOLON, ";"}, {TOK_UNION, "union"}, {TOK_STRUCT, "struct"}, {TOK_SWITCH, "switch"}, {TOK_CASE, "case"}, {TOK_DEFAULT, "default"}, {TOK_ENUM, "enum"}, {TOK_TYPEDEF, "typedef"}, {TOK_INT, "int"}, {TOK_SHORT, "short"}, {TOK_LONG, "long"}, {TOK_UNSIGNED, "unsigned"}, {TOK_DOUBLE, "double"}, {TOK_FLOAT, "float"}, {TOK_CHAR, "char"}, {TOK_STRING, "string"}, {TOK_OPAQUE, "opaque"}, {TOK_BOOL, "bool"}, {TOK_VOID, "void"}, {TOK_PROGRAM, "program"}, {TOK_VERSION, "version"}, {TOK_EOF, "??????"} }; static const char * toktostr(tok_kind kind) { token *sp; for (sp = tokstrings; sp->kind != TOK_EOF && sp->kind != kind; sp++); return (sp->str); } static void printbuf(void) { char c; int i; int cnt; # define TABSIZE 4 for (i = 0; (c = curline[i]); i++) { if (c == '\t') { cnt = 8 - (i % TABSIZE); c = ' '; } else { cnt = 1; } while (cnt--) { (void) fputc(c, stderr); } } } static void printwhere(void) { int i; char c; int cnt; printbuf(); for (i = 0; i < where - curline; i++) { c = curline[i]; if (c == '\t') { cnt = 8 - (i % TABSIZE); } else { cnt = 1; } while (cnt--) { (void) fputc('^', stderr); } } (void) fputc('\n', stderr); } char * make_argname(const char *pname, const char *vname) { char *name; name = xmalloc(strlen(pname) + strlen(vname) + strlen(ARGEXT) + 3); sprintf(name, "%s_%s_%s", locase(pname), vname, ARGEXT); return (name); } bas_type *typ_list_h; bas_type *typ_list_t; void add_type(int len, const char *type) { bas_type *ptr; ptr = XALLOC(bas_type); ptr->name = type; ptr->length = len; ptr->next = NULL; if (typ_list_t == NULL) { typ_list_t = ptr; typ_list_h = ptr; } else { typ_list_t->next = ptr; typ_list_t = ptr; - }; + } } bas_type * find_type(const char *type) { bas_type * ptr; ptr = typ_list_h; while (ptr != NULL) { if (strcmp(ptr->name, type) == 0) return (ptr); else ptr = ptr->next; - }; + } return (NULL); } void * xmalloc(size_t size) { void *p; if ((p = malloc(size)) == NULL) { warnx("malloc failed"); crash(); } return (p); } void * xrealloc(void *ptr, size_t size) { void *p; if ((p = realloc(ptr, size)) == NULL) { warnx("realloc failed"); crash(); } return (p); } char * xstrdup(const char *str) { char *p; if ((p = strdup(str)) == NULL) { warnx("strdup failed"); crash(); } return (p); } Index: head/usr.bin/showmount/showmount.c =================================================================== --- head/usr.bin/showmount/showmount.c (revision 298088) +++ head/usr.bin/showmount/showmount.c (revision 298089) @@ -1,413 +1,413 @@ /* * Copyright (c) 1989, 1993, 1995 * 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1989, 1993, 1995\n\ The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)showmount.c 8.3 (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 /* Constant defs */ #define ALL 1 #define DIRS 2 #define DODUMP 0x1 #define DOEXPORTS 0x2 #define DOPARSABLEEXPORTS 0x4 struct mountlist { struct mountlist *ml_left; struct mountlist *ml_right; char ml_host[MNTNAMLEN+1]; char ml_dirp[MNTPATHLEN+1]; }; struct grouplist { struct grouplist *gr_next; char gr_name[MNTNAMLEN+1]; }; struct exportslist { struct exportslist *ex_next; struct grouplist *ex_groups; char ex_dirp[MNTPATHLEN+1]; }; static struct mountlist *mntdump; static struct exportslist *exportslist; static int type = 0; void print_dump(struct mountlist *); static void usage(void); int xdr_mntdump(XDR *, struct mountlist **); int xdr_exportslist(XDR *, struct exportslist **); int tcp_callrpc(const char *host, int prognum, int versnum, int procnum, xdrproc_t inproc, char *in, xdrproc_t outproc, char *out); /* * This command queries the NFS mount daemon for it's mount list and/or * it's exports list and prints them out. * See "NFS: Network File System Protocol Specification, RFC1094, Appendix A" * and the "Network File System Protocol XXX.." * for detailed information on the protocol. */ int main(int argc, char **argv) { char strvised[MNTPATHLEN * 4 + 1]; register struct exportslist *exp; register struct grouplist *grp; register int rpcs = 0, mntvers = 3; const char *host; int ch, estat, nbytes; while ((ch = getopt(argc, argv, "adEe13")) != -1) switch (ch) { case 'a': if (type == 0) { type = ALL; rpcs |= DODUMP; } else usage(); break; case 'd': if (type == 0) { type = DIRS; rpcs |= DODUMP; } else usage(); break; case 'E': rpcs |= DOPARSABLEEXPORTS; break; case 'e': rpcs |= DOEXPORTS; break; case '1': mntvers = 1; break; case '3': mntvers = 3; break; case '?': default: usage(); } argc -= optind; argv += optind; if ((rpcs & DOPARSABLEEXPORTS) != 0) { if ((rpcs & DOEXPORTS) != 0) errx(1, "-E cannot be used with -e"); if ((rpcs & DODUMP) != 0) errx(1, "-E cannot be used with -a or -d"); } if (argc > 0) host = *argv; else host = "localhost"; if (rpcs == 0) rpcs = DODUMP; if (rpcs & DODUMP) if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers, MOUNTPROC_DUMP, (xdrproc_t)xdr_void, (char *)0, (xdrproc_t)xdr_mntdump, (char *)&mntdump)) != 0) { clnt_perrno(estat); errx(1, "can't do mountdump rpc"); } if (rpcs & (DOEXPORTS | DOPARSABLEEXPORTS)) if ((estat = tcp_callrpc(host, MOUNTPROG, mntvers, MOUNTPROC_EXPORT, (xdrproc_t)xdr_void, (char *)0, (xdrproc_t)xdr_exportslist, (char *)&exportslist)) != 0) { clnt_perrno(estat); errx(1, "can't do exports rpc"); } /* Now just print out the results */ if (rpcs & DODUMP) { switch (type) { case ALL: printf("All mount points on %s:\n", host); break; case DIRS: printf("Directories on %s:\n", host); break; default: printf("Hosts on %s:\n", host); break; - }; + } print_dump(mntdump); } if (rpcs & DOEXPORTS) { printf("Exports list on %s:\n", host); exp = exportslist; while (exp) { printf("%-34s ", exp->ex_dirp); grp = exp->ex_groups; if (grp == NULL) { printf("Everyone\n"); } else { while (grp) { printf("%s ", grp->gr_name); grp = grp->gr_next; } printf("\n"); } exp = exp->ex_next; } } if (rpcs & DOPARSABLEEXPORTS) { exp = exportslist; while (exp) { nbytes = strsnvis(strvised, sizeof(strvised), exp->ex_dirp, VIS_GLOB | VIS_NL, "\"'$"); if (nbytes == -1) err(1, "strsnvis"); printf("%s\n", strvised); exp = exp->ex_next; } } exit(0); } /* * tcp_callrpc has the same interface as callrpc, but tries to * use tcp as transport method in order to handle large replies. */ int tcp_callrpc(const char *host, int prognum, int versnum, int procnum, xdrproc_t inproc, char *in, xdrproc_t outproc, char *out) { CLIENT *client; struct timeval timeout; int rval; if ((client = clnt_create(host, prognum, versnum, "tcp")) == NULL && (client = clnt_create(host, prognum, versnum, "udp")) == NULL) return ((int) rpc_createerr.cf_stat); timeout.tv_sec = 25; timeout.tv_usec = 0; rval = (int) clnt_call(client, procnum, inproc, in, outproc, out, timeout); clnt_destroy(client); return rval; } /* * Xdr routine for retrieving the mount dump list */ int xdr_mntdump(XDR *xdrsp, struct mountlist **mlp) { register struct mountlist *mp; register struct mountlist *tp; register struct mountlist **otp; int val, val2; int bool; char *strp; *mlp = (struct mountlist *)0; if (!xdr_bool(xdrsp, &bool)) return (0); while (bool) { mp = (struct mountlist *)malloc(sizeof(struct mountlist)); if (mp == NULL) return (0); mp->ml_left = mp->ml_right = (struct mountlist *)0; strp = mp->ml_host; if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) return (0); strp = mp->ml_dirp; if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) return (0); /* * Build a binary tree on sorted order of either host or dirp. * Drop any duplications. */ if (*mlp == NULL) { *mlp = mp; } else { tp = *mlp; while (tp) { val = strcmp(mp->ml_host, tp->ml_host); val2 = strcmp(mp->ml_dirp, tp->ml_dirp); switch (type) { case ALL: if (val == 0) { if (val2 == 0) { free((caddr_t)mp); goto next; } val = val2; } break; case DIRS: if (val2 == 0) { free((caddr_t)mp); goto next; } val = val2; break; default: if (val == 0) { free((caddr_t)mp); goto next; } break; - }; + } if (val < 0) { otp = &tp->ml_left; tp = tp->ml_left; } else { otp = &tp->ml_right; tp = tp->ml_right; } } *otp = mp; } next: if (!xdr_bool(xdrsp, &bool)) return (0); } return (1); } /* * Xdr routine to retrieve exports list */ int xdr_exportslist(XDR *xdrsp, struct exportslist **exp) { register struct exportslist *ep; register struct grouplist *gp; int bool, grpbool; char *strp; *exp = (struct exportslist *)0; if (!xdr_bool(xdrsp, &bool)) return (0); while (bool) { ep = (struct exportslist *)malloc(sizeof(struct exportslist)); if (ep == NULL) return (0); ep->ex_groups = (struct grouplist *)0; strp = ep->ex_dirp; if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) return (0); if (!xdr_bool(xdrsp, &grpbool)) return (0); while (grpbool) { gp = (struct grouplist *)malloc(sizeof(struct grouplist)); if (gp == NULL) return (0); strp = gp->gr_name; if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) return (0); gp->gr_next = ep->ex_groups; ep->ex_groups = gp; if (!xdr_bool(xdrsp, &grpbool)) return (0); } ep->ex_next = *exp; *exp = ep; if (!xdr_bool(xdrsp, &bool)) return (0); } return (1); } static void usage(void) { fprintf(stderr, "usage: showmount [-a | -d] [-e3] [host]\n"); exit(1); } /* * Print the binary tree in inorder so that output is sorted. */ void print_dump(struct mountlist *mp) { if (mp == NULL) return; if (mp->ml_left) print_dump(mp->ml_left); switch (type) { case ALL: printf("%s:%s\n", mp->ml_host, mp->ml_dirp); break; case DIRS: printf("%s\n", mp->ml_dirp); break; default: printf("%s\n", mp->ml_host); break; - }; + } if (mp->ml_right) print_dump(mp->ml_right); } Index: head/usr.bin/sort/bwstring.c =================================================================== --- head/usr.bin/sort/bwstring.c (revision 298088) +++ head/usr.bin/sort/bwstring.c (revision 298089) @@ -1,1149 +1,1149 @@ /*- * Copyright (C) 2009 Gabor Kovesdan * Copyright (C) 2012 Oleg Moskalenko * 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. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include "bwstring.h" #include "sort.h" bool byte_sort; static wchar_t **wmonths; static unsigned char **cmonths; /* initialise months */ void initialise_months(void) { const nl_item item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4, ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10, ABMON_11, ABMON_12 }; unsigned char *tmp; size_t len; if (MB_CUR_MAX == 1) { if (cmonths == NULL) { unsigned char *m; cmonths = sort_malloc(sizeof(unsigned char*) * 12); for (int i = 0; i < 12; i++) { cmonths[i] = NULL; tmp = (unsigned char *) nl_langinfo(item[i]); if (debug_sort) printf("month[%d]=%s\n", i, tmp); if (*tmp == '\0') continue; m = sort_strdup(tmp); len = strlen(tmp); for (unsigned int j = 0; j < len; j++) m[j] = toupper(m[j]); cmonths[i] = m; } } } else { if (wmonths == NULL) { wchar_t *m; wmonths = sort_malloc(sizeof(wchar_t *) * 12); for (int i = 0; i < 12; i++) { wmonths[i] = NULL; tmp = (unsigned char *) nl_langinfo(item[i]); if (debug_sort) printf("month[%d]=%s\n", i, tmp); if (*tmp == '\0') continue; len = strlen(tmp); m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1)); if (mbstowcs(m, (char*)tmp, len) == ((size_t) - 1)) { sort_free(m); continue; } m[len] = L'\0'; for (unsigned int j = 0; j < len; j++) m[j] = towupper(m[j]); wmonths[i] = m; } } } } /* * Compare two wide-character strings */ static int wide_str_coll(const wchar_t *s1, const wchar_t *s2) { int ret = 0; errno = 0; ret = wcscoll(s1, s2); if (errno == EILSEQ) { errno = 0; ret = wcscmp(s1, s2); if (errno != 0) { for (size_t i = 0; ; ++i) { wchar_t c1 = s1[i]; wchar_t c2 = s2[i]; if (c1 == L'\0') return ((c2 == L'\0') ? 0 : -1); if (c2 == L'\0') return (+1); if (c1 == c2) continue; return ((int)(c1 - c2)); } } } return (ret); } /* counterparts of wcs functions */ void bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix) { if (MB_CUR_MAX == 1) fprintf(f, "%s%s%s", prefix, bws->data.cstr, suffix); else fprintf(f, "%s%S%s", prefix, bws->data.wstr, suffix); } const void* bwsrawdata(const struct bwstring *bws) { return (&(bws->data)); } size_t bwsrawlen(const struct bwstring *bws) { return ((MB_CUR_MAX == 1) ? bws->len : SIZEOF_WCHAR_STRING(bws->len)); } size_t bws_memsize(const struct bwstring *bws) { return ((MB_CUR_MAX == 1) ? (bws->len + 2 + sizeof(struct bwstring)) : (SIZEOF_WCHAR_STRING(bws->len + 1) + sizeof(struct bwstring))); } void bws_setlen(struct bwstring *bws, size_t newlen) { if (bws && newlen != bws->len && newlen <= bws->len) { bws->len = newlen; if (MB_CUR_MAX == 1) bws->data.cstr[newlen] = '\0'; else bws->data.wstr[newlen] = L'\0'; } } /* * Allocate a new binary string of specified size */ struct bwstring * bwsalloc(size_t sz) { struct bwstring *ret; if (MB_CUR_MAX == 1) ret = sort_malloc(sizeof(struct bwstring) + 1 + sz); else ret = sort_malloc(sizeof(struct bwstring) + SIZEOF_WCHAR_STRING(sz + 1)); ret->len = sz; if (MB_CUR_MAX == 1) ret->data.cstr[ret->len] = '\0'; else ret->data.wstr[ret->len] = L'\0'; return (ret); } /* * Create a copy of binary string. * New string size equals the length of the old string. */ struct bwstring * bwsdup(const struct bwstring *s) { if (s == NULL) return (NULL); else { struct bwstring *ret = bwsalloc(s->len); if (MB_CUR_MAX == 1) memcpy(ret->data.cstr, s->data.cstr, (s->len)); else memcpy(ret->data.wstr, s->data.wstr, SIZEOF_WCHAR_STRING(s->len)); return (ret); } } /* * Create a new binary string from a wide character buffer. */ struct bwstring * bwssbdup(const wchar_t *str, size_t len) { if (str == NULL) return ((len == 0) ? bwsalloc(0) : NULL); else { struct bwstring *ret; ret = bwsalloc(len); if (MB_CUR_MAX == 1) for (size_t i = 0; i < len; ++i) ret->data.cstr[i] = (unsigned char) str[i]; else memcpy(ret->data.wstr, str, SIZEOF_WCHAR_STRING(len)); return (ret); } } /* * Create a new binary string from a raw binary buffer. */ struct bwstring * bwscsbdup(const unsigned char *str, size_t len) { struct bwstring *ret; ret = bwsalloc(len); if (str) { if (MB_CUR_MAX == 1) memcpy(ret->data.cstr, str, len); else { mbstate_t mbs; const char *s; size_t charlen, chars, cptr; charlen = chars = 0; cptr = 0; s = (const char *) str; memset(&mbs, 0, sizeof(mbs)); while (cptr < len) { size_t n = MB_CUR_MAX; if (n > len - cptr) n = len - cptr; charlen = mbrlen(s + cptr, n, &mbs); switch (charlen) { case 0: /* FALLTHROUGH */ case (size_t) -1: /* FALLTHROUGH */ case (size_t) -2: ret->data.wstr[chars++] = (unsigned char) s[cptr]; ++cptr; break; default: n = mbrtowc(ret->data.wstr + (chars++), s + cptr, charlen, &mbs); if ((n == (size_t)-1) || (n == (size_t)-2)) /* NOTREACHED */ err(2, "mbrtowc error"); cptr += charlen; - }; + } } ret->len = chars; ret->data.wstr[ret->len] = L'\0'; } } return (ret); } /* * De-allocate object memory */ void bwsfree(const struct bwstring *s) { if (s) sort_free(s); } /* * Copy content of src binary string to dst. * If the capacity of the dst string is not sufficient, * then the data is truncated. */ size_t bwscpy(struct bwstring *dst, const struct bwstring *src) { size_t nums = src->len; if (nums > dst->len) nums = dst->len; dst->len = nums; if (MB_CUR_MAX == 1) { memcpy(dst->data.cstr, src->data.cstr, nums); dst->data.cstr[dst->len] = '\0'; } else { memcpy(dst->data.wstr, src->data.wstr, SIZEOF_WCHAR_STRING(nums + 1)); dst->data.wstr[dst->len] = L'\0'; } return (nums); } /* * Copy content of src binary string to dst, * with specified number of symbols to be copied. * If the capacity of the dst string is not sufficient, * then the data is truncated. */ struct bwstring * bwsncpy(struct bwstring *dst, const struct bwstring *src, size_t size) { size_t nums = src->len; if (nums > dst->len) nums = dst->len; if (nums > size) nums = size; dst->len = nums; if (MB_CUR_MAX == 1) { memcpy(dst->data.cstr, src->data.cstr, nums); dst->data.cstr[dst->len] = '\0'; } else { memcpy(dst->data.wstr, src->data.wstr, SIZEOF_WCHAR_STRING(nums + 1)); dst->data.wstr[dst->len] = L'\0'; } return (dst); } /* * Copy content of src binary string to dst, * with specified number of symbols to be copied. * An offset value can be specified, from the start of src string. * If the capacity of the dst string is not sufficient, * then the data is truncated. */ struct bwstring * bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset, size_t size) { if (offset >= src->len) { dst->data.wstr[0] = 0; dst->len = 0; } else { size_t nums = src->len - offset; if (nums > dst->len) nums = dst->len; if (nums > size) nums = size; dst->len = nums; if (MB_CUR_MAX == 1) { memcpy(dst->data.cstr, src->data.cstr + offset, (nums)); dst->data.cstr[dst->len] = '\0'; } else { memcpy(dst->data.wstr, src->data.wstr + offset, SIZEOF_WCHAR_STRING(nums)); dst->data.wstr[dst->len] = L'\0'; } } return (dst); } /* * Write binary string to the file. * The output is ended either with '\n' (nl == true) * or '\0' (nl == false). */ size_t bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended) { if (MB_CUR_MAX == 1) { size_t len = bws->len; if (!zero_ended) { bws->data.cstr[len] = '\n'; if (fwrite(bws->data.cstr, len + 1, 1, f) < 1) err(2, NULL); bws->data.cstr[len] = '\0'; } else if (fwrite(bws->data.cstr, len + 1, 1, f) < 1) err(2, NULL); return (len + 1); } else { wchar_t eols; size_t printed = 0; eols = zero_ended ? btowc('\0') : btowc('\n'); while (printed < BWSLEN(bws)) { const wchar_t *s = bws->data.wstr + printed; if (*s == L'\0') { int nums; nums = fwprintf(f, L"%lc", *s); if (nums != 1) err(2, NULL); ++printed; } else { int nums; nums = fwprintf(f, L"%ls", s); if (nums < 1) err(2, NULL); printed += nums; } } fwprintf(f, L"%lc", eols); return (printed + 1); } } /* * Allocate and read a binary string from file. * The strings are nl-ended or zero-ended, depending on the sort setting. */ struct bwstring * bwsfgetln(FILE *f, size_t *len, bool zero_ended, struct reader_buffer *rb) { wint_t eols; eols = zero_ended ? btowc('\0') : btowc('\n'); if (!zero_ended && (MB_CUR_MAX > 1)) { wchar_t *ret; ret = fgetwln(f, len); if (ret == NULL) { if (!feof(f)) err(2, NULL); return (NULL); } if (*len > 0) { if (ret[*len - 1] == (wchar_t)eols) --(*len); } return (bwssbdup(ret, *len)); } else if (!zero_ended && (MB_CUR_MAX == 1)) { char *ret; ret = fgetln(f, len); if (ret == NULL) { if (!feof(f)) err(2, NULL); return (NULL); } if (*len > 0) { if (ret[*len - 1] == '\n') --(*len); } return (bwscsbdup((unsigned char*)ret, *len)); } else { *len = 0; if (feof(f)) return (NULL); if (2 >= rb->fgetwln_z_buffer_size) { rb->fgetwln_z_buffer_size += 256; rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, sizeof(wchar_t) * rb->fgetwln_z_buffer_size); } rb->fgetwln_z_buffer[*len] = 0; if (MB_CUR_MAX == 1) while (!feof(f)) { int c; c = fgetc(f); if (c == EOF) { if (*len == 0) return (NULL); goto line_read_done; } if (c == eols) goto line_read_done; if (*len + 1 >= rb->fgetwln_z_buffer_size) { rb->fgetwln_z_buffer_size += 256; rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); } rb->fgetwln_z_buffer[*len] = c; rb->fgetwln_z_buffer[++(*len)] = 0; } else while (!feof(f)) { wint_t c = 0; c = fgetwc(f); if (c == WEOF) { if (*len == 0) return (NULL); goto line_read_done; } if (c == eols) goto line_read_done; if (*len + 1 >= rb->fgetwln_z_buffer_size) { rb->fgetwln_z_buffer_size += 256; rb->fgetwln_z_buffer = sort_realloc(rb->fgetwln_z_buffer, SIZEOF_WCHAR_STRING(rb->fgetwln_z_buffer_size)); } rb->fgetwln_z_buffer[*len] = c; rb->fgetwln_z_buffer[++(*len)] = 0; } line_read_done: /* we do not count the last 0 */ return (bwssbdup(rb->fgetwln_z_buffer, *len)); } } int bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset, size_t len) { size_t cmp_len, len1, len2; int res = 0; cmp_len = 0; len1 = bws1->len; len2 = bws2->len; if (len1 <= offset) { return ((len2 <= offset) ? 0 : -1); } else { if (len2 <= offset) return (+1); else { len1 -= offset; len2 -= offset; cmp_len = len1; if (len2 < cmp_len) cmp_len = len2; if (len < cmp_len) cmp_len = len; if (MB_CUR_MAX == 1) { const unsigned char *s1, *s2; s1 = bws1->data.cstr + offset; s2 = bws2->data.cstr + offset; res = memcmp(s1, s2, cmp_len); } else { const wchar_t *s1, *s2; s1 = bws1->data.wstr + offset; s2 = bws2->data.wstr + offset; res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len)); } } } if (res == 0) { if (len1 < cmp_len && len1 < len2) res = -1; else if (len2 < cmp_len && len2 < len1) res = +1; } return (res); } int bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset) { size_t len1, len2, cmp_len; int res; len1 = bws1->len; len2 = bws2->len; len1 -= offset; len2 -= offset; cmp_len = len1; if (len2 < cmp_len) cmp_len = len2; res = bwsncmp(bws1, bws2, offset, cmp_len); if (res == 0) { if( len1 < len2) res = -1; else if (len2 < len1) res = +1; } return (res); } int bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len) { wchar_t c1, c2; size_t i = 0; for (i = 0; i < len; ++i) { c1 = bws_get_iter_value(iter1); c2 = bws_get_iter_value(iter2); if (c1 != c2) return (c1 - c2); iter1 = bws_iterator_inc(iter1, 1); iter2 = bws_iterator_inc(iter2, 1); } return (0); } int bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset) { size_t len1, len2; len1 = bws1->len; len2 = bws2->len; if (len1 <= offset) return ((len2 <= offset) ? 0 : -1); else { if (len2 <= offset) return (+1); else { len1 -= offset; len2 -= offset; if (MB_CUR_MAX == 1) { const unsigned char *s1, *s2; s1 = bws1->data.cstr + offset; s2 = bws2->data.cstr + offset; if (byte_sort) { int res = 0; if (len1 > len2) { res = memcmp(s1, s2, len2); if (!res) res = +1; } else if (len1 < len2) { res = memcmp(s1, s2, len1); if (!res) res = -1; } else res = memcmp(s1, s2, len1); return (res); } else { int res = 0; size_t i, maxlen; i = 0; maxlen = len1; if (maxlen > len2) maxlen = len2; while (i < maxlen) { /* goto next non-zero part: */ while ((i < maxlen) && !s1[i] && !s2[i]) ++i; if (i >= maxlen) break; if (s1[i] == 0) { if (s2[i] == 0) /* NOTREACHED */ err(2, "bwscoll error 01"); else return (-1); } else if (s2[i] == 0) return (+1); res = strcoll((const char*)(s1 + i), (const char*)(s2 + i)); if (res) return (res); while ((i < maxlen) && s1[i] && s2[i]) ++i; if (i >= maxlen) break; if (s1[i] == 0) { if (s2[i] == 0) { ++i; continue; } else return (-1); } else if (s2[i] == 0) return (+1); else /* NOTREACHED */ err(2, "bwscoll error 02"); } if (len1 < len2) return (-1); else if (len1 > len2) return (+1); return (0); } } else { const wchar_t *s1, *s2; size_t i, maxlen; int res = 0; s1 = bws1->data.wstr + offset; s2 = bws2->data.wstr + offset; i = 0; maxlen = len1; if (maxlen > len2) maxlen = len2; while (i < maxlen) { /* goto next non-zero part: */ while ((i < maxlen) && !s1[i] && !s2[i]) ++i; if (i >= maxlen) break; if (s1[i] == 0) { if (s2[i] == 0) /* NOTREACHED */ err(2, "bwscoll error 1"); else return (-1); } else if (s2[i] == 0) return (+1); res = wide_str_coll(s1 + i, s2 + i); if (res) return (res); while ((i < maxlen) && s1[i] && s2[i]) ++i; if (i >= maxlen) break; if (s1[i] == 0) { if (s2[i] == 0) { ++i; continue; } else return (-1); } else if (s2[i] == 0) return (+1); else /* NOTREACHED */ err(2, "bwscoll error 2"); } if (len1 < len2) return (-1); else if (len1 > len2) return (+1); return (0); } } } } /* * Correction of the system API */ double bwstod(struct bwstring *s0, bool *empty) { double ret = 0; if (MB_CUR_MAX == 1) { unsigned char *end, *s; char *ep; s = s0->data.cstr; end = s + s0->len; ep = NULL; while (isblank(*s) && s < end) ++s; if (!isprint(*s)) { *empty = true; return (0); } ret = strtod((char*)s, &ep); if ((unsigned char*) ep == s) { *empty = true; return (0); } } else { wchar_t *end, *ep, *s; s = s0->data.wstr; end = s + s0->len; ep = NULL; while (iswblank(*s) && s < end) ++s; if (!iswprint(*s)) { *empty = true; return (0); } ret = wcstod(s, &ep); if (ep == s) { *empty = true; return (0); } } *empty = false; return (ret); } /* * A helper function for monthcoll. If a line matches * a month name, it returns (number of the month - 1), * while if there is no match, it just return -1. */ int bws_month_score(const struct bwstring *s0) { if (MB_CUR_MAX == 1) { const unsigned char *end, *s; size_t len; s = s0->data.cstr; end = s + s0->len; while (isblank(*s) && s < end) ++s; len = strlen((const char*)s); for (int i = 11; i >= 0; --i) { if (cmonths[i] && (s == (unsigned char*)strstr((const char*)s, (char*)(cmonths[i])))) return (i); } } else { const wchar_t *end, *s; size_t len; s = s0->data.wstr; end = s + s0->len; while (iswblank(*s) && s < end) ++s; len = wcslen(s); for (int i = 11; i >= 0; --i) { if (wmonths[i] && (s == wcsstr(s, wmonths[i]))) return (i); } } return (-1); } /* * Rips out leading blanks (-b). */ struct bwstring * ignore_leading_blanks(struct bwstring *str) { if (MB_CUR_MAX == 1) { unsigned char *dst, *end, *src; src = str->data.cstr; dst = src; end = src + str->len; while (src < end && isblank(*src)) ++src; if (src != dst) { size_t newlen; newlen = BWSLEN(str) - (src - dst); while (src < end) { *dst = *src; ++dst; ++src; } bws_setlen(str, newlen); } } else { wchar_t *dst, *end, *src; src = str->data.wstr; dst = src; end = src + str->len; while (src < end && iswblank(*src)) ++src; if (src != dst) { size_t newlen = BWSLEN(str) - (src - dst); while (src < end) { *dst = *src; ++dst; ++src; } bws_setlen(str, newlen); } } return (str); } /* * Rips out nonprinting characters (-i). */ struct bwstring * ignore_nonprinting(struct bwstring *str) { size_t newlen = str->len; if (MB_CUR_MAX == 1) { unsigned char *dst, *end, *src; unsigned char c; src = str->data.cstr; dst = src; end = src + str->len; while (src < end) { c = *src; if (isprint(c)) { *dst = c; ++dst; ++src; } else { ++src; --newlen; } } } else { wchar_t *dst, *end, *src; wchar_t c; src = str->data.wstr; dst = src; end = src + str->len; while (src < end) { c = *src; if (iswprint(c)) { *dst = c; ++dst; ++src; } else { ++src; --newlen; } } } bws_setlen(str, newlen); return (str); } /* * Rips out any characters that are not alphanumeric characters * nor blanks (-d). */ struct bwstring * dictionary_order(struct bwstring *str) { size_t newlen = str->len; if (MB_CUR_MAX == 1) { unsigned char *dst, *end, *src; unsigned char c; src = str->data.cstr; dst = src; end = src + str->len; while (src < end) { c = *src; if (isalnum(c) || isblank(c)) { *dst = c; ++dst; ++src; } else { ++src; --newlen; } } } else { wchar_t *dst, *end, *src; wchar_t c; src = str->data.wstr; dst = src; end = src + str->len; while (src < end) { c = *src; if (iswalnum(c) || iswblank(c)) { *dst = c; ++dst; ++src; } else { ++src; --newlen; } } } bws_setlen(str, newlen); return (str); } /* * Converts string to lower case(-f). */ struct bwstring * ignore_case(struct bwstring *str) { if (MB_CUR_MAX == 1) { unsigned char *end, *s; s = str->data.cstr; end = s + str->len; while (s < end) { *s = toupper(*s); ++s; } } else { wchar_t *end, *s; s = str->data.wstr; end = s + str->len; while (s < end) { *s = towupper(*s); ++s; } } return (str); } void bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos) { if (MB_CUR_MAX == 1) warnx("%s:%zu: disorder: %s", fn, pos + 1, s->data.cstr); else warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->data.wstr); } Index: head/usr.bin/sort/coll.c =================================================================== --- head/usr.bin/sort/coll.c (revision 298088) +++ head/usr.bin/sort/coll.c (revision 298089) @@ -1,1302 +1,1302 @@ /*- * Copyright (C) 2009 Gabor Kovesdan * Copyright (C) 2012 Oleg Moskalenko * 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. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #include #include #include #include #include #include "coll.h" #include "vsort.h" struct key_specs *keys; size_t keys_num = 0; wint_t symbol_decimal_point = L'.'; /* there is no default thousands separator in collate rules: */ wint_t symbol_thousands_sep = 0; wint_t symbol_negative_sign = L'-'; wint_t symbol_positive_sign = L'+'; static int wstrcoll(struct key_value *kv1, struct key_value *kv2, size_t offset); static int gnumcoll(struct key_value*, struct key_value *, size_t offset); static int monthcoll(struct key_value*, struct key_value *, size_t offset); static int numcoll(struct key_value*, struct key_value *, size_t offset); static int hnumcoll(struct key_value*, struct key_value *, size_t offset); static int randomcoll(struct key_value*, struct key_value *, size_t offset); static int versioncoll(struct key_value*, struct key_value *, size_t offset); /* * Allocate keys array */ struct keys_array * keys_array_alloc(void) { struct keys_array *ka; size_t sz; sz = keys_array_size(); ka = sort_malloc(sz); memset(ka, 0, sz); return (ka); } /* * Calculate whether we need key hint space */ static size_t key_hint_size(void) { return (need_hint ? sizeof(struct key_hint) : 0); } /* * Calculate keys array size */ size_t keys_array_size(void) { return (keys_num * (sizeof(struct key_value) + key_hint_size())); } /* * Clean data of keys array */ void clean_keys_array(const struct bwstring *s, struct keys_array *ka) { if (ka) { for (size_t i = 0; i < keys_num; ++i) if (ka->key[i].k && ka->key[i].k != s) bwsfree(ka->key[i].k); memset(ka, 0, keys_array_size()); } } /* * Set value of a key in the keys set */ void set_key_on_keys_array(struct keys_array *ka, struct bwstring *s, size_t ind) { if (ka && keys_num > ind) { struct key_value *kv; kv = &(ka->key[ind]); if (kv->k && kv->k != s) bwsfree(kv->k); kv->k = s; } } /* * Initialize a sort list item */ struct sort_list_item * sort_list_item_alloc(void) { struct sort_list_item *si; size_t sz; sz = sizeof(struct sort_list_item) + keys_array_size(); si = sort_malloc(sz); memset(si, 0, sz); return (si); } size_t sort_list_item_size(struct sort_list_item *si) { size_t ret = 0; if (si) { ret = sizeof(struct sort_list_item) + keys_array_size(); if (si->str) ret += bws_memsize(si->str); for (size_t i = 0; i < keys_num; ++i) { struct key_value *kv; kv = &(si->ka.key[i]); if (kv->k != si->str) ret += bws_memsize(kv->k); } } return (ret); } /* * Calculate key for a sort list item */ static void sort_list_item_make_key(struct sort_list_item *si) { preproc(si->str, &(si->ka)); } /* * Set value of a sort list item. * Return combined string and keys memory size. */ void sort_list_item_set(struct sort_list_item *si, struct bwstring *str) { if (si) { clean_keys_array(si->str, &(si->ka)); if (si->str) { if (si->str == str) { /* we are trying to reset the same string */ return; } else { bwsfree(si->str); si->str = NULL; } } si->str = str; sort_list_item_make_key(si); } } /* * De-allocate a sort list item object memory */ void sort_list_item_clean(struct sort_list_item *si) { if (si) { clean_keys_array(si->str, &(si->ka)); if (si->str) { bwsfree(si->str); si->str = NULL; } } } /* * Skip columns according to specs */ static size_t skip_cols_to_start(const struct bwstring *s, size_t cols, size_t start, bool skip_blanks, bool *empty_key) { if (cols < 1) return (BWSLEN(s) + 1); if (skip_blanks) while (start < BWSLEN(s) && iswblank(BWS_GET(s,start))) ++start; while (start < BWSLEN(s) && cols > 1) { --cols; ++start; } if (start >= BWSLEN(s)) *empty_key = true; return (start); } /* * Skip fields according to specs */ static size_t skip_fields_to_start(const struct bwstring *s, size_t fields, bool *empty_field) { if (fields < 2) { if (BWSLEN(s) == 0) *empty_field = true; return (0); } else if (!(sort_opts_vals.tflag)) { size_t cpos = 0; bool pb = true; while (cpos < BWSLEN(s)) { bool isblank; isblank = iswblank(BWS_GET(s, cpos)); if (isblank && !pb) { --fields; if (fields <= 1) return (cpos); } pb = isblank; ++cpos; } if (fields > 1) *empty_field = true; return (cpos); } else { size_t cpos = 0; while (cpos < BWSLEN(s)) { if (BWS_GET(s,cpos) == (wchar_t)sort_opts_vals.field_sep) { --fields; if (fields <= 1) return (cpos + 1); } ++cpos; } if (fields > 1) *empty_field = true; return (cpos); } } /* * Find fields start */ static void find_field_start(const struct bwstring *s, struct key_specs *ks, size_t *field_start, size_t *key_start, bool *empty_field, bool *empty_key) { *field_start = skip_fields_to_start(s, ks->f1, empty_field); if (!*empty_field) *key_start = skip_cols_to_start(s, ks->c1, *field_start, ks->pos1b, empty_key); else *empty_key = true; } /* * Find end key position */ static size_t find_field_end(const struct bwstring *s, struct key_specs *ks) { size_t f2, next_field_start, pos_end; bool empty_field, empty_key; pos_end = 0; next_field_start = 0; empty_field = false; empty_key = false; f2 = ks->f2; if (f2 == 0) return (BWSLEN(s) + 1); else { if (ks->c2 == 0) { next_field_start = skip_fields_to_start(s, f2 + 1, &empty_field); if ((next_field_start > 0) && sort_opts_vals.tflag && ((wchar_t)sort_opts_vals.field_sep == BWS_GET(s, next_field_start - 1))) --next_field_start; } else next_field_start = skip_fields_to_start(s, f2, &empty_field); } if (empty_field || (next_field_start >= BWSLEN(s))) return (BWSLEN(s) + 1); if (ks->c2) { pos_end = skip_cols_to_start(s, ks->c2, next_field_start, ks->pos2b, &empty_key); if (pos_end < BWSLEN(s)) ++pos_end; } else pos_end = next_field_start; return (pos_end); } /* * Cut a field according to the key specs */ static struct bwstring * cut_field(const struct bwstring *s, struct key_specs *ks) { struct bwstring *ret = NULL; if (s && ks) { size_t field_start, key_end, key_start, sz; bool empty_field, empty_key; field_start = 0; key_start = 0; empty_field = false; empty_key = false; find_field_start(s, ks, &field_start, &key_start, &empty_field, &empty_key); if (empty_key) sz = 0; else { key_end = find_field_end(s, ks); sz = (key_end < key_start) ? 0 : (key_end - key_start); } ret = bwsalloc(sz); if (sz) bwsnocpy(ret, s, key_start, sz); } else ret = bwsalloc(0); return (ret); } /* * Preprocesses a line applying the necessary transformations * specified by command line options and returns the preprocessed * string, which can be used to compare. */ int preproc(struct bwstring *s, struct keys_array *ka) { if (sort_opts_vals.kflag) for (size_t i = 0; i < keys_num; i++) { struct bwstring *key; struct key_specs *kspecs; struct sort_mods *sm; kspecs = &(keys[i]); key = cut_field(s, kspecs); sm = &(kspecs->sm); if (sm->dflag) key = dictionary_order(key); else if (sm->iflag) key = ignore_nonprinting(key); if (sm->fflag || sm->Mflag) key = ignore_case(key); set_key_on_keys_array(ka, key, i); } else { struct bwstring *ret = NULL; struct sort_mods *sm = default_sort_mods; if (sm->bflag) { if (ret == NULL) ret = bwsdup(s); ret = ignore_leading_blanks(ret); } if (sm->dflag) { if (ret == NULL) ret = bwsdup(s); ret = dictionary_order(ret); } else if (sm->iflag) { if (ret == NULL) ret = bwsdup(s); ret = ignore_nonprinting(ret); } if (sm->fflag || sm->Mflag) { if (ret == NULL) ret = bwsdup(s); ret = ignore_case(ret); } if (ret == NULL) set_key_on_keys_array(ka, s, 0); else set_key_on_keys_array(ka, ret, 0); } return 0; } cmpcoll_t get_sort_func(struct sort_mods *sm) { if (sm->nflag) return (numcoll); else if (sm->hflag) return (hnumcoll); else if (sm->gflag) return (gnumcoll); else if (sm->Mflag) return (monthcoll); else if (sm->Rflag) return (randomcoll); else if (sm->Vflag) return (versioncoll); else return (wstrcoll); } /* * Compares the given strings. Returns a positive number if * the first precedes the second, a negative number if the second is * the preceding one, and zero if they are equal. This function calls * the underlying collate functions, which done the actual comparison. */ int key_coll(struct keys_array *ps1, struct keys_array *ps2, size_t offset) { struct sort_mods *sm; int res = 0; for (size_t i = 0; i < keys_num; ++i) { sm = &(keys[i].sm); if (sm->rflag) res = sm->func(&(ps2->key[i]), &(ps1->key[i]), offset); else res = sm->func(&(ps1->key[i]), &(ps2->key[i]), offset); if (res) break; /* offset applies to only the first key */ offset = 0; } return (res); } /* * Compare two strings. * Plain symbol-by-symbol comparison. */ int top_level_str_coll(const struct bwstring *s1, const struct bwstring *s2) { if (default_sort_mods->rflag) { const struct bwstring *tmp; tmp = s1; s1 = s2; s2 = tmp; } return (bwscoll(s1, s2, 0)); } /* * Compare a string and a sort list item, according to the sort specs. */ int str_list_coll(struct bwstring *str1, struct sort_list_item **ss2) { struct keys_array *ka1; int ret = 0; ka1 = keys_array_alloc(); preproc(str1, ka1); sort_list_item_make_key(*ss2); if (debug_sort) { bwsprintf(stdout, str1, "; s1=<", ">"); bwsprintf(stdout, (*ss2)->str, ", s2=<", ">"); } ret = key_coll(ka1, &((*ss2)->ka), 0); if (debug_sort) printf("; cmp1=%d", ret); clean_keys_array(str1, ka1); sort_free(ka1); if ((ret == 0) && !(sort_opts_vals.sflag) && sort_opts_vals.complex_sort) { ret = top_level_str_coll(str1, ((*ss2)->str)); if (debug_sort) printf("; cmp2=%d", ret); } if (debug_sort) printf("\n"); return (ret); } /* * Compare two sort list items, according to the sort specs. */ int list_coll_offset(struct sort_list_item **ss1, struct sort_list_item **ss2, size_t offset) { int ret; ret = key_coll(&((*ss1)->ka), &((*ss2)->ka), offset); if (debug_sort) { if (offset) printf("; offset=%d", (int) offset); bwsprintf(stdout, ((*ss1)->str), "; s1=<", ">"); bwsprintf(stdout, ((*ss2)->str), ", s2=<", ">"); printf("; cmp1=%d\n", ret); } if (ret) return (ret); if (!(sort_opts_vals.sflag) && sort_opts_vals.complex_sort) { ret = top_level_str_coll(((*ss1)->str), ((*ss2)->str)); if (debug_sort) printf("; cmp2=%d\n", ret); } return (ret); } /* * Compare two sort list items, according to the sort specs. */ int list_coll(struct sort_list_item **ss1, struct sort_list_item **ss2) { return (list_coll_offset(ss1, ss2, 0)); } #define LSCDEF(N) \ static int \ list_coll_##N(struct sort_list_item **ss1, struct sort_list_item **ss2) \ { \ \ return (list_coll_offset(ss1, ss2, N)); \ } LSCDEF(1) LSCDEF(2) LSCDEF(3) LSCDEF(4) LSCDEF(5) LSCDEF(6) LSCDEF(7) LSCDEF(8) LSCDEF(9) LSCDEF(10) LSCDEF(11) LSCDEF(12) LSCDEF(13) LSCDEF(14) LSCDEF(15) LSCDEF(16) LSCDEF(17) LSCDEF(18) LSCDEF(19) LSCDEF(20) listcoll_t get_list_call_func(size_t offset) { static const listcoll_t lsarray[] = { list_coll, list_coll_1, list_coll_2, list_coll_3, list_coll_4, list_coll_5, list_coll_6, list_coll_7, list_coll_8, list_coll_9, list_coll_10, list_coll_11, list_coll_12, list_coll_13, list_coll_14, list_coll_15, list_coll_16, list_coll_17, list_coll_18, list_coll_19, list_coll_20 }; if (offset <= 20) return (lsarray[offset]); return (list_coll); } /* * Compare two sort list items, only by their original string. */ int list_coll_by_str_only(struct sort_list_item **ss1, struct sort_list_item **ss2) { return (top_level_str_coll(((*ss1)->str), ((*ss2)->str))); } /* * Maximum size of a number in the string (before or after decimal point) */ #define MAX_NUM_SIZE (128) /* * Set suffix value */ static void setsuffix(wchar_t c, unsigned char *si) { switch (c){ case L'k': case L'K': *si = 1; break; case L'M': *si = 2; break; case L'G': *si = 3; break; case L'T': *si = 4; break; case L'P': *si = 5; break; case L'E': *si = 6; break; case L'Z': *si = 7; break; case L'Y': *si = 8; break; default: *si = 0; - }; + } } /* * Read string s and parse the string into a fixed-decimal-point number. * sign equals -1 if the number is negative (explicit plus is not allowed, * according to GNU sort's "info sort". * The number part before decimal point is in the smain, after the decimal * point is in sfrac, tail is the pointer to the remainder of the string. */ static int read_number(struct bwstring *s0, int *sign, wchar_t *smain, size_t *main_len, wchar_t *sfrac, size_t *frac_len, unsigned char *si) { bwstring_iterator s; s = bws_begin(s0); /* always end the fraction with zero, even if we have no fraction */ sfrac[0] = 0; while (iswblank(bws_get_iter_value(s))) s = bws_iterator_inc(s, 1); if (bws_get_iter_value(s) == (wchar_t)symbol_negative_sign) { *sign = -1; s = bws_iterator_inc(s, 1); } // This is '0', not '\0', do not change this while (iswdigit(bws_get_iter_value(s)) && (bws_get_iter_value(s) == L'0')) s = bws_iterator_inc(s, 1); while (bws_get_iter_value(s) && *main_len < MAX_NUM_SIZE) { if (iswdigit(bws_get_iter_value(s))) { smain[*main_len] = bws_get_iter_value(s); s = bws_iterator_inc(s, 1); *main_len += 1; } else if (symbol_thousands_sep && (bws_get_iter_value(s) == (wchar_t)symbol_thousands_sep)) s = bws_iterator_inc(s, 1); else break; } smain[*main_len] = 0; if (bws_get_iter_value(s) == (wchar_t)symbol_decimal_point) { s = bws_iterator_inc(s, 1); while (iswdigit(bws_get_iter_value(s)) && *frac_len < MAX_NUM_SIZE) { sfrac[*frac_len] = bws_get_iter_value(s); s = bws_iterator_inc(s, 1); *frac_len += 1; } sfrac[*frac_len] = 0; while (*frac_len > 0 && sfrac[*frac_len - 1] == L'0') { --(*frac_len); sfrac[*frac_len] = L'\0'; } } setsuffix(bws_get_iter_value(s),si); if ((*main_len + *frac_len) == 0) *sign = 0; return (0); } /* * Implements string sort. */ static int wstrcoll(struct key_value *kv1, struct key_value *kv2, size_t offset) { if (debug_sort) { if (offset) printf("; offset=%d\n", (int) offset); bwsprintf(stdout, kv1->k, "; k1=<", ">"); printf("(%zu)", BWSLEN(kv1->k)); bwsprintf(stdout, kv2->k, ", k2=<", ">"); printf("(%zu)", BWSLEN(kv2->k)); } return (bwscoll(kv1->k, kv2->k, offset)); } /* * Compare two suffixes */ static inline int cmpsuffix(unsigned char si1, unsigned char si2) { return ((char)si1 - (char)si2); } /* * Implements numeric sort for -n and -h. */ static int numcoll_impl(struct key_value *kv1, struct key_value *kv2, size_t offset __unused, bool use_suffix) { struct bwstring *s1, *s2; wchar_t sfrac1[MAX_NUM_SIZE + 1], sfrac2[MAX_NUM_SIZE + 1]; wchar_t smain1[MAX_NUM_SIZE + 1], smain2[MAX_NUM_SIZE + 1]; int cmp_res, sign1, sign2; size_t frac1, frac2, main1, main2; unsigned char SI1, SI2; bool e1, e2, key1_read, key2_read; s1 = kv1->k; s2 = kv2->k; sign1 = sign2 = 0; main1 = main2 = 0; frac1 = frac2 = 0; cmp_res = 0; key1_read = key2_read = false; if (debug_sort) { bwsprintf(stdout, s1, "; k1=<", ">"); bwsprintf(stdout, s2, ", k2=<", ">"); } if (s1 == s2) return (0); if (kv1->hint->status == HS_UNINITIALIZED) { /* read the number from the string */ read_number(s1, &sign1, smain1, &main1, sfrac1, &frac1, &SI1); key1_read = true; kv1->hint->v.nh.n1 = wcstoull(smain1, NULL, 10); if(main1 < 1 && frac1 < 1) kv1->hint->v.nh.empty=true; kv1->hint->v.nh.si = SI1; kv1->hint->status = (kv1->hint->v.nh.n1 != ULLONG_MAX) ? HS_INITIALIZED : HS_ERROR; kv1->hint->v.nh.neg = (sign1 < 0) ? true : false; } if (kv2->hint->status == HS_UNINITIALIZED) { /* read the number from the string */ read_number(s2, &sign2, smain2, &main2, sfrac2, &frac2,&SI2); key2_read = true; kv2->hint->v.nh.n1 = wcstoull(smain2, NULL, 10); if(main2 < 1 && frac2 < 1) kv2->hint->v.nh.empty=true; kv2->hint->v.nh.si = SI2; kv2->hint->status = (kv2->hint->v.nh.n1 != ULLONG_MAX) ? HS_INITIALIZED : HS_ERROR; kv2->hint->v.nh.neg = (sign2 < 0) ? true : false; } if (kv1->hint->status == HS_INITIALIZED && kv2->hint->status == HS_INITIALIZED) { unsigned long long n1, n2; bool neg1, neg2; e1 = kv1->hint->v.nh.empty; e2 = kv2->hint->v.nh.empty; if (e1 && e2) return (0); neg1 = kv1->hint->v.nh.neg; neg2 = kv2->hint->v.nh.neg; if (neg1 && !neg2) return (-1); if (neg2 && !neg1) return (+1); if (e1) return (neg2 ? +1 : -1); else if (e2) return (neg1 ? -1 : +1); if (use_suffix) { cmp_res = cmpsuffix(kv1->hint->v.nh.si, kv2->hint->v.nh.si); if (cmp_res) return (neg1 ? -cmp_res : cmp_res); } n1 = kv1->hint->v.nh.n1; n2 = kv2->hint->v.nh.n1; if (n1 < n2) return (neg1 ? +1 : -1); else if (n1 > n2) return (neg1 ? -1 : +1); } /* read the numbers from the strings */ if (!key1_read) read_number(s1, &sign1, smain1, &main1, sfrac1, &frac1, &SI1); if (!key2_read) read_number(s2, &sign2, smain2, &main2, sfrac2, &frac2, &SI2); e1 = ((main1 + frac1) == 0); e2 = ((main2 + frac2) == 0); if (e1 && e2) return (0); /* we know the result if the signs are different */ if (sign1 < 0 && sign2 >= 0) return (-1); if (sign1 >= 0 && sign2 < 0) return (+1); if (e1) return ((sign2 < 0) ? +1 : -1); else if (e2) return ((sign1 < 0) ? -1 : +1); if (use_suffix) { cmp_res = cmpsuffix(SI1, SI2); if (cmp_res) return ((sign1 < 0) ? -cmp_res : cmp_res); } /* if both numbers are empty assume that the strings are equal */ if (main1 < 1 && main2 < 1 && frac1 < 1 && frac2 < 1) return (0); /* * if the main part is of different size, we know the result * (because the leading zeros are removed) */ if (main1 < main2) cmp_res = -1; else if (main1 > main2) cmp_res = +1; /* if the sizes are equal then simple non-collate string compare gives the correct result */ else cmp_res = wcscmp(smain1, smain2); /* check fraction */ if (!cmp_res) cmp_res = wcscmp(sfrac1, sfrac2); if (!cmp_res) return (0); /* reverse result if the signs are negative */ if (sign1 < 0 && sign2 < 0) cmp_res = -cmp_res; return (cmp_res); } /* * Implements numeric sort (-n). */ static int numcoll(struct key_value *kv1, struct key_value *kv2, size_t offset) { return (numcoll_impl(kv1, kv2, offset, false)); } /* * Implements 'human' numeric sort (-h). */ static int hnumcoll(struct key_value *kv1, struct key_value *kv2, size_t offset) { return (numcoll_impl(kv1, kv2, offset, true)); } /* * Implements random sort (-R). */ static int randomcoll(struct key_value *kv1, struct key_value *kv2, size_t offset __unused) { struct bwstring *s1, *s2; MD5_CTX ctx1, ctx2; char *b1, *b2; s1 = kv1->k; s2 = kv2->k; if (debug_sort) { bwsprintf(stdout, s1, "; k1=<", ">"); bwsprintf(stdout, s2, ", k2=<", ">"); } if (s1 == s2) return (0); memcpy(&ctx1,&md5_ctx,sizeof(MD5_CTX)); memcpy(&ctx2,&md5_ctx,sizeof(MD5_CTX)); MD5Update(&ctx1, bwsrawdata(s1), bwsrawlen(s1)); MD5Update(&ctx2, bwsrawdata(s2), bwsrawlen(s2)); b1 = MD5End(&ctx1, NULL); b2 = MD5End(&ctx2, NULL); if (b1 == NULL) { if (b2 == NULL) return (0); else { sort_free(b2); return (-1); } } else if (b2 == NULL) { sort_free(b1); return (+1); } else { int cmp_res; cmp_res = strcmp(b1,b2); sort_free(b1); sort_free(b2); if (!cmp_res) cmp_res = bwscoll(s1, s2, 0); return (cmp_res); } } /* * Implements version sort (-V). */ static int versioncoll(struct key_value *kv1, struct key_value *kv2, size_t offset __unused) { struct bwstring *s1, *s2; s1 = kv1->k; s2 = kv2->k; if (debug_sort) { bwsprintf(stdout, s1, "; k1=<", ">"); bwsprintf(stdout, s2, ", k2=<", ">"); } if (s1 == s2) return (0); return (vcmp(s1, s2)); } /* * Check for minus infinity */ static inline bool huge_minus(double d, int err1) { if (err1 == ERANGE) if (d == -HUGE_VAL || d == -HUGE_VALF || d == -HUGE_VALL) return (+1); return (0); } /* * Check for plus infinity */ static inline bool huge_plus(double d, int err1) { if (err1 == ERANGE) if (d == HUGE_VAL || d == HUGE_VALF || d == HUGE_VALL) return (+1); return (0); } /* * Check whether a function is a NAN */ static bool is_nan(double d) { return ((d == NAN) || (isnan(d))); } /* * Compare two NANs */ static int cmp_nans(double d1, double d2) { if (d1 < d2) return (-1); if (d2 > d2) return (+1); return (0); } /* * Implements general numeric sort (-g). */ static int gnumcoll(struct key_value *kv1, struct key_value *kv2, size_t offset __unused) { double d1, d2; int err1, err2; bool empty1, empty2, key1_read, key2_read; d1 = d2 = 0; err1 = err2 = 0; key1_read = key2_read = false; if (debug_sort) { bwsprintf(stdout, kv1->k, "; k1=<", ">"); bwsprintf(stdout, kv2->k, "; k2=<", ">"); } if (kv1->hint->status == HS_UNINITIALIZED) { errno = 0; d1 = bwstod(kv1->k, &empty1); err1 = errno; if (empty1) kv1->hint->v.gh.notnum = true; else if (err1 == 0) { kv1->hint->v.gh.d = d1; kv1->hint->v.gh.nan = is_nan(d1); kv1->hint->status = HS_INITIALIZED; } else kv1->hint->status = HS_ERROR; key1_read = true; } if (kv2->hint->status == HS_UNINITIALIZED) { errno = 0; d2 = bwstod(kv2->k, &empty2); err2 = errno; if (empty2) kv2->hint->v.gh.notnum = true; else if (err2 == 0) { kv2->hint->v.gh.d = d2; kv2->hint->v.gh.nan = is_nan(d2); kv2->hint->status = HS_INITIALIZED; } else kv2->hint->status = HS_ERROR; key2_read = true; } if (kv1->hint->status == HS_INITIALIZED && kv2->hint->status == HS_INITIALIZED) { if (kv1->hint->v.gh.notnum) return ((kv2->hint->v.gh.notnum) ? 0 : -1); else if (kv2->hint->v.gh.notnum) return (+1); if (kv1->hint->v.gh.nan) return ((kv2->hint->v.gh.nan) ? cmp_nans(kv1->hint->v.gh.d, kv2->hint->v.gh.d) : -1); else if (kv2->hint->v.gh.nan) return (+1); d1 = kv1->hint->v.gh.d; d2 = kv2->hint->v.gh.d; if (d1 < d2) return (-1); else if (d1 > d2) return (+1); else return (0); } if (!key1_read) { errno = 0; d1 = bwstod(kv1->k, &empty1); err1 = errno; } if (!key2_read) { errno = 0; d2 = bwstod(kv2->k, &empty2); err2 = errno; } /* Non-value case: */ if (empty1) return (empty2 ? 0 : -1); else if (empty2) return (+1); /* NAN case */ if (is_nan(d1)) return (is_nan(d2) ? cmp_nans(d1, d2) : -1); else if (is_nan(d2)) return (+1); /* Infinities */ if (err1 == ERANGE || err2 == ERANGE) { /* Minus infinity case */ if (huge_minus(d1, err1)) { if (huge_minus(d2, err2)) { if (d1 < d2) return (-1); if (d1 > d2) return (+1); return (0); } else return (-1); } else if (huge_minus(d2, err2)) { if (huge_minus(d1, err1)) { if (d1 < d2) return (-1); if (d1 > d2) return (+1); return (0); } else return (+1); } /* Plus infinity case */ if (huge_plus(d1, err1)) { if (huge_plus(d2, err2)) { if (d1 < d2) return (-1); if (d1 > d2) return (+1); return (0); } else return (+1); } else if (huge_plus(d2, err2)) { if (huge_plus(d1, err1)) { if (d1 < d2) return (-1); if (d1 > d2) return (+1); return (0); } else return (-1); } } if (d1 < d2) return (-1); if (d1 > d2) return (+1); return (0); } /* * Implements month sort (-M). */ static int monthcoll(struct key_value *kv1, struct key_value *kv2, size_t offset __unused) { int val1, val2; bool key1_read, key2_read; val1 = val2 = 0; key1_read = key2_read = false; if (debug_sort) { bwsprintf(stdout, kv1->k, "; k1=<", ">"); bwsprintf(stdout, kv2->k, "; k2=<", ">"); } if (kv1->hint->status == HS_UNINITIALIZED) { kv1->hint->v.Mh.m = bws_month_score(kv1->k); key1_read = true; kv1->hint->status = HS_INITIALIZED; } if (kv2->hint->status == HS_UNINITIALIZED) { kv2->hint->v.Mh.m = bws_month_score(kv2->k); key2_read = true; kv2->hint->status = HS_INITIALIZED; } if (kv1->hint->status == HS_INITIALIZED) { val1 = kv1->hint->v.Mh.m; key1_read = true; } if (kv2->hint->status == HS_INITIALIZED) { val2 = kv2->hint->v.Mh.m; key2_read = true; } if (!key1_read) val1 = bws_month_score(kv1->k); if (!key2_read) val2 = bws_month_score(kv2->k); if (val1 == val2) { return (0); } if (val1 < val2) return (-1); return (+1); } Index: head/usr.bin/sort/file.c =================================================================== --- head/usr.bin/sort/file.c (revision 298088) +++ head/usr.bin/sort/file.c (revision 298089) @@ -1,1597 +1,1597 @@ /*- * Copyright (C) 2009 Gabor Kovesdan * Copyright (C) 2012 Oleg Moskalenko * 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. */ #include __FBSDID("$FreeBSD$"); #include #include #include #include #include #include #if defined(SORT_THREADS) #include #endif #include #include #include #include #include #include #include #include "coll.h" #include "file.h" #include "radixsort.h" unsigned long long free_memory = 1000000; unsigned long long available_free_memory = 1000000; bool use_mmap; const char *tmpdir = "/var/tmp"; const char *compress_program; size_t max_open_files = 16; /* * How much space we read from file at once */ #define READ_CHUNK (4096) /* * File reader structure */ struct file_reader { struct reader_buffer rb; FILE *file; char *fname; unsigned char *buffer; unsigned char *mmapaddr; unsigned char *mmapptr; size_t bsz; size_t cbsz; size_t mmapsize; size_t strbeg; int fd; char elsymb; }; /* * Structure to be used in file merge process. */ struct file_header { struct file_reader *fr; struct sort_list_item *si; /* current top line */ size_t file_pos; }; /* * List elements of "cleanable" files list. */ struct CLEANABLE_FILE { char *fn; LIST_ENTRY(CLEANABLE_FILE) files; }; /* * List header of "cleanable" files list. */ static LIST_HEAD(CLEANABLE_FILES,CLEANABLE_FILE) tmp_files; /* * Semaphore to protect the tmp file list. * We use semaphore here because it is signal-safe, according to POSIX. * And semaphore does not require pthread library. */ static sem_t tmp_files_sem; static void mt_sort(struct sort_list *list, int (*sort_func)(void *, size_t, size_t, int (*)(const void *, const void *)), const char* fn); /* * Init tmp files list */ void init_tmp_files(void) { LIST_INIT(&tmp_files); sem_init(&tmp_files_sem, 0, 1); } /* * Save name of a tmp file for signal cleanup */ void tmp_file_atexit(const char *tmp_file) { if (tmp_file) { sem_wait(&tmp_files_sem); struct CLEANABLE_FILE *item = sort_malloc(sizeof(struct CLEANABLE_FILE)); item->fn = sort_strdup(tmp_file); LIST_INSERT_HEAD(&tmp_files, item, files); sem_post(&tmp_files_sem); } } /* * Clear tmp files */ void clear_tmp_files(void) { struct CLEANABLE_FILE *item; sem_wait(&tmp_files_sem); LIST_FOREACH(item,&tmp_files,files) { if ((item) && (item->fn)) unlink(item->fn); } sem_post(&tmp_files_sem); } /* * Check whether a file is a temporary file */ static bool file_is_tmp(const char* fn) { struct CLEANABLE_FILE *item; bool ret = false; if (fn) { sem_wait(&tmp_files_sem); LIST_FOREACH(item,&tmp_files,files) { if ((item) && (item->fn)) if (strcmp(item->fn, fn) == 0) { ret = true; break; } } sem_post(&tmp_files_sem); } return (ret); } /* * Generate new temporary file name */ char * new_tmp_file_name(void) { static size_t tfcounter = 0; static const char *fn = ".bsdsort."; char *ret; size_t sz; sz = strlen(tmpdir) + 1 + strlen(fn) + 32 + 1; ret = sort_malloc(sz); sprintf(ret, "%s/%s%d.%lu", tmpdir, fn, (int) getpid(), (unsigned long)(tfcounter++)); tmp_file_atexit(ret); return (ret); } /* * Initialize file list */ void file_list_init(struct file_list *fl, bool tmp) { if (fl) { fl->count = 0; fl->sz = 0; fl->fns = NULL; fl->tmp = tmp; } } /* * Add a file name to the list */ void file_list_add(struct file_list *fl, char *fn, bool allocate) { if (fl && fn) { if (fl->count >= fl->sz || (fl->fns == NULL)) { fl->sz = (fl->sz) * 2 + 1; fl->fns = sort_realloc(fl->fns, fl->sz * sizeof(char *)); } fl->fns[fl->count] = allocate ? sort_strdup(fn) : fn; fl->count += 1; } } /* * Populate file list from array of file names */ void file_list_populate(struct file_list *fl, int argc, char **argv, bool allocate) { if (fl && argv) { int i; for (i = 0; i < argc; i++) file_list_add(fl, argv[i], allocate); } } /* * Clean file list data and delete the files, * if this is a list of temporary files */ void file_list_clean(struct file_list *fl) { if (fl) { if (fl->fns) { size_t i; for (i = 0; i < fl->count; i++) { if (fl->fns[i]) { if (fl->tmp) unlink(fl->fns[i]); sort_free(fl->fns[i]); fl->fns[i] = 0; } } sort_free(fl->fns); fl->fns = NULL; } fl->sz = 0; fl->count = 0; fl->tmp = false; } } /* * Init sort list */ void sort_list_init(struct sort_list *l) { if (l) { l->count = 0; l->size = 0; l->memsize = sizeof(struct sort_list); l->list = NULL; } } /* * Add string to sort list */ void sort_list_add(struct sort_list *l, struct bwstring *str) { if (l && str) { size_t indx = l->count; if ((l->list == NULL) || (indx >= l->size)) { size_t newsize = (l->size + 1) + 1024; l->list = sort_realloc(l->list, sizeof(struct sort_list_item*) * newsize); l->memsize += (newsize - l->size) * sizeof(struct sort_list_item*); l->size = newsize; } l->list[indx] = sort_list_item_alloc(); sort_list_item_set(l->list[indx], str); l->memsize += sort_list_item_size(l->list[indx]); l->count += 1; } } /* * Clean sort list data */ void sort_list_clean(struct sort_list *l) { if (l) { if (l->list) { size_t i; for (i = 0; i < l->count; i++) { struct sort_list_item *item; item = l->list[i]; if (item) { sort_list_item_clean(item); sort_free(item); l->list[i] = NULL; } } sort_free(l->list); l->list = NULL; } l->count = 0; l->size = 0; l->memsize = sizeof(struct sort_list); } } /* * Write sort list to file */ void sort_list_dump(struct sort_list *l, const char *fn) { if (l && fn) { FILE *f; f = openfile(fn, "w"); if (f == NULL) err(2, NULL); if (l->list) { size_t i; if (!(sort_opts_vals.uflag)) { for (i = 0; i < l->count; ++i) bwsfwrite(l->list[i]->str, f, sort_opts_vals.zflag); } else { struct sort_list_item *last_printed_item = NULL; struct sort_list_item *item; for (i = 0; i < l->count; ++i) { item = l->list[i]; if ((last_printed_item == NULL) || list_coll(&last_printed_item, &item)) { bwsfwrite(item->str, f, sort_opts_vals.zflag); last_printed_item = item; } } } } closefile(f, fn); } } /* * Checks if the given file is sorted. Stops at the first disorder, * prints the disordered line and returns 1. */ int check(const char *fn) { struct bwstring *s1, *s2, *s1disorder, *s2disorder; struct file_reader *fr; struct keys_array *ka1, *ka2; int res; size_t pos, posdisorder; s1 = s2 = s1disorder = s2disorder = NULL; ka1 = ka2 = NULL; fr = file_reader_init(fn); res = 0; pos = 1; posdisorder = 1; if (fr == NULL) { err(2, NULL); goto end; } s1 = file_reader_readline(fr); if (s1 == NULL) goto end; ka1 = keys_array_alloc(); preproc(s1, ka1); s2 = file_reader_readline(fr); if (s2 == NULL) goto end; ka2 = keys_array_alloc(); preproc(s2, ka2); for (;;) { if (debug_sort) { bwsprintf(stdout, s2, "s1=<", ">"); bwsprintf(stdout, s1, "s2=<", ">"); } int cmp = key_coll(ka2, ka1, 0); if (debug_sort) printf("; cmp1=%d", cmp); if (!cmp && sort_opts_vals.complex_sort && !(sort_opts_vals.uflag) && !(sort_opts_vals.sflag)) { cmp = top_level_str_coll(s2, s1); if (debug_sort) printf("; cmp2=%d", cmp); } if (debug_sort) printf("\n"); if ((sort_opts_vals.uflag && (cmp <= 0)) || (cmp < 0)) { if (!(sort_opts_vals.csilentflag)) { s2disorder = bwsdup(s2); posdisorder = pos; if (debug_sort) s1disorder = bwsdup(s1); } res = 1; goto end; } pos++; clean_keys_array(s1, ka1); sort_free(ka1); ka1 = ka2; ka2 = NULL; bwsfree(s1); s1 = s2; s2 = file_reader_readline(fr); if (s2 == NULL) goto end; ka2 = keys_array_alloc(); preproc(s2, ka2); } end: if (ka1) { clean_keys_array(s1, ka1); sort_free(ka1); } if (s1) bwsfree(s1); if (ka2) { clean_keys_array(s2, ka2); sort_free(ka2); } if (s2) bwsfree(s2); if ((fn == NULL) || (*fn == 0) || (strcmp(fn, "-") == 0)) { for (;;) { s2 = file_reader_readline(fr); if (s2 == NULL) break; bwsfree(s2); } } file_reader_free(fr); if (s2disorder) { bws_disorder_warnx(s2disorder, fn, posdisorder); if (s1disorder) { bws_disorder_warnx(s1disorder, fn, posdisorder); if (s1disorder != s2disorder) bwsfree(s1disorder); } bwsfree(s2disorder); s1disorder = NULL; s2disorder = NULL; } if (res) exit(res); return (0); } /* * Opens a file. If the given filename is "-", stdout will be * opened. */ FILE * openfile(const char *fn, const char *mode) { FILE *file; if (strcmp(fn, "-") == 0) { return ((mode && mode[0] == 'r') ? stdin : stdout); } else { mode_t orig_file_mask = 0; int is_tmp = file_is_tmp(fn); if (is_tmp && (mode[0] == 'w')) orig_file_mask = umask(S_IWGRP | S_IWOTH | S_IRGRP | S_IROTH); if (is_tmp && (compress_program != NULL)) { char *cmd; size_t cmdsz; cmdsz = strlen(fn) + 128; cmd = sort_malloc(cmdsz); fflush(stdout); if (mode[0] == 'r') snprintf(cmd, cmdsz - 1, "cat %s | %s -d", fn, compress_program); else if (mode[0] == 'w') snprintf(cmd, cmdsz - 1, "%s > %s", compress_program, fn); else err(2, "%s", getstr(7)); if ((file = popen(cmd, mode)) == NULL) err(2, NULL); sort_free(cmd); } else if ((file = fopen(fn, mode)) == NULL) err(2, NULL); if (is_tmp && (mode[0] == 'w')) umask(orig_file_mask); } return (file); } /* * Close file */ void closefile(FILE *f, const char *fn) { if (f == NULL) { ; } else if (f == stdin) { ; } else if (f == stdout) { fflush(f); } else { if (file_is_tmp(fn) && compress_program != NULL) { if(pclose(f)<0) err(2,NULL); } else fclose(f); } } /* * Reads a file into the internal buffer. */ struct file_reader * file_reader_init(const char *fsrc) { struct file_reader *ret; if (fsrc == NULL) fsrc = "-"; ret = sort_malloc(sizeof(struct file_reader)); memset(ret, 0, sizeof(struct file_reader)); ret->elsymb = '\n'; if (sort_opts_vals.zflag) ret->elsymb = 0; ret->fname = sort_strdup(fsrc); if (strcmp(fsrc, "-") && (compress_program == NULL) && use_mmap) { do { struct stat stat_buf; void *addr; size_t sz = 0; int fd, flags; flags = MAP_NOCORE | MAP_NOSYNC; addr = MAP_FAILED; fd = open(fsrc, O_RDONLY); if (fd < 0) err(2, NULL); if (fstat(fd, &stat_buf) < 0) { close(fd); break; } sz = stat_buf.st_size; #if defined(MAP_PREFAULT_READ) flags |= MAP_PREFAULT_READ; #endif addr = mmap(NULL, sz, PROT_READ, flags, fd, 0); if (addr == MAP_FAILED) { close(fd); break; } ret->fd = fd; ret->mmapaddr = addr; ret->mmapsize = sz; ret->mmapptr = ret->mmapaddr; } while (0); } if (ret->mmapaddr == NULL) { ret->file = openfile(fsrc, "r"); if (ret->file == NULL) err(2, NULL); if (strcmp(fsrc, "-")) { ret->cbsz = READ_CHUNK; ret->buffer = sort_malloc(ret->cbsz); ret->bsz = 0; ret->strbeg = 0; ret->bsz = fread(ret->buffer, 1, ret->cbsz, ret->file); if (ret->bsz == 0) { if (ferror(ret->file)) err(2, NULL); } } } return (ret); } struct bwstring * file_reader_readline(struct file_reader *fr) { struct bwstring *ret = NULL; if (fr->mmapaddr) { unsigned char *mmapend; mmapend = fr->mmapaddr + fr->mmapsize; if (fr->mmapptr >= mmapend) return (NULL); else { unsigned char *strend; size_t sz; sz = mmapend - fr->mmapptr; strend = memchr(fr->mmapptr, fr->elsymb, sz); if (strend == NULL) { ret = bwscsbdup(fr->mmapptr, sz); fr->mmapptr = mmapend; } else { ret = bwscsbdup(fr->mmapptr, strend - fr->mmapptr); fr->mmapptr = strend + 1; } } } else if (fr->file != stdin) { unsigned char *strend; size_t bsz1, remsz, search_start; search_start = 0; remsz = 0; strend = NULL; if (fr->bsz > fr->strbeg) remsz = fr->bsz - fr->strbeg; /* line read cycle */ for (;;) { if (remsz > search_start) strend = memchr(fr->buffer + fr->strbeg + search_start, fr->elsymb, remsz - search_start); else strend = NULL; if (strend) break; if (feof(fr->file)) break; if (fr->bsz != fr->cbsz) /* NOTREACHED */ err(2, "File read software error 1"); if (remsz > (READ_CHUNK >> 1)) { search_start = fr->cbsz - fr->strbeg; fr->cbsz += READ_CHUNK; fr->buffer = sort_realloc(fr->buffer, fr->cbsz); bsz1 = fread(fr->buffer + fr->bsz, 1, READ_CHUNK, fr->file); if (bsz1 == 0) { if (ferror(fr->file)) err(2, NULL); break; } fr->bsz += bsz1; remsz += bsz1; } else { if (remsz > 0 && fr->strbeg>0) bcopy(fr->buffer + fr->strbeg, fr->buffer, remsz); fr->strbeg = 0; search_start = remsz; bsz1 = fread(fr->buffer + remsz, 1, fr->cbsz - remsz, fr->file); if (bsz1 == 0) { if (ferror(fr->file)) err(2, NULL); break; } fr->bsz = remsz + bsz1; remsz = fr->bsz; } } if (strend == NULL) strend = fr->buffer + fr->bsz; if ((fr->buffer + fr->strbeg <= strend) && (fr->strbeg < fr->bsz) && (remsz>0)) ret = bwscsbdup(fr->buffer + fr->strbeg, strend - fr->buffer - fr->strbeg); fr->strbeg = (strend - fr->buffer) + 1; } else { size_t len = 0; ret = bwsfgetln(fr->file, &len, sort_opts_vals.zflag, &(fr->rb)); } return (ret); } static void file_reader_clean(struct file_reader *fr) { if (fr) { if (fr->mmapaddr) munmap(fr->mmapaddr, fr->mmapsize); if (fr->fd) close(fr->fd); if (fr->buffer) sort_free(fr->buffer); if (fr->file) if (fr->file != stdin) closefile(fr->file, fr->fname); if(fr->fname) sort_free(fr->fname); memset(fr, 0, sizeof(struct file_reader)); } } void file_reader_free(struct file_reader *fr) { if (fr) { file_reader_clean(fr); sort_free(fr); } } int procfile(const char *fsrc, struct sort_list *list, struct file_list *fl) { struct file_reader *fr; fr = file_reader_init(fsrc); if (fr == NULL) err(2, NULL); /* file browse cycle */ for (;;) { struct bwstring *bws; bws = file_reader_readline(fr); if (bws == NULL) break; sort_list_add(list, bws); if (list->memsize >= available_free_memory) { char *fn; fn = new_tmp_file_name(); sort_list_to_file(list, fn); file_list_add(fl, fn, false); sort_list_clean(list); } } file_reader_free(fr); return (0); } /* * Compare file headers. Files with EOF always go to the end of the list. */ static int file_header_cmp(struct file_header *f1, struct file_header *f2) { if (f1 == f2) return (0); else { if (f1->fr == NULL) { return ((f2->fr == NULL) ? 0 : +1); } else if (f2->fr == NULL) return (-1); else { int ret; ret = list_coll(&(f1->si), &(f2->si)); if (!ret) return ((f1->file_pos < f2->file_pos) ? -1 : +1); return (ret); } } } /* * Allocate and init file header structure */ static void file_header_init(struct file_header **fh, const char *fn, size_t file_pos) { if (fh && fn) { struct bwstring *line; *fh = sort_malloc(sizeof(struct file_header)); (*fh)->file_pos = file_pos; (*fh)->fr = file_reader_init(fn); if ((*fh)->fr == NULL) { perror(fn); err(2, "%s", getstr(8)); } line = file_reader_readline((*fh)->fr); if (line == NULL) { file_reader_free((*fh)->fr); (*fh)->fr = NULL; (*fh)->si = NULL; } else { (*fh)->si = sort_list_item_alloc(); sort_list_item_set((*fh)->si, line); } } } /* * Close file */ static void file_header_close(struct file_header **fh) { if (fh && *fh) { if ((*fh)->fr) { file_reader_free((*fh)->fr); (*fh)->fr = NULL; } if ((*fh)->si) { sort_list_item_clean((*fh)->si); sort_free((*fh)->si); (*fh)->si = NULL; } sort_free(*fh); *fh = NULL; } } /* * Swap two array elements */ static void file_header_swap(struct file_header **fh, size_t i1, size_t i2) { struct file_header *tmp; tmp = fh[i1]; fh[i1] = fh[i2]; fh[i2] = tmp; } /* heap algorithm ==>> */ /* * See heap sort algorithm * "Raises" last element to its right place */ static void file_header_heap_swim(struct file_header **fh, size_t indx) { if (indx > 0) { size_t parent_index; parent_index = (indx - 1) >> 1; if (file_header_cmp(fh[indx], fh[parent_index]) < 0) { /* swap child and parent and continue */ file_header_swap(fh, indx, parent_index); file_header_heap_swim(fh, parent_index); } } } /* * Sink the top element to its correct position */ static void file_header_heap_sink(struct file_header **fh, size_t indx, size_t size) { size_t left_child_index; size_t right_child_index; left_child_index = indx + indx + 1; right_child_index = left_child_index + 1; if (left_child_index < size) { size_t min_child_index; min_child_index = left_child_index; if ((right_child_index < size) && (file_header_cmp(fh[left_child_index], fh[right_child_index]) > 0)) min_child_index = right_child_index; if (file_header_cmp(fh[indx], fh[min_child_index]) > 0) { file_header_swap(fh, indx, min_child_index); file_header_heap_sink(fh, min_child_index, size); } } } /* <<== heap algorithm */ /* * Adds element to the "left" end */ static void file_header_list_rearrange_from_header(struct file_header **fh, size_t size) { file_header_heap_sink(fh, 0, size); } /* * Adds element to the "right" end */ static void file_header_list_push(struct file_header *f, struct file_header **fh, size_t size) { fh[size++] = f; file_header_heap_swim(fh, size - 1); } struct last_printed { struct bwstring *str; }; /* * Prints the current line of the file */ static void file_header_print(struct file_header *fh, FILE *f_out, struct last_printed *lp) { if (fh && fh->fr && f_out && fh->si && fh->si->str) { if (sort_opts_vals.uflag) { if ((lp->str == NULL) || (str_list_coll(lp->str, &(fh->si)))) { bwsfwrite(fh->si->str, f_out, sort_opts_vals.zflag); if (lp->str) bwsfree(lp->str); lp->str = bwsdup(fh->si->str); } } else bwsfwrite(fh->si->str, f_out, sort_opts_vals.zflag); } } /* * Read next line */ static void file_header_read_next(struct file_header *fh) { if (fh && fh->fr) { struct bwstring *tmp; tmp = file_reader_readline(fh->fr); if (tmp == NULL) { file_reader_free(fh->fr); fh->fr = NULL; if (fh->si) { sort_list_item_clean(fh->si); sort_free(fh->si); fh->si = NULL; } } else { if (fh->si == NULL) fh->si = sort_list_item_alloc(); sort_list_item_set(fh->si, tmp); } } } /* * Merge array of "files headers" */ static void file_headers_merge(size_t fnum, struct file_header **fh, FILE *f_out) { struct last_printed lp; size_t i; memset(&lp, 0, sizeof(lp)); /* * construct the initial sort structure */ for (i = 0; i < fnum; i++) file_header_list_push(fh[i], fh, i); while (fh[0]->fr) { /* unfinished files are always in front */ /* output the smallest line: */ file_header_print(fh[0], f_out, &lp); /* read a new line, if possible: */ file_header_read_next(fh[0]); /* re-arrange the list: */ file_header_list_rearrange_from_header(fh, fnum); } if (lp.str) bwsfree(lp.str); } /* * Merges the given files into the output file, which can be * stdout. */ static void merge_files_array(size_t argc, char **argv, const char *fn_out) { if (argv && fn_out) { struct file_header **fh; FILE *f_out; size_t i; f_out = openfile(fn_out, "w"); if (f_out == NULL) err(2, NULL); fh = sort_malloc((argc + 1) * sizeof(struct file_header *)); for (i = 0; i < argc; i++) file_header_init(fh + i, argv[i], (size_t) i); file_headers_merge(argc, fh, f_out); for (i = 0; i < argc; i++) file_header_close(fh + i); sort_free(fh); closefile(f_out, fn_out); } } /* * Shrinks the file list until its size smaller than max number of opened files */ static int shrink_file_list(struct file_list *fl) { if ((fl == NULL) || (size_t) (fl->count) < max_open_files) return (0); else { struct file_list new_fl; size_t indx = 0; file_list_init(&new_fl, true); while (indx < fl->count) { char *fnew; size_t num; num = fl->count - indx; fnew = new_tmp_file_name(); if ((size_t) num >= max_open_files) num = max_open_files - 1; merge_files_array(num, fl->fns + indx, fnew); if (fl->tmp) { size_t i; for (i = 0; i < num; i++) unlink(fl->fns[indx + i]); } file_list_add(&new_fl, fnew, false); indx += num; } fl->tmp = false; /* already taken care of */ file_list_clean(fl); fl->count = new_fl.count; fl->fns = new_fl.fns; fl->sz = new_fl.sz; fl->tmp = new_fl.tmp; return (1); } } /* * Merge list of files */ void merge_files(struct file_list *fl, const char *fn_out) { if (fl && fn_out) { while (shrink_file_list(fl)); merge_files_array(fl->count, fl->fns, fn_out); } } static const char * get_sort_method_name(int sm) { if (sm == SORT_MERGESORT) return "mergesort"; else if (sort_opts_vals.sort_method == SORT_RADIXSORT) return "radixsort"; else if (sort_opts_vals.sort_method == SORT_HEAPSORT) return "heapsort"; else return "quicksort"; } /* * Wrapper for qsort */ static int sort_qsort(void *list, size_t count, size_t elem_size, int (*cmp_func)(const void *, const void *)) { qsort(list, count, elem_size, cmp_func); return (0); } /* * Sort list of lines and writes it to the file */ void sort_list_to_file(struct sort_list *list, const char *outfile) { struct sort_mods *sm = &(keys[0].sm); if (!(sm->Mflag) && !(sm->Rflag) && !(sm->Vflag) && !(sm->Vflag) && !(sm->gflag) && !(sm->hflag) && !(sm->nflag)) { if ((sort_opts_vals.sort_method == SORT_DEFAULT) && byte_sort) sort_opts_vals.sort_method = SORT_RADIXSORT; } else if (sort_opts_vals.sort_method == SORT_RADIXSORT) err(2, "%s", getstr(9)); /* * to handle stable sort and the unique cases in the * right order, we need stable basic algorithm */ if (sort_opts_vals.sflag) { switch (sort_opts_vals.sort_method){ case SORT_MERGESORT: break; case SORT_RADIXSORT: break; case SORT_DEFAULT: sort_opts_vals.sort_method = SORT_MERGESORT; break; default: errx(2, "%s", getstr(10)); - }; + } } if (sort_opts_vals.sort_method == SORT_DEFAULT) sort_opts_vals.sort_method = DEFAULT_SORT_ALGORITHM; if (debug_sort) printf("sort_method=%s\n", get_sort_method_name(sort_opts_vals.sort_method)); switch (sort_opts_vals.sort_method){ case SORT_RADIXSORT: rxsort(list->list, list->count); sort_list_dump(list, outfile); break; case SORT_MERGESORT: mt_sort(list, mergesort, outfile); break; case SORT_HEAPSORT: mt_sort(list, heapsort, outfile); break; case SORT_QSORT: mt_sort(list, sort_qsort, outfile); break; default: mt_sort(list, DEFAULT_SORT_FUNC, outfile); break; } } /******************* MT SORT ************************/ #if defined(SORT_THREADS) /* semaphore to count threads */ static sem_t mtsem; /* current system sort function */ static int (*g_sort_func)(void *, size_t, size_t, int(*)(const void *, const void *)); /* * Sort cycle thread (in multi-threaded mode) */ static void* mt_sort_thread(void* arg) { struct sort_list *list = arg; g_sort_func(list->list, list->count, sizeof(struct sort_list_item *), (int(*)(const void *, const void *)) list_coll); sem_post(&mtsem); return (arg); } /* * Compare sub-lists. Empty sub-lists always go to the end of the list. */ static int sub_list_cmp(struct sort_list *l1, struct sort_list *l2) { if (l1 == l2) return (0); else { if (l1->count == 0) { return ((l2->count == 0) ? 0 : +1); } else if (l2->count == 0) { return (-1); } else { int ret; ret = list_coll(&(l1->list[0]), &(l2->list[0])); if (!ret) return ((l1->sub_list_pos < l2->sub_list_pos) ? -1 : +1); return (ret); } } } /* * Swap two array elements */ static void sub_list_swap(struct sort_list **sl, size_t i1, size_t i2) { struct sort_list *tmp; tmp = sl[i1]; sl[i1] = sl[i2]; sl[i2] = tmp; } /* heap algorithm ==>> */ /* * See heap sort algorithm * "Raises" last element to its right place */ static void sub_list_swim(struct sort_list **sl, size_t indx) { if (indx > 0) { size_t parent_index; parent_index = (indx - 1) >> 1; if (sub_list_cmp(sl[indx], sl[parent_index]) < 0) { /* swap child and parent and continue */ sub_list_swap(sl, indx, parent_index); sub_list_swim(sl, parent_index); } } } /* * Sink the top element to its correct position */ static void sub_list_sink(struct sort_list **sl, size_t indx, size_t size) { size_t left_child_index; size_t right_child_index; left_child_index = indx + indx + 1; right_child_index = left_child_index + 1; if (left_child_index < size) { size_t min_child_index; min_child_index = left_child_index; if ((right_child_index < size) && (sub_list_cmp(sl[left_child_index], sl[right_child_index]) > 0)) min_child_index = right_child_index; if (sub_list_cmp(sl[indx], sl[min_child_index]) > 0) { sub_list_swap(sl, indx, min_child_index); sub_list_sink(sl, min_child_index, size); } } } /* <<== heap algorithm */ /* * Adds element to the "right" end */ static void sub_list_push(struct sort_list *s, struct sort_list **sl, size_t size) { sl[size++] = s; sub_list_swim(sl, size - 1); } struct last_printed_item { struct sort_list_item *item; }; /* * Prints the current line of the file */ static void sub_list_header_print(struct sort_list *sl, FILE *f_out, struct last_printed_item *lp) { if (sl && sl->count && f_out && sl->list[0]->str) { if (sort_opts_vals.uflag) { if ((lp->item == NULL) || (list_coll(&(lp->item), &(sl->list[0])))) { bwsfwrite(sl->list[0]->str, f_out, sort_opts_vals.zflag); lp->item = sl->list[0]; } } else bwsfwrite(sl->list[0]->str, f_out, sort_opts_vals.zflag); } } /* * Read next line */ static void sub_list_next(struct sort_list *sl) { if (sl && sl->count) { sl->list += 1; sl->count -= 1; } } /* * Merge sub-lists to a file */ static void merge_sub_lists(struct sort_list **sl, size_t n, FILE* f_out) { struct last_printed_item lp; size_t i; memset(&lp,0,sizeof(lp)); /* construct the initial list: */ for (i = 0; i < n; i++) sub_list_push(sl[i], sl, i); while (sl[0]->count) { /* unfinished lists are always in front */ /* output the smallest line: */ sub_list_header_print(sl[0], f_out, &lp); /* move to a new line, if possible: */ sub_list_next(sl[0]); /* re-arrange the list: */ sub_list_sink(sl, 0, n); } } /* * Merge sub-lists to a file */ static void merge_list_parts(struct sort_list **parts, size_t n, const char *fn) { FILE* f_out; f_out = openfile(fn,"w"); merge_sub_lists(parts, n, f_out); closefile(f_out, fn); } #endif /* defined(SORT_THREADS) */ /* * Multi-threaded sort algorithm "driver" */ static void mt_sort(struct sort_list *list, int(*sort_func)(void *, size_t, size_t, int(*)(const void *, const void *)), const char* fn) { #if defined(SORT_THREADS) if (nthreads < 2 || list->count < MT_SORT_THRESHOLD) { size_t nthreads_save = nthreads; nthreads = 1; #endif /* if single thread or small data, do simple sort */ sort_func(list->list, list->count, sizeof(struct sort_list_item *), (int(*)(const void *, const void *)) list_coll); sort_list_dump(list, fn); #if defined(SORT_THREADS) nthreads = nthreads_save; } else { /* multi-threaded sort */ struct sort_list **parts; size_t avgsize, cstart, i; /* array of sub-lists */ parts = sort_malloc(sizeof(struct sort_list*) * nthreads); cstart = 0; avgsize = list->count / nthreads; /* set global system sort function */ g_sort_func = sort_func; /* set sublists */ for (i = 0; i < nthreads; ++i) { size_t sz = 0; parts[i] = sort_malloc(sizeof(struct sort_list)); parts[i]->list = list->list + cstart; parts[i]->memsize = 0; parts[i]->sub_list_pos = i; sz = (i == nthreads - 1) ? list->count - cstart : avgsize; parts[i]->count = sz; parts[i]->size = parts[i]->count; cstart += sz; } /* init threads counting semaphore */ sem_init(&mtsem, 0, 0); /* start threads */ for (i = 0; i < nthreads; ++i) { pthread_t pth; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_DETACHED); for (;;) { int res = pthread_create(&pth, &attr, mt_sort_thread, parts[i]); if (res >= 0) break; if (errno == EAGAIN) { pthread_yield(); continue; } err(2, NULL); } pthread_attr_destroy(&attr); } /* wait for threads completion */ for (i = 0; i < nthreads; ++i) { sem_wait(&mtsem); } /* destroy the semaphore - we do not need it anymore */ sem_destroy(&mtsem); /* merge sorted sub-lists to the file */ merge_list_parts(parts, nthreads, fn); /* free sub-lists data */ for (i = 0; i < nthreads; ++i) { sort_free(parts[i]); } sort_free(parts); } #endif /* defined(SORT_THREADS) */ } Index: head/usr.bin/truss/syscalls.c =================================================================== --- head/usr.bin/truss/syscalls.c (revision 298088) +++ head/usr.bin/truss/syscalls.c (revision 298089) @@ -1,2114 +1,2114 @@ /* * Copyright 1997 Sean Eric Fagan * * 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 Sean Eric Fagan * 4. Neither the name of the author may be used to endorse or promote * products derived from this software without specific prior written * permission. * * 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$"); /* * This file has routines used to print out system calls and their * arguments. */ #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 #include #include #include #include #include #include "truss.h" #include "extern.h" #include "syscall.h" /* 64-bit alignment on 32-bit platforms. */ #if !defined(__LP64__) && defined(__powerpc__) #define QUAD_ALIGN 1 #else #define QUAD_ALIGN 0 #endif /* Number of slots needed for a 64-bit argument. */ #ifdef __LP64__ #define QUAD_SLOTS 1 #else #define QUAD_SLOTS 2 #endif /* * This should probably be in its own file, sorted alphabetically. */ static struct syscall decoded_syscalls[] = { /* Native ABI */ { .name = "__getcwd", .ret_type = 1, .nargs = 2, .args = { { Name | OUT, 0 }, { Int, 1 } } }, { .name = "_umtx_op", .ret_type = 1, .nargs = 5, .args = { { Ptr, 0 }, { Umtxop, 1 }, { LongHex, 2 }, { Ptr, 3 }, { Ptr, 4 } } }, { .name = "accept", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, { .name = "access", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Accessmode, 1 } } }, { .name = "bind", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, { .name = "bindat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 }, { Int, 3 } } }, { .name = "break", .ret_type = 1, .nargs = 1, .args = { { Ptr, 0 } } }, { .name = "chdir", .ret_type = 1, .nargs = 1, .args = { { Name, 0 } } }, { .name = "chflags", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Hex, 1 } } }, { .name = "chmod", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Octal, 1 } } }, { .name = "chown", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } }, { .name = "chroot", .ret_type = 1, .nargs = 1, .args = { { Name, 0 } } }, { .name = "clock_gettime", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Timespec | OUT, 1 } } }, { .name = "close", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "connect", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Sockaddr | IN, 1 }, { Int, 2 } } }, { .name = "connectat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Int, 1 }, { Sockaddr | IN, 2 }, { Int, 3 } } }, { .name = "eaccess", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Accessmode, 1 } } }, { .name = "execve", .ret_type = 1, .nargs = 3, .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 }, { ExecEnv | IN, 2 } } }, { .name = "exit", .ret_type = 0, .nargs = 1, .args = { { Hex, 0 } } }, { .name = "faccessat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name | IN, 1 }, { Accessmode, 2 }, { Atflags, 3 } } }, { .name = "fchmod", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Octal, 1 } } }, { .name = "fchmodat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Atflags, 3 } } }, { .name = "fchown", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Int, 1 }, { Int, 2 } } }, { .name = "fchownat", .ret_type = 1, .nargs = 5, .args = { { Atfd, 0 }, { Name, 1 }, { Int, 2 }, { Int, 3 }, { Atflags, 4 } } }, { .name = "fcntl", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Fcntl, 1 }, { Fcntlflag, 2 } } }, { .name = "fstat", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Stat | OUT, 1 } } }, { .name = "fstatat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name | IN, 1 }, { Stat | OUT, 2 }, { Atflags, 3 } } }, { .name = "fstatfs", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { StatFs | OUT, 1 } } }, { .name = "ftruncate", .ret_type = 1, .nargs = 2, .args = { { Int | IN, 0 }, { QuadHex | IN, 1 + QUAD_ALIGN } } }, { .name = "futimens", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Timespec2 | IN, 1 } } }, { .name = "futimes", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Timeval2 | IN, 1 } } }, { .name = "futimesat", .ret_type = 1, .nargs = 3, .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timeval2 | IN, 2 } } }, { .name = "getitimer", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Itimerval | OUT, 2 } } }, { .name = "getpeername", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, { .name = "getpgid", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "getrlimit", .ret_type = 1, .nargs = 2, .args = { { Resource, 0 }, { Rlimit | OUT, 1 } } }, { .name = "getrusage", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Rusage | OUT, 1 } } }, { .name = "getsid", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "getsockname", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Sockaddr | OUT, 1 }, { Ptr | OUT, 2 } } }, { .name = "gettimeofday", .ret_type = 1, .nargs = 2, .args = { { Timeval | OUT, 0 }, { Ptr, 1 } } }, { .name = "ioctl", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Ioctl, 1 }, { Hex, 2 } } }, { .name = "kevent", .ret_type = 1, .nargs = 6, .args = { { Int, 0 }, { Kevent, 1 }, { Int, 2 }, { Kevent | OUT, 3 }, { Int, 4 }, { Timespec, 5 } } }, { .name = "kill", .ret_type = 1, .nargs = 2, .args = { { Int | IN, 0 }, { Signal | IN, 1 } } }, { .name = "kldfind", .ret_type = 1, .nargs = 1, .args = { { Name | IN, 0 } } }, { .name = "kldfirstmod", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "kldload", .ret_type = 1, .nargs = 1, .args = { { Name | IN, 0 } } }, { .name = "kldnext", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "kldstat", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Ptr, 1 } } }, { .name = "kldunload", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "kse_release", .ret_type = 0, .nargs = 1, .args = { { Timespec, 0 } } }, { .name = "lchflags", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Hex, 1 } } }, { .name = "lchmod", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Octal, 1 } } }, { .name = "lchown", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Int, 1 }, { Int, 2 } } }, { .name = "link", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Name, 1 } } }, { .name = "linkat", .ret_type = 1, .nargs = 5, .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 }, { Atflags, 4 } } }, { .name = "lseek", .ret_type = 2, .nargs = 3, .args = { { Int, 0 }, { QuadHex, 1 + QUAD_ALIGN }, { Whence, 1 + QUAD_SLOTS + QUAD_ALIGN } } }, { .name = "lstat", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } }, { .name = "lutimes", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } }, { .name = "mkdir", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Octal, 1 } } }, { .name = "mkdirat", .ret_type = 1, .nargs = 3, .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } }, { .name = "mkfifo", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Octal, 1 } } }, { .name = "mkfifoat", .ret_type = 1, .nargs = 3, .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 } } }, { .name = "mknod", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Octal, 1 }, { Int, 2 } } }, { .name = "mknodat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name, 1 }, { Octal, 2 }, { Int, 3 } } }, { .name = "mmap", .ret_type = 1, .nargs = 6, .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 }, { Mmapflags, 3 }, { Int, 4 }, { QuadHex, 5 + QUAD_ALIGN } } }, { .name = "modfind", .ret_type = 1, .nargs = 1, .args = { { Name | IN, 0 } } }, { .name = "mount", .ret_type = 1, .nargs = 4, .args = { { Name, 0 }, { Name, 1 }, { Int, 2 }, { Ptr, 3 } } }, { .name = "mprotect", .ret_type = 1, .nargs = 3, .args = { { Ptr, 0 }, { Int, 1 }, { Mprot, 2 } } }, { .name = "munmap", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { Int, 1 } } }, { .name = "nanosleep", .ret_type = 1, .nargs = 1, .args = { { Timespec, 0 } } }, { .name = "open", .ret_type = 1, .nargs = 3, .args = { { Name | IN, 0 }, { Open, 1 }, { Octal, 2 } } }, { .name = "openat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name | IN, 1 }, { Open, 2 }, { Octal, 3 } } }, { .name = "pathconf", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Pathconf, 1 } } }, { .name = "pipe", .ret_type = 1, .nargs = 1, .args = { { PipeFds | OUT, 0 } } }, { .name = "pipe2", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { Open, 1 } } }, { .name = "poll", .ret_type = 1, .nargs = 3, .args = { { Pollfd, 0 }, { Int, 1 }, { Int, 2 } } }, { .name = "posix_openpt", .ret_type = 1, .nargs = 1, .args = { { Open, 0 } } }, { .name = "procctl", .ret_type = 1, .nargs = 4, .args = { { Idtype, 0 }, { Quad, 1 + QUAD_ALIGN }, { Procctl, 1 + QUAD_ALIGN + QUAD_SLOTS }, { Ptr, 2 + QUAD_ALIGN + QUAD_SLOTS } } }, { .name = "read", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 } } }, { .name = "readlink", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Readlinkres | OUT, 1 }, { Int, 2 } } }, { .name = "readlinkat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name, 1 }, { Readlinkres | OUT, 2 }, { Int, 3 } } }, { .name = "recvfrom", .ret_type = 1, .nargs = 6, .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | OUT, 4 }, { Ptr | OUT, 5 } } }, { .name = "rename", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Name, 1 } } }, { .name = "renameat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name, 1 }, { Atfd, 2 }, { Name, 3 } } }, { .name = "rfork", .ret_type = 1, .nargs = 1, .args = { { Rforkflags, 0 } } }, { .name = "select", .ret_type = 1, .nargs = 5, .args = { { Int, 0 }, { Fd_set, 1 }, { Fd_set, 2 }, { Fd_set, 3 }, { Timeval, 4 } } }, { .name = "sendto", .ret_type = 1, .nargs = 6, .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, { Hex, 3 }, { Sockaddr | IN, 4 }, { Ptr | IN, 5 } } }, { .name = "setitimer", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Itimerval, 1 }, { Itimerval | OUT, 2 } } }, { .name = "setrlimit", .ret_type = 1, .nargs = 2, .args = { { Resource, 0 }, { Rlimit | IN, 1 } } }, { .name = "shutdown", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Shutdown, 1 } } }, { .name = "sigaction", .ret_type = 1, .nargs = 3, .args = { { Signal, 0 }, { Sigaction | IN, 1 }, { Sigaction | OUT, 2 } } }, { .name = "sigpending", .ret_type = 1, .nargs = 1, .args = { { Sigset | OUT, 0 } } }, { .name = "sigprocmask", .ret_type = 1, .nargs = 3, .args = { { Sigprocmask, 0 }, { Sigset, 1 }, { Sigset | OUT, 2 } } }, { .name = "sigqueue", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Signal, 1 }, { LongHex, 2 } } }, { .name = "sigreturn", .ret_type = 1, .nargs = 1, .args = { { Ptr, 0 } } }, { .name = "sigsuspend", .ret_type = 1, .nargs = 1, .args = { { Sigset | IN, 0 } } }, { .name = "sigtimedwait", .ret_type = 1, .nargs = 3, .args = { { Sigset | IN, 0 }, { Ptr, 1 }, { Timespec | IN, 2 } } }, { .name = "sigwait", .ret_type = 1, .nargs = 2, .args = { { Sigset | IN, 0 }, { Ptr, 1 } } }, { .name = "sigwaitinfo", .ret_type = 1, .nargs = 2, .args = { { Sigset | IN, 0 }, { Ptr, 1 } } }, { .name = "socket", .ret_type = 1, .nargs = 3, .args = { { Sockdomain, 0 }, { Socktype, 1 }, { Int, 2 } } }, { .name = "stat", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Stat | OUT, 1 } } }, { .name = "statfs", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { StatFs | OUT, 1 } } }, { .name = "symlink", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Name, 1 } } }, { .name = "symlinkat", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Atfd, 1 }, { Name, 2 } } }, { .name = "sysarch", .ret_type = 1, .nargs = 2, .args = { { Sysarch, 0 }, { Ptr, 1 } } }, { .name = "thr_kill", .ret_type = 1, .nargs = 2, .args = { { Long, 0 }, { Signal, 1 } } }, { .name = "thr_self", .ret_type = 1, .nargs = 1, .args = { { Ptr, 0 } } }, { .name = "truncate", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { QuadHex | IN, 1 + QUAD_ALIGN } } }, #if 0 /* Does not exist */ { .name = "umount", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Int, 2 } } }, #endif { .name = "unlink", .ret_type = 1, .nargs = 1, .args = { { Name, 0 } } }, { .name = "unlinkat", .ret_type = 1, .nargs = 3, .args = { { Atfd, 0 }, { Name, 1 }, { Atflags, 2 } } }, { .name = "unmount", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Int, 1 } } }, { .name = "utimensat", .ret_type = 1, .nargs = 4, .args = { { Atfd, 0 }, { Name | IN, 1 }, { Timespec2 | IN, 2 }, { Atflags, 3 } } }, { .name = "utimes", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Timeval2 | IN, 1 } } }, { .name = "utrace", .ret_type = 1, .nargs = 1, .args = { { Utrace, 0 } } }, { .name = "wait4", .ret_type = 1, .nargs = 4, .args = { { Int, 0 }, { ExitStatus | OUT, 1 }, { Waitoptions, 2 }, { Rusage | OUT, 3 } } }, { .name = "wait6", .ret_type = 1, .nargs = 6, .args = { { Idtype, 0 }, { Quad, 1 + QUAD_ALIGN }, { ExitStatus | OUT, 1 + QUAD_ALIGN + QUAD_SLOTS }, { Waitoptions, 2 + QUAD_ALIGN + QUAD_SLOTS }, { Rusage | OUT, 3 + QUAD_ALIGN + QUAD_SLOTS }, { Ptr, 4 + QUAD_ALIGN + QUAD_SLOTS } } }, { .name = "write", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 } } }, /* Linux ABI */ { .name = "linux_access", .ret_type = 1, .nargs = 2, .args = { { Name, 0 }, { Accessmode, 1 } } }, { .name = "linux_execve", .ret_type = 1, .nargs = 3, .args = { { Name | IN, 0 }, { ExecArgs | IN, 1 }, { ExecEnv | IN, 2 } } }, { .name = "linux_lseek", .ret_type = 2, .nargs = 3, .args = { { Int, 0 }, { Int, 1 }, { Whence, 2 } } }, { .name = "linux_mkdir", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Int, 1 } } }, { .name = "linux_newfstat", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Ptr | OUT, 1 } } }, { .name = "linux_newstat", .ret_type = 1, .nargs = 2, .args = { { Name | IN, 0 }, { Ptr | OUT, 1 } } }, { .name = "linux_open", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Hex, 1 }, { Octal, 2 } } }, { .name = "linux_readlink", .ret_type = 1, .nargs = 3, .args = { { Name, 0 }, { Name | OUT, 1 }, { Int, 2 } } }, { .name = "linux_socketcall", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { LinuxSockArgs, 1 } } }, { .name = "linux_stat64", .ret_type = 1, .nargs = 3, .args = { { Name | IN, 0 }, { Ptr | OUT, 1 }, { Ptr | IN, 1 } } }, /* CloudABI system calls. */ { .name = "cloudabi_sys_clock_res_get", .ret_type = 1, .nargs = 1, .args = { { CloudABIClockID, 0 } } }, { .name = "cloudabi_sys_clock_time_get", .ret_type = 1, .nargs = 2, .args = { { CloudABIClockID, 0 }, { CloudABITimestamp, 1 } } }, { .name = "cloudabi_sys_condvar_signal", .ret_type = 1, .nargs = 3, .args = { { Ptr, 0 }, { CloudABIMFlags, 1 }, { UInt, 2 } } }, { .name = "cloudabi_sys_fd_close", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "cloudabi_sys_fd_create1", .ret_type = 1, .nargs = 1, .args = { { CloudABIFileType, 0 } } }, { .name = "cloudabi_sys_fd_create2", .ret_type = 1, .nargs = 2, .args = { { CloudABIFileType, 0 }, { PipeFds | OUT, 0 } } }, { .name = "cloudabi_sys_fd_datasync", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "cloudabi_sys_fd_dup", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "cloudabi_sys_fd_replace", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Int, 1 } } }, { .name = "cloudabi_sys_fd_seek", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Int, 1 }, { CloudABIWhence, 2 } } }, { .name = "cloudabi_sys_fd_stat_get", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { CloudABIFDStat | OUT, 1 } } }, { .name = "cloudabi_sys_fd_stat_put", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { CloudABIFDStat | IN, 1 }, { ClouduABIFDSFlags, 2 } } }, { .name = "cloudabi_sys_fd_sync", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "cloudabi_sys_file_advise", .ret_type = 1, .nargs = 4, .args = { { Int, 0 }, { Int, 1 }, { Int, 2 }, { CloudABIAdvice, 3 } } }, { .name = "cloudabi_sys_file_allocate", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Int, 1 }, { Int, 2 } } }, { .name = "cloudabi_sys_file_create", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { BinString | IN, 1 }, { CloudABIFileType, 3 } } }, { .name = "cloudabi_sys_file_link", .ret_type = 1, .nargs = 4, .args = { { CloudABILookup, 0 }, { BinString | IN, 1 }, { Int, 3 }, { BinString | IN, 4 } } }, { .name = "cloudabi_sys_file_open", .ret_type = 1, .nargs = 4, .args = { { Int, 0 }, { BinString | IN, 1 }, { CloudABIOFlags, 3 }, { CloudABIFDStat | IN, 4 } } }, { .name = "cloudabi_sys_file_readdir", .ret_type = 1, .nargs = 4, .args = { { Int, 0 }, { BinString | OUT, 1 }, { Int, 2 }, { Int, 3 } } }, { .name = "cloudabi_sys_file_readlink", .ret_type = 1, .nargs = 4, .args = { { Int, 0 }, { BinString | IN, 1 }, { BinString | OUT, 3 }, { Int, 4 } } }, { .name = "cloudabi_sys_file_rename", .ret_type = 1, .nargs = 4, .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 3 }, { BinString | IN, 4 } } }, { .name = "cloudabi_sys_file_stat_fget", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { CloudABIFileStat | OUT, 1 } } }, { .name = "cloudabi_sys_file_stat_fput", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { CloudABIFileStat | IN, 1 }, { CloudABIFSFlags, 2 } } }, { .name = "cloudabi_sys_file_stat_get", .ret_type = 1, .nargs = 3, .args = { { CloudABILookup, 0 }, { BinString | IN, 1 }, { CloudABIFileStat | OUT, 3 } } }, { .name = "cloudabi_sys_file_stat_put", .ret_type = 1, .nargs = 4, .args = { { CloudABILookup, 0 }, { BinString | IN, 1 }, { CloudABIFileStat | IN, 3 }, { CloudABIFSFlags, 4 } } }, { .name = "cloudabi_sys_file_symlink", .ret_type = 1, .nargs = 3, .args = { { BinString | IN, 0 }, { Int, 2 }, { BinString | IN, 3 } } }, { .name = "cloudabi_sys_file_unlink", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { BinString | IN, 1 }, { CloudABIULFlags, 3 } } }, { .name = "cloudabi_sys_lock_unlock", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { CloudABIMFlags, 1 } } }, { .name = "cloudabi_sys_mem_advise", .ret_type = 1, .nargs = 3, .args = { { Ptr, 0 }, { Int, 1 }, { CloudABIAdvice, 2 } } }, { .name = "cloudabi_sys_mem_lock", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { Int, 1 } } }, { .name = "cloudabi_sys_mem_map", .ret_type = 1, .nargs = 6, .args = { { Ptr, 0 }, { Int, 1 }, { CloudABIMProt, 2 }, { CloudABIMFlags, 3 }, { Int, 4 }, { Int, 5 } } }, { .name = "cloudabi_sys_mem_protect", .ret_type = 1, .nargs = 3, .args = { { Ptr, 0 }, { Int, 1 }, { CloudABIMProt, 2 } } }, { .name = "cloudabi_sys_mem_sync", .ret_type = 1, .nargs = 3, .args = { { Ptr, 0 }, { Int, 1 }, { CloudABIMSFlags, 2 } } }, { .name = "cloudabi_sys_mem_unlock", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { Int, 1 } } }, { .name = "cloudabi_sys_mem_unmap", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { Int, 1 } } }, { .name = "cloudabi_sys_proc_exec", .ret_type = 1, .nargs = 5, .args = { { Int, 0 }, { BinString | IN, 1 }, { Int, 2 }, { IntArray, 3 }, { Int, 4 } } }, { .name = "cloudabi_sys_proc_exit", .ret_type = 1, .nargs = 1, .args = { { Int, 0 } } }, { .name = "cloudabi_sys_proc_fork", .ret_type = 1, .nargs = 0 }, { .name = "cloudabi_sys_proc_raise", .ret_type = 1, .nargs = 1, .args = { { CloudABISignal, 0 } } }, { .name = "cloudabi_sys_random_get", .ret_type = 1, .nargs = 2, .args = { { BinString | OUT, 0 }, { Int, 1 } } }, { .name = "cloudabi_sys_sock_accept", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { CloudABISockStat | OUT, 1 } } }, { .name = "cloudabi_sys_sock_bind", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Int, 1 }, { BinString | IN, 2 } } }, { .name = "cloudabi_sys_sock_connect", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { Int, 1 }, { BinString | IN, 2 } } }, { .name = "cloudabi_sys_sock_listen", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { Int, 1 } } }, { .name = "cloudabi_sys_sock_shutdown", .ret_type = 1, .nargs = 2, .args = { { Int, 0 }, { CloudABISDFlags, 1 } } }, { .name = "cloudabi_sys_sock_stat_get", .ret_type = 1, .nargs = 3, .args = { { Int, 0 }, { CloudABISockStat | OUT, 1 }, { CloudABISSFlags, 2 } } }, { .name = "cloudabi_sys_thread_exit", .ret_type = 1, .nargs = 2, .args = { { Ptr, 0 }, { CloudABIMFlags, 1 } } }, { .name = "cloudabi_sys_thread_tcb_set", .ret_type = 1, .nargs = 1, .args = { { Ptr, 0 } } }, { .name = "cloudabi_sys_thread_yield", .ret_type = 1, .nargs = 0 }, { .name = 0 }, }; static STAILQ_HEAD(, syscall) syscalls; /* Xlat idea taken from strace */ struct xlat { int val; const char *str; }; #define X(a) { a, #a }, #define XEND { 0, NULL } static struct xlat kevent_filters[] = { X(EVFILT_READ) X(EVFILT_WRITE) X(EVFILT_AIO) X(EVFILT_VNODE) X(EVFILT_PROC) X(EVFILT_SIGNAL) X(EVFILT_TIMER) X(EVFILT_PROCDESC) X(EVFILT_FS) X(EVFILT_LIO) X(EVFILT_USER) X(EVFILT_SENDFILE) XEND }; static struct xlat kevent_flags[] = { X(EV_ADD) X(EV_DELETE) X(EV_ENABLE) X(EV_DISABLE) X(EV_ONESHOT) X(EV_CLEAR) X(EV_RECEIPT) X(EV_DISPATCH) X(EV_FORCEONESHOT) X(EV_DROP) X(EV_FLAG1) X(EV_ERROR) X(EV_EOF) XEND }; static struct xlat kevent_user_ffctrl[] = { X(NOTE_FFNOP) X(NOTE_FFAND) X(NOTE_FFOR) X(NOTE_FFCOPY) XEND }; static struct xlat kevent_rdwr_fflags[] = { X(NOTE_LOWAT) X(NOTE_FILE_POLL) XEND }; static struct xlat kevent_vnode_fflags[] = { X(NOTE_DELETE) X(NOTE_WRITE) X(NOTE_EXTEND) X(NOTE_ATTRIB) X(NOTE_LINK) X(NOTE_RENAME) X(NOTE_REVOKE) XEND }; static struct xlat kevent_proc_fflags[] = { X(NOTE_EXIT) X(NOTE_FORK) X(NOTE_EXEC) X(NOTE_TRACK) X(NOTE_TRACKERR) X(NOTE_CHILD) XEND }; static struct xlat kevent_timer_fflags[] = { X(NOTE_SECONDS) X(NOTE_MSECONDS) X(NOTE_USECONDS) X(NOTE_NSECONDS) XEND }; static struct xlat poll_flags[] = { X(POLLSTANDARD) X(POLLIN) X(POLLPRI) X(POLLOUT) X(POLLERR) X(POLLHUP) X(POLLNVAL) X(POLLRDNORM) X(POLLRDBAND) X(POLLWRBAND) X(POLLINIGNEOF) XEND }; static struct xlat mmap_flags[] = { X(MAP_SHARED) X(MAP_PRIVATE) X(MAP_FIXED) X(MAP_RESERVED0020) X(MAP_RESERVED0040) X(MAP_RESERVED0080) X(MAP_RESERVED0100) X(MAP_HASSEMAPHORE) X(MAP_STACK) X(MAP_NOSYNC) X(MAP_ANON) X(MAP_EXCL) X(MAP_NOCORE) X(MAP_PREFAULT_READ) #ifdef MAP_32BIT X(MAP_32BIT) #endif XEND }; static struct xlat mprot_flags[] = { X(PROT_NONE) X(PROT_READ) X(PROT_WRITE) X(PROT_EXEC) XEND }; static struct xlat whence_arg[] = { X(SEEK_SET) X(SEEK_CUR) X(SEEK_END) X(SEEK_DATA) X(SEEK_HOLE) XEND }; static struct xlat sigaction_flags[] = { X(SA_ONSTACK) X(SA_RESTART) X(SA_RESETHAND) X(SA_NOCLDSTOP) X(SA_NODEFER) X(SA_NOCLDWAIT) X(SA_SIGINFO) XEND }; static struct xlat fcntl_arg[] = { X(F_DUPFD) X(F_GETFD) X(F_SETFD) X(F_GETFL) X(F_SETFL) X(F_GETOWN) X(F_SETOWN) X(F_OGETLK) X(F_OSETLK) X(F_OSETLKW) X(F_DUP2FD) X(F_GETLK) X(F_SETLK) X(F_SETLKW) X(F_SETLK_REMOTE) X(F_READAHEAD) X(F_RDAHEAD) X(F_DUPFD_CLOEXEC) X(F_DUP2FD_CLOEXEC) XEND }; static struct xlat fcntlfd_arg[] = { X(FD_CLOEXEC) XEND }; static struct xlat fcntlfl_arg[] = { X(O_APPEND) X(O_ASYNC) X(O_FSYNC) X(O_NONBLOCK) X(O_NOFOLLOW) X(FRDAHEAD) X(O_DIRECT) XEND }; static struct xlat sockdomain_arg[] = { X(PF_UNSPEC) X(PF_LOCAL) X(PF_UNIX) X(PF_INET) X(PF_IMPLINK) X(PF_PUP) X(PF_CHAOS) X(PF_NETBIOS) X(PF_ISO) X(PF_OSI) X(PF_ECMA) X(PF_DATAKIT) X(PF_CCITT) X(PF_SNA) X(PF_DECnet) X(PF_DLI) X(PF_LAT) X(PF_HYLINK) X(PF_APPLETALK) X(PF_ROUTE) X(PF_LINK) X(PF_XTP) X(PF_COIP) X(PF_CNT) X(PF_SIP) X(PF_IPX) X(PF_RTIP) X(PF_PIP) X(PF_ISDN) X(PF_KEY) X(PF_INET6) X(PF_NATM) X(PF_ATM) X(PF_NETGRAPH) X(PF_SLOW) X(PF_SCLUSTER) X(PF_ARP) X(PF_BLUETOOTH) X(PF_IEEE80211) X(PF_INET_SDP) X(PF_INET6_SDP) XEND }; static struct xlat socktype_arg[] = { X(SOCK_STREAM) X(SOCK_DGRAM) X(SOCK_RAW) X(SOCK_RDM) X(SOCK_SEQPACKET) XEND }; static struct xlat open_flags[] = { X(O_RDONLY) X(O_WRONLY) X(O_RDWR) X(O_ACCMODE) X(O_NONBLOCK) X(O_APPEND) X(O_SHLOCK) X(O_EXLOCK) X(O_ASYNC) X(O_FSYNC) X(O_NOFOLLOW) X(O_CREAT) X(O_TRUNC) X(O_EXCL) X(O_NOCTTY) X(O_DIRECT) X(O_DIRECTORY) X(O_EXEC) X(O_TTY_INIT) X(O_CLOEXEC) X(O_VERIFY) XEND }; static struct xlat shutdown_arg[] = { X(SHUT_RD) X(SHUT_WR) X(SHUT_RDWR) XEND }; static struct xlat resource_arg[] = { X(RLIMIT_CPU) X(RLIMIT_FSIZE) X(RLIMIT_DATA) X(RLIMIT_STACK) X(RLIMIT_CORE) X(RLIMIT_RSS) X(RLIMIT_MEMLOCK) X(RLIMIT_NPROC) X(RLIMIT_NOFILE) X(RLIMIT_SBSIZE) X(RLIMIT_VMEM) X(RLIMIT_NPTS) X(RLIMIT_SWAP) X(RLIMIT_KQUEUES) XEND }; static struct xlat pathconf_arg[] = { X(_PC_LINK_MAX) X(_PC_MAX_CANON) X(_PC_MAX_INPUT) X(_PC_NAME_MAX) X(_PC_PATH_MAX) X(_PC_PIPE_BUF) X(_PC_CHOWN_RESTRICTED) X(_PC_NO_TRUNC) X(_PC_VDISABLE) X(_PC_ASYNC_IO) X(_PC_PRIO_IO) X(_PC_SYNC_IO) X(_PC_ALLOC_SIZE_MIN) X(_PC_FILESIZEBITS) X(_PC_REC_INCR_XFER_SIZE) X(_PC_REC_MAX_XFER_SIZE) X(_PC_REC_MIN_XFER_SIZE) X(_PC_REC_XFER_ALIGN) X(_PC_SYMLINK_MAX) X(_PC_ACL_EXTENDED) X(_PC_ACL_PATH_MAX) X(_PC_CAP_PRESENT) X(_PC_INF_PRESENT) X(_PC_MAC_PRESENT) X(_PC_ACL_NFS4) X(_PC_MIN_HOLE_SIZE) XEND }; static struct xlat rfork_flags[] = { X(RFFDG) X(RFPROC) X(RFMEM) X(RFNOWAIT) X(RFCFDG) X(RFTHREAD) X(RFSIGSHARE) X(RFLINUXTHPN) X(RFTSIGZMB) X(RFPPWAIT) XEND }; static struct xlat wait_options[] = { X(WNOHANG) X(WUNTRACED) X(WCONTINUED) X(WNOWAIT) X(WEXITED) X(WTRAPPED) XEND }; static struct xlat idtype_arg[] = { X(P_PID) X(P_PPID) X(P_PGID) X(P_SID) X(P_CID) X(P_UID) X(P_GID) X(P_ALL) X(P_LWPID) X(P_TASKID) X(P_PROJID) X(P_POOLID) X(P_JAILID) X(P_CTID) X(P_CPUID) X(P_PSETID) XEND }; static struct xlat procctl_arg[] = { X(PROC_SPROTECT) X(PROC_REAP_ACQUIRE) X(PROC_REAP_RELEASE) X(PROC_REAP_STATUS) X(PROC_REAP_GETPIDS) X(PROC_REAP_KILL) X(PROC_TRACE_CTL) X(PROC_TRACE_STATUS) XEND }; static struct xlat umtx_ops[] = { X(UMTX_OP_RESERVED0) X(UMTX_OP_RESERVED1) X(UMTX_OP_WAIT) X(UMTX_OP_WAKE) X(UMTX_OP_MUTEX_TRYLOCK) X(UMTX_OP_MUTEX_LOCK) X(UMTX_OP_MUTEX_UNLOCK) X(UMTX_OP_SET_CEILING) X(UMTX_OP_CV_WAIT) X(UMTX_OP_CV_SIGNAL) X(UMTX_OP_CV_BROADCAST) X(UMTX_OP_WAIT_UINT) X(UMTX_OP_RW_RDLOCK) X(UMTX_OP_RW_WRLOCK) X(UMTX_OP_RW_UNLOCK) X(UMTX_OP_WAIT_UINT_PRIVATE) X(UMTX_OP_WAKE_PRIVATE) X(UMTX_OP_MUTEX_WAIT) X(UMTX_OP_MUTEX_WAKE) X(UMTX_OP_SEM_WAIT) X(UMTX_OP_SEM_WAKE) X(UMTX_OP_NWAKE_PRIVATE) X(UMTX_OP_MUTEX_WAKE2) X(UMTX_OP_SEM2_WAIT) X(UMTX_OP_SEM2_WAKE) XEND }; static struct xlat at_flags[] = { X(AT_EACCESS) X(AT_SYMLINK_NOFOLLOW) X(AT_SYMLINK_FOLLOW) X(AT_REMOVEDIR) XEND }; static struct xlat access_modes[] = { X(R_OK) X(W_OK) X(X_OK) XEND }; static struct xlat sysarch_ops[] = { #if defined(__i386__) || defined(__amd64__) X(I386_GET_LDT) X(I386_SET_LDT) X(I386_GET_IOPERM) X(I386_SET_IOPERM) X(I386_VM86) X(I386_GET_FSBASE) X(I386_SET_FSBASE) X(I386_GET_GSBASE) X(I386_SET_GSBASE) X(I386_GET_XFPUSTATE) X(AMD64_GET_FSBASE) X(AMD64_SET_FSBASE) X(AMD64_GET_GSBASE) X(AMD64_SET_GSBASE) X(AMD64_GET_XFPUSTATE) #endif XEND }; static struct xlat linux_socketcall_ops[] = { X(LINUX_SOCKET) X(LINUX_BIND) X(LINUX_CONNECT) X(LINUX_LISTEN) X(LINUX_ACCEPT) X(LINUX_GETSOCKNAME) X(LINUX_GETPEERNAME) X(LINUX_SOCKETPAIR) X(LINUX_SEND) X(LINUX_RECV) X(LINUX_SENDTO) X(LINUX_RECVFROM) X(LINUX_SHUTDOWN) X(LINUX_SETSOCKOPT) X(LINUX_GETSOCKOPT) X(LINUX_SENDMSG) X(LINUX_RECVMSG) XEND }; static struct xlat sigprocmask_ops[] = { X(SIG_BLOCK) X(SIG_UNBLOCK) X(SIG_SETMASK) XEND }; #undef X #define X(a) { CLOUDABI_##a, #a }, static struct xlat cloudabi_advice[] = { X(ADVICE_DONTNEED) X(ADVICE_NOREUSE) X(ADVICE_NORMAL) X(ADVICE_RANDOM) X(ADVICE_SEQUENTIAL) X(ADVICE_WILLNEED) XEND }; static struct xlat cloudabi_clockid[] = { X(CLOCK_MONOTONIC) X(CLOCK_PROCESS_CPUTIME_ID) X(CLOCK_REALTIME) X(CLOCK_THREAD_CPUTIME_ID) XEND }; static struct xlat cloudabi_errno[] = { X(E2BIG) X(EACCES) X(EADDRINUSE) X(EADDRNOTAVAIL) X(EAFNOSUPPORT) X(EAGAIN) X(EALREADY) X(EBADF) X(EBADMSG) X(EBUSY) X(ECANCELED) X(ECHILD) X(ECONNABORTED) X(ECONNREFUSED) X(ECONNRESET) X(EDEADLK) X(EDESTADDRREQ) X(EDOM) X(EDQUOT) X(EEXIST) X(EFAULT) X(EFBIG) X(EHOSTUNREACH) X(EIDRM) X(EILSEQ) X(EINPROGRESS) X(EINTR) X(EINVAL) X(EIO) X(EISCONN) X(EISDIR) X(ELOOP) X(EMFILE) X(EMLINK) X(EMSGSIZE) X(EMULTIHOP) X(ENAMETOOLONG) X(ENETDOWN) X(ENETRESET) X(ENETUNREACH) X(ENFILE) X(ENOBUFS) X(ENODEV) X(ENOENT) X(ENOEXEC) X(ENOLCK) X(ENOLINK) X(ENOMEM) X(ENOMSG) X(ENOPROTOOPT) X(ENOSPC) X(ENOSYS) X(ENOTCONN) X(ENOTDIR) X(ENOTEMPTY) X(ENOTRECOVERABLE) X(ENOTSOCK) X(ENOTSUP) X(ENOTTY) X(ENXIO) X(EOVERFLOW) X(EOWNERDEAD) X(EPERM) X(EPIPE) X(EPROTO) X(EPROTONOSUPPORT) X(EPROTOTYPE) X(ERANGE) X(EROFS) X(ESPIPE) X(ESRCH) X(ESTALE) X(ETIMEDOUT) X(ETXTBSY) X(EXDEV) X(ENOTCAPABLE) XEND }; static struct xlat cloudabi_fdflags[] = { X(FDFLAG_APPEND) X(FDFLAG_DSYNC) X(FDFLAG_NONBLOCK) X(FDFLAG_RSYNC) X(FDFLAG_SYNC) XEND }; static struct xlat cloudabi_fdsflags[] = { X(FDSTAT_FLAGS) X(FDSTAT_RIGHTS) XEND }; static struct xlat cloudabi_filetype[] = { X(FILETYPE_UNKNOWN) X(FILETYPE_BLOCK_DEVICE) X(FILETYPE_CHARACTER_DEVICE) X(FILETYPE_DIRECTORY) X(FILETYPE_FIFO) X(FILETYPE_POLL) X(FILETYPE_PROCESS) X(FILETYPE_REGULAR_FILE) X(FILETYPE_SHARED_MEMORY) X(FILETYPE_SOCKET_DGRAM) X(FILETYPE_SOCKET_SEQPACKET) X(FILETYPE_SOCKET_STREAM) X(FILETYPE_SYMBOLIC_LINK) XEND }; static struct xlat cloudabi_fsflags[] = { X(FILESTAT_ATIM) X(FILESTAT_ATIM_NOW) X(FILESTAT_MTIM) X(FILESTAT_MTIM_NOW) X(FILESTAT_SIZE) XEND }; static struct xlat cloudabi_mflags[] = { X(MAP_ANON) X(MAP_FIXED) X(MAP_PRIVATE) X(MAP_SHARED) XEND }; static struct xlat cloudabi_mprot[] = { X(PROT_EXEC) X(PROT_WRITE) X(PROT_READ) XEND }; static struct xlat cloudabi_msflags[] = { X(MS_ASYNC) X(MS_INVALIDATE) X(MS_SYNC) XEND }; static struct xlat cloudabi_oflags[] = { X(O_CREAT) X(O_DIRECTORY) X(O_EXCL) X(O_TRUNC) XEND }; static struct xlat cloudabi_sa_family[] = { X(AF_UNSPEC) X(AF_INET) X(AF_INET6) X(AF_UNIX) XEND }; static struct xlat cloudabi_sdflags[] = { X(SHUT_RD) X(SHUT_WR) XEND }; static struct xlat cloudabi_signal[] = { X(SIGABRT) X(SIGALRM) X(SIGBUS) X(SIGCHLD) X(SIGCONT) X(SIGFPE) X(SIGHUP) X(SIGILL) X(SIGINT) X(SIGKILL) X(SIGPIPE) X(SIGQUIT) X(SIGSEGV) X(SIGSTOP) X(SIGSYS) X(SIGTERM) X(SIGTRAP) X(SIGTSTP) X(SIGTTIN) X(SIGTTOU) X(SIGURG) X(SIGUSR1) X(SIGUSR2) X(SIGVTALRM) X(SIGXCPU) X(SIGXFSZ) XEND }; static struct xlat cloudabi_ssflags[] = { X(SOCKSTAT_CLEAR_ERROR) XEND }; static struct xlat cloudabi_ssstate[] = { X(SOCKSTATE_ACCEPTCONN) XEND }; static struct xlat cloudabi_ulflags[] = { X(UNLINK_REMOVEDIR) XEND }; static struct xlat cloudabi_whence[] = { X(WHENCE_CUR) X(WHENCE_END) X(WHENCE_SET) XEND }; #undef X #undef XEND /* * Searches an xlat array for a value, and returns it if found. Otherwise * return a string representation. */ static const char * lookup(struct xlat *xlat, int val, int base) { static char tmp[16]; for (; xlat->str != NULL; xlat++) if (xlat->val == val) return (xlat->str); switch (base) { case 8: sprintf(tmp, "0%o", val); break; case 16: sprintf(tmp, "0x%x", val); break; case 10: sprintf(tmp, "%u", val); break; default: errx(1,"Unknown lookup base"); break; } return (tmp); } static const char * xlookup(struct xlat *xlat, int val) { return (lookup(xlat, val, 16)); } /* * Searches an xlat array containing bitfield values. Remaining bits * set after removing the known ones are printed at the end: * IN|0x400. */ static char * xlookup_bits(struct xlat *xlat, int val) { int len, rem; static char str[512]; len = 0; rem = val; for (; xlat->str != NULL; xlat++) { if ((xlat->val & rem) == xlat->val) { /* * Don't print the "all-bits-zero" string unless all * bits are really zero. */ if (xlat->val == 0 && val != 0) continue; len += sprintf(str + len, "%s|", xlat->str); rem &= ~(xlat->val); } } /* * If we have leftover bits or didn't match anything, print * the remainder. */ if (rem || len == 0) len += sprintf(str + len, "0x%x", rem); if (len && str[len - 1] == '|') len--; str[len] = 0; return (str); } void init_syscalls(void) { struct syscall *sc; STAILQ_INIT(&syscalls); for (sc = decoded_syscalls; sc->name != NULL; sc++) STAILQ_INSERT_HEAD(&syscalls, sc, entries); } /* * If/when the list gets big, it might be desirable to do it * as a hash table or binary search. */ struct syscall * get_syscall(const char *name, int nargs) { struct syscall *sc; int i; if (name == NULL) return (NULL); STAILQ_FOREACH(sc, &syscalls, entries) if (strcmp(name, sc->name) == 0) return (sc); /* It is unknown. Add it into the list. */ #if DEBUG fprintf(stderr, "unknown syscall %s -- setting args to %d\n", name, nargs); #endif sc = calloc(1, sizeof(struct syscall)); sc->name = strdup(name); sc->ret_type = 1; sc->nargs = nargs; for (i = 0; i < nargs; i++) { sc->args[i].offset = i; /* Treat all unknown arguments as LongHex. */ sc->args[i].type = LongHex; } STAILQ_INSERT_HEAD(&syscalls, sc, entries); return (sc); } /* * Copy a fixed amount of bytes from the process. */ static int get_struct(pid_t pid, void *offset, void *buf, int len) { struct ptrace_io_desc iorequest; iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = offset; iorequest.piod_addr = buf; iorequest.piod_len = len; if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) return (-1); return (0); } #define MAXSIZE 4096 /* * Copy a string from the process. Note that it is * expected to be a C string, but if max is set, it will * only get that much. */ static char * get_string(pid_t pid, void *addr, int max) { struct ptrace_io_desc iorequest; char *buf, *nbuf; size_t offset, size, totalsize; offset = 0; if (max) size = max + 1; else { /* Read up to the end of the current page. */ size = PAGE_SIZE - ((uintptr_t)addr % PAGE_SIZE); if (size > MAXSIZE) size = MAXSIZE; } totalsize = size; buf = malloc(totalsize); if (buf == NULL) return (NULL); for (;;) { iorequest.piod_op = PIOD_READ_D; iorequest.piod_offs = (char *)addr + offset; iorequest.piod_addr = buf + offset; iorequest.piod_len = size; if (ptrace(PT_IO, pid, (caddr_t)&iorequest, 0) < 0) { free(buf); return (NULL); } if (memchr(buf + offset, '\0', size) != NULL) return (buf); offset += size; if (totalsize < MAXSIZE && max == 0) { size = MAXSIZE - totalsize; if (size > PAGE_SIZE) size = PAGE_SIZE; nbuf = realloc(buf, totalsize + size); if (nbuf == NULL) { buf[totalsize - 1] = '\0'; return (buf); } buf = nbuf; totalsize += size; } else { buf[totalsize - 1] = '\0'; return (buf); } } } static char * strsig2(int sig) { static char tmp[sizeof(int) * 3 + 1]; char *ret; ret = strsig(sig); if (ret == NULL) { snprintf(tmp, sizeof(tmp), "%d", sig); ret = tmp; } return (ret); } static void print_kevent(FILE *fp, struct kevent *ke, int input) { switch (ke->filter) { case EVFILT_READ: case EVFILT_WRITE: case EVFILT_VNODE: case EVFILT_PROC: case EVFILT_TIMER: case EVFILT_PROCDESC: fprintf(fp, "%ju", (uintmax_t)ke->ident); break; case EVFILT_SIGNAL: fputs(strsig2(ke->ident), fp); break; default: fprintf(fp, "%p", (void *)ke->ident); } fprintf(fp, ",%s,%s,", xlookup(kevent_filters, ke->filter), xlookup_bits(kevent_flags, ke->flags)); switch (ke->filter) { case EVFILT_READ: case EVFILT_WRITE: fputs(xlookup_bits(kevent_rdwr_fflags, ke->fflags), fp); break; case EVFILT_VNODE: fputs(xlookup_bits(kevent_vnode_fflags, ke->fflags), fp); break; case EVFILT_PROC: case EVFILT_PROCDESC: fputs(xlookup_bits(kevent_proc_fflags, ke->fflags), fp); break; case EVFILT_TIMER: fputs(xlookup_bits(kevent_timer_fflags, ke->fflags), fp); break; case EVFILT_USER: { int ctrl, data; ctrl = ke->fflags & NOTE_FFCTRLMASK; data = ke->fflags & NOTE_FFLAGSMASK; if (input) { fputs(xlookup(kevent_user_ffctrl, ctrl), fp); if (ke->fflags & NOTE_TRIGGER) fputs("|NOTE_TRIGGER", fp); if (data != 0) fprintf(fp, "|%#x", data); } else { fprintf(fp, "%#x", data); } break; } default: fprintf(fp, "%#x", ke->fflags); } fprintf(fp, ",%p,%p", (void *)ke->data, (void *)ke->udata); } static void print_utrace(FILE *fp, void *utrace_addr, size_t len) { unsigned char *utrace_buffer; fprintf(fp, "{ "); if (sysdecode_utrace(fp, utrace_addr, len)) { fprintf(fp, " }"); return; } utrace_buffer = utrace_addr; fprintf(fp, "%zu:", len); while (len--) fprintf(fp, " %02x", *utrace_buffer++); fprintf(fp, " }"); } /* * Converts a syscall argument into a string. Said string is * allocated via malloc(), so needs to be free()'d. sc is * a pointer to the syscall description (see above); args is * an array of all of the system call arguments. */ char * print_arg(struct syscall_args *sc, unsigned long *args, long *retval, struct trussinfo *trussinfo) { FILE *fp; char *tmp; size_t tmplen; pid_t pid; fp = open_memstream(&tmp, &tmplen); pid = trussinfo->curthread->proc->pid; switch (sc->type & ARG_MASK) { case Hex: fprintf(fp, "0x%x", (int)args[sc->offset]); break; case Octal: fprintf(fp, "0%o", (int)args[sc->offset]); break; case Int: fprintf(fp, "%d", (int)args[sc->offset]); break; case UInt: fprintf(fp, "%u", (unsigned int)args[sc->offset]); break; case LongHex: fprintf(fp, "0x%lx", args[sc->offset]); break; case Long: fprintf(fp, "%ld", args[sc->offset]); break; case Name: { /* NULL-terminated string. */ char *tmp2; tmp2 = get_string(pid, (void*)args[sc->offset], 0); fprintf(fp, "\"%s\"", tmp2); free(tmp2); break; } case BinString: { /* * Binary block of data that might have printable characters. * XXX If type|OUT, assume that the length is the syscall's * return value. Otherwise, assume that the length of the block * is in the next syscall argument. */ int max_string = trussinfo->strsize; char tmp2[max_string + 1], *tmp3; int len; int truncated = 0; if (sc->type & OUT) len = retval[0]; else len = args[sc->offset + 1]; /* * Don't print more than max_string characters, to avoid word * wrap. If we have to truncate put some ... after the string. */ if (len > max_string) { len = max_string; truncated = 1; } if (len && get_struct(pid, (void*)args[sc->offset], &tmp2, len) != -1) { tmp3 = malloc(len * 4 + 1); while (len) { if (strvisx(tmp3, tmp2, len, VIS_CSTYLE|VIS_TAB|VIS_NL) <= max_string) break; len--; truncated = 1; - }; + } fprintf(fp, "\"%s\"%s", tmp3, truncated ? "..." : ""); free(tmp3); } else { fprintf(fp, "0x%lx", args[sc->offset]); } break; } case ExecArgs: case ExecEnv: case StringArray: { uintptr_t addr; union { char *strarray[0]; char buf[PAGE_SIZE]; } u; char *string; size_t len; u_int first, i; /* * Only parse argv[] and environment arrays from exec calls * if requested. */ if (((sc->type & ARG_MASK) == ExecArgs && (trussinfo->flags & EXECVEARGS) == 0) || ((sc->type & ARG_MASK) == ExecEnv && (trussinfo->flags & EXECVEENVS) == 0)) { fprintf(fp, "0x%lx", args[sc->offset]); break; } /* * Read a page of pointers at a time. Punt if the top-level * pointer is not aligned. Note that the first read is of * a partial page. */ addr = args[sc->offset]; if (addr % sizeof(char *) != 0) { fprintf(fp, "0x%lx", args[sc->offset]); break; } len = PAGE_SIZE - (addr & PAGE_MASK); if (get_struct(pid, (void *)addr, u.buf, len) == -1) { fprintf(fp, "0x%lx", args[sc->offset]); break; } fputc('[', fp); first = 1; i = 0; while (u.strarray[i] != NULL) { string = get_string(pid, u.strarray[i], 0); fprintf(fp, "%s \"%s\"", first ? "" : ",", string); free(string); first = 0; i++; if (i == len / sizeof(char *)) { addr += len; len = PAGE_SIZE; if (get_struct(pid, (void *)addr, u.buf, len) == -1) { fprintf(fp, ", "); break; } i = 0; } } fputs(" ]", fp); break; } #ifdef __LP64__ case Quad: fprintf(fp, "%ld", args[sc->offset]); break; case QuadHex: fprintf(fp, "0x%lx", args[sc->offset]); break; #else case Quad: case QuadHex: { unsigned long long ll; #if _BYTE_ORDER == _LITTLE_ENDIAN ll = (unsigned long long)args[sc->offset + 1] << 32 | args[sc->offset]; #else ll = (unsigned long long)args[sc->offset] << 32 | args[sc->offset + 1]; #endif if ((sc->type & ARG_MASK) == Quad) fprintf(fp, "%lld", ll); else fprintf(fp, "0x%llx", ll); break; } #endif case Ptr: fprintf(fp, "0x%lx", args[sc->offset]); break; case Readlinkres: { char *tmp2; if (retval[0] == -1) break; tmp2 = get_string(pid, (void*)args[sc->offset], retval[0]); fprintf(fp, "\"%s\"", tmp2); free(tmp2); break; } case Ioctl: { const char *temp; unsigned long cmd; cmd = args[sc->offset]; temp = sysdecode_ioctlname(cmd); if (temp) fputs(temp, fp); else { fprintf(fp, "0x%lx { IO%s%s 0x%lx('%c'), %lu, %lu }", cmd, cmd & IOC_OUT ? "R" : "", cmd & IOC_IN ? "W" : "", IOCGROUP(cmd), isprint(IOCGROUP(cmd)) ? (char)IOCGROUP(cmd) : '?', cmd & 0xFF, IOCPARM_LEN(cmd)); } break; } case Timespec: { struct timespec ts; if (get_struct(pid, (void *)args[sc->offset], &ts, sizeof(ts)) != -1) fprintf(fp, "{ %jd.%09ld }", (intmax_t)ts.tv_sec, ts.tv_nsec); else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Timespec2: { struct timespec ts[2]; const char *sep; unsigned int i; if (get_struct(pid, (void *)args[sc->offset], &ts, sizeof(ts)) != -1) { fputs("{ ", fp); sep = ""; for (i = 0; i < nitems(ts); i++) { fputs(sep, fp); sep = ", "; switch (ts[i].tv_nsec) { case UTIME_NOW: fprintf(fp, "UTIME_NOW"); break; case UTIME_OMIT: fprintf(fp, "UTIME_OMIT"); break; default: fprintf(fp, "%jd.%09ld", (intmax_t)ts[i].tv_sec, ts[i].tv_nsec); break; } } fputs(" }", fp); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Timeval: { struct timeval tv; if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv)) != -1) fprintf(fp, "{ %jd.%06ld }", (intmax_t)tv.tv_sec, tv.tv_usec); else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Timeval2: { struct timeval tv[2]; if (get_struct(pid, (void *)args[sc->offset], &tv, sizeof(tv)) != -1) fprintf(fp, "{ %jd.%06ld, %jd.%06ld }", (intmax_t)tv[0].tv_sec, tv[0].tv_usec, (intmax_t)tv[1].tv_sec, tv[1].tv_usec); else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Itimerval: { struct itimerval itv; if (get_struct(pid, (void *)args[sc->offset], &itv, sizeof(itv)) != -1) fprintf(fp, "{ %jd.%06ld, %jd.%06ld }", (intmax_t)itv.it_interval.tv_sec, itv.it_interval.tv_usec, (intmax_t)itv.it_value.tv_sec, itv.it_value.tv_usec); else fprintf(fp, "0x%lx", args[sc->offset]); break; } case LinuxSockArgs: { struct linux_socketcall_args largs; if (get_struct(pid, (void *)args[sc->offset], (void *)&largs, sizeof(largs)) != -1) fprintf(fp, "{ %s, 0x%lx }", lookup(linux_socketcall_ops, largs.what, 10), (long unsigned int)largs.args); else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Pollfd: { /* * XXX: A Pollfd argument expects the /next/ syscall argument * to be the number of fds in the array. This matches the poll * syscall. */ struct pollfd *pfd; int numfds = args[sc->offset + 1]; size_t bytes = sizeof(struct pollfd) * numfds; int i; if ((pfd = malloc(bytes)) == NULL) err(1, "Cannot malloc %zu bytes for pollfd array", bytes); if (get_struct(pid, (void *)args[sc->offset], pfd, bytes) != -1) { fputs("{", fp); for (i = 0; i < numfds; i++) { fprintf(fp, " %d/%s", pfd[i].fd, xlookup_bits(poll_flags, pfd[i].events)); } fputs(" }", fp); } else { fprintf(fp, "0x%lx", args[sc->offset]); } free(pfd); break; } case Fd_set: { /* * XXX: A Fd_set argument expects the /first/ syscall argument * to be the number of fds in the array. This matches the * select syscall. */ fd_set *fds; int numfds = args[0]; size_t bytes = _howmany(numfds, _NFDBITS) * _NFDBITS; int i; if ((fds = malloc(bytes)) == NULL) err(1, "Cannot malloc %zu bytes for fd_set array", bytes); if (get_struct(pid, (void *)args[sc->offset], fds, bytes) != -1) { fputs("{", fp); for (i = 0; i < numfds; i++) { if (FD_ISSET(i, fds)) fprintf(fp, " %d", i); } fputs(" }", fp); } else fprintf(fp, "0x%lx", args[sc->offset]); free(fds); break; } case Signal: fputs(strsig2(args[sc->offset]), fp); break; case Sigset: { long sig; sigset_t ss; int i, first; sig = args[sc->offset]; if (get_struct(pid, (void *)args[sc->offset], (void *)&ss, sizeof(ss)) == -1) { fprintf(fp, "0x%lx", args[sc->offset]); break; } fputs("{ ", fp); first = 1; for (i = 1; i < sys_nsig; i++) { if (sigismember(&ss, i)) { fprintf(fp, "%s%s", !first ? "|" : "", strsig(i)); first = 0; } } if (!first) fputc(' ', fp); fputc('}', fp); break; } case Sigprocmask: { fputs(xlookup(sigprocmask_ops, args[sc->offset]), fp); break; } case Fcntlflag: { /* XXX: Output depends on the value of the previous argument. */ switch (args[sc->offset - 1]) { case F_SETFD: fputs(xlookup_bits(fcntlfd_arg, args[sc->offset]), fp); break; case F_SETFL: fputs(xlookup_bits(fcntlfl_arg, args[sc->offset]), fp); break; case F_GETFD: case F_GETFL: case F_GETOWN: break; default: fprintf(fp, "0x%lx", args[sc->offset]); break; } break; } case Open: fputs(xlookup_bits(open_flags, args[sc->offset]), fp); break; case Fcntl: fputs(xlookup(fcntl_arg, args[sc->offset]), fp); break; case Mprot: fputs(xlookup_bits(mprot_flags, args[sc->offset]), fp); break; case Mmapflags: { int align, flags; /* * MAP_ALIGNED can't be handled by xlookup_bits(), so * generate that string manually and prepend it to the * string from xlookup_bits(). Have to be careful to * avoid outputting MAP_ALIGNED|0 if MAP_ALIGNED is * the only flag. */ flags = args[sc->offset] & ~MAP_ALIGNMENT_MASK; align = args[sc->offset] & MAP_ALIGNMENT_MASK; if (align != 0) { if (align == MAP_ALIGNED_SUPER) fputs("MAP_ALIGNED_SUPER", fp); else fprintf(fp, "MAP_ALIGNED(%d)", align >> MAP_ALIGNMENT_SHIFT); if (flags == 0) break; fputc('|', fp); } fputs(xlookup_bits(mmap_flags, flags), fp); break; } case Whence: fputs(xlookup(whence_arg, args[sc->offset]), fp); break; case Sockdomain: fputs(xlookup(sockdomain_arg, args[sc->offset]), fp); break; case Socktype: { int type, flags; flags = args[sc->offset] & (SOCK_CLOEXEC | SOCK_NONBLOCK); type = args[sc->offset] & ~flags; fputs(xlookup(socktype_arg, type), fp); if (flags & SOCK_CLOEXEC) fprintf(fp, "|SOCK_CLOEXEC"); if (flags & SOCK_NONBLOCK) fprintf(fp, "|SOCK_NONBLOCK"); break; } case Shutdown: fputs(xlookup(shutdown_arg, args[sc->offset]), fp); break; case Resource: fputs(xlookup(resource_arg, args[sc->offset]), fp); break; case Pathconf: fputs(xlookup(pathconf_arg, args[sc->offset]), fp); break; case Rforkflags: fputs(xlookup_bits(rfork_flags, args[sc->offset]), fp); break; case Sockaddr: { char addr[64]; struct sockaddr_in *lsin; struct sockaddr_in6 *lsin6; struct sockaddr_un *sun; struct sockaddr *sa; socklen_t len; u_char *q; if (args[sc->offset] == 0) { fputs("NULL", fp); break; } /* * Extract the address length from the next argument. If * this is an output sockaddr (OUT is set), then the * next argument is a pointer to a socklen_t. Otherwise * the next argument contains a socklen_t by value. */ if (sc->type & OUT) { if (get_struct(pid, (void *)args[sc->offset + 1], &len, sizeof(len)) == -1) { fprintf(fp, "0x%lx", args[sc->offset]); break; } } else len = args[sc->offset + 1]; /* If the length is too small, just bail. */ if (len < sizeof(*sa)) { fprintf(fp, "0x%lx", args[sc->offset]); break; } sa = calloc(1, len); if (get_struct(pid, (void *)args[sc->offset], sa, len) == -1) { free(sa); fprintf(fp, "0x%lx", args[sc->offset]); break; } switch (sa->sa_family) { case AF_INET: if (len < sizeof(*lsin)) goto sockaddr_short; lsin = (struct sockaddr_in *)(void *)sa; inet_ntop(AF_INET, &lsin->sin_addr, addr, sizeof(addr)); fprintf(fp, "{ AF_INET %s:%d }", addr, htons(lsin->sin_port)); break; case AF_INET6: if (len < sizeof(*lsin6)) goto sockaddr_short; lsin6 = (struct sockaddr_in6 *)(void *)sa; inet_ntop(AF_INET6, &lsin6->sin6_addr, addr, sizeof(addr)); fprintf(fp, "{ AF_INET6 [%s]:%d }", addr, htons(lsin6->sin6_port)); break; case AF_UNIX: sun = (struct sockaddr_un *)sa; fprintf(fp, "{ AF_UNIX \"%.*s\" }", (int)(len - offsetof(struct sockaddr_un, sun_path)), sun->sun_path); break; default: sockaddr_short: fprintf(fp, "{ sa_len = %d, sa_family = %d, sa_data = {", (int)sa->sa_len, (int)sa->sa_family); for (q = (u_char *)sa->sa_data; q < (u_char *)sa + len; q++) fprintf(fp, "%s 0x%02x", q == (u_char *)sa->sa_data ? "" : ",", *q); fputs(" } }", fp); } free(sa); break; } case Sigaction: { struct sigaction sa; if (get_struct(pid, (void *)args[sc->offset], &sa, sizeof(sa)) != -1) { fputs("{ ", fp); if (sa.sa_handler == SIG_DFL) fputs("SIG_DFL", fp); else if (sa.sa_handler == SIG_IGN) fputs("SIG_IGN", fp); else fprintf(fp, "%p", sa.sa_handler); fprintf(fp, " %s ss_t }", xlookup_bits(sigaction_flags, sa.sa_flags)); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Kevent: { /* * XXX XXX: The size of the array is determined by either the * next syscall argument, or by the syscall return value, * depending on which argument number we are. This matches the * kevent syscall, but luckily that's the only syscall that uses * them. */ struct kevent *ke; int numevents = -1; size_t bytes; int i; if (sc->offset == 1) numevents = args[sc->offset+1]; else if (sc->offset == 3 && retval[0] != -1) numevents = retval[0]; if (numevents >= 0) { bytes = sizeof(struct kevent) * numevents; if ((ke = malloc(bytes)) == NULL) err(1, "Cannot malloc %zu bytes for kevent array", bytes); } else ke = NULL; if (numevents >= 0 && get_struct(pid, (void *)args[sc->offset], ke, bytes) != -1) { fputc('{', fp); for (i = 0; i < numevents; i++) { fputc(' ', fp); print_kevent(fp, &ke[i], sc->offset == 1); } fputs(" }", fp); } else { fprintf(fp, "0x%lx", args[sc->offset]); } free(ke); break; } case Stat: { struct stat st; if (get_struct(pid, (void *)args[sc->offset], &st, sizeof(st)) != -1) { char mode[12]; strmode(st.st_mode, mode); fprintf(fp, "{ mode=%s,inode=%ju,size=%jd,blksize=%ld }", mode, (uintmax_t)st.st_ino, (intmax_t)st.st_size, (long)st.st_blksize); } else { fprintf(fp, "0x%lx", args[sc->offset]); } break; } case StatFs: { unsigned int i; struct statfs buf; if (get_struct(pid, (void *)args[sc->offset], &buf, sizeof(buf)) != -1) { char fsid[17]; bzero(fsid, sizeof(fsid)); if (buf.f_fsid.val[0] != 0 || buf.f_fsid.val[1] != 0) { for (i = 0; i < sizeof(buf.f_fsid); i++) snprintf(&fsid[i*2], sizeof(fsid) - (i*2), "%02x", ((u_char *)&buf.f_fsid)[i]); } fprintf(fp, "{ fstypename=%s,mntonname=%s,mntfromname=%s," "fsid=%s }", buf.f_fstypename, buf.f_mntonname, buf.f_mntfromname, fsid); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Rusage: { struct rusage ru; if (get_struct(pid, (void *)args[sc->offset], &ru, sizeof(ru)) != -1) { fprintf(fp, "{ u=%jd.%06ld,s=%jd.%06ld,in=%ld,out=%ld }", (intmax_t)ru.ru_utime.tv_sec, ru.ru_utime.tv_usec, (intmax_t)ru.ru_stime.tv_sec, ru.ru_stime.tv_usec, ru.ru_inblock, ru.ru_oublock); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Rlimit: { struct rlimit rl; if (get_struct(pid, (void *)args[sc->offset], &rl, sizeof(rl)) != -1) { fprintf(fp, "{ cur=%ju,max=%ju }", rl.rlim_cur, rl.rlim_max); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case ExitStatus: { int status; if (get_struct(pid, (void *)args[sc->offset], &status, sizeof(status)) != -1) { fputs("{ ", fp); if (WIFCONTINUED(status)) fputs("CONTINUED", fp); else if (WIFEXITED(status)) fprintf(fp, "EXITED,val=%d", WEXITSTATUS(status)); else if (WIFSIGNALED(status)) fprintf(fp, "SIGNALED,sig=%s%s", strsig2(WTERMSIG(status)), WCOREDUMP(status) ? ",cored" : ""); else fprintf(fp, "STOPPED,sig=%s", strsig2(WTERMSIG(status))); fputs(" }", fp); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case Waitoptions: fputs(xlookup_bits(wait_options, args[sc->offset]), fp); break; case Idtype: fputs(xlookup(idtype_arg, args[sc->offset]), fp); break; case Procctl: fputs(xlookup(procctl_arg, args[sc->offset]), fp); break; case Umtxop: fputs(xlookup(umtx_ops, args[sc->offset]), fp); break; case Atfd: if ((int)args[sc->offset] == AT_FDCWD) fputs("AT_FDCWD", fp); else fprintf(fp, "%d", (int)args[sc->offset]); break; case Atflags: fputs(xlookup_bits(at_flags, args[sc->offset]), fp); break; case Accessmode: if (args[sc->offset] == F_OK) fputs("F_OK", fp); else fputs(xlookup_bits(access_modes, args[sc->offset]), fp); break; case Sysarch: fputs(xlookup(sysarch_ops, args[sc->offset]), fp); break; case PipeFds: /* * The pipe() system call in the kernel returns its * two file descriptors via return values. However, * the interface exposed by libc is that pipe() * accepts a pointer to an array of descriptors. * Format the output to match the libc API by printing * the returned file descriptors as a fake argument. * * Overwrite the first retval to signal a successful * return as well. */ fprintf(fp, "{ %ld, %ld }", retval[0], retval[1]); retval[0] = 0; break; case Utrace: { size_t len; void *utrace_addr; len = args[sc->offset + 1]; utrace_addr = calloc(1, len); if (get_struct(pid, (void *)args[sc->offset], (void *)utrace_addr, len) != -1) print_utrace(fp, utrace_addr, len); else fprintf(fp, "0x%lx", args[sc->offset]); free(utrace_addr); break; } case IntArray: { int descriptors[16]; unsigned long i, ndescriptors; bool truncated; ndescriptors = args[sc->offset + 1]; truncated = false; if (ndescriptors > nitems(descriptors)) { ndescriptors = nitems(descriptors); truncated = true; } if (get_struct(pid, (void *)args[sc->offset], descriptors, ndescriptors * sizeof(descriptors[0])) != -1) { fprintf(fp, "{"); for (i = 0; i < ndescriptors; i++) fprintf(fp, i == 0 ? " %d" : ", %d", descriptors[i]); fprintf(fp, truncated ? ", ... }" : " }"); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case CloudABIAdvice: fputs(xlookup(cloudabi_advice, args[sc->offset]), fp); break; case CloudABIClockID: fputs(xlookup(cloudabi_clockid, args[sc->offset]), fp); break; case ClouduABIFDSFlags: fputs(xlookup_bits(cloudabi_fdsflags, args[sc->offset]), fp); break; case CloudABIFDStat: { cloudabi_fdstat_t fds; if (get_struct(pid, (void *)args[sc->offset], &fds, sizeof(fds)) != -1) { fprintf(fp, "{ %s, ", xlookup(cloudabi_filetype, fds.fs_filetype)); fprintf(fp, "%s, ... }", xlookup_bits(cloudabi_fdflags, fds.fs_flags)); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case CloudABIFileStat: { cloudabi_filestat_t fsb; if (get_struct(pid, (void *)args[sc->offset], &fsb, sizeof(fsb)) != -1) fprintf(fp, "{ %s, %lu }", xlookup(cloudabi_filetype, fsb.st_filetype), fsb.st_size); else fprintf(fp, "0x%lx", args[sc->offset]); break; } case CloudABIFileType: fputs(xlookup(cloudabi_filetype, args[sc->offset]), fp); break; case CloudABIFSFlags: fputs(xlookup_bits(cloudabi_fsflags, args[sc->offset]), fp); break; case CloudABILookup: if ((args[sc->offset] & CLOUDABI_LOOKUP_SYMLINK_FOLLOW) != 0) fprintf(fp, "%d|LOOKUP_SYMLINK_FOLLOW", (int)args[sc->offset]); else fprintf(fp, "%d", (int)args[sc->offset]); break; case CloudABIMFlags: fputs(xlookup_bits(cloudabi_mflags, args[sc->offset]), fp); break; case CloudABIMProt: fputs(xlookup_bits(cloudabi_mprot, args[sc->offset]), fp); break; case CloudABIMSFlags: fputs(xlookup_bits(cloudabi_msflags, args[sc->offset]), fp); break; case CloudABIOFlags: fputs(xlookup_bits(cloudabi_oflags, args[sc->offset]), fp); break; case CloudABISDFlags: fputs(xlookup_bits(cloudabi_sdflags, args[sc->offset]), fp); break; case CloudABISignal: fputs(xlookup(cloudabi_signal, args[sc->offset]), fp); break; case CloudABISockStat: { cloudabi_sockstat_t ss; if (get_struct(pid, (void *)args[sc->offset], &ss, sizeof(ss)) != -1) { fprintf(fp, "{ %s, ", xlookup( cloudabi_sa_family, ss.ss_sockname.sa_family)); fprintf(fp, "%s, ", xlookup( cloudabi_sa_family, ss.ss_peername.sa_family)); fprintf(fp, "%s, ", xlookup( cloudabi_errno, ss.ss_error)); fprintf(fp, "%s }", xlookup_bits( cloudabi_ssstate, ss.ss_state)); } else fprintf(fp, "0x%lx", args[sc->offset]); break; } case CloudABISSFlags: fputs(xlookup_bits(cloudabi_ssflags, args[sc->offset]), fp); break; case CloudABITimestamp: fprintf(fp, "%lu.%09lus", args[sc->offset] / 1000000000, args[sc->offset] % 1000000000); break; case CloudABIULFlags: fputs(xlookup_bits(cloudabi_ulflags, args[sc->offset]), fp); break; case CloudABIWhence: fputs(xlookup(cloudabi_whence, args[sc->offset]), fp); break; default: errx(1, "Invalid argument type %d\n", sc->type & ARG_MASK); } fclose(fp); return (tmp); } /* * Print (to outfile) the system call and its arguments. */ void print_syscall(struct trussinfo *trussinfo) { struct threadinfo *t; const char *name; char **s_args; int i, len, nargs; t = trussinfo->curthread; name = t->cs.name; nargs = t->cs.nargs; s_args = t->cs.s_args; len = print_line_prefix(trussinfo); len += fprintf(trussinfo->outfile, "%s(", name); for (i = 0; i < nargs; i++) { if (s_args[i] != NULL) len += fprintf(trussinfo->outfile, "%s", s_args[i]); else len += fprintf(trussinfo->outfile, ""); len += fprintf(trussinfo->outfile, "%s", i < (nargs - 1) ? "," : ""); } len += fprintf(trussinfo->outfile, ")"); for (i = 0; i < 6 - (len / 8); i++) fprintf(trussinfo->outfile, "\t"); } void print_syscall_ret(struct trussinfo *trussinfo, int errorp, long *retval) { struct timespec timediff; struct threadinfo *t; struct syscall *sc; int error; t = trussinfo->curthread; sc = t->cs.sc; if (trussinfo->flags & COUNTONLY) { timespecsubt(&t->after, &t->before, &timediff); timespecadd(&sc->time, &timediff, &sc->time); sc->ncalls++; if (errorp) sc->nerror++; return; } print_syscall(trussinfo); fflush(trussinfo->outfile); if (retval == NULL) { /* * This system call resulted in the current thread's exit, * so there is no return value or error to display. */ fprintf(trussinfo->outfile, "\n"); return; } if (errorp) { error = sysdecode_abi_to_freebsd_errno(t->proc->abi->abi, retval[0]); fprintf(trussinfo->outfile, " ERR#%ld '%s'\n", retval[0], error == INT_MAX ? "Unknown error" : strerror(error)); } #ifndef __LP64__ else if (sc->ret_type == 2) { off_t off; #if _BYTE_ORDER == _LITTLE_ENDIAN off = (off_t)retval[1] << 32 | retval[0]; #else off = (off_t)retval[0] << 32 | retval[1]; #endif fprintf(trussinfo->outfile, " = %jd (0x%jx)\n", (intmax_t)off, (intmax_t)off); } #endif else fprintf(trussinfo->outfile, " = %ld (0x%lx)\n", retval[0], retval[0]); } void print_summary(struct trussinfo *trussinfo) { struct timespec total = {0, 0}; struct syscall *sc; int ncall, nerror; fprintf(trussinfo->outfile, "%-20s%15s%8s%8s\n", "syscall", "seconds", "calls", "errors"); ncall = nerror = 0; STAILQ_FOREACH(sc, &syscalls, entries) if (sc->ncalls) { fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n", sc->name, (intmax_t)sc->time.tv_sec, sc->time.tv_nsec, sc->ncalls, sc->nerror); timespecadd(&total, &sc->time, &total); ncall += sc->ncalls; nerror += sc->nerror; } fprintf(trussinfo->outfile, "%20s%15s%8s%8s\n", "", "-------------", "-------", "-------"); fprintf(trussinfo->outfile, "%-20s%5jd.%09ld%8d%8d\n", "", (intmax_t)total.tv_sec, total.tv_nsec, ncall, nerror); } Index: head/usr.bin/xlint/lint1/func.c =================================================================== --- head/usr.bin/xlint/lint1/func.c (revision 298088) +++ head/usr.bin/xlint/lint1/func.c (revision 298089) @@ -1,1288 +1,1288 @@ /* $NetBSD: func.c,v 1.22 2005/09/24 15:30:35 perry Exp $ */ /* * Copyright (c) 1994, 1995 Jochen Pohl * 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. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Jochen Pohl for * The NetBSD Project. * 4. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``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 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 #if defined(__RCSID) && !defined(lint) __RCSID("$NetBSD: func.c,v 1.16 2002/01/03 04:25:15 thorpej Exp $"); #endif __FBSDID("$FreeBSD$"); #include #include #include "lint1.h" #include "cgram.h" /* * Contains a pointer to the symbol table entry of the current function * definition. */ sym_t *funcsym; /* Is set as long as a statement can be reached. Must be set at level 0. */ int reached = 1; /* * Is set as long as NOTREACHED is in effect. * Is reset everywhere where reached can become 0. */ int rchflg; /* * In conjunction with reached controls printing of "fallthrough on ..." * warnings. * Reset by each statement and set by FALLTHROUGH, switch (switch1()) * and case (label()). * * Control statements if, for, while and switch do not reset ftflg because * this must be done by the controlled statement. At least for if this is * important because ** FALLTHROUGH ** after "if (expr) stmnt" is evaluated * before the following token, which causes reduction of above, is read. * This means that ** FALLTHROUGH ** after "if ..." would always be ignored. */ int ftflg; /* Top element of stack for control statements */ cstk_t *cstk; /* * Number of arguments which will be checked for usage in following * function definition. -1 stands for all arguments. * * The position of the last ARGSUSED comment is stored in aupos. */ int nargusg = -1; pos_t aupos; /* * Number of arguments of the following function definition whose types * shall be checked by lint2. -1 stands for all arguments. * * The position of the last VARARGS comment is stored in vapos. */ int nvararg = -1; pos_t vapos; /* * Both prflstr and scflstrg contain the number of the argument which * shall be used to check the types of remaining arguments (for PRINTFLIKE * and SCANFLIKE). * * prflpos and scflpos are the positions of the last PRINTFLIKE or * SCANFLIKE comment. */ int prflstrg = -1; int scflstrg = -1; pos_t prflpos; pos_t scflpos; /* * Are both plibflg and llibflg set, prototypes are written as function * definitions to the output file. */ int plibflg; /* * Nonzero means that no warnings about constants in conditional * context are printed. */ int ccflg; /* * llibflg is set if a lint library shall be created. The effect of * llibflg is that all defined symbols are treated as used. * (The LINTLIBRARY comment also resets vflag.) */ int llibflg; /* * Nonzero if warnings are suppressed by a LINTED directive */ int nowarn; /* * Nonzero if bitfield type errors are suppressed by a BITFIELDTYPE * directive. */ int bitfieldtype_ok; /* * Nonzero if complaints about use of "long long" are suppressed in * the next statement or declaration. */ int quadflg; /* * Puts a new element at the top of the stack used for control statements. */ void pushctrl(int env) { cstk_t *ci; if ((ci = calloc(1, sizeof (cstk_t))) == NULL) nomem(); ci->c_env = env; ci->c_nxt = cstk; cstk = ci; } /* * Removes the top element of the stack used for control statements. */ void popctrl(int env) { cstk_t *ci; clst_t *cl; if (cstk == NULL || cstk->c_env != env) LERROR("popctrl()"); cstk = (ci = cstk)->c_nxt; while ((cl = ci->c_clst) != NULL) { ci->c_clst = cl->cl_nxt; free(cl); } if (ci->c_swtype != NULL) free(ci->c_swtype); free(ci); } /* * Prints a warning if a statement cannot be reached. */ void chkreach(void) { if (!reached && !rchflg) { /* statement not reached */ warning(193); reached = 1; } } /* * Called after a function declaration which introduces a function definition * and before an (optional) old style argument declaration list. * * Puts all symbols declared in the Prototype or in an old style argument * list back to the symbol table. * * Does the usual checking of storage class, type (return value), * redeclaration etc.. */ void funcdef(sym_t *fsym) { int n, warn; sym_t *arg, *sym, *rdsym; funcsym = fsym; /* * Put all symbols declared in the argument list back to the * symbol table. */ for (sym = dcs->d_fpsyms; sym != NULL; sym = sym->s_dlnxt) { if (sym->s_blklev != -1) { if (sym->s_blklev != 1) LERROR("funcdef()"); inssym(1, sym); } } /* * In osfunc() we did not know whether it is an old style function * definition or only an old style declaration, if there are no * arguments inside the argument list ("f()"). */ if (!fsym->s_type->t_proto && fsym->s_args == NULL) fsym->s_osdef = 1; chktyp(fsym); /* * chktyp() checks for almost all possible errors, but not for * incomplete return values (these are allowed in declarations) */ if (fsym->s_type->t_subt->t_tspec != VOID && incompl(fsym->s_type->t_subt)) { /* cannot return incomplete type */ error(67); } fsym->s_def = DEF; if (fsym->s_scl == TYPEDEF) { fsym->s_scl = EXTERN; /* illegal storage class */ error(8); } if (dcs->d_inline) fsym->s_inline = 1; /* * Arguments in new style function declarations need a name. * (void is already removed from the list of arguments) */ n = 1; for (arg = fsym->s_type->t_args; arg != NULL; arg = arg->s_nxt) { if (arg->s_scl == ABSTRACT) { if (arg->s_name != unnamed) LERROR("funcdef()"); /* formal parameter lacks name: param #%d */ error(59, n); } else { if (arg->s_name == unnamed) LERROR("funcdef()"); } n++; } /* * We must also remember the position. s_dpos is overwritten * if this is an old style definition and we had already a * prototype. */ STRUCT_ASSIGN(dcs->d_fdpos, fsym->s_dpos); if ((rdsym = dcs->d_rdcsym) != NULL) { if (!isredec(fsym, (warn = 0, &warn))) { /* * Print nothing if the newly defined function * is defined in old style. A better warning will * be printed in cluparg(). */ if (warn && !fsym->s_osdef) { /* redeclaration of %s */ (*(sflag ? error : warning))(27, fsym->s_name); prevdecl(-1, rdsym); } /* copy usage information */ cpuinfo(fsym, rdsym); /* * If the old symbol was a prototype and the new * one is none, overtake the position of the * declaration of the prototype. */ if (fsym->s_osdef && rdsym->s_type->t_proto) STRUCT_ASSIGN(fsym->s_dpos, rdsym->s_dpos); /* complete the type */ compltyp(fsym, rdsym); /* once a function is inline it remains inline */ if (rdsym->s_inline) fsym->s_inline = 1; } /* remove the old symbol from the symbol table */ rmsym(rdsym); } if (fsym->s_osdef && !fsym->s_type->t_proto) { if (sflag && hflag && strcmp(fsym->s_name, "main") != 0) /* function definition is not a prototype */ warning(286); } if (dcs->d_notyp) /* return value is implicitly declared to be int */ fsym->s_rimpl = 1; reached = 1; } /* * Called at the end of a function definition. */ void funcend(void) { sym_t *arg; int n; if (reached) { cstk->c_noretval = 1; if (funcsym->s_type->t_subt->t_tspec != VOID && !funcsym->s_rimpl) { /* func. %s falls off bottom without returning value */ warning(217, funcsym->s_name); } } /* * This warning is printed only if the return value was implicitly * declared to be int. Otherwise the wrong return statement * has already printed a warning. */ if (cstk->c_noretval && cstk->c_retval && funcsym->s_rimpl) /* function %s has return (e); and return; */ warning(216, funcsym->s_name); /* Print warnings for unused arguments */ arg = dcs->d_fargs; n = 0; while (arg != NULL && (nargusg == -1 || n < nargusg)) { chkusg1(dcs->d_asm, arg); arg = arg->s_nxt; n++; } nargusg = -1; /* * write the information about the function definition to the * output file * inline functions explicitly declared extern are written as * declarations only. */ if (dcs->d_scl == EXTERN && funcsym->s_inline) { outsym(funcsym, funcsym->s_scl, DECL); } else { outfdef(funcsym, &dcs->d_fdpos, cstk->c_retval, funcsym->s_osdef, dcs->d_fargs); } /* * remove all symbols declared during argument declaration from * the symbol table */ if (dcs->d_nxt != NULL || dcs->d_ctx != EXTERN) LERROR("funcend()"); rmsyms(dcs->d_fpsyms); /* must be set on level 0 */ reached = 1; } /* * Process a label. * * typ type of the label (T_NAME, T_DEFAULT or T_CASE). * sym symbol table entry of label if typ == T_NAME * tn expression if typ == T_CASE */ void label(int typ, sym_t *sym, tnode_t *tn) { cstk_t *ci; clst_t *cl; val_t *v; val_t nv; tspec_t t; switch (typ) { case T_NAME: if (sym->s_set) { /* label %s redefined */ error(194, sym->s_name); } else { setsflg(sym); } break; case T_CASE: /* find the stack entry for the innermost switch statement */ for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) continue; if (ci == NULL) { /* case not in switch */ error(195); tn = NULL; } else if (tn != NULL && tn->tn_op != CON) { /* non-constant case expression */ error(197); tn = NULL; } else if (tn != NULL && !isityp(tn->tn_type->t_tspec)) { /* non-integral case expression */ error(198); tn = NULL; } if (tn != NULL) { if (ci->c_swtype == NULL) LERROR("label()"); if (reached && !ftflg) { if (hflag) /* fallthrough on case statement */ warning(220); } t = tn->tn_type->t_tspec; if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) { if (tflag) /* case label must be of type ... */ warning(203); } /* * get the value of the expression and convert it * to the type of the switch expression */ v = constant(tn, 1); (void) memset(&nv, 0, sizeof nv); cvtcon(CASE, 0, ci->c_swtype, &nv, v); free(v); /* look if we had this value already */ for (cl = ci->c_clst; cl != NULL; cl = cl->cl_nxt) { if (cl->cl_val.v_quad == nv.v_quad) break; } if (cl != NULL && isutyp(nv.v_tspec)) { /* duplicate case in switch, %lu */ error(200, (u_long)nv.v_quad); } else if (cl != NULL) { /* duplicate case in switch, %ld */ error(199, (long)nv.v_quad); } else { /* * append the value to the list of * case values */ cl = xcalloc(1, sizeof (clst_t)); STRUCT_ASSIGN(cl->cl_val, nv); cl->cl_nxt = ci->c_clst; ci->c_clst = cl; } } tfreeblk(); break; case T_DEFAULT: /* find the stack entry for the innermost switch statement */ for (ci = cstk; ci != NULL && !ci->c_switch; ci = ci->c_nxt) continue; if (ci == NULL) { /* default outside switch */ error(201); } else if (ci->c_default) { /* duplicate default in switch */ error(202); } else { if (reached && !ftflg) { if (hflag) /* fallthrough on default statement */ warning(284); } ci->c_default = 1; } break; - }; + } reached = 1; } /* * T_IF T_LPARN expr T_RPARN */ void if1(tnode_t *tn) { if (tn != NULL) tn = cconv(tn); if (tn != NULL) tn = promote(NOOP, 0, tn); expr(tn, 0, 1, 1); pushctrl(T_IF); } /* * if_without_else * if_without_else T_ELSE */ void if2(void) { cstk->c_rchif = reached ? 1 : 0; reached = 1; } /* * if_without_else * if_without_else T_ELSE stmnt */ void if3(int els) { if (els) { reached |= cstk->c_rchif; } else { reached = 1; } popctrl(T_IF); } /* * T_SWITCH T_LPARN expr T_RPARN */ void switch1(tnode_t *tn) { tspec_t t; type_t *tp; if (tn != NULL) tn = cconv(tn); if (tn != NULL) tn = promote(NOOP, 0, tn); if (tn != NULL && !isityp(tn->tn_type->t_tspec)) { /* switch expression must have integral type */ error(205); tn = NULL; } if (tn != NULL && tflag) { t = tn->tn_type->t_tspec; if (t == LONG || t == ULONG || t == QUAD || t == UQUAD) { /* switch expr. must be of type `int' in trad. C */ warning(271); } } /* * Remember the type of the expression. Because its possible * that (*tp) is allocated on tree memory the type must be * duplicated. This is not too complicated because it is * only an integer type. */ if ((tp = calloc(1, sizeof (type_t))) == NULL) nomem(); if (tn != NULL) { tp->t_tspec = tn->tn_type->t_tspec; if ((tp->t_isenum = tn->tn_type->t_isenum) != 0) tp->t_enum = tn->tn_type->t_enum; } else { tp->t_tspec = INT; } expr(tn, 1, 0, 1); pushctrl(T_SWITCH); cstk->c_switch = 1; cstk->c_swtype = tp; reached = rchflg = 0; ftflg = 1; } /* * switch_expr stmnt */ void switch2(void) { int nenum = 0, nclab = 0; sym_t *esym; clst_t *cl; if (cstk->c_swtype == NULL) LERROR("switch2()"); /* * If the switch expression was of type enumeration, count the case * labels and the number of enumerators. If both counts are not * equal print a warning. */ if (cstk->c_swtype->t_isenum) { nenum = nclab = 0; if (cstk->c_swtype->t_enum == NULL) LERROR("switch2()"); for (esym = cstk->c_swtype->t_enum->elem; esym != NULL; esym = esym->s_nxt) { nenum++; } for (cl = cstk->c_clst; cl != NULL; cl = cl->cl_nxt) nclab++; if (hflag && eflag && nenum != nclab && !cstk->c_default) { /* enumeration value(s) not handled in switch */ warning(206); } } if (cstk->c_break) { /* * end of switch always reached (c_break is only set if the * break statement can be reached). */ reached = 1; } else if (!cstk->c_default && (!hflag || !cstk->c_swtype->t_isenum || nenum != nclab)) { /* * there are possible values which are not handled in * switch */ reached = 1; } /* * otherwise the end of the switch expression is reached * if the end of the last statement inside it is reached. */ popctrl(T_SWITCH); } /* * T_WHILE T_LPARN expr T_RPARN */ void while1(tnode_t *tn) { if (!reached) { /* loop not entered at top */ warning(207); reached = 1; } if (tn != NULL) tn = cconv(tn); if (tn != NULL) tn = promote(NOOP, 0, tn); if (tn != NULL && !issclt(tn->tn_type->t_tspec)) { /* controlling expressions must have scalar type */ error(204); tn = NULL; } pushctrl(T_WHILE); cstk->c_loop = 1; if (tn != NULL && tn->tn_op == CON) { if (isityp(tn->tn_type->t_tspec)) { cstk->c_infinite = tn->tn_val->v_quad != 0; } else { cstk->c_infinite = tn->tn_val->v_ldbl != 0.0; } } expr(tn, 0, 1, 1); } /* * while_expr stmnt * while_expr error */ void while2(void) { /* * The end of the loop can be reached if it is no endless loop * or there was a break statement which was reached. */ reached = !cstk->c_infinite || cstk->c_break; rchflg = 0; popctrl(T_WHILE); } /* * T_DO */ void do1(void) { if (!reached) { /* loop not entered at top */ warning(207); reached = 1; } pushctrl(T_DO); cstk->c_loop = 1; } /* * do stmnt do_while_expr * do error */ void do2(tnode_t *tn) { /* * If there was a continue statement the expression controlling the * loop is reached. */ if (cstk->c_cont) reached = 1; if (tn != NULL) tn = cconv(tn); if (tn != NULL) tn = promote(NOOP, 0, tn); if (tn != NULL && !issclt(tn->tn_type->t_tspec)) { /* controlling expressions must have scalar type */ error(204); tn = NULL; } if (tn != NULL && tn->tn_op == CON) { if (isityp(tn->tn_type->t_tspec)) { cstk->c_infinite = tn->tn_val->v_quad != 0; } else { cstk->c_infinite = tn->tn_val->v_ldbl != 0.0; } } expr(tn, 0, 1, 1); /* * The end of the loop is only reached if it is no endless loop * or there was a break statement which could be reached. */ reached = !cstk->c_infinite || cstk->c_break; rchflg = 0; popctrl(T_DO); } /* * T_FOR T_LPARN opt_expr T_SEMI opt_expr T_SEMI opt_expr T_RPARN */ void for1(tnode_t *tn1, tnode_t *tn2, tnode_t *tn3) { /* * If there is no initialisation expression it is possible that * it is intended not to enter the loop at top. */ if (tn1 != NULL && !reached) { /* loop not entered at top */ warning(207); reached = 1; } pushctrl(T_FOR); cstk->c_loop = 1; /* * Store the tree memory for the reinitialisation expression. * Also remember this expression itself. We must check it at * the end of the loop to get "used but not set" warnings correct. */ cstk->c_fexprm = tsave(); cstk->c_f3expr = tn3; STRUCT_ASSIGN(cstk->c_fpos, curr_pos); STRUCT_ASSIGN(cstk->c_cfpos, csrc_pos); if (tn1 != NULL) expr(tn1, 0, 0, 1); if (tn2 != NULL) tn2 = cconv(tn2); if (tn2 != NULL) tn2 = promote(NOOP, 0, tn2); if (tn2 != NULL && !issclt(tn2->tn_type->t_tspec)) { /* controlling expressions must have scalar type */ error(204); tn2 = NULL; } if (tn2 != NULL) expr(tn2, 0, 1, 1); if (tn2 == NULL) { cstk->c_infinite = 1; } else if (tn2->tn_op == CON) { if (isityp(tn2->tn_type->t_tspec)) { cstk->c_infinite = tn2->tn_val->v_quad != 0; } else { cstk->c_infinite = tn2->tn_val->v_ldbl != 0.0; } } /* Checking the reinitialisation expression is done in for2() */ reached = 1; } /* * for_exprs stmnt * for_exprs error */ void for2(void) { pos_t cpos, cspos; tnode_t *tn3; if (cstk->c_cont) reached = 1; STRUCT_ASSIGN(cpos, curr_pos); STRUCT_ASSIGN(cspos, csrc_pos); /* Restore the tree memory for the reinitialisation expression */ trestor(cstk->c_fexprm); tn3 = cstk->c_f3expr; STRUCT_ASSIGN(curr_pos, cstk->c_fpos); STRUCT_ASSIGN(csrc_pos, cstk->c_cfpos); /* simply "statement not reached" would be confusing */ if (!reached && !rchflg) { /* end-of-loop code not reached */ warning(223); reached = 1; } if (tn3 != NULL) { expr(tn3, 0, 0, 1); } else { tfreeblk(); } STRUCT_ASSIGN(curr_pos, cpos); STRUCT_ASSIGN(csrc_pos, cspos); /* An endless loop without break will never terminate */ reached = cstk->c_break || !cstk->c_infinite; rchflg = 0; popctrl(T_FOR); } /* * T_GOTO identifier T_SEMI * T_GOTO error T_SEMI */ void dogoto(sym_t *lab) { setuflg(lab, 0, 0); chkreach(); reached = rchflg = 0; } /* * T_BREAK T_SEMI */ void dobreak(void) { cstk_t *ci; ci = cstk; while (ci != NULL && !ci->c_loop && !ci->c_switch) ci = ci->c_nxt; if (ci == NULL) { /* break outside loop or switch */ error(208); } else { if (reached) ci->c_break = 1; } if (bflag) chkreach(); reached = rchflg = 0; } /* * T_CONTINUE T_SEMI */ void docont(void) { cstk_t *ci; for (ci = cstk; ci != NULL && !ci->c_loop; ci = ci->c_nxt) continue; if (ci == NULL) { /* continue outside loop */ error(209); } else { ci->c_cont = 1; } chkreach(); reached = rchflg = 0; } /* * T_RETURN T_SEMI * T_RETURN expr T_SEMI */ void doreturn(tnode_t *tn) { tnode_t *ln, *rn; cstk_t *ci; op_t op; for (ci = cstk; ci->c_nxt != NULL; ci = ci->c_nxt) continue; if (tn != NULL) { ci->c_retval = 1; } else { ci->c_noretval = 1; } if (tn != NULL && funcsym->s_type->t_subt->t_tspec == VOID) { /* void function %s cannot return value */ error(213, funcsym->s_name); tfreeblk(); tn = NULL; } else if (tn == NULL && funcsym->s_type->t_subt->t_tspec != VOID) { /* * Assume that the function has a return value only if it * is explicitly declared. */ if (!funcsym->s_rimpl) /* function %s expects to return value */ warning(214, funcsym->s_name); } if (tn != NULL) { /* Create a temporary node for the left side */ ln = tgetblk(sizeof (tnode_t)); ln->tn_op = NAME; ln->tn_type = tduptyp(funcsym->s_type->t_subt); ln->tn_type->t_const = 0; ln->tn_lvalue = 1; ln->tn_sym = funcsym; /* better than nothing */ tn = build(RETURN, ln, tn); if (tn != NULL) { rn = tn->tn_right; while ((op = rn->tn_op) == CVT || op == PLUS) rn = rn->tn_left; if (rn->tn_op == AMPER && rn->tn_left->tn_op == NAME && rn->tn_left->tn_sym->s_scl == AUTO) { /* %s returns pointer to automatic object */ warning(302, funcsym->s_name); } } expr(tn, 1, 0, 1); } else { chkreach(); } reached = rchflg = 0; } /* * Do some cleanup after a global declaration or definition. * Especially remove informations about unused lint comments. */ void glclup(int silent) { pos_t cpos; STRUCT_ASSIGN(cpos, curr_pos); if (nargusg != -1) { if (!silent) { STRUCT_ASSIGN(curr_pos, aupos); /* must precede function definition: %s */ warning(282, "ARGSUSED"); } nargusg = -1; } if (nvararg != -1) { if (!silent) { STRUCT_ASSIGN(curr_pos, vapos); /* must precede function definition: %s */ warning(282, "VARARGS"); } nvararg = -1; } if (prflstrg != -1) { if (!silent) { STRUCT_ASSIGN(curr_pos, prflpos); /* must precede function definition: %s */ warning(282, "PRINTFLIKE"); } prflstrg = -1; } if (scflstrg != -1) { if (!silent) { STRUCT_ASSIGN(curr_pos, scflpos); /* must precede function definition: %s */ warning(282, "SCANFLIKE"); } scflstrg = -1; } STRUCT_ASSIGN(curr_pos, cpos); dcs->d_asm = 0; } /* * ARGSUSED comment * * Only the first n arguments of the following function are checked * for usage. A missing argument is taken to be 0. */ void argsused(int n) { if (n == -1) n = 0; if (dcs->d_ctx != EXTERN) { /* must be outside function: ** %s ** */ warning(280, "ARGSUSED"); return; } if (nargusg != -1) { /* duplicate use of ** %s ** */ warning(281, "ARGSUSED"); } nargusg = n; STRUCT_ASSIGN(aupos, curr_pos); } /* * VARARGS comment * * Makes that lint2 checks only the first n arguments for compatibility * to the function definition. A missing argument is taken to be 0. */ void varargs(int n) { if (n == -1) n = 0; if (dcs->d_ctx != EXTERN) { /* must be outside function: ** %s ** */ warning(280, "VARARGS"); return; } if (nvararg != -1) { /* duplicate use of ** %s ** */ warning(281, "VARARGS"); } nvararg = n; STRUCT_ASSIGN(vapos, curr_pos); } /* * PRINTFLIKE comment * * Check all arguments until the (n-1)-th as usual. The n-th argument is * used the check the types of remaining arguments. */ void printflike(int n) { if (n == -1) n = 0; if (dcs->d_ctx != EXTERN) { /* must be outside function: ** %s ** */ warning(280, "PRINTFLIKE"); return; } if (prflstrg != -1) { /* duplicate use of ** %s ** */ warning(281, "PRINTFLIKE"); } prflstrg = n; STRUCT_ASSIGN(prflpos, curr_pos); } /* * SCANFLIKE comment * * Check all arguments until the (n-1)-th as usual. The n-th argument is * used the check the types of remaining arguments. */ void scanflike(int n) { if (n == -1) n = 0; if (dcs->d_ctx != EXTERN) { /* must be outside function: ** %s ** */ warning(280, "SCANFLIKE"); return; } if (scflstrg != -1) { /* duplicate use of ** %s ** */ warning(281, "SCANFLIKE"); } scflstrg = n; STRUCT_ASSIGN(scflpos, curr_pos); } /* * Set the linenumber for a CONSTCOND comment. At this and the following * line no warnings about constants in conditional contexts are printed. */ /* ARGSUSED */ void constcond(int n) { ccflg = 1; } /* * Suppress printing of "fallthrough on ..." warnings until next * statement. */ /* ARGSUSED */ void fallthru(int n) { ftflg = 1; } /* * Stop warnings about statements which cannot be reached. Also tells lint * that the following statements cannot be reached (e.g. after exit()). */ /* ARGSUSED */ void notreach(int n) { reached = 0; rchflg = 1; } /* ARGSUSED */ void lintlib(int n) { if (dcs->d_ctx != EXTERN) { /* must be outside function: ** %s ** */ warning(280, "LINTLIBRARY"); return; } llibflg = 1; vflag = 0; } /* * Suppress most warnings at the current and the following line. */ /* ARGSUSED */ void linted(int n) { #ifdef DEBUG printf("%s, %d: nowarn = 1\n", curr_pos.p_file, curr_pos.p_line); #endif nowarn = 1; } /* * Suppress bitfield type errors on the current line. */ /* ARGSUSED */ void bitfieldtype(int n) { #ifdef DEBUG printf("%s, %d: bitfieldtype_ok = 1\n", curr_pos.p_file, curr_pos.p_line); #endif bitfieldtype_ok = 1; } /* * PROTOTLIB in conjunction with LINTLIBRARY can be used to handle * prototypes like function definitions. This is done if the argument * to PROTOLIB is nonzero. Otherwise prototypes are handled normaly. */ void protolib(int n) { if (dcs->d_ctx != EXTERN) { /* must be outside function: ** %s ** */ warning(280, "PROTOLIB"); return; } plibflg = n == 0 ? 0 : 1; } /* * Set quadflg to nonzero which means that the next statement/declaration * may use "long long" without an error or warning. */ /* ARGSUSED */ void longlong(int n) { quadflg = 1; } Index: head/usr.bin/xstr/xstr.c =================================================================== --- head/usr.bin/xstr/xstr.c (revision 298088) +++ head/usr.bin/xstr/xstr.c (revision 298089) @@ -1,491 +1,491 @@ /* * 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. * 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$"); #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1980, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif #ifndef lint static const char sccsid[] = "@(#)xstr.c 8.1 (Berkeley) 6/9/93"; #endif #include #include #include #include #include #include #include #include #include "pathnames.h" /* * xstr - extract and hash strings in a C program * * Bill Joy UCB * November, 1978 */ #define ignore(a) ((void) a) static off_t tellpt; static off_t mesgpt; static char cstrings[] = "strings"; static char *strings = cstrings; static int cflg; static int vflg; static int readstd; static char lastchr(char *); static int fgetNUL(char *, int, FILE *); static int istail(char *, char *); static int octdigit(char); static int xgetc(FILE *); static off_t hashit(char *, int); static off_t yankstr(char **); static void usage(void); static void flushsh(void); static void found(int, off_t, char *); static void inithash(void); static void onintr(int); static void process(const char *); static void prstr(char *); static void xsdotc(void); int main(int argc, char *argv[]) { int c; int fdesc; while ((c = getopt(argc, argv, "-cv")) != -1) switch (c) { case '-': readstd++; break; case 'c': cflg++; break; case 'v': vflg++; break; default: usage(); } argc -= optind; argv += optind; if (signal(SIGINT, SIG_IGN) == SIG_DFL) signal(SIGINT, onintr); if (cflg || (argc == 0 && !readstd)) inithash(); else { strings = strdup(_PATH_TMP); if (strings == NULL) err(1, "strdup() failed"); fdesc = mkstemp(strings); if (fdesc == -1) err(1, "Unable to create temporary file"); close(fdesc); } while (readstd || argc > 0) { if (freopen("x.c", "w", stdout) == NULL) err(1, "x.c"); if (!readstd && freopen(argv[0], "r", stdin) == NULL) err(2, "%s", argv[0]); process("x.c"); if (readstd == 0) argc--, argv++; else readstd = 0; - }; + } flushsh(); if (cflg == 0) xsdotc(); if (strings[0] == '/') ignore(unlink(strings)); exit(0); } static void usage(void) { fprintf(stderr, "usage: xstr [-cv] [-] [file ...]\n"); exit (1); } static char linebuf[BUFSIZ]; static void process(const char *name) { char *cp; int c; int incomm = 0; int ret; printf("extern char\txstr[];\n"); for (;;) { if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { if (ferror(stdin)) err(3, "%s", name); break; } if (linebuf[0] == '#') { if (linebuf[1] == ' ' && isdigit(linebuf[2])) printf("#line%s", &linebuf[1]); else printf("%s", linebuf); continue; } for (cp = linebuf; (c = *cp++);) switch (c) { case '"': if (incomm) goto def; if ((ret = (int) yankstr(&cp)) == -1) goto out; printf("(&xstr[%d])", ret); break; case '\'': if (incomm) goto def; putchar(c); if (*cp) putchar(*cp++); break; case '/': if (incomm || *cp != '*') goto def; incomm = 1; cp++; printf("/*"); continue; case '*': if (incomm && *cp == '/') { incomm = 0; cp++; printf("*/"); continue; } goto def; def: default: putchar(c); break; } } out: if (ferror(stdout)) warn("x.c"), onintr(0); } static off_t yankstr(char **cpp) { char *cp = *cpp; int c, ch; char dbuf[BUFSIZ]; char *dp = dbuf; char *tp; static char tmp[] = "b\bt\tr\rn\nf\f\\\\\"\""; while ((c = *cp++)) { if (dp == dbuf + sizeof(dbuf) - 3) errx(1, "message too long"); switch (c) { case '"': cp++; goto out; case '\\': c = *cp++; if (c == 0) break; if (c == '\n') { if (fgets(linebuf, sizeof linebuf, stdin) == NULL) { if (ferror(stdin)) err(3, "x.c"); return(-1); } cp = linebuf; continue; } for (tp = tmp; (ch = *tp++); tp++) if (c == ch) { c = *tp; goto gotc; } if (!octdigit(c)) { *dp++ = '\\'; break; } c -= '0'; if (!octdigit(*cp)) break; c <<= 3, c += *cp++ - '0'; if (!octdigit(*cp)) break; c <<= 3, c += *cp++ - '0'; break; } gotc: *dp++ = c; } out: *cpp = --cp; *dp = 0; return (hashit(dbuf, 1)); } static int octdigit(char c) { return (isdigit(c) && c != '8' && c != '9'); } static void inithash(void) { char buf[BUFSIZ]; FILE *mesgread = fopen(strings, "r"); if (mesgread == NULL) return; for (;;) { mesgpt = tellpt; if (fgetNUL(buf, sizeof buf, mesgread) == 0) break; ignore(hashit(buf, 0)); } ignore(fclose(mesgread)); } static int fgetNUL(char *obuf, int rmdr, FILE *file) { int c; char *buf = obuf; while (--rmdr > 0 && (c = xgetc(file)) != 0 && c != EOF) *buf++ = c; *buf++ = 0; return ((feof(file) || ferror(file)) ? 0 : 1); } static int xgetc(FILE *file) { tellpt++; return (getc(file)); } #define BUCKETS 128 static struct hash { off_t hpt; char *hstr; struct hash *hnext; short hnew; } bucket[BUCKETS]; static off_t hashit(char *str, int new) { int i; struct hash *hp, *hp0; hp = hp0 = &bucket[lastchr(str) & 0177]; while (hp->hnext) { hp = hp->hnext; i = istail(str, hp->hstr); if (i >= 0) return (hp->hpt + i); } if ((hp = (struct hash *) calloc(1, sizeof (*hp))) == NULL) errx(8, "calloc"); hp->hpt = mesgpt; if (!(hp->hstr = strdup(str))) err(1, NULL); mesgpt += strlen(hp->hstr) + 1; hp->hnext = hp0->hnext; hp->hnew = new; hp0->hnext = hp; return (hp->hpt); } static void flushsh(void) { int i; struct hash *hp; FILE *mesgwrit; int old = 0, new = 0; for (i = 0; i < BUCKETS; i++) for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) if (hp->hnew) new++; else old++; if (new == 0 && old != 0) return; mesgwrit = fopen(strings, old ? "r+" : "w"); if (mesgwrit == NULL) err(4, "%s", strings); for (i = 0; i < BUCKETS; i++) for (hp = bucket[i].hnext; hp != NULL; hp = hp->hnext) { found(hp->hnew, hp->hpt, hp->hstr); if (hp->hnew) { fseek(mesgwrit, hp->hpt, 0); ignore(fwrite(hp->hstr, strlen(hp->hstr) + 1, 1, mesgwrit)); if (ferror(mesgwrit)) err(4, "%s", strings); } } if (fclose(mesgwrit) == EOF) err(4, "%s", strings); } static void found(int new, off_t off, char *str) { if (vflg == 0) return; if (!new) fprintf(stderr, "found at %d:", (int) off); else fprintf(stderr, "new at %d:", (int) off); prstr(str); fprintf(stderr, "\n"); } static void prstr(char *cp) { int c; while ((c = (*cp++ & 0377))) if (c < ' ') fprintf(stderr, "^%c", c + '`'); else if (c == 0177) fprintf(stderr, "^?"); else if (c > 0200) fprintf(stderr, "\\%03o", c); else fprintf(stderr, "%c", c); } static void xsdotc(void) { FILE *strf = fopen(strings, "r"); FILE *xdotcf; if (strf == NULL) err(5, "%s", strings); xdotcf = fopen("xs.c", "w"); if (xdotcf == NULL) err(6, "xs.c"); fprintf(xdotcf, "char\txstr[] = {\n"); for (;;) { int i, c; for (i = 0; i < 8; i++) { c = getc(strf); if (ferror(strf)) { warn("%s", strings); onintr(0); } if (feof(strf)) { fprintf(xdotcf, "\n"); goto out; } fprintf(xdotcf, "0x%02x,", c); } fprintf(xdotcf, "\n"); } out: fprintf(xdotcf, "};\n"); ignore(fclose(xdotcf)); ignore(fclose(strf)); } static char lastchr(char *cp) { while (cp[0] && cp[1]) cp++; return (*cp); } static int istail(char *str, char *of) { int d = strlen(of) - strlen(str); if (d < 0 || strcmp(&of[d], str) != 0) return (-1); return (d); } static void onintr(int dummy __unused) { ignore(signal(SIGINT, SIG_IGN)); if (strings[0] == '/') ignore(unlink(strings)); ignore(unlink("x.c")); ignore(unlink("xs.c")); exit(7); } Index: head/usr.sbin/keyserv/keyserv.c =================================================================== --- head/usr.sbin/keyserv/keyserv.c (revision 298088) +++ head/usr.sbin/keyserv/keyserv.c (revision 298089) @@ -1,821 +1,821 @@ /* * Sun RPC is a product of Sun Microsystems, Inc. and is provided for * unrestricted use provided that this legend is included on all tape * media and as a part of the software program in whole or part. Users * may copy or modify Sun RPC without charge, but are not authorized * to license or distribute it to anyone else except as part of a product or * program developed by the user. * * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. * * Sun RPC is provided with no support and without any obligation on the * part of Sun Microsystems, Inc. to assist in its use, correction, * modification or enhancement. * * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC * OR ANY PART THEREOF. * * In no event will Sun Microsystems, Inc. be liable for any lost revenue * or profits or other special, indirect and consequential damages, even if * Sun has been advised of the possibility of such damages. * * Sun Microsystems, Inc. * 2550 Garcia Avenue * Mountain View, California 94043 */ #ifndef lint #if 0 static char sccsid[] = "@(#)keyserv.c 1.15 94/04/25 SMI"; #endif static const char rcsid[] = "$FreeBSD$"; #endif /* not lint */ /* * Copyright (c) 1986 - 1991 by Sun Microsystems, Inc. */ /* * Keyserver * Store secret keys per uid. Do public key encryption and decryption * operations. Generate "random" keys. * Do not talk to anything but a local root * process on the local transport only */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "keyserv.h" #ifndef NGROUPS #define NGROUPS 16 #endif #ifndef KEYSERVSOCK #define KEYSERVSOCK "/var/run/keyservsock" #endif static void randomize( des_block * ); static void usage( void ); static int getrootkey( des_block *, int ); static int root_auth( SVCXPRT *, struct svc_req * ); #ifdef DEBUG static int debugging = 1; #else static int debugging = 0; #endif static void keyprogram(); static des_block masterkey; char *getenv(); static char ROOTKEY[] = "/etc/.rootkey"; /* * Hack to allow the keyserver to use AUTH_DES (for authenticated * NIS+ calls, for example). The only functions that get called * are key_encryptsession_pk, key_decryptsession_pk, and key_gendes. * * The approach is to have the keyserver fill in pointers to local * implementations of these functions, and to call those in key_call(). */ extern cryptkeyres *(*__key_encryptsession_pk_LOCAL)(); extern cryptkeyres *(*__key_decryptsession_pk_LOCAL)(); extern des_block *(*__key_gendes_LOCAL)(); extern int (*__des_crypt_LOCAL)(); cryptkeyres *key_encrypt_pk_2_svc_prog( uid_t, cryptkeyarg2 * ); cryptkeyres *key_decrypt_pk_2_svc_prog( uid_t, cryptkeyarg2 * ); des_block *key_gen_1_svc_prog( void *, struct svc_req * ); int main(argc, argv) int argc; char *argv[]; { int nflag = 0; int c; int warn = 0; char *path = NULL; void *localhandle; register SVCXPRT *transp; struct netconfig *nconf = NULL; __key_encryptsession_pk_LOCAL = &key_encrypt_pk_2_svc_prog; __key_decryptsession_pk_LOCAL = &key_decrypt_pk_2_svc_prog; __key_gendes_LOCAL = &key_gen_1_svc_prog; while ((c = getopt(argc, argv, "ndDvp:")) != -1) switch (c) { case 'n': nflag++; break; case 'd': pk_nodefaultkeys(); break; case 'D': debugging = 1; break; case 'v': warn = 1; break; case 'p': path = optarg; break; default: usage(); } load_des(warn, path); __des_crypt_LOCAL = _my_crypt; if (svc_auth_reg(AUTH_DES, _svcauth_des) == -1) errx(1, "failed to register AUTH_DES authenticator"); if (optind != argc) { usage(); } /* * Initialize */ (void) umask(S_IXUSR|S_IXGRP|S_IXOTH); if (geteuid() != 0) errx(1, "keyserv must be run as root"); setmodulus(HEXMODULUS); getrootkey(&masterkey, nflag); rpcb_unset(KEY_PROG, KEY_VERS, NULL); rpcb_unset(KEY_PROG, KEY_VERS2, NULL); if (svc_create(keyprogram, KEY_PROG, KEY_VERS, "netpath") == 0) { (void) fprintf(stderr, "%s: unable to create service\n", argv[0]); exit(1); } if (svc_create(keyprogram, KEY_PROG, KEY_VERS2, "netpath") == 0) { (void) fprintf(stderr, "%s: unable to create service\n", argv[0]); exit(1); } localhandle = setnetconfig(); while ((nconf = getnetconfig(localhandle)) != NULL) { if (nconf->nc_protofmly != NULL && strcmp(nconf->nc_protofmly, NC_LOOPBACK) == 0) break; } if (nconf == NULL) errx(1, "getnetconfig: %s", nc_sperror()); unlink(KEYSERVSOCK); rpcb_unset(CRYPT_PROG, CRYPT_VERS, nconf); transp = svcunix_create(RPC_ANYSOCK, 0, 0, KEYSERVSOCK); if (transp == NULL) errx(1, "cannot create AF_LOCAL service"); if (!svc_reg(transp, KEY_PROG, KEY_VERS, keyprogram, nconf)) errx(1, "unable to register (KEY_PROG, KEY_VERS, unix)"); if (!svc_reg(transp, KEY_PROG, KEY_VERS2, keyprogram, nconf)) errx(1, "unable to register (KEY_PROG, KEY_VERS2, unix)"); if (!svc_reg(transp, CRYPT_PROG, CRYPT_VERS, crypt_prog_1, nconf)) errx(1, "unable to register (CRYPT_PROG, CRYPT_VERS, unix)"); endnetconfig(localhandle); (void) umask(066); /* paranoia */ if (!debugging) { daemon(0,0); } signal(SIGPIPE, SIG_IGN); svc_run(); abort(); /* NOTREACHED */ } /* * In the event that we don't get a root password, we try to * randomize the master key the best we can */ static void randomize(master) des_block *master; { #ifndef __FreeBSD__ int i; int seed; struct timeval tv; int shift; seed = 0; for (i = 0; i < 1024; i++) { (void)gettimeofday(&tv, NULL); shift = i % 8 * sizeof (int); seed ^= (tv.tv_usec << shift) | (tv.tv_usec >> (32 - shift)); } #endif #ifdef KEYSERV_RANDOM #ifdef __FreeBSD__ master->key.low = arc4random(); master->key.high = arc4random(); #else srandom(seed); master->key.low = random(); master->key.high = random(); #endif #else /* use stupid dangerous bad rand() */ #ifdef __FreeBSD__ sranddev(); #else srand(seed); #endif master->key.low = rand(); master->key.high = rand(); #endif } /* * Try to get root's secret key, by prompting if terminal is a tty, else trying * from standard input. * Returns 1 on success. */ static int getrootkey(master, prompt) des_block *master; int prompt; { char *passwd; char name[MAXNETNAMELEN + 1]; char secret[HEXKEYBYTES]; key_netstarg netstore; int fd; if (!prompt) { /* * Read secret key out of ROOTKEY */ fd = open(ROOTKEY, O_RDONLY, 0); if (fd < 0) { randomize(master); return (0); } if (read(fd, secret, HEXKEYBYTES) < HEXKEYBYTES) { warnx("the key read from %s was too short", ROOTKEY); (void) close(fd); return (0); } (void) close(fd); if (!getnetname(name)) { warnx( "failed to generate host's netname when establishing root's key"); return (0); } memcpy(netstore.st_priv_key, secret, HEXKEYBYTES); memset(netstore.st_pub_key, 0, HEXKEYBYTES); netstore.st_netname = name; if (pk_netput(0, &netstore) != KEY_SUCCESS) { warnx("could not set root's key and netname"); return (0); } return (1); } /* * Decrypt yellow pages publickey entry to get secret key */ passwd = getpass("root password:"); passwd2des(passwd, (char *)master); getnetname(name); if (!getsecretkey(name, secret, passwd)) { warnx("can't find %s's secret key", name); return (0); } if (secret[0] == 0) { warnx("password does not decrypt secret key for %s", name); return (0); } (void) pk_setkey(0, secret); /* * Store it for future use in $ROOTKEY, if possible */ fd = open(ROOTKEY, O_WRONLY|O_TRUNC|O_CREAT, 0); if (fd > 0) { char newline = '\n'; write(fd, secret, strlen(secret)); write(fd, &newline, sizeof (newline)); close(fd); } return (1); } /* * Procedures to implement RPC service */ char * strstatus(status) keystatus status; { switch (status) { case KEY_SUCCESS: return ("KEY_SUCCESS"); case KEY_NOSECRET: return ("KEY_NOSECRET"); case KEY_UNKNOWN: return ("KEY_UNKNOWN"); case KEY_SYSTEMERR: return ("KEY_SYSTEMERR"); default: return ("(bad result code)"); } } keystatus * key_set_1_svc_prog(uid, key) uid_t uid; keybuf key; { static keystatus status; if (debugging) { (void) fprintf(stderr, "set(%u, %.*s) = ", uid, (int) sizeof (keybuf), key); } status = pk_setkey(uid, key); if (debugging) { (void) fprintf(stderr, "%s\n", strstatus(status)); (void) fflush(stderr); } return (&status); } cryptkeyres * key_encrypt_pk_2_svc_prog(uid, arg) uid_t uid; cryptkeyarg2 *arg; { static cryptkeyres res; if (debugging) { (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid, arg->remotename, arg->deskey.key.high, arg->deskey.key.low); } res.cryptkeyres_u.deskey = arg->deskey; res.status = pk_encrypt(uid, arg->remotename, &(arg->remotekey), &res.cryptkeyres_u.deskey); if (debugging) { if (res.status == KEY_SUCCESS) { (void) fprintf(stderr, "%08x%08x\n", res.cryptkeyres_u.deskey.key.high, res.cryptkeyres_u.deskey.key.low); } else { (void) fprintf(stderr, "%s\n", strstatus(res.status)); } (void) fflush(stderr); } return (&res); } cryptkeyres * key_decrypt_pk_2_svc_prog(uid, arg) uid_t uid; cryptkeyarg2 *arg; { static cryptkeyres res; if (debugging) { (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid, arg->remotename, arg->deskey.key.high, arg->deskey.key.low); } res.cryptkeyres_u.deskey = arg->deskey; res.status = pk_decrypt(uid, arg->remotename, &(arg->remotekey), &res.cryptkeyres_u.deskey); if (debugging) { if (res.status == KEY_SUCCESS) { (void) fprintf(stderr, "%08x%08x\n", res.cryptkeyres_u.deskey.key.high, res.cryptkeyres_u.deskey.key.low); } else { (void) fprintf(stderr, "%s\n", strstatus(res.status)); } (void) fflush(stderr); } return (&res); } keystatus * key_net_put_2_svc_prog(uid, arg) uid_t uid; key_netstarg *arg; { static keystatus status; if (debugging) { (void) fprintf(stderr, "net_put(%s, %.*s, %.*s) = ", arg->st_netname, (int)sizeof (arg->st_pub_key), arg->st_pub_key, (int)sizeof (arg->st_priv_key), arg->st_priv_key); - }; + } status = pk_netput(uid, arg); if (debugging) { (void) fprintf(stderr, "%s\n", strstatus(status)); (void) fflush(stderr); } return (&status); } key_netstres * key_net_get_2_svc_prog(uid, arg) uid_t uid; void *arg; { static key_netstres keynetname; if (debugging) (void) fprintf(stderr, "net_get(%u) = ", uid); keynetname.status = pk_netget(uid, &keynetname.key_netstres_u.knet); if (debugging) { if (keynetname.status == KEY_SUCCESS) { fprintf(stderr, "<%s, %.*s, %.*s>\n", keynetname.key_netstres_u.knet.st_netname, (int)sizeof (keynetname.key_netstres_u.knet.st_pub_key), keynetname.key_netstres_u.knet.st_pub_key, (int)sizeof (keynetname.key_netstres_u.knet.st_priv_key), keynetname.key_netstres_u.knet.st_priv_key); } else { (void) fprintf(stderr, "NOT FOUND\n"); } (void) fflush(stderr); } return (&keynetname); } cryptkeyres * key_get_conv_2_svc_prog(uid, arg) uid_t uid; keybuf arg; { static cryptkeyres res; if (debugging) (void) fprintf(stderr, "get_conv(%u, %.*s) = ", uid, (int)sizeof (keybuf), arg); res.status = pk_get_conv_key(uid, arg, &res); if (debugging) { if (res.status == KEY_SUCCESS) { (void) fprintf(stderr, "%08x%08x\n", res.cryptkeyres_u.deskey.key.high, res.cryptkeyres_u.deskey.key.low); } else { (void) fprintf(stderr, "%s\n", strstatus(res.status)); } (void) fflush(stderr); } return (&res); } cryptkeyres * key_encrypt_1_svc_prog(uid, arg) uid_t uid; cryptkeyarg *arg; { static cryptkeyres res; if (debugging) { (void) fprintf(stderr, "encrypt(%u, %s, %08x%08x) = ", uid, arg->remotename, arg->deskey.key.high, arg->deskey.key.low); } res.cryptkeyres_u.deskey = arg->deskey; res.status = pk_encrypt(uid, arg->remotename, NULL, &res.cryptkeyres_u.deskey); if (debugging) { if (res.status == KEY_SUCCESS) { (void) fprintf(stderr, "%08x%08x\n", res.cryptkeyres_u.deskey.key.high, res.cryptkeyres_u.deskey.key.low); } else { (void) fprintf(stderr, "%s\n", strstatus(res.status)); } (void) fflush(stderr); } return (&res); } cryptkeyres * key_decrypt_1_svc_prog(uid, arg) uid_t uid; cryptkeyarg *arg; { static cryptkeyres res; if (debugging) { (void) fprintf(stderr, "decrypt(%u, %s, %08x%08x) = ", uid, arg->remotename, arg->deskey.key.high, arg->deskey.key.low); } res.cryptkeyres_u.deskey = arg->deskey; res.status = pk_decrypt(uid, arg->remotename, NULL, &res.cryptkeyres_u.deskey); if (debugging) { if (res.status == KEY_SUCCESS) { (void) fprintf(stderr, "%08x%08x\n", res.cryptkeyres_u.deskey.key.high, res.cryptkeyres_u.deskey.key.low); } else { (void) fprintf(stderr, "%s\n", strstatus(res.status)); } (void) fflush(stderr); } return (&res); } /* ARGSUSED */ des_block * key_gen_1_svc_prog(v, s) void *v; struct svc_req *s; { struct timeval time; static des_block keygen; static des_block key; (void)gettimeofday(&time, NULL); keygen.key.high += (time.tv_sec ^ time.tv_usec); keygen.key.low += (time.tv_sec ^ time.tv_usec); ecb_crypt((char *)&masterkey, (char *)&keygen, sizeof (keygen), DES_ENCRYPT | DES_HW); key = keygen; des_setparity((char *)&key); if (debugging) { (void) fprintf(stderr, "gen() = %08x%08x\n", key.key.high, key.key.low); (void) fflush(stderr); } return (&key); } getcredres * key_getcred_1_svc_prog(uid, name) uid_t uid; netnamestr *name; { static getcredres res; static u_int gids[NGROUPS]; struct unixcred *cred; cred = &res.getcredres_u.cred; cred->gids.gids_val = gids; if (!netname2user(*name, (uid_t *) &cred->uid, (gid_t *) &cred->gid, (int *)&cred->gids.gids_len, (gid_t *)gids)) { res.status = KEY_UNKNOWN; } else { res.status = KEY_SUCCESS; } if (debugging) { (void) fprintf(stderr, "getcred(%s) = ", *name); if (res.status == KEY_SUCCESS) { (void) fprintf(stderr, "uid=%d, gid=%d, grouplen=%d\n", cred->uid, cred->gid, cred->gids.gids_len); } else { (void) fprintf(stderr, "%s\n", strstatus(res.status)); } (void) fflush(stderr); } return (&res); } /* * RPC boilerplate */ static void keyprogram(rqstp, transp) struct svc_req *rqstp; SVCXPRT *transp; { union { keybuf key_set_1_arg; cryptkeyarg key_encrypt_1_arg; cryptkeyarg key_decrypt_1_arg; netnamestr key_getcred_1_arg; cryptkeyarg key_encrypt_2_arg; cryptkeyarg key_decrypt_2_arg; netnamestr key_getcred_2_arg; cryptkeyarg2 key_encrypt_pk_2_arg; cryptkeyarg2 key_decrypt_pk_2_arg; key_netstarg key_net_put_2_arg; netobj key_get_conv_2_arg; } argument; char *result; xdrproc_t xdr_argument, xdr_result; char *(*local) (); uid_t uid = -1; int check_auth; switch (rqstp->rq_proc) { case NULLPROC: svc_sendreply(transp, (xdrproc_t)xdr_void, NULL); return; case KEY_SET: xdr_argument = (xdrproc_t)xdr_keybuf; xdr_result = (xdrproc_t)xdr_int; local = (char *(*)()) key_set_1_svc_prog; check_auth = 1; break; case KEY_ENCRYPT: xdr_argument = (xdrproc_t)xdr_cryptkeyarg; xdr_result = (xdrproc_t)xdr_cryptkeyres; local = (char *(*)()) key_encrypt_1_svc_prog; check_auth = 1; break; case KEY_DECRYPT: xdr_argument = (xdrproc_t)xdr_cryptkeyarg; xdr_result = (xdrproc_t)xdr_cryptkeyres; local = (char *(*)()) key_decrypt_1_svc_prog; check_auth = 1; break; case KEY_GEN: xdr_argument = (xdrproc_t)xdr_void; xdr_result = (xdrproc_t)xdr_des_block; local = (char *(*)()) key_gen_1_svc_prog; check_auth = 0; break; case KEY_GETCRED: xdr_argument = (xdrproc_t)xdr_netnamestr; xdr_result = (xdrproc_t)xdr_getcredres; local = (char *(*)()) key_getcred_1_svc_prog; check_auth = 0; break; case KEY_ENCRYPT_PK: xdr_argument = (xdrproc_t)xdr_cryptkeyarg2; xdr_result = (xdrproc_t)xdr_cryptkeyres; local = (char *(*)()) key_encrypt_pk_2_svc_prog; check_auth = 1; break; case KEY_DECRYPT_PK: xdr_argument = (xdrproc_t)xdr_cryptkeyarg2; xdr_result = (xdrproc_t)xdr_cryptkeyres; local = (char *(*)()) key_decrypt_pk_2_svc_prog; check_auth = 1; break; case KEY_NET_PUT: xdr_argument = (xdrproc_t)xdr_key_netstarg; xdr_result = (xdrproc_t)xdr_keystatus; local = (char *(*)()) key_net_put_2_svc_prog; check_auth = 1; break; case KEY_NET_GET: xdr_argument = (xdrproc_t) xdr_void; xdr_result = (xdrproc_t)xdr_key_netstres; local = (char *(*)()) key_net_get_2_svc_prog; check_auth = 1; break; case KEY_GET_CONV: xdr_argument = (xdrproc_t) xdr_keybuf; xdr_result = (xdrproc_t)xdr_cryptkeyres; local = (char *(*)()) key_get_conv_2_svc_prog; check_auth = 1; break; default: svcerr_noproc(transp); return; } if (check_auth) { if (root_auth(transp, rqstp) == 0) { if (debugging) { (void) fprintf(stderr, "not local privileged process\n"); } svcerr_weakauth(transp); return; } if (rqstp->rq_cred.oa_flavor != AUTH_SYS) { if (debugging) { (void) fprintf(stderr, "not unix authentication\n"); } svcerr_weakauth(transp); return; } uid = ((struct authsys_parms *)rqstp->rq_clntcred)->aup_uid; } memset(&argument, 0, sizeof (argument)); if (!svc_getargs(transp, xdr_argument, &argument)) { svcerr_decode(transp); return; } result = (*local) (uid, &argument); if (!svc_sendreply(transp, xdr_result, result)) { if (debugging) (void) fprintf(stderr, "unable to reply\n"); svcerr_systemerr(transp); } if (!svc_freeargs(transp, xdr_argument, &argument)) { if (debugging) (void) fprintf(stderr, "unable to free arguments\n"); exit(1); } return; } static int root_auth(trans, rqstp) SVCXPRT *trans; struct svc_req *rqstp; { uid_t uid; struct sockaddr *remote; remote = svc_getrpccaller(trans)->buf; if (remote->sa_family != AF_UNIX) { if (debugging) fprintf(stderr, "client didn't use AF_UNIX\n"); return (0); } if (__rpc_get_local_uid(trans, &uid) < 0) { if (debugging) fprintf(stderr, "__rpc_get_local_uid failed\n"); return (0); } if (debugging) fprintf(stderr, "local_uid %u\n", uid); if (uid == 0) return (1); if (rqstp->rq_cred.oa_flavor == AUTH_SYS) { if (((uid_t) ((struct authunix_parms *) rqstp->rq_clntcred)->aup_uid) == uid) { return (1); } else { if (debugging) fprintf(stderr, "local_uid %u mismatches auth %u\n", uid, ((uid_t) ((struct authunix_parms *)rqstp->rq_clntcred)->aup_uid)); return (0); } } else { if (debugging) fprintf(stderr, "Not auth sys\n"); return (0); } } static void usage() { (void) fprintf(stderr, "usage: keyserv [-n] [-D] [-d] [-v] [-p path]\n"); (void) fprintf(stderr, "-d disables the use of default keys\n"); exit(1); } Index: head/usr.sbin/lmcconfig/lmcconfig.c =================================================================== --- head/usr.sbin/lmcconfig/lmcconfig.c (revision 298088) +++ head/usr.sbin/lmcconfig/lmcconfig.c (revision 298089) @@ -1,2553 +1,2553 @@ /* * First author: Michael Graff. * Copyright (c) 1997-2000 Lan Media Corp. (www.lanmedia.com). * All rights reserved. * * Second author: Andrew Stanley-Jones. * Copyright (c) 2000-2002 SBE Corp. (www.sbei.com). * All rights reserved. * * Third author: David Boggs. * Copyright (c) 2002-2004 David Boggs. (boggs@boggs.palo-alto.ca.us). * All rights reserved. * * BSD License: * * 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. * * GNU General Public License: * * This program is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the Free * Software Foundation; either version 2 of the License, or (at your option) * any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for * more details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 59 * Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Description: * * This program configures the Unix/Linux device driver for SBE Corp's * wanADAPT and wanPMC series of Wide Area Network Interface Cards. * There is a man page for this program; go find it. * * If Netgraph is present (FreeBSD only): * cc -o lmcconfig -l netgraph -D NETGRAPH lmcconfig.c * If Netgraph is NOT present: * cc -o lmcconfig lmcconfig.c * Install the executable program in /usr/local/sbin/lmcconfig. * * $FreeBSD$ */ #include #include #include #include #include #include #include #include #include #include #include #if defined(NETGRAPH) # include #endif #include #include /* program global variables */ char * progname; /* name of this program */ char * ifname; /* interface name */ int fdcs; /* ifnet File Desc or ng Ctl Socket */ struct status status; /* card status (read only) */ struct config config; /* card configuration (read/write) */ int netgraph = 0; /* non-zero if netgraph present */ int summary = 0; /* print summary at end */ int update = 0; /* update driver config */ int verbose = 0; /* verbose output */ u_int8_t checksum; /* gate array ucode file checksum */ /* Functions currently unused. Keep compiler happy and provide prototypes. */ void ioctl_snmp_loop(u_int32_t); void init_srom(int); static void usage(void) { fprintf(stderr, "Usage: %s interface [-abBcCdDeEfhiLmMpPsStTuUvVwWxXyYzZ?]\n", progname); fprintf(stderr, "or\n"); fprintf(stderr, "Usage: %s interface -1 [-aABcdeEfFgiIlLpPstTuUvxX]\n", progname); fprintf(stderr, "or\n"); fprintf(stderr, "Usage: %s interface -3 [-aABcdefFlLsSvV]\n\n", progname); fprintf(stderr, "\tInterface is the interface name, e.g. '%s'\n", ifname); #if defined(NETGRAPH) fprintf(stderr, "\tIf interface name ends with ':' then use netgraph\n"); #endif fprintf(stderr, "\t-1 following parameters apply to T1E1 cards\n"); fprintf(stderr, "\t-3 following parameters apply to T3 cards\n"); fprintf(stderr, "\t-a Set Tx clock source, where:\n"); fprintf(stderr, "\t 1:modem Tx clk 2:int src 3:modem Rx Clk 4:ext conn\n"); fprintf(stderr, "\t-b Read and print bios rom addrs 0-255\n"); fprintf(stderr, "\t-B Write bios rom with address pattern\n"); fprintf(stderr, "\t-c Set 16-bit CRC (default)\n"); fprintf(stderr, "\t-C Set 32-bit CRC\n"); fprintf(stderr, "\t-d Clear driver DEBUG flag\n"); fprintf(stderr, "\t-D Set driver DEBUG flag (more log msgs)\n"); fprintf(stderr, "\t-e Set DTE mode (default)\n"); fprintf(stderr, "\t-E Set DCE mode\n"); fprintf(stderr, "\t-f Set synth osc freq in bits/sec\n"); fprintf(stderr, "\t-F Set SPPP line protocol to Frame-Relay\n"); fprintf(stderr, "\t-h Help: this usage message\n"); fprintf(stderr, "\t-i Interface name (eg, lmc0)\n"); fprintf(stderr, "\t-L Set loopback: 1:none 2:payload 3:line 4:other\n"); fprintf(stderr, "\t 5:inward 6:dual 16:Tulip 17:pins 18:LA/LL 19:LB/RL\n"); fprintf(stderr, "\t-m Read and print MII regs\n"); fprintf(stderr, "\t-M Write MII reg\n"); fprintf(stderr, "\t-p Read and print PCI config regs\n"); fprintf(stderr, "\t-P Write PCI config reg\n"); fprintf(stderr, "\t-s Read and print Tulip SROM\n"); fprintf(stderr, "\t-S Initialize Tulip SROM\n"); fprintf(stderr, "\t-t Read and print Tulip Control/Status regs\n"); fprintf(stderr, "\t-T Write Tulip Control/status reg\n"); fprintf(stderr, "\t-u Reset event counters\n"); fprintf(stderr, "\t-U Reset gate array\n"); fprintf(stderr, "\t-v Set verbose printout mode\n"); fprintf(stderr, "\t-V Print card configuration\n"); fprintf(stderr, "\t-w Load gate array from ROM\n"); fprintf(stderr, "\t-W Load gate array from file\n"); fprintf(stderr, "\t-x select RAWIP mode and bypass line protocols\n"); fprintf(stderr, "\t-X Select line protocols: SPPP, P2P or HDLC\n"); fprintf(stderr, "\t-y disable SPPP keep-alive packets\n"); fprintf(stderr, "\t-Y enable SPPP keep-alive packets\n"); fprintf(stderr, "\t-z Set SPPP line protocol to Cisco-HDLC\n"); fprintf(stderr, "\t-Z Set SPPP line protocol to PPP\n"); fprintf(stderr, "The -1 switch precedes T1/E1 commands.\n"); fprintf(stderr, "\t-a Stop sending Yellow|Blue|AIS signal\n"); fprintf(stderr, "\t-A Start sending Yellow|Blue|AIS signal\n"); fprintf(stderr, "\t-B Send BOP msg 25 times\n"); fprintf(stderr, "\t-c Set cable length in meters\n"); fprintf(stderr, "\t-d Print status of T1 DSU/CSU\n"); fprintf(stderr, "\t-e Set framing format, where:\n"); fprintf(stderr, "\t 27:T1-ESF 9:T1-SF 0:E1-FAS 8:E1-FAS+CRC\n"); fprintf(stderr, "\t 16:E1-FAS+CAS 24:E1-FAS+CRC+CAS 32:E1-NO-FRAMING\n"); fprintf(stderr, "\t-E <32-bit hex number> 1 activates a channel and 0 deactivates it.\n"); fprintf(stderr, "\t Use this to config a link in fractional T1/E1 mode\n"); fprintf(stderr, "\t-f Read and print Framer/LIU registers\n"); fprintf(stderr, "\t-F Write Framer/LIU register\n"); fprintf(stderr, "\t-g Set receiver gain, where:\n"); fprintf(stderr, "\t 0:short range 1:medium range\n"); fprintf(stderr, "\t 2:long range 3:extended range\n"); fprintf(stderr, "\t 4:auto-set based on cable length\n"); fprintf(stderr, "\t-i Send 'CSU Loop Down' inband msg\n"); fprintf(stderr, "\t-I Send 'CSU Loop Up' inband msg\n"); fprintf(stderr, "\t-l Send 'Line Loop Down' BOP msg\n"); fprintf(stderr, "\t-L Send 'Line Loop Up' BOP msg\n"); fprintf(stderr, "\t-p Send 'Payload Loop Down' BOP msg\n"); fprintf(stderr, "\t-P Send 'Payload Loop Up' BOP msg\n"); fprintf(stderr, "\t-s Print status of T1 DSU/CSU\n"); fprintf(stderr, "\t-t Stop sending test pattern\n"); fprintf(stderr, "\t-T Start sending test pattern, where:\n"); fprintf(stderr, "\t 0:unframed 2^11 1:unframed 2^15\n"); fprintf(stderr, "\t 2:unframed 2^20 3:unframed 2^23\n"); fprintf(stderr, "\t 4:unframed 2^11 w/ZS 5:unframed 2^15 w/ZS\n"); fprintf(stderr, "\t 6:unframed QRSS 7:unframed 2^23 w/ZS\n"); fprintf(stderr, "\t 8: framed 2^11 9: framed 2^15\n"); fprintf(stderr, "\t 10: framed 2^20 11: framed 2^23\n"); fprintf(stderr, "\t 12: framed 2^11 w/ZS 13: framed 2^15 w/ZS\n"); fprintf(stderr, "\t 14: framed QRSS 15: framed 2^23 w/ZS\n"); fprintf(stderr, "\t-u Set transmitter pulse shape, where:\n"); fprintf(stderr, "\t 0:T1-DSX 0-40m 1:T1-DSX 40-80m\n"); fprintf(stderr, "\t 2:T1-DSX 80-120m 3:T1-DSX 120-160m\n"); fprintf(stderr, "\t 4:T1-DSX 160-200m 5:E1-G.703 75ohm coax\n"); fprintf(stderr, "\t 6:E1-G.703 120ohm TP 7:T1-CSU Long range\n"); fprintf(stderr, "\t 8:auto-set based on cable length (T1 only)\n"); fprintf(stderr, "\t-U Set line build out where:\n"); fprintf(stderr, "\t 0:0dB 1:7.5dB 2:15dB 3:22.5dB\n"); fprintf(stderr, "\t 4:auto-set based on cable length\n"); fprintf(stderr, "\t-v Set verbose printout mode\n"); fprintf(stderr, "\t-x disable Transmitter outputs\n"); fprintf(stderr, "\t-X enable Transmitter outputs\n"); fprintf(stderr, "The -3 switch precedes T3 commands.\n"); fprintf(stderr, "\t-a Stop sending Yellow|Blue|AIS|Idle signal\n"); fprintf(stderr, "\t-A Start sending Yellow|Blue|AIS|Idle signal\n"); fprintf(stderr, "\t-B Send BOP msg 10 times\n"); fprintf(stderr, "\t-c Set cable length in meters\n"); fprintf(stderr, "\t-d Print status of T3 DSU/CSU\n"); fprintf(stderr, "\t-e Set T3 frame format, where:\n"); fprintf(stderr, "\t 100:C-Bit Parity 101:M13\n"); fprintf(stderr, "\t-f Read and print Framer registers\n"); fprintf(stderr, "\t-F Write Framer register\n"); fprintf(stderr, "\t-l Send 'Line Loop Down' BOP msg\n"); fprintf(stderr, "\t-L Send 'Line Loop Up' BOP msg\n"); fprintf(stderr, "\t-s Print status of T3 DSU/CSU\n"); fprintf(stderr, "\t-S Set DS3 scrambler mode, where:\n"); fprintf(stderr, "\t 1:OFF 2:DigitalLink|Kentrox 3:Larse\n"); fprintf(stderr, "\t-v Set verbose printout mode\n"); fprintf(stderr, "\t-V Write to T3 VCXO freq control DAC\n"); } static void call_driver(unsigned long cmd, struct iohdr *iohdr) { int error = 0; strncpy(iohdr->ifname, ifname, sizeof(iohdr->ifname)); iohdr->cookie = NGM_LMC_COOKIE; iohdr->iohdr = iohdr; /* Exchange data with a running device driver. */ #if defined(NETGRAPH) if (netgraph) { NgSendMsg(fdcs, ifname, NGM_LMC_COOKIE, cmd, iohdr, IOCPARM_LEN(cmd)); if (cmd & IOC_OUT) { int replen = sizeof(struct ng_mesg) + IOCPARM_LEN(cmd); char rep[replen]; /* storage for the reply */ struct ng_mesg *reply = (struct ng_mesg *)rep; int rl = NgRecvMsg(fdcs, reply, replen, NULL); if (rl == replen) bcopy(&reply->data, iohdr, IOCPARM_LEN(cmd)); else { fprintf(stderr, "%s: NgRecvMsg returned %d bytes, expected %d\n", progname, rl, replen); exit(1); } } } else #endif { if ((error = ioctl(fdcs, cmd, (caddr_t)iohdr)) < 0) { fprintf(stderr, "%s: ioctl() returned error code %d: %s\n", progname, errno, strerror(errno)); if (errno == ENETDOWN) printf("Type: 'ifconfig %s up' then try again.\n", ifname); exit(1); } } if (iohdr->cookie != NGM_LMC_COOKIE) { fprintf(stderr, "%s: cookie = 0x%08X, expected 0x%08X\n", progname, iohdr->cookie, NGM_LMC_COOKIE); fprintf(stderr, "%s: This version of %s is incompatible with the device driver\n", progname, progname); exit(1); } } static u_int32_t read_pci_config(u_int8_t addr) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_PCI; ioctl.address = addr; call_driver(LMCIOCREAD, &ioctl.iohdr); return ioctl.data; } static void write_pci_config(u_int8_t addr, u_int32_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_PCI; ioctl.address = addr; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static u_int32_t read_csr(u_int8_t addr) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_CSR; ioctl.address = addr; call_driver(LMCIOCREAD, &ioctl.iohdr); return ioctl.data; } static void write_csr(u_int8_t addr, u_int32_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_CSR; ioctl.address = addr; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static u_int16_t read_srom(u_int8_t addr) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_SROM; ioctl.address = addr; call_driver(LMCIOCREAD, &ioctl.iohdr); return ioctl.data; } static void write_srom(u_int8_t addr, u_int16_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_SROM; ioctl.address = addr; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static u_int8_t read_bios_rom(u_int32_t addr) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_BIOS; ioctl.address = addr; call_driver(LMCIOCREAD, &ioctl.iohdr); return ioctl.data; } static void write_bios_rom(u_int32_t addr, u_int8_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_BIOS; ioctl.address = addr; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static u_int16_t read_mii(u_int8_t addr) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_MII; ioctl.address = addr; call_driver(LMCIOCREAD, &ioctl.iohdr); return ioctl.data; } static void write_mii(u_int8_t addr, u_int16_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_MII; ioctl.address = addr; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static unsigned char read_framer(u_int16_t addr) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_FRAME; ioctl.address = addr; call_driver(LMCIOCREAD, &ioctl.iohdr); return ioctl.data; } static void write_framer(u_int16_t addr, u_int8_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RW_FRAME; ioctl.address = addr; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static void write_synth(struct synth synth) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_WO_SYNTH; bcopy(&synth, &ioctl.data, sizeof(synth)); call_driver(LMCIOCWRITE, &ioctl.iohdr); } static void write_dac(u_int16_t data) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOW; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_WO_DAC; ioctl.data = data; call_driver(LMCIOCWRITE, &ioctl.iohdr); } static void reset_xilinx(void) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_XILINX_RESET; call_driver(LMCIOCTL, &ioctl.iohdr); } static void load_xilinx_from_rom(void) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_XILINX_ROM; call_driver(LMCIOCTL, &ioctl.iohdr); } static void load_xilinx_from_file(char *ucode, u_int32_t len) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_XILINX_FILE; ioctl.data = len; ioctl.ucode = ucode; call_driver(LMCIOCTL, &ioctl.iohdr); } static void ioctl_snmp_send(u_int32_t send) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_SNMP_SEND; ioctl.data = send; call_driver(LMCIOCTL, &ioctl.iohdr); } void ioctl_snmp_loop(u_int32_t loop) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_SNMP_LOOP; ioctl.data = loop; call_driver(LMCIOCTL, &ioctl.iohdr); } static void ioctl_reset_cntrs(void) { struct ioctl ioctl; ioctl.iohdr.direction = DIR_IOWR; ioctl.iohdr.length = sizeof(struct ioctl); ioctl.cmd = IOCTL_RESET_CNTRS; call_driver(LMCIOCTL, &ioctl.iohdr); } static void ioctl_read_config(void) { config.iohdr.direction = DIR_IOWR; config.iohdr.length = sizeof(struct config); call_driver(LMCIOCGCFG, &config.iohdr); } static void ioctl_write_config(void) { config.iohdr.direction = DIR_IOW; config.iohdr.length = sizeof(struct config); call_driver(LMCIOCSCFG, &config.iohdr); } static void ioctl_read_status(void) { status.iohdr.direction = DIR_IOWR; status.iohdr.length = sizeof(struct status); call_driver(LMCIOCGSTAT, &status.iohdr); } static void print_card_name(void) { printf("Card name:\t\t%s\n", ifname); } static void print_card_type(void) { printf("Card type:\t\t"); switch(status.card_type) { case TLP_CSID_HSSI: printf("HSSI (lmc5200)\n"); break; case TLP_CSID_T3: printf("T3 (lmc5245)\n"); break; case TLP_CSID_SSI: printf("SSI (lmc1000)\n"); break; case TLP_CSID_T1E1: printf("T1E1 (lmc1200)\n"); break; case TLP_CSID_HSSIc: printf("HSSI (lmc5200C)\n"); break; default: printf("unknown card_type: %d\n", status.card_type); break; } } static void print_status(void) { char *status_string; if (status.oper_status == STATUS_UP) status_string = "Up"; else if (status.oper_status == STATUS_DOWN) status_string = "Down"; else if (status.oper_status == STATUS_TEST) status_string = "Test"; else status_string = "Unknown"; printf("Link status:\t\t%s\n", status_string); } static void print_tx_speed(void) { printf("Tx Speed:\t\t%u\n", status.tx_speed); } static void print_debug(void) { if (config.debug != 0) printf("Debug:\t\t\t%s\n", "On"); } static void print_line_prot(void) { char *on = "On", *off = "Off"; printf("Line Prot/Pkg:\t\t"); switch (status.line_prot) { case 0: printf("NotSet/"); break; case PROT_PPP: printf("PPP/"); break; case PROT_C_HDLC: printf("Cisco-HDLC/"); break; case PROT_FRM_RLY: printf("Frame-Relay/"); break; case PROT_IP_HDLC: printf("IP-in-HDLC/"); break; case PROT_ETH_HDLC: printf("Ether-in-HDLC/"); break; case PROT_X25: printf("X25+LAPB/"); break; default: printf("unknown line_prot: %d/", status.line_prot); break; } switch (status.line_pkg) { case 0: printf("NotSet\n"); break; case PKG_RAWIP: printf("Driver\n"); break; case PKG_NG: printf("Netgraph\n"); break; case PKG_GEN_HDLC: printf("GenHDLC\n"); break; case PKG_SPPP: printf("SPPP\n"); break; case PKG_P2P: printf("P2P\n"); break; default: printf("unknown line_pkg: %d\n", status.line_pkg); break; } if (status.line_pkg == PKG_SPPP) printf("SPPP Keep-alives:\t%s\n", config.keep_alive ? on : off); } static void print_crc_len(void) { printf("CRC length:\t\t"); if (config.crc_len == CFG_CRC_0) printf("no CRC\n"); else if (config.crc_len == CFG_CRC_16) printf("16 bits\n"); else if (config.crc_len == CFG_CRC_32) printf("32 bits\n"); else printf("bad crc_len: %d\n", config.crc_len); } static void print_loop_back(void) { printf("Loopback:\t\t"); switch (config.loop_back) { case CFG_LOOP_NONE: printf("None\n"); break; case CFG_LOOP_PAYLOAD: printf("Outward thru framer (payload loop)\n"); break; case CFG_LOOP_LINE: printf("Outward thru line interface (line loop)\n"); break; case CFG_LOOP_OTHER: printf("Inward thru line interface\n"); break; case CFG_LOOP_INWARD: printf("Inward thru framer\n"); break; case CFG_LOOP_DUAL: printf("Inward & outward (dual loop)\n"); break; case CFG_LOOP_TULIP: printf("Inward thru Tulip chip\n"); break; case CFG_LOOP_PINS: printf("Inward thru drvrs/rcvrs\n"); break; case CFG_LOOP_LL: printf("LA/LL asserted\n"); break; case CFG_LOOP_RL: printf("LB/RL asserted\n"); break; default: printf("unknown loop_back: %d\n", config.loop_back); break; } } static void print_tx_clk_src(void) { printf("Tx Clk src:\t\t"); switch (config.tx_clk_src) { case CFG_CLKMUX_ST: printf("Tx Clk from modem\n"); break; case CFG_CLKMUX_INT: printf("Internal source\n"); break; case CFG_CLKMUX_RT: printf("Rx Clk from modem (loop timed)\n"); break; case CFG_CLKMUX_EXT: printf("External connector\n"); break; default: printf("unknown tx_clk_src: %d\n", config.tx_clk_src); break; } } static void print_format(void) { printf("Format-Frame/Code:\t"); switch (config.format) { case CFG_FORMAT_T1SF: printf("T1-SF/AMI\n"); break; case CFG_FORMAT_T1ESF: printf("T1-ESF/B8ZS\n"); break; case CFG_FORMAT_E1FAS: printf("E1-FAS/HDB3\n"); break; case CFG_FORMAT_E1FASCRC: printf("E1-FAS+CRC/HDB3\n"); break; case CFG_FORMAT_E1FASCAS: printf("E1-FAS+CAS/HDB3\n"); break; case CFG_FORMAT_E1FASCRCCAS: printf("E1-FAS+CRC+CAS/HDB3\n"); break; case CFG_FORMAT_E1NONE: printf("E1-NOFRAMING/HDB3\n"); break; case CFG_FORMAT_T3CPAR: printf("T3-CParity/B3ZS\n"); break; case CFG_FORMAT_T3M13: printf("T3-M13/B3ZS\n"); break; default: printf("unknown format: %d\n", config.format); break; } } static void print_dte_dce(void) { printf("DTE or DCE:\t\t"); switch(config.dte_dce) { case CFG_DTE: printf("DTE (receiving TxClk)\n"); break; case CFG_DCE: printf("DCE (driving TxClk)\n"); break; default: printf("unknown dte_dce: %d\n", config.dte_dce); break; } } static void print_synth_freq(void) { double Fref = 20e6; double Fout, Fvco; /* decode the synthesizer params */ Fvco = (Fref * (config.synth.n<<(3*config.synth.v)))/config.synth.m; Fout = Fvco / (1<<(config.synth.x+config.synth.r+config.synth.prescale)); printf("Synth freq:\t\t%.0f\n", Fout); } static void synth_freq(unsigned long target) { unsigned int n, m, v, x, r; double Fout, Fvco, Ftarg; double newdiff, olddiff; double bestF=0.0, bestV=0.0; unsigned prescale = (target < 50000) ? 9:4; Ftarg = target< SYNTH_FMAX) continue; Fout = Fvco / (1<<(x+r)); if (Fout >= Ftarg) newdiff = Fout - Ftarg; else newdiff = Ftarg - Fout; if (bestF >= Ftarg) olddiff = bestF - Ftarg; else olddiff = Ftarg - bestF; if ((newdiff < olddiff) || ((newdiff == olddiff) && (Fvco < bestV))) { config.synth.n = n; config.synth.m = m; config.synth.v = v; config.synth.x = x; config.synth.r = r; config.synth.prescale = prescale; bestF = Fout; bestV = Fvco; } } #if 0 printf("Fbest=%.0f, Ftarg=%u, Fout=%.0f\n", bestF>>prescale, target, bestF); printf("N=%u, M=%u, V=%u, X=%u, R=%u\n", config.synth.n, config.synth.m, config.synth.v, config.synth.x, config.synth.r); #endif } static void print_cable_len(void) { printf("Cable length:\t\t%d meters\n", config.cable_len); } static void print_cable_type(void) { printf("Cable type:\t\t"); if (status.cable_type > 7) printf("unknown cable_type: %d\n", status.cable_type); else printf("%s\n", ssi_cables[status.cable_type]); } static void print_time_slots(void) { printf("TimeSlot [31-0]:\t0x%08X\n", config.time_slots); } static void print_scrambler(void) { printf("Scrambler:\t\t"); if (config.scrambler == CFG_SCRAM_OFF) printf("off\n"); else if (config.scrambler == CFG_SCRAM_DL_KEN) printf("DigLink/Kentrox: X^43+1\n"); else if (config.scrambler == CFG_SCRAM_LARS) printf("Larse: X^20+X^17+1 w/28ZS\n"); else printf("unknown scrambler: %d\n", config.scrambler); } static double vga_dbs(u_int8_t vga) { if (vga < 0x0F) return 0.0; if ((vga >= 0x0F) && (vga <= 0x1B)) return 0.0 + 0.77 * (vga - 0x0F); if ((vga >= 0x1C) && (vga <= 0x33)) return 10.0 + 1.25 * (vga - 0x1C); if ((vga >= 0x34) && (vga <= 0x39)) return 40.0 + 1.67 * (vga - 0x34); if ((vga >= 0x3A) && (vga < 0x3F)) return 50.0 + 2.80 * (vga - 0x3A); return 64.0; } static void print_rx_gain(void) { printf("Rx gain max:\t\t"); if (config.rx_gain == CFG_GAIN_AUTO) printf("auto-set to %02.1f dB\n", vga_dbs(read_framer(Bt8370_VGA_MAX) & 0x3F)); else printf("up to %02.1f dB\n", vga_dbs(config.rx_gain)); } static void print_tx_lbo(void) { u_int8_t saved_lbo = config.tx_lbo; printf("LBO = "); if (config.tx_lbo == CFG_LBO_AUTO) { config.tx_lbo = read_framer(Bt8370_TLIU_CR) & 0x30; printf("auto-set to "); } switch (config.tx_lbo) { case CFG_LBO_0DB: printf("0 dB\n"); break; case CFG_LBO_7DB: printf("7.5 dB\n"); break; case CFG_LBO_15DB: printf("15 dB\n"); break; case CFG_LBO_22DB: printf("22.5 dB\n"); break; default: printf("unknown tx_lbo: %d\n", config.tx_lbo); break; } if (saved_lbo == CFG_LBO_AUTO) config.tx_lbo = saved_lbo; } static void print_tx_pulse(void) { u_int8_t saved_pulse = config.tx_pulse; printf("Tx pulse shape:\t\t"); if (config.tx_pulse == CFG_PULSE_AUTO) { config.tx_pulse = read_framer(Bt8370_TLIU_CR) & 0x0E; printf("auto-set to "); } switch (config.tx_pulse) { case CFG_PULSE_T1DSX0: printf("T1-DSX: 0 to 40 meters\n"); break; case CFG_PULSE_T1DSX1: printf("T1-DSX: 40 to 80 meters\n"); break; case CFG_PULSE_T1DSX2: printf("T1-DSX: 80 to 120 meters\n"); break; case CFG_PULSE_T1DSX3: printf("T1-DSX: 120 to 160 meters\n"); break; case CFG_PULSE_T1DSX4: printf("T1-DSX: 160 to 200 meters\n"); break; case CFG_PULSE_E1COAX: printf("E1: Twin Coax\n"); break; case CFG_PULSE_E1TWIST: printf("E1: Twisted Pairs\n"); break; case CFG_PULSE_T1CSU: printf("T1-CSU; "); print_tx_lbo(); break; default: printf("unknown tx_pulse: %d\n", config.tx_pulse); break; } if (saved_pulse == CFG_PULSE_AUTO) config.tx_pulse = saved_pulse; } static void print_ssi_sigs(void) { u_int32_t mii16 = status.snmp.ssi.sigs; char *on = "On", *off = "Off"; printf("Modem signals:\t\tDTR=%s DSR=%s RTS=%s CTS=%s\n", (mii16 & MII16_SSI_DTR) ? on : off, (mii16 & MII16_SSI_DSR) ? on : off, (mii16 & MII16_SSI_RTS) ? on : off, (mii16 & MII16_SSI_CTS) ? on : off); printf("Modem signals:\t\tDCD=%s RI=%s LL=%s RL=%s TM=%s\n", (mii16 & MII16_SSI_DCD) ? on : off, (mii16 & MII16_SSI_RI) ? on : off, (mii16 & MII16_SSI_LL) ? on : off, (mii16 & MII16_SSI_RL) ? on : off, (mii16 & MII16_SSI_TM) ? on : off); } static void print_hssi_sigs(void) { u_int32_t mii16 = status.snmp.hssi.sigs; char *on = "On", *off = "Off"; printf("Modem signals:\t\tTA=%s CA=%s\n", (mii16 & MII16_HSSI_TA) ? on : off, (mii16 & MII16_HSSI_CA) ? on : off); printf("Modem signals:\t\tLA=%s LB=%s LC=%s TM=%s\n", (mii16 & MII16_HSSI_LA) ? on : off, (mii16 & MII16_HSSI_LB) ? on : off, (mii16 & MII16_HSSI_LC) ? on : off, (mii16 & MII16_HSSI_TM) ? on : off); } static void print_events(void) { const char *reset_time; time_t now; now = time(NULL); printf("Current time:\t\t%s", ctime(&now)); if (status.cntrs.reset_time.tv_sec < 1000) reset_time = "Never\n"; else reset_time = ctime(&status.cntrs.reset_time.tv_sec); printf("Cntrs reset:\t\t%s", reset_time); if (status.cntrs.ibytes) printf("Rx bytes:\t\t%ju\n", (uintmax_t)status.cntrs.ibytes); if (status.cntrs.obytes) printf("Tx bytes:\t\t%ju\n", (uintmax_t)status.cntrs.obytes); if (status.cntrs.ipackets) printf("Rx packets:\t\t%ju\n", (uintmax_t)status.cntrs.ipackets); if (status.cntrs.opackets) printf("Tx packets:\t\t%ju\n", (uintmax_t)status.cntrs.opackets); if (status.cntrs.ierrors) printf("Rx errors:\t\t%u\n", status.cntrs.ierrors); if (status.cntrs.oerrors) printf("Tx errors:\t\t%u\n", status.cntrs.oerrors); if (status.cntrs.idiscards) printf("Rx discards:\t\t%u\n", status.cntrs.idiscards); if (status.cntrs.odiscards) printf("Tx discards:\t\t%u\n", status.cntrs.odiscards); if (status.cntrs.fifo_over) printf("Rx fifo overruns:\t%u\n", status.cntrs.fifo_over); if (status.cntrs.fifo_under) printf("Tx fifo underruns:\t%u\n", status.cntrs.fifo_under); if (status.cntrs.missed) printf("Rx missed:\t\t%u\n", status.cntrs.missed); if (status.cntrs.overruns) printf("Rx overruns:\t\t%u\n", status.cntrs.overruns); if (status.cntrs.fdl_pkts) printf("Rx FDL pkts:\t\t%u\n", status.cntrs.fdl_pkts); if (status.cntrs.crc_errs) printf("Rx CRC:\t\t\t%u\n", status.cntrs.crc_errs); if (status.cntrs.lcv_errs) printf("Rx line code:\t\t%u\n", status.cntrs.lcv_errs); if (status.cntrs.frm_errs) printf("Rx F-bits:\t\t%u\n", status.cntrs.frm_errs); if (status.cntrs.febe_errs) printf("Rx FEBE:\t\t%u\n", status.cntrs.febe_errs); if (status.cntrs.par_errs) printf("Rx P-parity:\t\t%u\n", status.cntrs.par_errs); if (status.cntrs.cpar_errs) printf("Rx C-parity:\t\t%u\n", status.cntrs.cpar_errs); if (status.cntrs.mfrm_errs) printf("Rx M-bits:\t\t%u\n", status.cntrs.mfrm_errs); if (config.debug) { /* These events are hard to explain and may worry users, */ if (status.cntrs.rxdma) printf("Rx no buffs:\t\t%u\n", status.cntrs.rxdma); if (status.cntrs.txdma) printf("Tx no descs:\t\t%u\n", status.cntrs.txdma); if (status.cntrs.lck_watch) printf("Lck watch:\t\t%u\n", status.cntrs.lck_watch); if (status.cntrs.lck_ioctl) printf("Lck ioctl:\t\t%u\n", status.cntrs.lck_ioctl); if (status.cntrs.lck_intr) printf("Lck intr:\t\t%u\n", status.cntrs.lck_intr); } } static void print_summary(void) { switch(status.card_type) { case TLP_CSID_HSSI: { print_card_name(); print_card_type(); print_debug(); print_status(); print_tx_speed(); print_line_prot(); print_crc_len(); print_loop_back(); print_tx_clk_src(); print_hssi_sigs(); print_events(); break; } case TLP_CSID_T3: { print_card_name(); print_card_type(); print_debug(); print_status(); print_tx_speed(); print_line_prot(); print_crc_len(); print_loop_back(); print_format(); print_cable_len(); print_scrambler(); print_events(); break; } case TLP_CSID_SSI: { print_card_name(); print_card_type(); print_debug(); print_status(); print_tx_speed(); print_line_prot(); print_crc_len(); print_loop_back(); print_dte_dce(); print_synth_freq(); print_cable_type(); print_ssi_sigs(); print_events(); break; } case TLP_CSID_T1E1: { print_card_name(); print_card_type(); print_debug(); print_status(); print_tx_speed(); print_line_prot(); print_crc_len(); print_loop_back(); print_tx_clk_src(); print_format(); print_time_slots(); print_cable_len(); print_tx_pulse(); print_rx_gain(); print_events(); break; } case TLP_CSID_HSSIc: { print_card_name(); print_card_type(); print_debug(); print_status(); print_line_prot(); print_tx_speed(); print_crc_len(); print_loop_back(); print_tx_clk_src(); print_dte_dce(); print_synth_freq(); print_hssi_sigs(); print_events(); break; } default: { printf("%s: Unknown card type: %d\n", ifname, status.card_type); break; } } } static char * print_t3_bop(int bop_code) { switch(bop_code) { case 0x00: return "far end LOF"; case 0x0E: return "far end LOS"; case 0x16: return "far end AIS"; case 0x1A: return "far end IDL"; case 0x07: return "Line Loopback activate"; case 0x1C: return "Line Loopback deactivate"; case 0x1B: return "Entire DS3 line"; default: return "Unknown BOP code"; } } static void print_t3_snmp(void) { printf("SNMP performance data:\n"); printf(" LCV=%d", status.snmp.t3.lcv); printf(" LOS=%d", (status.snmp.t3.line & TLINE_LOS) ? 1 : 0); printf(" PCV=%d", status.snmp.t3.pcv); printf(" CCV=%d", status.snmp.t3.ccv); printf(" AIS=%d", (status.snmp.t3.line & TLINE_RX_AIS) ? 1 : 0); printf(" SEF=%d", (status.snmp.t3.line & T1LINE_SEF) ? 1 : 0); printf(" OOF=%d", (status.snmp.t3.line & TLINE_LOF) ? 1 : 0); printf(" FEBE=%d", status.snmp.t3.febe); printf(" RAI=%d", (status.snmp.t3.line & TLINE_RX_RAI) ? 1 : 0); printf("\n"); } static void print_t3_dsu(void) { char *no = "No", *yes = "Yes"; u_int16_t mii16 = read_mii(16); u_int8_t ctl1 = read_framer(T3CSR_CTL1); u_int8_t ctl8 = read_framer(T3CSR_CTL8); u_int8_t stat9 = read_framer(T3CSR_STAT9); u_int8_t ctl12 = read_framer(T3CSR_CTL12); u_int8_t stat16 = read_framer(T3CSR_STAT16); printf("Framing: \t\t%s\n", ctl1 & CTL1_M13MODE ? "M13" : "CPAR"); print_tx_speed(); printf("Scrambler: \t\t%s\n", mii16 & MII16_DS3_SCRAM ? yes : no); printf("Scram poly: \t\t%s\n", mii16 & MII16_DS3_POLY ? "X^20" : "X^43"); printf("Cable length \t\t%s\n", mii16 & MII16_DS3_ZERO ? "Short" : "Long"); printf("Line loop: \t\t%s\n", mii16 & MII16_DS3_LNLBK ? yes : no); printf("Payload loop: \t\t%s\n", ctl12 & CTL12_RTPLOOP ? yes : no); printf("Frame loop: \t\t%s\n", ctl1 & CTL1_3LOOP ? yes : no); printf("Host loop: \t\t%s\n", mii16 & MII16_DS3_TRLBK ? yes : no); printf("Transmit RAI: \t\t%s\n", ctl1 & CTL1_XTX ? no : yes); printf("Receive RAI \t\t%s\n", stat16 & STAT16_XERR ? yes : no); printf("Transmit AIS: \t\t%s\n", ctl1 & CTL1_TXAIS ? yes : no); printf("Receive AIS: \t\t%s\n", stat16 & STAT16_RAIS ? yes : no); printf("Transmit IDLE: \t\t%s\n", ctl1 & CTL1_TXIDL ? yes : no); printf("Receive IDLE: \t\t%s\n", stat16 & STAT16_RIDL ? yes : no); printf("Transmit BLUE: \t\t%s\n", ctl8 & CTL8_TBLU ? yes : no); printf("Receive BLUE: \t\t%s\n", stat9 & STAT9_RBLU ? yes : no); printf("Loss of Signal:\t\t%s\n", stat16 & STAT16_RLOS ? yes : no); printf("Loss of Frame: \t\t%s\n", stat16 & STAT16_ROOF ? yes : no); printf("Sev Err Frms: \t\t%s\n", stat16 & STAT16_SEF ? yes : no); printf("Code errors: \t\t%d\n", read_framer(T3CSR_CVLO) + (read_framer(T3CSR_CVHI)<<8)); printf("C-Par errors: \t\t%d\n", read_framer(T3CSR_CERR)); printf("P-Par errors: \t\t%d\n", read_framer(T3CSR_PERR)); printf("F-Bit errors: \t\t%d\n", read_framer(T3CSR_FERR)); printf("M-Bit errors: \t\t%d\n", read_framer(T3CSR_MERR)); printf("FarEndBitErrs: \t\t%d\n", read_framer(T3CSR_FEBE)); printf("Last Tx FEAC msg:\t0x%02X (%s)\n", read_framer(T3CSR_TX_FEAC) & 0x3F, print_t3_bop(read_framer(T3CSR_TX_FEAC) & 0x3F)); printf("Last dbl FEAC msg;\t0x%02X (%s)\n", read_framer(T3CSR_DBL_FEAC) & 0x3F, print_t3_bop(read_framer(T3CSR_DBL_FEAC) & 0x3F)); printf("Last Rx FEAC msg:\t0x%02X (%s)\n", read_framer(T3CSR_RX_FEAC) & 0x3F, print_t3_bop(read_framer(T3CSR_RX_FEAC) & 0x3F)); print_t3_snmp(); } static void t3_cmd(int argc, char **argv) { int ch; while ((ch = getopt(argc, argv, "a:A:B:c:de:fF:lLsS:vV:")) != -1) { switch (ch) { case 'a': /* stop alarms */ { switch (optarg[0]) { case 'a': /* Stop sending AIS Signal */ { write_mii(16, read_mii(16) & ~MII16_DS3_FRAME); write_framer(T3CSR_CTL1, read_framer(T3CSR_CTL1) & ~CTL1_TXAIS); if (verbose) printf("Stop sending Alarm Indication Signal (AIS)\n"); break; } case 'b': /* Stop sending Blue signal */ { write_mii(16, read_mii(16) & ~MII16_DS3_FRAME); write_framer(T3CSR_CTL8, read_framer(T3CSR_CTL8) & ~CTL8_TBLU); if (verbose) printf("Stop sending Blue signal\n"); break; } case 'i': /* Stop sending IDLE signal */ { write_framer(T3CSR_CTL1, read_framer(T3CSR_CTL1) & ~CTL1_TXIDL); if (verbose) printf("Stop sending IDLE signal\n"); break; } case 'y': /* Stop sending Yellow alarm */ { write_framer(T3CSR_CTL1, read_framer(T3CSR_CTL1) | CTL1_XTX); if (verbose) printf("Stop sending Yellow alarm\n"); break; } default: printf("Unknown alarm: %c\n", optarg[0]); break; } break; } case 'A': /* start alarms */ { switch (optarg[0]) { case 'a': /* Start sending AIS Signal */ { write_mii(16, read_mii(16) | MII16_DS3_FRAME); write_framer(T3CSR_CTL1, read_framer(T3CSR_CTL1) | CTL1_TXAIS); if (verbose) printf("Sending AIS signal (framed 1010..)\n"); break; } case 'b': /* Start sending Blue signal */ { write_mii(16, read_mii(16) | MII16_DS3_FRAME); write_framer(T3CSR_CTL8, read_framer(T3CSR_CTL8) | CTL8_TBLU); if (verbose) printf("Sending Blue signal (unframed all 1s)\n"); break; } case 'i': /* Start sending IDLE signal */ { write_framer(T3CSR_CTL1, read_framer(T3CSR_CTL1) | CTL1_TXIDL); if (verbose) printf("Sending IDLE signal (framed 1100..)\n"); break; } case 'y': /* Start sending Yellow alarm */ { write_framer(T3CSR_CTL1, read_framer(T3CSR_CTL1) & ~CTL1_XTX); if (verbose) printf("Sending Yellow alarm (X-bits=0)\n"); break; } default: printf("Unknown alarm: %c\n", optarg[0]); break; } break; } case 'B': /* send BOP msg */ { u_int8_t bop = strtoul(optarg, NULL, 0); write_framer(T3CSR_TX_FEAC, 0xC0 + bop); if (verbose) printf("Sent '0x%02X' BOP msg 10 times\n", bop); break; } case 'c': /* set cable length */ { config.cable_len = strtoul(optarg, NULL, 0); if (verbose) print_cable_len(); update = 1; break; } case 'd': /* DSU status */ case 's': /* deprecated */ { print_t3_dsu(); break; } case 'e': /* set framimg format */ { config.format = strtoul(optarg, NULL, 0); if (verbose) print_format(); update = 1; break; } case 'f': /* read and print framer regs */ { int i; printf("TXC03401 regs:\n"); printf(" 0 1 2 3 4 5 6 7"); for (i=0; i<21; i++) { if (i%8 == 0) printf("\n%02X: ", i); printf("%02X ", read_framer(i)); } printf("\n\n"); break; } case 'F': /* write framer reg */ { u_int32_t addr = strtoul(optarg, NULL, 0); u_int32_t data = strtoul(argv[optind++], NULL, 0); write_framer(addr, data); if (verbose) { data = read_framer(addr); printf("Write framer register: addr = 0x%02X data = 0x%02X\n", addr, data); } break; } case 'l': /* send DS3 line loopback deactivate BOP cmd */ { ioctl_snmp_send(TSEND_RESET); if (verbose) printf("Sent 'DS3 Line Loopback deactivate' BOP cmd\n"); break; } case 'L': /* send DS3 line loopback activate BOP cmd */ { ioctl_snmp_send(TSEND_LINE); if (verbose) printf("Sent 'DS3 Line Loopback activate' BOP cmd\n"); break; } case 'S': /* set scrambler */ { config.scrambler = strtoul(optarg, NULL, 0); if (verbose) print_scrambler(); update = 1; break; } case 'v': /* set verbose mode */ { verbose = 1; break; } case 'V': /* set T3 freq control DAC */ { u_int32_t dac = strtoul(optarg, NULL, 0); write_dac(dac); if (verbose) printf("VCXO DAC value is %d\n", dac); break; } default: { printf("Unknown command char: %c\n", ch); exit(1); } /* case */ } /* switch */ } /* while */ } /* proc */ static void print_test_pattern(int patt) { printf("Test Pattern:\t\t"); switch (patt) { case 0: printf("unframed X^11+X^9+1\n"); break; case 1: printf("unframed X^15+X^14+1\n"); break; case 2: printf("unframed X^20+X^17+1\n"); break; case 3: printf("unframed X^23+X^18+1\n"); break; case 4: printf("unframed X^11+X^9+1 w/7ZS\n"); break; case 5: printf("unframed X^15+X^14+1 w/7ZS\n"); break; case 6: printf("unframed X^20+X^17+1 w/14ZS (QRSS)\n"); break; case 7: printf("unframed X^23+X^18+1 w/14ZS\n"); break; case 8: printf("framed X^11+X^9+1\n"); break; case 9: printf("framed X^15+X^14+1\n"); break; case 10: printf("framed X^20+X^17+1\n"); break; case 11: printf("framed X^23+X^18+1\n"); break; - case 12:; + case 12: printf("framed X^11+X^9+1 w/7ZS\n"); break; case 13: printf("framed X^15+X^14+1 w/7ZS\n"); break; case 14: printf("framed X^20+X^17+1 w/14ZS (QRSS)\n"); break; case 15: printf("framed X^23+X^18+1 w/14ZS\n"); break; } } static char * print_t1_bop(int bop_code) { switch(bop_code) { case 0x00: return "Yellow Alarm (far end LOF)"; case 0x07: return "Line Loop up"; case 0x1C: return "Line Loop down"; case 0x0A: return "Payload Loop up"; case 0x19: return "Payload Loop down"; case 0x09: return "Network Loop up"; case 0x12: return "Network Loop down"; default: return "Unknown BOP code"; } } static void print_far_report(int index) { u_int16_t far = status.snmp.t1.prm[index]; printf(" SEQ=%d ", (far & T1PRM_SEQ)>>8); if (far & T1PRM_G1) printf("CRC=1"); else if (far & T1PRM_G2) printf("CRC=1 to 5"); else if (far & T1PRM_G3) printf("CRC=5 to 10"); else if (far & T1PRM_G4) printf("CRC=10 to 100"); else if (far & T1PRM_G5) printf("CRC=100 to 319"); else if (far & T1PRM_G6) printf("CRC>=320"); else printf("CRC=0"); printf(" SE=%d", (far & T1PRM_SE) ? 1 : 0); printf(" FE=%d", (far & T1PRM_FE) ? 1 : 0); printf(" LV=%d", (far & T1PRM_LV) ? 1 : 0); printf(" SL=%d", (far & T1PRM_SL) ? 1 : 0); printf(" LB=%d", (far & T1PRM_LB) ? 1 : 0); printf("\n"); } static void print_t1_snmp(void) { printf("SNMP Near-end performance data:\n"); printf(" LCV=%d", status.snmp.t1.lcv); printf(" LOS=%d", (status.snmp.t1.line & TLINE_LOS) ? 1 : 0); printf(" FE=%d", status.snmp.t1.fe); printf(" CRC=%d", status.snmp.t1.crc); printf(" AIS=%d", (status.snmp.t1.line & TLINE_RX_AIS) ? 1 : 0); printf(" SEF=%d", (status.snmp.t1.line & T1LINE_SEF) ? 1 : 0); printf(" OOF=%d", (status.snmp.t1.line & TLINE_LOF) ? 1 : 0); printf(" RAI=%d",(status.snmp.t1.line & TLINE_RX_RAI) ? 1 : 0); printf("\n"); if (config.format == CFG_FORMAT_T1ESF) { printf("ANSI Far-end performance reports:\n"); print_far_report(0); print_far_report(1); print_far_report(2); print_far_report(3); } } static void print_t1_dsu(void) { char *no = "No", *yes = "Yes"; u_int16_t mii16 = read_mii(16); u_int8_t isr0 = read_framer(Bt8370_ISR0); u_int8_t loop = read_framer(Bt8370_LOOP); u_int8_t vga_max = read_framer(Bt8370_VGA_MAX) & 0x3F; u_int8_t alm1 = read_framer(Bt8370_ALM1); u_int8_t alm3 = read_framer(Bt8370_ALM3); u_int8_t talm = read_framer(Bt8370_TALM); u_int8_t tpatt = read_framer(Bt8370_TPATT); u_int8_t tpulse = read_framer(Bt8370_TLIU_CR); u_int8_t vga; u_int8_t saved_pulse, saved_lbo; /* d/c write required before read */ write_framer(Bt8370_VGA, 0); vga = read_framer(Bt8370_VGA) & 0x3F; print_format(); print_time_slots(); print_tx_clk_src(); print_tx_speed(); saved_pulse = config.tx_pulse; config.tx_pulse = tpulse & 0x0E; saved_lbo = config.tx_lbo; config.tx_lbo = tpulse & 0x30; print_tx_pulse(); config.tx_pulse = saved_pulse; config.tx_lbo = saved_lbo; printf("Tx outputs: \t\t%sabled\n", (mii16 & MII16_T1_XOE) ? "En" : "Dis"); printf("Line impedance:\t\t%s ohms\n", (mii16 & MII16_T1_Z) ? "120" : "100"); printf("Max line loss: \t\t%4.1f dB\n", vga_dbs(vga_max)); printf("Cur line loss: \t\t%4.1f dB\n", vga_dbs(vga)); printf("Invert data: \t\t%s\n", (mii16 & MII16_T1_INVERT) ? yes : no); printf("Line loop: \t\t%s\n", (loop & LOOP_LINE) ? yes : no); printf("Payload loop: \t\t%s\n", (loop & LOOP_PAYLOAD) ? yes : no); printf("Framer loop: \t\t%s\n", (loop & LOOP_FRAMER) ? yes : no); printf("Analog loop: \t\t%s\n", (loop & LOOP_ANALOG) ? yes : no); printf("Tx AIS: \t\t%s\n", ((talm & TALM_TAIS) || ((talm & TALM_AUTO_AIS) && (alm1 & ALM1_RLOS))) ? yes : no); printf("Rx AIS: \t\t%s\n", (alm1 & ALM1_RAIS) ? yes : no); if (((config.format & 1)==0) && (config.format != CFG_FORMAT_E1NONE)) { printf("Tx RAI: \t\t%s\n", ((talm & TALM_TYEL) || ((talm & TALM_AUTO_YEL) && (alm3 & ALM3_FRED))) ? yes : no); printf("Rx RAI: \t\t%s\n", (alm1 & ALM1_RYEL) ? yes : no); } if (config.format == CFG_FORMAT_T1ESF) { printf("Tx BOP RAI: \t\t%s\n", (alm1 & ALM1_RLOF) ? yes : no); printf("Rx BOP RAI: \t\t%s\n", (alm1 & ALM1_RMYEL) ? yes : no); } if ((config.format & 0x11) == 0x10) /* E1CAS */ { printf("Rx TS16 AIS: \t\t%s\n", (alm3 & ALM3_RMAIS) ? yes : no); printf("Tx TS16 RAI; \t\t%s\n", ((talm & TALM_AUTO_MYEL) && (alm3 & ALM3_SRED)) ? yes : no); } printf("Rx LOS analog: \t\t%s\n", (alm1 & ALM1_RALOS) ? yes : no); printf("Rx LOS digital:\t\t%s\n", (alm1 & ALM1_RLOS) ? yes : no); printf("Rx LOF: \t\t%s\n", (alm1 & ALM1_RLOF) ? yes : no); printf("Tx QRS: \t\t%s\n", (tpatt & 0x10) ? yes : no); printf("Rx QRS: \t\t%s\n", (isr0 & 0x10) ? yes : no); printf("LCV errors: \t\t%d\n", read_framer(Bt8370_LCV_LO) + (read_framer(Bt8370_LCV_HI)<<8)); if (config.format != CFG_FORMAT_E1NONE) { if ((config.format & 1)==0) printf("Far End Block Errors:\t%d\n", read_framer(Bt8370_FEBE_LO) + (read_framer(Bt8370_FEBE_HI)<<8)); printf("CRC errors: \t\t%d\n", read_framer(Bt8370_CRC_LO) + (read_framer(Bt8370_CRC_HI)<<8)); printf("Frame errors: \t\t%d\n", read_framer(Bt8370_FERR_LO) + (read_framer(Bt8370_FERR_HI)<<8)); printf("Sev Err Frms: \t\t%d\n", read_framer(Bt8370_AERR) & 0x03); printf("Change of Frm align:\t%d\n", (read_framer(Bt8370_AERR) & 0x0C)>>2); printf("Loss of Frame events:\t%d\n", (read_framer(Bt8370_AERR) & 0xF0)>>4); } if (config.format == CFG_FORMAT_T1ESF) { printf("Last Tx BOP msg:\t0x%02X (%s)\n", read_framer(Bt8370_TBOP), print_t1_bop(read_framer(Bt8370_TBOP))); printf("Last Rx BOP msg:\t0x%02X (%s)\n", read_framer(Bt8370_RBOP), print_t1_bop(read_framer(Bt8370_RBOP)&0x3F)); } print_t1_snmp(); } static void t1_cmd(int argc, char **argv) { int ch; while ((ch = getopt(argc, argv, "a:A:B:c:de:E:fF:g:iIlLpPstT:u:U:vxX")) != -1) { switch (ch) { case 'a': /* stop alarms */ { switch (optarg[0]) { case 'y': /* Stop sending Yellow Alarm */ { if ((config.format == CFG_FORMAT_T1SF) || (config.format == CFG_FORMAT_E1NONE)) printf("No Yellow alarm for this frame format\n"); else if (config.format == CFG_FORMAT_T1ESF) write_framer(Bt8370_BOP, 0xE0); /* rbop 25, tbop off */ else { u_int8_t talm = read_framer(Bt8370_TALM); write_framer(Bt8370_TALM, talm & ~TALM_TYEL); } if (verbose) printf("Stop sending Yellow alarm\n"); break; } case 'a': /* Stop sending AIS */ case 'b': /* Stop sending Blue Alarm */ { u_int8_t talm = read_framer(Bt8370_TALM); write_framer(Bt8370_TALM, talm & ~TALM_TAIS); if (verbose) printf("Stop sending AIS/Blue signal\n"); break; } default: printf("Unknown alarm: %c\n", optarg[0]); } break; } case 'A': /* start alarms */ { switch (optarg[0]) { case 'y': /* Start sending Yellow Alarm */ { if ((config.format == CFG_FORMAT_T1SF) || (config.format == CFG_FORMAT_E1NONE)) printf("No Yellow alarm for this frame format\n"); else if (config.format == CFG_FORMAT_T1ESF) { write_framer(Bt8370_BOP, 0x0F); /* rbop off, tbop cont */ write_framer(Bt8370_TBOP, T1BOP_OOF); } else { u_int8_t talm = read_framer(Bt8370_TALM); write_framer(Bt8370_TALM, talm | TALM_TYEL); } if (verbose) printf("Sending Yellow alarm\n"); break; } case 'a': /* Start sending AIS */ case 'b': /* Start sending Blue Alarm */ { u_int8_t talm = read_framer(Bt8370_TALM); write_framer(Bt8370_TALM, talm | TALM_TAIS); if (verbose) printf("Sending AIS/Blue signal\n"); break; } default: printf("Unknown alarm: %c\n", optarg[0]); } break; } case 'B': /* send BOP msg */ { u_int8_t bop = strtoul(optarg, NULL, 0); if (config.format == CFG_FORMAT_T1ESF) { write_framer(Bt8370_BOP, 0x0B); /* rbop off, tbop 25 */ write_framer(Bt8370_TBOP, bop); /* start sending BOP msg */ sleep(1); /* sending 25 BOP msgs takes about 100 ms. */ write_framer(Bt8370_BOP, 0xE0); /* rbop 25, tbop off */ if (verbose) printf("Sent '0x%02X' BOP msg 25 times\n", bop); } else printf("BOP msgs only work in T1-ESF format\n"); break; } case 'c': /* set cable length */ { config.cable_len = strtoul(optarg, NULL, 0); if (verbose) print_cable_len(); update = 1; break; } case 'd': /* DSU status */ case 's': /* deprecated */ { print_t1_dsu(); break; } case 'e': /* set framimg format */ { config.format = strtoul(optarg, NULL, 0); if (verbose) print_format(); update = 1; break; } case 'E': /* set time slots */ { config.time_slots = strtoul(optarg, NULL, 16); if (verbose) print_time_slots(); update = 1; break; } case 'f': /* read and print framer regs */ { int i; printf("Bt8370 regs:\n"); printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F"); for (i=0; i<512; i++) { if (i%16 == 0) printf("\n%03X: ", i); printf("%02X ", read_framer(i)); } printf("\n\n"); break; } case 'F': /* write framer reg */ { u_int32_t addr = strtoul(optarg, NULL, 0); u_int32_t data = strtoul(argv[optind++], NULL, 0); write_framer(addr, data); if (verbose) { data = read_framer(addr); printf("Write framer register: addr = 0x%02X data = 0x%02X\n", addr, data); } break; } case 'g': /* set receiver gain */ { config.rx_gain = strtoul(optarg, NULL, 0); if (verbose) print_rx_gain(); update = 1; break; } case 'i': /* send CSU loopback deactivate inband cmd */ { if (config.format == CFG_FORMAT_T1SF) { if (verbose) printf("Sending 'CSU loop down' inband cmd for 10 secs..."); ioctl_snmp_send(TSEND_RESET); sleep(10); ioctl_snmp_send(TSEND_NORMAL); if (verbose) printf("done\n"); } else printf("Inband loopback cmds only work in T1-SF format"); break; } case 'I': /* send CSU loopback activate inband cmd */ { if (config.format == CFG_FORMAT_T1SF) { if (verbose) printf("Sending 'CSU loop up' inband cmd for 10 secs..."); ioctl_snmp_send(TSEND_LINE); sleep(10); ioctl_snmp_send(TSEND_NORMAL); if (verbose) printf("done\n"); } else printf("Inband loopback cmds only work in T1-SF format"); break; } case 'l': /* send line loopback deactivate BOP msg */ { if (config.format == CFG_FORMAT_T1ESF) { ioctl_snmp_send(TSEND_RESET); if (verbose) printf("Sent 'Line Loop Down' BOP cmd\n"); } else printf("BOP msgs only work in T1-ESF format\n"); break; } case 'L': /* send line loopback activate BOP msg */ { if (config.format == CFG_FORMAT_T1ESF) { ioctl_snmp_send(TSEND_LINE); if (verbose) printf("Sent 'Line Loop Up' BOP cmd\n"); } else printf("BOP msgs only work in T1-ESF format\n"); break; } case 'p': /* send payload loopback deactivate BOP msg */ { if (config.format == CFG_FORMAT_T1ESF) { ioctl_snmp_send(TSEND_RESET); if (verbose) printf("Sent 'Payload Loop Down' BOP cmd\n"); } else printf("BOP msgs only work in T1-ESF format\n"); break; } case 'P': /* send payload loopback activate BOP msg */ { if (config.format == CFG_FORMAT_T1ESF) { ioctl_snmp_send(TSEND_PAYLOAD); if (verbose) printf("Sent 'Payload Loop Up' BOP cmd\n"); } else printf("BOP msgs only work in T1-ESF format\n"); break; } case 't': /* stop sending test pattern */ { ioctl_snmp_send(TSEND_NORMAL); if (verbose) printf("Stop sending test pattern\n"); break; } case 'T': /* start sending test pattern */ { u_int8_t patt = strtoul(optarg, NULL, 0); write_framer(Bt8370_TPATT, 0x10 + patt); write_framer(Bt8370_RPATT, 0x30 + patt); if (verbose) print_test_pattern(patt); break; } case 'u': /* set transmit pulse shape */ { config.tx_pulse = strtoul(optarg, NULL, 0); if (verbose) print_tx_pulse(); update = 1; break; } case 'U': /* set tx line build-out */ { if (config.tx_pulse == CFG_PULSE_T1CSU) { config.tx_lbo = strtoul(optarg, NULL, 0); if (verbose) print_tx_pulse(); update = 1; } else printf("LBO only meaningful if Tx Pulse is T1CSU\n"); break; } case 'v': /* set verbose mode */ { verbose = 1; break; } case 'x': /* disable transmitter outputs */ { write_mii(16, read_mii(16) & ~MII16_T1_XOE); if (verbose) printf("Transmitter outputs disabled\n"); break; } case 'X': /* enable transmitter outputs */ { write_mii(16, read_mii(16) | MII16_T1_XOE); if (verbose) printf("Transmitter outputs enabled\n"); break; } default: { printf("Unknown command char: %c\n", ch); exit(1); } /* case */ } /* switch */ } /* while */ } /* proc */ /* used when reading Motorola S-Record format ROM files */ static unsigned char read_hex(FILE *f) { unsigned char a, b, c; for (a=0, b=0; a<2; a++) { c = fgetc(f); c -= 48; if (c > 9) c -= 7; b = (b<<4) | (c & 0xF); } checksum += b; return b; } static void load_xilinx(char *name) { FILE *f; char *ucode; int i, length; int c; if (verbose) printf("Load firmware from file %s...\n", name); if ((f = fopen(name, "r")) == NULL) { perror("Failed to open file"); exit(1); } ucode = (char *)malloc(8192); bzero(ucode, 8192); c = fgetc(f); if (c == 'X') { /* Xilinx raw bits file (foo.rbt) */ /* skip seven lines of boiler plate */ for (i=0; i<7;) if ((c=fgetc(f))=='\n') i++; /* build a dense bit array */ i = length = 0; while ((c=fgetc(f))!=EOF) { /* LSB first */ if (c=='1') ucode[length] |= 1<>= 1) crc = (crc >> 1) ^ (((crc ^ data) & 1) ? poly : 0); return crc; } /* 8-bit CRC calculated left-to-right over 16-bit words */ static u_int8_t crc8(u_int16_t *bufp, int len) { int bit, i; u_int16_t data; u_int8_t crc = 0xFF; u_int8_t poly = 0x07; for (i = 0; i < len; i++) for (data = *bufp++, bit = 15; bit >= 0; bit--) { if ((i==8) && (bit==7)) break; crc = (crc << 1) ^ ((((crc >> 7) ^ (data >> bit)) & 1) ? poly : 0); } return crc; } /* HSSI=3, DS3=4, SSI=5, T1E1=6, HSSIc=7, SDSL=8 */ void init_srom(int board) { int i; u_int16_t srom[64]; /* zero the entire rom */ for (i=0; i<64; i++) srom[i] = 0; srom[0] = 0x1376; /* subsys vendor id */ srom[1] = board ? board : (read_mii(3)>>4 & 0xF) +1; srom[8] = crc8(srom, 9); /* Tulip hardware checks this checksum */ srom[10] = 0x6000; /* ethernet address */ srom[11] = 0x0099; /* ethernet address */ srom[12] = 0x0000; /* ethernet address */ /* srom checksum is low 16 bits of Ethernet CRC-32 */ srom[63] = crc32((char *)srom, 126) ^ 0xFFFFFFFFL; /* write the SROM */ #if 1 /* really write it */ for (i=0; i<64; i++) write_srom(i, srom[i]); #else /* print what would be written */ printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F"); for (i=0; i<64; i++) { if (i%8 == 0) printf("\n%02X: ", i<<1); printf("%02X %02X ", srom[i] & 0xFF, srom[i]>>8); } printf("\n\n"); #endif } int main(int argc, char **argv) { int i, error, ch; char *optstring = "13a:bBcCdDeEf:Fhi:L:mM:pP:sS:tT:uUvVwW:xXyYzZ?"; progname = (char *)argv[0]; /* Here is the overall plan: * 1) Read the interface name from the command line. * 2) Open the device; decide if netgraph is being used. * 3) Read the current interface configuration from the driver. * 4) Read the command line args and carry out their actions. * 5) Write the modified interface configuration to the driver. */ /* 1) Read the interface name from the command line. */ #if __linux__ ifname = (argc==1) ? "hdlc0" : (char *) argv[1]; #else ifname = (argc==1) ? DEVICE_NAME"0" : (char *) argv[1]; #endif /* 2) Open the device; decide if netgraph is being used, */ /* use netgraph if ifname ends with ":" */ for (i=0; i<16; i++) if (ifname[i] == 0) break; /* Get a socket type file descriptor. */ #if defined(NETGRAPH) if ((netgraph = (ifname[i-1] == ':'))) error = NgMkSockNode(NULL, &fdcs, NULL); else #endif error = fdcs = socket(AF_INET, SOCK_DGRAM, 0); if (error < 0) { fprintf(stderr, "%s: %s() failed: %s\n", progname, netgraph? "NgMkSockNode" : "socket", strerror(errno)); exit(1); } /* 3) Read the current interface configuration from the driver. */ ioctl_read_config(); ioctl_read_status(); summary = (argc <= 2); /* print summary at end */ update = 0; /* write to card at end */ /* 4) Read the command line args and carry out their actions. */ optind = 2; while (((ch = getopt(argc, argv, optstring)) != -1) && (argc > 2)) { switch (ch) { case '1': /* T1 commands */ { if (verbose) printf("Doing T1 settings\n"); if (status.card_type != TLP_CSID_T1E1) { printf("T1 settings only apply to T1E1 cards\n"); exit(1); } t1_cmd(argc, argv); break; } case '3': /* T3 commands */ { if (verbose) printf("Doing T3 settings\n"); if (status.card_type != TLP_CSID_T3) { printf("T3 settings only apply to T3 cards\n"); exit(1); } t3_cmd(argc, argv); break; } case 'a': /* clock source */ { if ((status.card_type != TLP_CSID_T1E1) || (status.card_type != TLP_CSID_HSSI) || (status.card_type != TLP_CSID_HSSIc)) { if (verbose) print_tx_clk_src(); config.tx_clk_src = strtoul(optarg, NULL, 0); update = 1; } else printf("txclksrc only applies to T1E1 and HSSI card types\n"); break; } case 'b': /* read bios rom */ { int i; printf("Bios ROM:\n"); printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F"); for (i=0; i<256; i++) { if (i%16 == 0) printf("\n%02X: ", i); printf("%02X ", read_bios_rom(i)); } printf("\n\n"); break; } case 'B': /* write bios rom */ { int i; for (i=0; i<256; i++) write_bios_rom(i, 255-i); if (verbose) printf("wrote (0..255) to bios rom addrs (0..255)\n"); break; } case 'c': /* set crc_len = 16 */ { config.crc_len = CFG_CRC_16; if (verbose) print_crc_len(); update = 1; break; } case 'C': /* set crc_len = 32 */ { config.crc_len = CFG_CRC_32; if (verbose) print_crc_len(); update = 1; break; } case 'd': /* clear DEBUG flag */ { config.debug = 0; if (verbose) printf("DEBUG flag cleared\n"); update = 1; break; } case 'D': /* set DEBUG flag */ { config.debug = 1; if (verbose) printf("DEBUG flag set\n"); update = 1; break; } case 'e': /* set DTE (default) */ { if ((status.card_type == TLP_CSID_SSI) || (status.card_type == TLP_CSID_HSSIc)) { config.dte_dce = CFG_DTE; if (verbose) print_dte_dce(); update = 1; } else printf("DTE cmd only applies to SSI & HSSIc cards\n"); break; } case 'E': /* set DCE */ { if ((status.card_type == TLP_CSID_SSI) || (status.card_type == TLP_CSID_HSSIc)) { config.dte_dce = CFG_DCE; if (verbose) print_dte_dce(); update = 1; } else printf("DCE cmd only applies to SSI & HSSIc cards\n"); break; } case 'f': /* set synth osc freq */ { if ((status.card_type == TLP_CSID_SSI) || (status.card_type == TLP_CSID_HSSIc)) { synth_freq(strtoul(optarg, NULL, 0)); write_synth(config.synth); if (verbose) print_synth_freq(); } else printf("synth osc freq only applies to SSI & HSSIc cards\n"); break; } case 'F': /* set SPPP line protocol to Frame-Relay */ { config.line_prot = PROT_FRM_RLY; config.keep_alive = 1; /* required for LMI operation */ if (verbose) printf("SPPP line protocol set to Frame-Relay\n"); update = 1; break; } case 'h': /* help */ case '?': { usage(); exit(0); } case 'i': /* interface name */ { /* already scanned this */ break; } case 'L': /* set loopback modes */ { config.loop_back = strtoul(optarg, NULL, 0); if (verbose) print_loop_back(); update = 1; break; } case 'm': /* read and print MII regs */ { printf("MII regs:\n"); printf(" 0 1 2 3 4 5 6 7"); for (i=0; i<32; i++) { u_int16_t mii = read_mii(i); if (i%8 == 0) printf("\n%02X: ", i); printf("%04X ", mii); } printf("\n\n"); break; } case 'M': /* write MII reg */ { u_int32_t addr = strtoul(optarg, NULL, 0); u_int32_t data = strtoul(argv[optind++], NULL, 0); write_mii(addr, data); if (verbose) { data = read_mii(addr); printf("Write mii register: addr = 0x%02X data = 0x%04X\n", addr, data); } break; } case 'p': /* read and print PCI config regs */ { int i; printf("21140A PCI Config regs:\n"); printf(" 0 1 2 3"); for (i=0; i<16; i++) { if (i%4 == 0) printf("\n%X: ", i); printf("%08X ", read_pci_config(i<<2)); } printf("\n\n"); break; } case 'P': /* write PCI config reg */ { u_int32_t addr = strtoul(optarg, NULL, 0); u_int32_t data = strtoul(argv[optind++], NULL, 0); write_pci_config(addr, data); if (verbose) { data = read_pci_config(addr); printf("Write PCI config reg: addr = 0x%02X data = 0x%08X\n", addr, data); } break; } case 's': /* read and print Tulip SROM */ { int i; printf("21140A SROM:\n"); printf(" 0 1 2 3 4 5 6 7 8 9 A B C D E F"); for (i=0; i<64; i++) { u_int16_t srom = read_srom(i); if (i%8 == 0) printf("\n%02X: ", i<<1); printf("%02X %02X ", srom & 0xFF, srom>>8); } printf("\n\n"); break; } case 'S': /* write Tulip SROM loc */ { #if 0 /* write a single location -- not too useful */ u_int32_t addr = strtoul(optarg, NULL, 0); u_int32_t data = strtoul(argv[optind++], NULL, 0); write_mii(addr, data); data = read_mii(addr); printf("Write SROM: addr = 0x%02X data = 0x%04X\n", addr, data); #endif #if 0 /* write the whole SROM -- very dangerous */ init_srom(strtoul(optarg, NULL, 0)); #endif printf("Caution! Recompile %s to enable this.\n", progname); break; } case 't': /* read and print Tulip CSRs */ { int i; printf("21140A CSRs:\n"); printf(" 0 1 2 3"); for (i=0; i<16; i++) { if (i%4 == 0) printf("\n%X: ", i); printf("%08X ", read_csr(i)); } printf("\n\n"); break; } case 'T': /* write Tulip CSR */ { u_int32_t addr = strtoul(optarg, NULL, 0); u_int32_t data = strtoul(argv[optind++], NULL, 0); write_csr(addr, data); if (verbose) { data = read_csr(addr); printf("Write 21140A CSR: addr = 0x%02X data = 0x%08X\n", addr, data); } break; } case 'u': /* reset event counters */ { ioctl_reset_cntrs(); if (verbose) printf("Event counters reset\n"); break; } case 'U': /* reset gate array */ { reset_xilinx(); if (verbose) printf("gate array reset\n"); break; } case 'v': /* set verbose mode */ { verbose = 1; break; } case 'V': /* print card configuration */ { summary = 1; break; } case 'w': /* load gate array microcode from ROM */ { load_xilinx_from_rom(); if (verbose) printf("gate array configured from on-board ROM\n"); break; } case 'W': /* load gate array microcode from file */ { load_xilinx(optarg); if (verbose) printf("gate array configured from file %s\n", optarg); break; } case 'x': /* select RAWIP protocol */ { config.line_pkg = PKG_RAWIP; if (verbose) printf("RAWIP mode selected\n"); update = 1; break; } case 'X': /* Select in-kernel line protocol packages */ { config.line_pkg = 0; if (verbose) printf("line protocol mode selected\n"); update = 1; break; } case 'y': /* disable SPPP keep-alive packets */ { if ((config.line_pkg == PKG_SPPP) && (config.line_prot == PROT_FRM_RLY)) printf("keep-alives must be ON for Frame-Relay/SPPP\n"); else { config.keep_alive = 0; if (verbose) printf("SPPP keep-alive packets disabled\n"); update = 1; } break; } case 'Y': /* enable SPPP keep-alive packets */ { config.keep_alive = 1; if (verbose) printf("SPPP keep-alive packets enabled\n"); update = 1; break; } case 'z': /* set SPPP line protocol to Cisco HDLC */ { config.line_prot = PROT_C_HDLC; config.keep_alive = 1; if (verbose) printf("SPPP line protocol set to Cisco-HDLC\n"); update = 1; break; } case 'Z': /* set SPPP line protocol to PPP */ { config.line_prot = PROT_PPP; config.keep_alive = 0; if (verbose) printf("SPPP line protocol set to PPP\n"); update = 1; break; } default: { printf("Unknown command char: %c\n", ch); exit(1); } } /* switch */ } /* while */ if (summary) print_summary(); /* 5) Write the modified interface configuration to the driver. */ if (update) ioctl_write_config(); exit(0); } Index: head/usr.sbin/mountd/mountd.c =================================================================== --- head/usr.sbin/mountd/mountd.c (revision 298088) +++ head/usr.sbin/mountd/mountd.c (revision 298089) @@ -1,3290 +1,3290 @@ /* * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Herb Hasler and 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. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ #ifndef lint static const char copyright[] = "@(#) Copyright (c) 1989, 1993\n\ The Regents of the University of California. All rights reserved.\n"; #endif /*not lint*/ #if 0 #ifndef lint static char sccsid[] = "@(#)mountd.c 8.15 (Berkeley) 5/1/95"; #endif /*not lint*/ #endif #include __FBSDID("$FreeBSD$"); #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 #include #include #include #include #include "pathnames.h" #include "mntopts.h" #ifdef DEBUG #include #endif /* * Structures for keeping the mount list and export list */ struct mountlist { struct mountlist *ml_next; char ml_host[MNTNAMLEN+1]; char ml_dirp[MNTPATHLEN+1]; }; struct dirlist { struct dirlist *dp_left; struct dirlist *dp_right; int dp_flag; struct hostlist *dp_hosts; /* List of hosts this dir exported to */ char dp_dirp[1]; /* Actually malloc'd to size of dir */ }; /* dp_flag bits */ #define DP_DEFSET 0x1 #define DP_HOSTSET 0x2 struct exportlist { struct exportlist *ex_next; struct dirlist *ex_dirl; struct dirlist *ex_defdir; int ex_flag; fsid_t ex_fs; char *ex_fsdir; char *ex_indexfile; int ex_numsecflavors; int ex_secflavors[MAXSECFLAVORS]; int ex_defnumsecflavors; int ex_defsecflavors[MAXSECFLAVORS]; }; /* ex_flag bits */ #define EX_LINKED 0x1 struct netmsk { struct sockaddr_storage nt_net; struct sockaddr_storage nt_mask; char *nt_name; }; union grouptypes { struct addrinfo *gt_addrinfo; struct netmsk gt_net; }; struct grouplist { int gr_type; union grouptypes gr_ptr; struct grouplist *gr_next; int gr_numsecflavors; int gr_secflavors[MAXSECFLAVORS]; }; /* Group types */ #define GT_NULL 0x0 #define GT_HOST 0x1 #define GT_NET 0x2 #define GT_DEFAULT 0x3 #define GT_IGNORE 0x5 struct hostlist { int ht_flag; /* Uses DP_xx bits */ struct grouplist *ht_grp; struct hostlist *ht_next; }; struct fhreturn { int fhr_flag; int fhr_vers; nfsfh_t fhr_fh; int fhr_numsecflavors; int *fhr_secflavors; }; #define GETPORT_MAXTRY 20 /* Max tries to get a port # */ /* Global defs */ static char *add_expdir(struct dirlist **, char *, int); static void add_dlist(struct dirlist **, struct dirlist *, struct grouplist *, int, struct exportlist *); static void add_mlist(char *, char *); static int check_dirpath(char *); static int check_options(struct dirlist *); static int checkmask(struct sockaddr *sa); static int chk_host(struct dirlist *, struct sockaddr *, int *, int *, int *, int **); static char *strsep_quote(char **stringp, const char *delim); static int create_service(struct netconfig *nconf); static void complete_service(struct netconfig *nconf, char *port_str); static void clearout_service(void); static void del_mlist(char *hostp, char *dirp); static struct dirlist *dirp_search(struct dirlist *, char *); static int do_mount(struct exportlist *, struct grouplist *, int, struct xucred *, char *, int, struct statfs *); static int do_opt(char **, char **, struct exportlist *, struct grouplist *, int *, int *, struct xucred *); static struct exportlist *ex_search(fsid_t *); static struct exportlist *get_exp(void); static void free_dir(struct dirlist *); static void free_exp(struct exportlist *); static void free_grp(struct grouplist *); static void free_host(struct hostlist *); static void get_exportlist(void); static int get_host(char *, struct grouplist *, struct grouplist *); static struct hostlist *get_ht(void); static int get_line(void); static void get_mountlist(void); static int get_net(char *, struct netmsk *, int); static void getexp_err(struct exportlist *, struct grouplist *); static struct grouplist *get_grp(void); static void hang_dirp(struct dirlist *, struct grouplist *, struct exportlist *, int); static void huphandler(int sig); static int makemask(struct sockaddr_storage *ssp, int bitlen); static void mntsrv(struct svc_req *, SVCXPRT *); static void nextfield(char **, char **); static void out_of_mem(void); static void parsecred(char *, struct xucred *); static int parsesec(char *, struct exportlist *); static int put_exlist(struct dirlist *, XDR *, struct dirlist *, int *, int); static void *sa_rawaddr(struct sockaddr *sa, int *nbytes); static int sacmp(struct sockaddr *sa1, struct sockaddr *sa2, struct sockaddr *samask); static int scan_tree(struct dirlist *, struct sockaddr *); static void usage(void); static int xdr_dir(XDR *, char *); static int xdr_explist(XDR *, caddr_t); static int xdr_explist_brief(XDR *, caddr_t); static int xdr_explist_common(XDR *, caddr_t, int); static int xdr_fhs(XDR *, caddr_t); static int xdr_mlist(XDR *, caddr_t); static void terminate(int); static struct exportlist *exphead; static struct mountlist *mlhead; static struct grouplist *grphead; static char *exnames_default[2] = { _PATH_EXPORTS, NULL }; static char **exnames; static char **hosts = NULL; static struct xucred def_anon = { XUCRED_VERSION, (uid_t)-2, 1, { (gid_t)-2 }, NULL }; static int force_v2 = 0; static int resvport_only = 1; static int nhosts = 0; static int dir_only = 1; static int dolog = 0; static int got_sighup = 0; static int xcreated = 0; static char *svcport_str = NULL; static int mallocd_svcport = 0; static int *sock_fd; static int sock_fdcnt; static int sock_fdpos; static int suspend_nfsd = 0; static int opt_flags; static int have_v6 = 1; static int v4root_phase = 0; static char v4root_dirpath[PATH_MAX + 1]; static int has_publicfh = 0; static struct pidfh *pfh = NULL; /* Bits for opt_flags above */ #define OP_MAPROOT 0x01 #define OP_MAPALL 0x02 /* 0x4 free */ #define OP_MASK 0x08 #define OP_NET 0x10 #define OP_ALLDIRS 0x40 #define OP_HAVEMASK 0x80 /* A mask was specified or inferred. */ #define OP_QUIET 0x100 #define OP_MASKLEN 0x200 #define OP_SEC 0x400 #ifdef DEBUG static int debug = 1; static void SYSLOG(int, const char *, ...) __printflike(2, 3); #define syslog SYSLOG #else static int debug = 0; #endif /* * Similar to strsep(), but it allows for quoted strings * and escaped characters. * * It returns the string (or NULL, if *stringp is NULL), * which is a de-quoted version of the string if necessary. * * It modifies *stringp in place. */ static char * strsep_quote(char **stringp, const char *delim) { char *srcptr, *dstptr, *retval; char quot = 0; if (stringp == NULL || *stringp == NULL) return (NULL); srcptr = dstptr = retval = *stringp; while (*srcptr) { /* * We're looking for several edge cases here. * First: if we're in quote state (quot != 0), * then we ignore the delim characters, but otherwise * process as normal, unless it is the quote character. * Second: if the current character is a backslash, * we take the next character as-is, without checking * for delim, quote, or backslash. Exception: if the * next character is a NUL, that's the end of the string. * Third: if the character is a quote character, we toggle * quote state. * Otherwise: check the current character for NUL, or * being in delim, and end the string if either is true. */ if (*srcptr == '\\') { srcptr++; /* * The edge case here is if the next character * is NUL, we want to stop processing. But if * it's not NUL, then we simply want to copy it. */ if (*srcptr) { *dstptr++ = *srcptr++; } continue; } if (quot == 0 && (*srcptr == '\'' || *srcptr == '"')) { quot = *srcptr++; continue; } if (quot && *srcptr == quot) { /* End of the quoted part */ quot = 0; srcptr++; continue; } if (!quot && strchr(delim, *srcptr)) break; *dstptr++ = *srcptr++; } *dstptr = 0; /* Terminate the string */ *stringp = (*srcptr == '\0') ? NULL : srcptr + 1; return (retval); } /* * Mountd server for NFS mount protocol as described in: * NFS: Network File System Protocol Specification, RFC1094, Appendix A * The optional arguments are the exports file name * default: _PATH_EXPORTS * and "-n" to allow nonroot mount. */ int main(int argc, char **argv) { fd_set readfds; struct netconfig *nconf; char *endptr, **hosts_bak; void *nc_handle; pid_t otherpid; in_port_t svcport; int c, k, s; int maxrec = RPC_MAXDATASIZE; int attempt_cnt, port_len, port_pos, ret; char **port_list; /* Check that another mountd isn't already running. */ pfh = pidfile_open(_PATH_MOUNTDPID, 0600, &otherpid); if (pfh == NULL) { if (errno == EEXIST) errx(1, "mountd already running, pid: %d.", otherpid); warn("cannot open or create pidfile"); } s = socket(AF_INET6, SOCK_DGRAM, IPPROTO_UDP); if (s < 0) have_v6 = 0; else close(s); while ((c = getopt(argc, argv, "2deh:lnp:rS")) != -1) switch (c) { case '2': force_v2 = 1; break; case 'e': /* now a no-op, since this is the default */ break; case 'n': resvport_only = 0; break; case 'r': dir_only = 0; break; case 'd': debug = debug ? 0 : 1; break; case 'l': dolog = 1; break; case 'p': endptr = NULL; svcport = (in_port_t)strtoul(optarg, &endptr, 10); if (endptr == NULL || *endptr != '\0' || svcport == 0 || svcport >= IPPORT_MAX) usage(); svcport_str = strdup(optarg); break; case 'h': ++nhosts; hosts_bak = hosts; hosts_bak = realloc(hosts, nhosts * sizeof(char *)); if (hosts_bak == NULL) { if (hosts != NULL) { for (k = 0; k < nhosts; k++) free(hosts[k]); free(hosts); out_of_mem(); } } hosts = hosts_bak; hosts[nhosts - 1] = strdup(optarg); if (hosts[nhosts - 1] == NULL) { for (k = 0; k < (nhosts - 1); k++) free(hosts[k]); free(hosts); out_of_mem(); } break; case 'S': suspend_nfsd = 1; break; default: usage(); - }; + } 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"); } argc -= optind; argv += optind; grphead = (struct grouplist *)NULL; exphead = (struct exportlist *)NULL; mlhead = (struct mountlist *)NULL; if (argc > 0) exnames = argv; else exnames = exnames_default; openlog("mountd", LOG_PID, LOG_DAEMON); if (debug) warnx("getting export list"); get_exportlist(); if (debug) warnx("getting mount list"); get_mountlist(); if (debug) warnx("here we go"); if (debug == 0) { daemon(0, 0); signal(SIGINT, SIG_IGN); signal(SIGQUIT, SIG_IGN); } signal(SIGHUP, huphandler); signal(SIGTERM, terminate); signal(SIGPIPE, SIG_IGN); pidfile_write(pfh); rpcb_unset(MOUNTPROG, MOUNTVERS, NULL); rpcb_unset(MOUNTPROG, MOUNTVERS3, NULL); rpc_control(RPC_SVC_CONNMAXREC_SET, &maxrec); if (!resvport_only) { if (sysctlbyname("vfs.nfsrv.nfs_privport", NULL, NULL, &resvport_only, sizeof(resvport_only)) != 0 && errno != ENOENT) { syslog(LOG_ERR, "sysctl: %m"); exit(1); } } /* * If no hosts were specified, add a wildcard entry to bind to * INADDR_ANY. Otherwise make sure 127.0.0.1 and ::1 are added to the * list. */ if (nhosts == 0) { hosts = malloc(sizeof(char *)); if (hosts == NULL) out_of_mem(); hosts[0] = "*"; nhosts = 1; } else { hosts_bak = hosts; if (have_v6) { hosts_bak = realloc(hosts, (nhosts + 2) * sizeof(char *)); if (hosts_bak == NULL) { for (k = 0; k < nhosts; k++) free(hosts[k]); free(hosts); out_of_mem(); } else hosts = hosts_bak; nhosts += 2; hosts[nhosts - 2] = "::1"; } else { hosts_bak = realloc(hosts, (nhosts + 1) * sizeof(char *)); if (hosts_bak == NULL) { for (k = 0; k < nhosts; k++) free(hosts[k]); free(hosts); out_of_mem(); } else { nhosts += 1; hosts = hosts_bak; } } hosts[nhosts - 1] = "127.0.0.1"; } attempt_cnt = 1; sock_fdcnt = 0; sock_fd = NULL; port_list = NULL; port_len = 0; nc_handle = setnetconfig(); while ((nconf = getnetconfig(nc_handle))) { if (nconf->nc_flag & NC_VISIBLE) { if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) { /* DO NOTHING */ } else { ret = create_service(nconf); if (ret == 1) /* Ignore this call */ continue; if (ret < 0) { /* * Failed to bind port, so close off * all sockets created and try again * if the port# was dynamically * assigned via bind(2). */ clearout_service(); if (mallocd_svcport != 0 && attempt_cnt < GETPORT_MAXTRY) { free(svcport_str); svcport_str = NULL; mallocd_svcport = 0; } else { errno = EADDRINUSE; syslog(LOG_ERR, "bindresvport_sa: %m"); exit(1); } /* Start over at the first service. */ free(sock_fd); sock_fdcnt = 0; sock_fd = NULL; nc_handle = setnetconfig(); attempt_cnt++; } else if (mallocd_svcport != 0 && attempt_cnt == GETPORT_MAXTRY) { /* * For the last attempt, allow * different port #s for each nconf * by saving the svcport_str and * setting it back to NULL. */ port_list = realloc(port_list, (port_len + 1) * sizeof(char *)); if (port_list == NULL) out_of_mem(); port_list[port_len++] = svcport_str; svcport_str = NULL; mallocd_svcport = 0; } } } } /* * Successfully bound the ports, so call complete_service() to * do the rest of the setup on the service(s). */ sock_fdpos = 0; port_pos = 0; nc_handle = setnetconfig(); while ((nconf = getnetconfig(nc_handle))) { if (nconf->nc_flag & NC_VISIBLE) { if (have_v6 == 0 && strcmp(nconf->nc_protofmly, "inet6") == 0) { /* DO NOTHING */ } else if (port_list != NULL) { if (port_pos >= port_len) { syslog(LOG_ERR, "too many port#s"); exit(1); } complete_service(nconf, port_list[port_pos++]); } else complete_service(nconf, svcport_str); } } endnetconfig(nc_handle); free(sock_fd); if (port_list != NULL) { for (port_pos = 0; port_pos < port_len; port_pos++) free(port_list[port_pos]); free(port_list); } if (xcreated == 0) { syslog(LOG_ERR, "could not create any services"); exit(1); } /* Expand svc_run() here so that we can call get_exportlist(). */ for (;;) { if (got_sighup) { get_exportlist(); got_sighup = 0; } readfds = svc_fdset; switch (select(svc_maxfd + 1, &readfds, NULL, NULL, NULL)) { case -1: if (errno == EINTR) continue; syslog(LOG_ERR, "mountd died: select: %m"); exit(1); case 0: continue; default: svc_getreqset(&readfds); } } } /* * This routine creates and binds sockets on the appropriate * addresses. It gets called one time for each transport. * It returns 0 upon success, 1 for ingore the call and -1 to indicate * bind failed with EADDRINUSE. * Any file descriptors that have been created are stored in sock_fd and * the total count of them is maintained in sock_fdcnt. */ static int create_service(struct netconfig *nconf) { struct addrinfo hints, *res = NULL; struct sockaddr_in *sin; struct sockaddr_in6 *sin6; struct __rpc_sockinfo si; int aicode; int fd; int nhostsbak; int one = 1; int r; u_int32_t host_addr[4]; /* IPv4 or IPv6 */ int mallocd_res; if ((nconf->nc_semantics != NC_TPI_CLTS) && (nconf->nc_semantics != NC_TPI_COTS) && (nconf->nc_semantics != NC_TPI_COTS_ORD)) return (1); /* not my type */ /* * XXX - using RPC library internal functions. */ if (!__rpc_nconf2sockinfo(nconf, &si)) { syslog(LOG_ERR, "cannot get information for %s", nconf->nc_netid); return (1); } /* Get mountd's address on this transport */ memset(&hints, 0, sizeof hints); hints.ai_family = si.si_af; hints.ai_socktype = si.si_socktype; hints.ai_protocol = si.si_proto; /* * Bind to specific IPs if asked to */ nhostsbak = nhosts; while (nhostsbak > 0) { --nhostsbak; sock_fd = realloc(sock_fd, (sock_fdcnt + 1) * sizeof(int)); if (sock_fd == NULL) out_of_mem(); sock_fd[sock_fdcnt++] = -1; /* Set invalid for now. */ mallocd_res = 0; hints.ai_flags = AI_PASSIVE; /* * XXX - using RPC library internal functions. */ if ((fd = __rpc_nconf2fd(nconf)) < 0) { int non_fatal = 0; if (errno == EAFNOSUPPORT && nconf->nc_semantics != NC_TPI_CLTS) non_fatal = 1; syslog(non_fatal ? LOG_DEBUG : LOG_ERR, "cannot create socket for %s", nconf->nc_netid); if (non_fatal != 0) continue; exit(1); } switch (hints.ai_family) { case AF_INET: if (inet_pton(AF_INET, hosts[nhostsbak], host_addr) == 1) { hints.ai_flags |= AI_NUMERICHOST; } else { /* * Skip if we have an AF_INET6 address. */ if (inet_pton(AF_INET6, hosts[nhostsbak], host_addr) == 1) { close(fd); continue; } } break; case AF_INET6: if (inet_pton(AF_INET6, hosts[nhostsbak], host_addr) == 1) { hints.ai_flags |= AI_NUMERICHOST; } else { /* * Skip if we have an AF_INET address. */ if (inet_pton(AF_INET, hosts[nhostsbak], host_addr) == 1) { close(fd); continue; } } /* * We're doing host-based access checks here, so don't * allow v4-in-v6 to confuse things. The kernel will * disable it by default on NFS sockets too. */ if (setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof one) < 0) { syslog(LOG_ERR, "can't disable v4-in-v6 on IPv6 socket"); exit(1); } break; default: break; } /* * If no hosts were specified, just bind to INADDR_ANY */ if (strcmp("*", hosts[nhostsbak]) == 0) { if (svcport_str == NULL) { res = malloc(sizeof(struct addrinfo)); if (res == NULL) out_of_mem(); mallocd_res = 1; res->ai_flags = hints.ai_flags; res->ai_family = hints.ai_family; res->ai_protocol = hints.ai_protocol; switch (res->ai_family) { case AF_INET: sin = malloc(sizeof(struct sockaddr_in)); if (sin == NULL) out_of_mem(); sin->sin_family = AF_INET; sin->sin_port = htons(0); sin->sin_addr.s_addr = htonl(INADDR_ANY); res->ai_addr = (struct sockaddr*) sin; res->ai_addrlen = (socklen_t) sizeof(struct sockaddr_in); break; case AF_INET6: sin6 = malloc(sizeof(struct sockaddr_in6)); if (sin6 == NULL) out_of_mem(); sin6->sin6_family = AF_INET6; sin6->sin6_port = htons(0); sin6->sin6_addr = in6addr_any; res->ai_addr = (struct sockaddr*) sin6; res->ai_addrlen = (socklen_t) sizeof(struct sockaddr_in6); break; default: syslog(LOG_ERR, "bad addr fam %d", res->ai_family); exit(1); } } else { if ((aicode = getaddrinfo(NULL, svcport_str, &hints, &res)) != 0) { syslog(LOG_ERR, "cannot get local address for %s: %s", nconf->nc_netid, gai_strerror(aicode)); close(fd); continue; } } } else { if ((aicode = getaddrinfo(hosts[nhostsbak], svcport_str, &hints, &res)) != 0) { syslog(LOG_ERR, "cannot get local address for %s: %s", nconf->nc_netid, gai_strerror(aicode)); close(fd); continue; } } /* Store the fd. */ sock_fd[sock_fdcnt - 1] = fd; /* Now, attempt the bind. */ r = bindresvport_sa(fd, res->ai_addr); if (r != 0) { if (errno == EADDRINUSE && mallocd_svcport != 0) { if (mallocd_res != 0) { free(res->ai_addr); free(res); } else freeaddrinfo(res); return (-1); } syslog(LOG_ERR, "bindresvport_sa: %m"); exit(1); } if (svcport_str == NULL) { svcport_str = malloc(NI_MAXSERV * sizeof(char)); if (svcport_str == NULL) out_of_mem(); mallocd_svcport = 1; if (getnameinfo(res->ai_addr, res->ai_addr->sa_len, NULL, NI_MAXHOST, svcport_str, NI_MAXSERV * sizeof(char), NI_NUMERICHOST | NI_NUMERICSERV)) errx(1, "Cannot get port number"); } if (mallocd_res != 0) { free(res->ai_addr); free(res); } else freeaddrinfo(res); res = NULL; } return (0); } /* * Called after all the create_service() calls have succeeded, to complete * the setup and registration. */ static void complete_service(struct netconfig *nconf, char *port_str) { struct addrinfo hints, *res = NULL; struct __rpc_sockinfo si; struct netbuf servaddr; SVCXPRT *transp = NULL; int aicode, fd, nhostsbak; int registered = 0; if ((nconf->nc_semantics != NC_TPI_CLTS) && (nconf->nc_semantics != NC_TPI_COTS) && (nconf->nc_semantics != NC_TPI_COTS_ORD)) return; /* not my type */ /* * XXX - using RPC library internal functions. */ if (!__rpc_nconf2sockinfo(nconf, &si)) { syslog(LOG_ERR, "cannot get information for %s", nconf->nc_netid); return; } nhostsbak = nhosts; while (nhostsbak > 0) { --nhostsbak; if (sock_fdpos >= sock_fdcnt) { /* Should never happen. */ syslog(LOG_ERR, "Ran out of socket fd's"); return; } fd = sock_fd[sock_fdpos++]; if (fd < 0) continue; if (nconf->nc_semantics != NC_TPI_CLTS) listen(fd, SOMAXCONN); if (nconf->nc_semantics == NC_TPI_CLTS ) transp = svc_dg_create(fd, 0, 0); else transp = svc_vc_create(fd, RPC_MAXDATASIZE, RPC_MAXDATASIZE); if (transp != (SVCXPRT *) NULL) { if (!svc_reg(transp, MOUNTPROG, MOUNTVERS, mntsrv, NULL)) syslog(LOG_ERR, "can't register %s MOUNTVERS service", nconf->nc_netid); if (!force_v2) { if (!svc_reg(transp, MOUNTPROG, MOUNTVERS3, mntsrv, NULL)) syslog(LOG_ERR, "can't register %s MOUNTVERS3 service", nconf->nc_netid); } } else syslog(LOG_WARNING, "can't create %s services", nconf->nc_netid); if (registered == 0) { registered = 1; memset(&hints, 0, sizeof hints); hints.ai_flags = AI_PASSIVE; hints.ai_family = si.si_af; hints.ai_socktype = si.si_socktype; hints.ai_protocol = si.si_proto; if ((aicode = getaddrinfo(NULL, port_str, &hints, &res)) != 0) { syslog(LOG_ERR, "cannot get local address: %s", gai_strerror(aicode)); exit(1); } servaddr.buf = malloc(res->ai_addrlen); memcpy(servaddr.buf, res->ai_addr, res->ai_addrlen); servaddr.len = res->ai_addrlen; rpcb_set(MOUNTPROG, MOUNTVERS, nconf, &servaddr); rpcb_set(MOUNTPROG, MOUNTVERS3, nconf, &servaddr); xcreated++; freeaddrinfo(res); } } /* end while */ } /* * Clear out sockets after a failure to bind one of them, so that the * cycle of socket creation/binding can start anew. */ static void clearout_service(void) { int i; for (i = 0; i < sock_fdcnt; i++) { if (sock_fd[i] >= 0) { shutdown(sock_fd[i], SHUT_RDWR); close(sock_fd[i]); } } } static void usage(void) { fprintf(stderr, "usage: mountd [-2] [-d] [-e] [-l] [-n] [-p ] [-r] " "[-S] [-h ] [export_file ...]\n"); exit(1); } /* * The mount rpc service */ void mntsrv(struct svc_req *rqstp, SVCXPRT *transp) { struct exportlist *ep; struct dirlist *dp; struct fhreturn fhr; struct stat stb; struct statfs fsb; char host[NI_MAXHOST], numerichost[NI_MAXHOST]; int lookup_failed = 1; struct sockaddr *saddr; u_short sport; char rpcpath[MNTPATHLEN + 1], dirpath[MAXPATHLEN]; int bad = 0, defset, hostset; sigset_t sighup_mask; int numsecflavors, *secflavorsp; sigemptyset(&sighup_mask); sigaddset(&sighup_mask, SIGHUP); saddr = svc_getrpccaller(transp)->buf; switch (saddr->sa_family) { case AF_INET6: sport = ntohs(((struct sockaddr_in6 *)saddr)->sin6_port); break; case AF_INET: sport = ntohs(((struct sockaddr_in *)saddr)->sin_port); break; default: syslog(LOG_ERR, "request from unknown address family"); return; } lookup_failed = getnameinfo(saddr, saddr->sa_len, host, sizeof host, NULL, 0, 0); getnameinfo(saddr, saddr->sa_len, numerichost, sizeof numerichost, NULL, 0, NI_NUMERICHOST); switch (rqstp->rq_proc) { case NULLPROC: if (!svc_sendreply(transp, (xdrproc_t)xdr_void, NULL)) syslog(LOG_ERR, "can't send reply"); return; case MOUNTPROC_MNT: if (sport >= IPPORT_RESERVED && resvport_only) { syslog(LOG_NOTICE, "mount request from %s from unprivileged port", numerichost); svcerr_weakauth(transp); return; } if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { syslog(LOG_NOTICE, "undecodable mount request from %s", numerichost); svcerr_decode(transp); return; } /* * Get the real pathname and make sure it is a directory * or a regular file if the -r option was specified * and it exists. */ if (realpath(rpcpath, dirpath) == NULL || stat(dirpath, &stb) < 0 || (!S_ISDIR(stb.st_mode) && (dir_only || !S_ISREG(stb.st_mode))) || statfs(dirpath, &fsb) < 0) { chdir("/"); /* Just in case realpath doesn't */ syslog(LOG_NOTICE, "mount request from %s for non existent path %s", numerichost, dirpath); if (debug) warnx("stat failed on %s", dirpath); bad = ENOENT; /* We will send error reply later */ } /* Check in the exports list */ sigprocmask(SIG_BLOCK, &sighup_mask, NULL); ep = ex_search(&fsb.f_fsid); hostset = defset = 0; if (ep && (chk_host(ep->ex_defdir, saddr, &defset, &hostset, &numsecflavors, &secflavorsp) || ((dp = dirp_search(ep->ex_dirl, dirpath)) && chk_host(dp, saddr, &defset, &hostset, &numsecflavors, &secflavorsp)) || (defset && scan_tree(ep->ex_defdir, saddr) == 0 && scan_tree(ep->ex_dirl, saddr) == 0))) { if (bad) { if (!svc_sendreply(transp, (xdrproc_t)xdr_long, (caddr_t)&bad)) syslog(LOG_ERR, "can't send reply"); sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); return; } if (hostset & DP_HOSTSET) { fhr.fhr_flag = hostset; fhr.fhr_numsecflavors = numsecflavors; fhr.fhr_secflavors = secflavorsp; } else { fhr.fhr_flag = defset; fhr.fhr_numsecflavors = ep->ex_defnumsecflavors; fhr.fhr_secflavors = ep->ex_defsecflavors; } fhr.fhr_vers = rqstp->rq_vers; /* Get the file handle */ memset(&fhr.fhr_fh, 0, sizeof(nfsfh_t)); if (getfh(dirpath, (fhandle_t *)&fhr.fhr_fh) < 0) { bad = errno; syslog(LOG_ERR, "can't get fh for %s", dirpath); if (!svc_sendreply(transp, (xdrproc_t)xdr_long, (caddr_t)&bad)) syslog(LOG_ERR, "can't send reply"); sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); return; } if (!svc_sendreply(transp, (xdrproc_t)xdr_fhs, (caddr_t)&fhr)) syslog(LOG_ERR, "can't send reply"); if (!lookup_failed) add_mlist(host, dirpath); else add_mlist(numerichost, dirpath); if (debug) warnx("mount successful"); if (dolog) syslog(LOG_NOTICE, "mount request succeeded from %s for %s", numerichost, dirpath); } else { bad = EACCES; syslog(LOG_NOTICE, "mount request denied from %s for %s", numerichost, dirpath); } if (bad && !svc_sendreply(transp, (xdrproc_t)xdr_long, (caddr_t)&bad)) syslog(LOG_ERR, "can't send reply"); sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); return; case MOUNTPROC_DUMP: if (!svc_sendreply(transp, (xdrproc_t)xdr_mlist, (caddr_t)NULL)) syslog(LOG_ERR, "can't send reply"); else if (dolog) syslog(LOG_NOTICE, "dump request succeeded from %s", numerichost); return; case MOUNTPROC_UMNT: if (sport >= IPPORT_RESERVED && resvport_only) { syslog(LOG_NOTICE, "umount request from %s from unprivileged port", numerichost); svcerr_weakauth(transp); return; } if (!svc_getargs(transp, (xdrproc_t)xdr_dir, rpcpath)) { syslog(LOG_NOTICE, "undecodable umount request from %s", numerichost); svcerr_decode(transp); return; } if (realpath(rpcpath, dirpath) == NULL) { syslog(LOG_NOTICE, "umount request from %s " "for non existent path %s", numerichost, dirpath); } if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) syslog(LOG_ERR, "can't send reply"); if (!lookup_failed) del_mlist(host, dirpath); del_mlist(numerichost, dirpath); if (dolog) syslog(LOG_NOTICE, "umount request succeeded from %s for %s", numerichost, dirpath); return; case MOUNTPROC_UMNTALL: if (sport >= IPPORT_RESERVED && resvport_only) { syslog(LOG_NOTICE, "umountall request from %s from unprivileged port", numerichost); svcerr_weakauth(transp); return; } if (!svc_sendreply(transp, (xdrproc_t)xdr_void, (caddr_t)NULL)) syslog(LOG_ERR, "can't send reply"); if (!lookup_failed) del_mlist(host, NULL); del_mlist(numerichost, NULL); if (dolog) syslog(LOG_NOTICE, "umountall request succeeded from %s", numerichost); return; case MOUNTPROC_EXPORT: if (!svc_sendreply(transp, (xdrproc_t)xdr_explist, (caddr_t)NULL)) if (!svc_sendreply(transp, (xdrproc_t)xdr_explist_brief, (caddr_t)NULL)) syslog(LOG_ERR, "can't send reply"); if (dolog) syslog(LOG_NOTICE, "export request succeeded from %s", numerichost); return; default: svcerr_noproc(transp); return; } } /* * Xdr conversion for a dirpath string */ static int xdr_dir(XDR *xdrsp, char *dirp) { return (xdr_string(xdrsp, &dirp, MNTPATHLEN)); } /* * Xdr routine to generate file handle reply */ static int xdr_fhs(XDR *xdrsp, caddr_t cp) { struct fhreturn *fhrp = (struct fhreturn *)cp; u_long ok = 0, len, auth; int i; if (!xdr_long(xdrsp, &ok)) return (0); switch (fhrp->fhr_vers) { case 1: return (xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, NFSX_V2FH)); case 3: len = NFSX_V3FH; if (!xdr_long(xdrsp, &len)) return (0); if (!xdr_opaque(xdrsp, (caddr_t)&fhrp->fhr_fh, len)) return (0); if (fhrp->fhr_numsecflavors) { if (!xdr_int(xdrsp, &fhrp->fhr_numsecflavors)) return (0); for (i = 0; i < fhrp->fhr_numsecflavors; i++) if (!xdr_int(xdrsp, &fhrp->fhr_secflavors[i])) return (0); return (1); } else { auth = AUTH_SYS; len = 1; if (!xdr_long(xdrsp, &len)) return (0); return (xdr_long(xdrsp, &auth)); } - }; + } return (0); } static int xdr_mlist(XDR *xdrsp, caddr_t cp __unused) { struct mountlist *mlp; int true = 1; int false = 0; char *strp; mlp = mlhead; while (mlp) { if (!xdr_bool(xdrsp, &true)) return (0); strp = &mlp->ml_host[0]; if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) return (0); strp = &mlp->ml_dirp[0]; if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) return (0); mlp = mlp->ml_next; } if (!xdr_bool(xdrsp, &false)) return (0); return (1); } /* * Xdr conversion for export list */ static int xdr_explist_common(XDR *xdrsp, caddr_t cp __unused, int brief) { struct exportlist *ep; int false = 0; int putdef; sigset_t sighup_mask; sigemptyset(&sighup_mask); sigaddset(&sighup_mask, SIGHUP); sigprocmask(SIG_BLOCK, &sighup_mask, NULL); ep = exphead; while (ep) { putdef = 0; if (put_exlist(ep->ex_dirl, xdrsp, ep->ex_defdir, &putdef, brief)) goto errout; if (ep->ex_defdir && putdef == 0 && put_exlist(ep->ex_defdir, xdrsp, (struct dirlist *)NULL, &putdef, brief)) goto errout; ep = ep->ex_next; } sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); if (!xdr_bool(xdrsp, &false)) return (0); return (1); errout: sigprocmask(SIG_UNBLOCK, &sighup_mask, NULL); return (0); } /* * Called from xdr_explist() to traverse the tree and export the * directory paths. */ static int put_exlist(struct dirlist *dp, XDR *xdrsp, struct dirlist *adp, int *putdefp, int brief) { struct grouplist *grp; struct hostlist *hp; int true = 1; int false = 0; int gotalldir = 0; char *strp; if (dp) { if (put_exlist(dp->dp_left, xdrsp, adp, putdefp, brief)) return (1); if (!xdr_bool(xdrsp, &true)) return (1); strp = dp->dp_dirp; if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) return (1); if (adp && !strcmp(dp->dp_dirp, adp->dp_dirp)) { gotalldir = 1; *putdefp = 1; } if (brief) { if (!xdr_bool(xdrsp, &true)) return (1); strp = "(...)"; if (!xdr_string(xdrsp, &strp, MNTPATHLEN)) return (1); } else if ((dp->dp_flag & DP_DEFSET) == 0 && (gotalldir == 0 || (adp->dp_flag & DP_DEFSET) == 0)) { hp = dp->dp_hosts; while (hp) { grp = hp->ht_grp; if (grp->gr_type == GT_HOST) { if (!xdr_bool(xdrsp, &true)) return (1); strp = grp->gr_ptr.gt_addrinfo->ai_canonname; if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) return (1); } else if (grp->gr_type == GT_NET) { if (!xdr_bool(xdrsp, &true)) return (1); strp = grp->gr_ptr.gt_net.nt_name; if (!xdr_string(xdrsp, &strp, MNTNAMLEN)) return (1); } hp = hp->ht_next; if (gotalldir && hp == (struct hostlist *)NULL) { hp = adp->dp_hosts; gotalldir = 0; } } } if (!xdr_bool(xdrsp, &false)) return (1); if (put_exlist(dp->dp_right, xdrsp, adp, putdefp, brief)) return (1); } return (0); } static int xdr_explist(XDR *xdrsp, caddr_t cp) { return xdr_explist_common(xdrsp, cp, 0); } static int xdr_explist_brief(XDR *xdrsp, caddr_t cp) { return xdr_explist_common(xdrsp, cp, 1); } static char *line; static size_t linesize; static FILE *exp_file; /* * Get the export list from one, currently open file */ static void get_exportlist_one(void) { struct exportlist *ep, *ep2; struct grouplist *grp, *tgrp; struct exportlist **epp; struct dirlist *dirhead; struct statfs fsb; struct xucred anon; char *cp, *endcp, *dirp, *hst, *usr, *dom, savedc; int len, has_host, exflags, got_nondir, dirplen, netgrp; v4root_phase = 0; dirhead = (struct dirlist *)NULL; while (get_line()) { if (debug) warnx("got line %s", line); cp = line; nextfield(&cp, &endcp); if (*cp == '#') goto nextline; /* * Set defaults. */ has_host = FALSE; anon = def_anon; exflags = MNT_EXPORTED; got_nondir = 0; opt_flags = 0; ep = (struct exportlist *)NULL; dirp = NULL; /* * Handle the V4 root dir. */ if (*cp == 'V' && *(cp + 1) == '4' && *(cp + 2) == ':') { /* * V4: just indicates that it is the v4 root point, * so skip over that and set v4root_phase. */ if (v4root_phase > 0) { syslog(LOG_ERR, "V4:duplicate line, ignored"); goto nextline; } v4root_phase = 1; cp += 3; nextfield(&cp, &endcp); } /* * Create new exports list entry */ len = endcp-cp; tgrp = grp = get_grp(); while (len > 0) { if (len > MNTNAMLEN) { getexp_err(ep, tgrp); goto nextline; } if (*cp == '-') { if (ep == (struct exportlist *)NULL) { getexp_err(ep, tgrp); goto nextline; } if (debug) warnx("doing opt %s", cp); got_nondir = 1; if (do_opt(&cp, &endcp, ep, grp, &has_host, &exflags, &anon)) { getexp_err(ep, tgrp); goto nextline; } } else if (*cp == '/') { savedc = *endcp; *endcp = '\0'; if (v4root_phase > 1) { if (dirp != NULL) { syslog(LOG_ERR, "Multiple V4 dirs"); getexp_err(ep, tgrp); goto nextline; } } if (check_dirpath(cp) && statfs(cp, &fsb) >= 0) { if ((fsb.f_flags & MNT_AUTOMOUNTED) != 0) syslog(LOG_ERR, "Warning: exporting of " "automounted fs %s not supported", cp); if (got_nondir) { syslog(LOG_ERR, "dirs must be first"); getexp_err(ep, tgrp); goto nextline; } if (v4root_phase == 1) { if (dirp != NULL) { syslog(LOG_ERR, "Multiple V4 dirs"); getexp_err(ep, tgrp); goto nextline; } if (strlen(v4root_dirpath) == 0) { strlcpy(v4root_dirpath, cp, sizeof (v4root_dirpath)); } else if (strcmp(v4root_dirpath, cp) != 0) { syslog(LOG_ERR, "different V4 dirpath %s", cp); getexp_err(ep, tgrp); goto nextline; } dirp = cp; v4root_phase = 2; got_nondir = 1; ep = get_exp(); } else { if (ep) { if (ep->ex_fs.val[0] != fsb.f_fsid.val[0] || ep->ex_fs.val[1] != fsb.f_fsid.val[1]) { getexp_err(ep, tgrp); goto nextline; } } else { /* * See if this directory is already * in the list. */ ep = ex_search(&fsb.f_fsid); if (ep == (struct exportlist *)NULL) { ep = get_exp(); ep->ex_fs = fsb.f_fsid; ep->ex_fsdir = (char *)malloc (strlen(fsb.f_mntonname) + 1); if (ep->ex_fsdir) strcpy(ep->ex_fsdir, fsb.f_mntonname); else out_of_mem(); if (debug) warnx( "making new ep fs=0x%x,0x%x", fsb.f_fsid.val[0], fsb.f_fsid.val[1]); } else if (debug) warnx("found ep fs=0x%x,0x%x", fsb.f_fsid.val[0], fsb.f_fsid.val[1]); } /* * Add dirpath to export mount point. */ dirp = add_expdir(&dirhead, cp, len); dirplen = len; } } else { getexp_err(ep, tgrp); goto nextline; } *endcp = savedc; } else { savedc = *endcp; *endcp = '\0'; got_nondir = 1; if (ep == (struct exportlist *)NULL) { getexp_err(ep, tgrp); goto nextline; } /* * Get the host or netgroup. */ setnetgrent(cp); netgrp = getnetgrent(&hst, &usr, &dom); do { if (has_host) { grp->gr_next = get_grp(); grp = grp->gr_next; } if (netgrp) { if (hst == 0) { syslog(LOG_ERR, "null hostname in netgroup %s, skipping", cp); grp->gr_type = GT_IGNORE; } else if (get_host(hst, grp, tgrp)) { syslog(LOG_ERR, "bad host %s in netgroup %s, skipping", hst, cp); grp->gr_type = GT_IGNORE; } } else if (get_host(cp, grp, tgrp)) { syslog(LOG_ERR, "bad host %s, skipping", cp); grp->gr_type = GT_IGNORE; } has_host = TRUE; } while (netgrp && getnetgrent(&hst, &usr, &dom)); endnetgrent(); *endcp = savedc; } cp = endcp; nextfield(&cp, &endcp); len = endcp - cp; } if (check_options(dirhead)) { getexp_err(ep, tgrp); goto nextline; } if (!has_host) { grp->gr_type = GT_DEFAULT; if (debug) warnx("adding a default entry"); /* * Don't allow a network export coincide with a list of * host(s) on the same line. */ } else if ((opt_flags & OP_NET) && tgrp->gr_next) { syslog(LOG_ERR, "network/host conflict"); getexp_err(ep, tgrp); goto nextline; /* * If an export list was specified on this line, make sure * that we have at least one valid entry, otherwise skip it. */ } else { grp = tgrp; while (grp && grp->gr_type == GT_IGNORE) grp = grp->gr_next; if (! grp) { getexp_err(ep, tgrp); goto nextline; } } if (v4root_phase == 1) { syslog(LOG_ERR, "V4:root, no dirp, ignored"); getexp_err(ep, tgrp); goto nextline; } /* * Loop through hosts, pushing the exports into the kernel. * After loop, tgrp points to the start of the list and * grp points to the last entry in the list. */ grp = tgrp; do { if (do_mount(ep, grp, exflags, &anon, dirp, dirplen, &fsb)) { getexp_err(ep, tgrp); goto nextline; } } while (grp->gr_next && (grp = grp->gr_next)); /* * For V4: don't enter in mount lists. */ if (v4root_phase > 0 && v4root_phase <= 2) { /* * Since these structures aren't used by mountd, * free them up now. */ if (ep != NULL) free_exp(ep); while (tgrp != NULL) { grp = tgrp; tgrp = tgrp->gr_next; free_grp(grp); } goto nextline; } /* * Success. Update the data structures. */ if (has_host) { hang_dirp(dirhead, tgrp, ep, opt_flags); grp->gr_next = grphead; grphead = tgrp; } else { hang_dirp(dirhead, (struct grouplist *)NULL, ep, opt_flags); free_grp(grp); } dirhead = (struct dirlist *)NULL; if ((ep->ex_flag & EX_LINKED) == 0) { ep2 = exphead; epp = &exphead; /* * Insert in the list in alphabetical order. */ while (ep2 && strcmp(ep2->ex_fsdir, ep->ex_fsdir) < 0) { epp = &ep2->ex_next; ep2 = ep2->ex_next; } if (ep2) ep->ex_next = ep2; *epp = ep; ep->ex_flag |= EX_LINKED; } nextline: v4root_phase = 0; if (dirhead) { free_dir(dirhead); dirhead = (struct dirlist *)NULL; } } } /* * Get the export list from all specified files */ static void get_exportlist(void) { struct exportlist *ep, *ep2; struct grouplist *grp, *tgrp; struct export_args export; struct iovec *iov; struct statfs *fsp, *mntbufp; struct xvfsconf vfc; char errmsg[255]; int num, i; int iovlen; int done; struct nfsex_args eargs; if (suspend_nfsd != 0) (void)nfssvc(NFSSVC_SUSPENDNFSD, NULL); v4root_dirpath[0] = '\0'; bzero(&export, sizeof(export)); export.ex_flags = MNT_DELEXPORT; iov = NULL; iovlen = 0; bzero(errmsg, sizeof(errmsg)); /* * First, get rid of the old list */ ep = exphead; while (ep) { ep2 = ep; ep = ep->ex_next; free_exp(ep2); } exphead = (struct exportlist *)NULL; grp = grphead; while (grp) { tgrp = grp; grp = grp->gr_next; free_grp(tgrp); } grphead = (struct grouplist *)NULL; /* * and the old V4 root dir. */ bzero(&eargs, sizeof (eargs)); eargs.export.ex_flags = MNT_DELEXPORT; if (nfssvc(NFSSVC_V4ROOTEXPORT, (caddr_t)&eargs) < 0 && errno != ENOENT) syslog(LOG_ERR, "Can't delete exports for V4:"); /* * and clear flag that notes if a public fh has been exported. */ has_publicfh = 0; /* * And delete exports that are in the kernel for all local * filesystems. * XXX: Should know how to handle all local exportable filesystems. */ num = getmntinfo(&mntbufp, MNT_NOWAIT); if (num > 0) { build_iovec(&iov, &iovlen, "fstype", NULL, 0); build_iovec(&iov, &iovlen, "fspath", NULL, 0); build_iovec(&iov, &iovlen, "from", NULL, 0); build_iovec(&iov, &iovlen, "update", NULL, 0); build_iovec(&iov, &iovlen, "export", &export, sizeof(export)); build_iovec(&iov, &iovlen, "errmsg", errmsg, sizeof(errmsg)); } for (i = 0; i < num; i++) { fsp = &mntbufp[i]; if (getvfsbyname(fsp->f_fstypename, &vfc) != 0) { syslog(LOG_ERR, "getvfsbyname() failed for %s", fsp->f_fstypename); continue; } /* * We do not need to delete "export" flag from * filesystems that do not have it set. */ if (!(fsp->f_flags & MNT_EXPORTED)) continue; /* * Do not delete export for network filesystem by * passing "export" arg to nmount(). * It only makes sense to do this for local filesystems. */ if (vfc.vfc_flags & VFCF_NETWORK) continue; iov[1].iov_base = fsp->f_fstypename; iov[1].iov_len = strlen(fsp->f_fstypename) + 1; iov[3].iov_base = fsp->f_mntonname; iov[3].iov_len = strlen(fsp->f_mntonname) + 1; iov[5].iov_base = fsp->f_mntfromname; iov[5].iov_len = strlen(fsp->f_mntfromname) + 1; errmsg[0] = '\0'; /* * EXDEV is returned when path exists but is not a * mount point. May happens if raced with unmount. */ if (nmount(iov, iovlen, fsp->f_flags) < 0 && errno != ENOENT && errno != ENOTSUP && errno != EXDEV) { syslog(LOG_ERR, "can't delete exports for %s: %m %s", fsp->f_mntonname, errmsg); } } if (iov != NULL) { /* Free strings allocated by strdup() in getmntopts.c */ free(iov[0].iov_base); /* fstype */ free(iov[2].iov_base); /* fspath */ free(iov[4].iov_base); /* from */ free(iov[6].iov_base); /* update */ free(iov[8].iov_base); /* export */ free(iov[10].iov_base); /* errmsg */ /* free iov, allocated by realloc() */ free(iov); iovlen = 0; } /* * Read in the exports file and build the list, calling * nmount() as we go along to push the export rules into the kernel. */ done = 0; for (i = 0; exnames[i] != NULL; i++) { if (debug) warnx("reading exports from %s", exnames[i]); if ((exp_file = fopen(exnames[i], "r")) == NULL) { syslog(LOG_WARNING, "can't open %s", exnames[i]); continue; } get_exportlist_one(); fclose(exp_file); done++; } if (done == 0) { syslog(LOG_ERR, "can't open any exports file"); exit(2); } /* * If there was no public fh, clear any previous one set. */ if (has_publicfh == 0) (void) nfssvc(NFSSVC_NOPUBLICFH, NULL); /* Resume the nfsd. If they weren't suspended, this is harmless. */ (void)nfssvc(NFSSVC_RESUMENFSD, NULL); } /* * Allocate an export list element */ static struct exportlist * get_exp(void) { struct exportlist *ep; ep = (struct exportlist *)calloc(1, sizeof (struct exportlist)); if (ep == (struct exportlist *)NULL) out_of_mem(); return (ep); } /* * Allocate a group list element */ static struct grouplist * get_grp(void) { struct grouplist *gp; gp = (struct grouplist *)calloc(1, sizeof (struct grouplist)); if (gp == (struct grouplist *)NULL) out_of_mem(); return (gp); } /* * Clean up upon an error in get_exportlist(). */ static void getexp_err(struct exportlist *ep, struct grouplist *grp) { struct grouplist *tgrp; if (!(opt_flags & OP_QUIET)) syslog(LOG_ERR, "bad exports list line %s", line); if (ep && (ep->ex_flag & EX_LINKED) == 0) free_exp(ep); while (grp) { tgrp = grp; grp = grp->gr_next; free_grp(tgrp); } } /* * Search the export list for a matching fs. */ static struct exportlist * ex_search(fsid_t *fsid) { struct exportlist *ep; ep = exphead; while (ep) { if (ep->ex_fs.val[0] == fsid->val[0] && ep->ex_fs.val[1] == fsid->val[1]) return (ep); ep = ep->ex_next; } return (ep); } /* * Add a directory path to the list. */ static char * add_expdir(struct dirlist **dpp, char *cp, int len) { struct dirlist *dp; dp = (struct dirlist *)malloc(sizeof (struct dirlist) + len); if (dp == (struct dirlist *)NULL) out_of_mem(); dp->dp_left = *dpp; dp->dp_right = (struct dirlist *)NULL; dp->dp_flag = 0; dp->dp_hosts = (struct hostlist *)NULL; strcpy(dp->dp_dirp, cp); *dpp = dp; return (dp->dp_dirp); } /* * Hang the dir list element off the dirpath binary tree as required * and update the entry for host. */ static void hang_dirp(struct dirlist *dp, struct grouplist *grp, struct exportlist *ep, int flags) { struct hostlist *hp; struct dirlist *dp2; if (flags & OP_ALLDIRS) { if (ep->ex_defdir) free((caddr_t)dp); else ep->ex_defdir = dp; if (grp == (struct grouplist *)NULL) { ep->ex_defdir->dp_flag |= DP_DEFSET; /* Save the default security flavors list. */ ep->ex_defnumsecflavors = ep->ex_numsecflavors; if (ep->ex_numsecflavors > 0) memcpy(ep->ex_defsecflavors, ep->ex_secflavors, sizeof(ep->ex_secflavors)); } else while (grp) { hp = get_ht(); hp->ht_grp = grp; hp->ht_next = ep->ex_defdir->dp_hosts; ep->ex_defdir->dp_hosts = hp; /* Save the security flavors list for this host set. */ grp->gr_numsecflavors = ep->ex_numsecflavors; if (ep->ex_numsecflavors > 0) memcpy(grp->gr_secflavors, ep->ex_secflavors, sizeof(ep->ex_secflavors)); grp = grp->gr_next; } } else { /* * Loop through the directories adding them to the tree. */ while (dp) { dp2 = dp->dp_left; add_dlist(&ep->ex_dirl, dp, grp, flags, ep); dp = dp2; } } } /* * Traverse the binary tree either updating a node that is already there * for the new directory or adding the new node. */ static void add_dlist(struct dirlist **dpp, struct dirlist *newdp, struct grouplist *grp, int flags, struct exportlist *ep) { struct dirlist *dp; struct hostlist *hp; int cmp; dp = *dpp; if (dp) { cmp = strcmp(dp->dp_dirp, newdp->dp_dirp); if (cmp > 0) { add_dlist(&dp->dp_left, newdp, grp, flags, ep); return; } else if (cmp < 0) { add_dlist(&dp->dp_right, newdp, grp, flags, ep); return; } else free((caddr_t)newdp); } else { dp = newdp; dp->dp_left = (struct dirlist *)NULL; *dpp = dp; } if (grp) { /* * Hang all of the host(s) off of the directory point. */ do { hp = get_ht(); hp->ht_grp = grp; hp->ht_next = dp->dp_hosts; dp->dp_hosts = hp; /* Save the security flavors list for this host set. */ grp->gr_numsecflavors = ep->ex_numsecflavors; if (ep->ex_numsecflavors > 0) memcpy(grp->gr_secflavors, ep->ex_secflavors, sizeof(ep->ex_secflavors)); grp = grp->gr_next; } while (grp); } else { dp->dp_flag |= DP_DEFSET; /* Save the default security flavors list. */ ep->ex_defnumsecflavors = ep->ex_numsecflavors; if (ep->ex_numsecflavors > 0) memcpy(ep->ex_defsecflavors, ep->ex_secflavors, sizeof(ep->ex_secflavors)); } } /* * Search for a dirpath on the export point. */ static struct dirlist * dirp_search(struct dirlist *dp, char *dirp) { int cmp; if (dp) { cmp = strcmp(dp->dp_dirp, dirp); if (cmp > 0) return (dirp_search(dp->dp_left, dirp)); else if (cmp < 0) return (dirp_search(dp->dp_right, dirp)); else return (dp); } return (dp); } /* * Scan for a host match in a directory tree. */ static int chk_host(struct dirlist *dp, struct sockaddr *saddr, int *defsetp, int *hostsetp, int *numsecflavors, int **secflavorsp) { struct hostlist *hp; struct grouplist *grp; struct addrinfo *ai; if (dp) { if (dp->dp_flag & DP_DEFSET) *defsetp = dp->dp_flag; hp = dp->dp_hosts; while (hp) { grp = hp->ht_grp; switch (grp->gr_type) { case GT_HOST: ai = grp->gr_ptr.gt_addrinfo; for (; ai; ai = ai->ai_next) { if (!sacmp(ai->ai_addr, saddr, NULL)) { *hostsetp = (hp->ht_flag | DP_HOSTSET); if (numsecflavors != NULL) { *numsecflavors = grp->gr_numsecflavors; *secflavorsp = grp->gr_secflavors; } return (1); } } break; case GT_NET: if (!sacmp(saddr, (struct sockaddr *) &grp->gr_ptr.gt_net.nt_net, (struct sockaddr *) &grp->gr_ptr.gt_net.nt_mask)) { *hostsetp = (hp->ht_flag | DP_HOSTSET); if (numsecflavors != NULL) { *numsecflavors = grp->gr_numsecflavors; *secflavorsp = grp->gr_secflavors; } return (1); } break; } hp = hp->ht_next; } } return (0); } /* * Scan tree for a host that matches the address. */ static int scan_tree(struct dirlist *dp, struct sockaddr *saddr) { int defset, hostset; if (dp) { if (scan_tree(dp->dp_left, saddr)) return (1); if (chk_host(dp, saddr, &defset, &hostset, NULL, NULL)) return (1); if (scan_tree(dp->dp_right, saddr)) return (1); } return (0); } /* * Traverse the dirlist tree and free it up. */ static void free_dir(struct dirlist *dp) { if (dp) { free_dir(dp->dp_left); free_dir(dp->dp_right); free_host(dp->dp_hosts); free((caddr_t)dp); } } /* * Parse a colon separated list of security flavors */ static int parsesec(char *seclist, struct exportlist *ep) { char *cp, savedc; int flavor; ep->ex_numsecflavors = 0; for (;;) { cp = strchr(seclist, ':'); if (cp) { savedc = *cp; *cp = '\0'; } if (!strcmp(seclist, "sys")) flavor = AUTH_SYS; else if (!strcmp(seclist, "krb5")) flavor = RPCSEC_GSS_KRB5; else if (!strcmp(seclist, "krb5i")) flavor = RPCSEC_GSS_KRB5I; else if (!strcmp(seclist, "krb5p")) flavor = RPCSEC_GSS_KRB5P; else { if (cp) *cp = savedc; syslog(LOG_ERR, "bad sec flavor: %s", seclist); return (1); } if (ep->ex_numsecflavors == MAXSECFLAVORS) { if (cp) *cp = savedc; syslog(LOG_ERR, "too many sec flavors: %s", seclist); return (1); } ep->ex_secflavors[ep->ex_numsecflavors] = flavor; ep->ex_numsecflavors++; if (cp) { *cp = savedc; seclist = cp + 1; } else { break; } } return (0); } /* * Parse the option string and update fields. * Option arguments may either be -