NetBSD/usr.sbin/sup/source/supscan.c

441 lines
12 KiB
C
Raw Normal View History

2011-08-31 20:24:54 +04:00
/* $NetBSD: supscan.c,v 1.19 2011/08/31 16:25:00 plunky Exp $ */
1993-05-21 18:52:16 +04:00
/*
* Copyright (c) 1992 Carnegie Mellon University
* All Rights Reserved.
*
1993-05-21 18:52:16 +04:00
* Permission to use, copy, modify and distribute this software and its
* documentation is hereby granted, provided that both the copyright
* notice and this permission notice appear in all copies of the
* software, derivative works or modified versions, and any portions
* thereof, and that both notices appear in supporting documentation.
*
* CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
* CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
* ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
*
* Carnegie Mellon requests users of this software to return to
*
* Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
1993-05-21 18:52:16 +04:00
* School of Computer Science
* Carnegie Mellon University
* Pittsburgh PA 15213-3890
*
* any improvements or extensions that they make and grant Carnegie Mellon
* the rights to redistribute these changes.
*/
/*
* supscan -- SUP Scan File Builder
*
* Usage: supscan [ -v ] collection [ basedir ]
* supscan [ -v ] -f dirfile
* supscan [ -v ] -s
* -f "file" -- use dirfile instead of system coll.dir
* -s "system" -- perform scan for system supfile
* -v "verbose" -- print messages as you go
* collection -- name of the desired collection if not -s
* basedir -- name of the base directory, if not
* the default or recorded in coll.dir
* dirfile -- name of replacement for system coll.dir.
*
**********************************************************************
* HISTORY
* Revision 1.14 92/08/11 12:08:30 mrt
* Picked up Brad's deliniting and variable argument changes
* [92/08/10 mrt]
*
1993-05-21 18:52:16 +04:00
* Revision 1.13 92/02/08 18:04:44 dlc
* Once again revised localhost(). Do not use gethostbyname() at
* all, but assume that the host names in the coll.host file are at
* least a prefix of the fully qualified name. Modcoll (and related
* scripts) will maintain this fact.
* [92/02/08 dlc]
*
1993-05-21 18:52:16 +04:00
* Revision 1.12 91/08/17 23:35:31 dlc
* Changes to localhost() function:
* - Use host name in kernel for local host name; assume it is
* fully qualified.
* - If gethostbyname() of host to see if we are the repository
* fails, with TRY_AGAIN or NO_RECOVERY, then use the "host"
* parameter. Print a diagnostic in this case.
* [91/08/17 dlc]
*
1993-05-21 18:52:16 +04:00
* Revision 1.11 90/04/04 10:53:01 dlc
* Changed localhost to retry getting the local host name 4 times with
* 30 second sleep intervals before aborting; after 4 tries, things are
* probably too messed up for the supscan to do anything useful
* [90/04/04 dlc]
*
1993-05-21 18:52:16 +04:00
* Revision 1.10 89/08/03 19:49:33 mja
* Updated to use v*printf() in place of _doprnt().
* [89/04/19 mja]
*
1993-05-21 18:52:16 +04:00
* Revision 1.9 89/06/18 14:41:37 gm0w
* Fixed up some notify messages of errors to use "SUP:" prefix.
* [89/06/18 gm0w]
*
1993-05-21 18:52:16 +04:00
* 13-May-88 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Changed goaway to longjmp back to top-level to scan next
* collection. [V7.6]
*
* 19-Feb-88 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Added -f <filename> switch to scan all (or part) of the
* collections in a file of collection/base-directory pairs.
* [V7.5]
*
* 27-Dec-87 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Removed nameserver support (which means to use a new
* datafile).
*
* 09-Sep-87 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Use case-insensitive hostname comparison.
*
* 28-Jun-87 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Added code for "release" support. [V6.4]
*
* 05-Jan-86 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Changed collection setup errors to be non-fatal. [V5.3]
*
* 29-Dec-85 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Moved most of the scanning code to scan.c. [V4.2]
*
* 02-Nov-85 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Added "-s" option.
*
* 22-Sep-85 Glenn Marcy (gm0w) at Carnegie-Mellon University
* Merged 4.1 and 4.2 versions together.
*
* 04-Jun-85 Steven Shafer (sas) at Carnegie-Mellon University
* Created for 4.2 BSD.
*
**********************************************************************
*/
#include <netdb.h>
#include <setjmp.h>
#include <stdarg.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/param.h>
#include "supcdefs.h"
#include "supextern.h"
#include "libc.h"
#include "c.h"
1993-05-21 18:52:16 +04:00
#define PGMVERSION 6
/*******************************************
*** D A T A S T R U C T U R E S ***
*******************************************/
struct scan_collstruct { /* one per collection to be upgraded */
char *Cname; /* collection name */
char *Cbase; /* local base directory */
char *Cprefix; /* local collection pathname prefix */
struct scan_collstruct *Cnext; /* next collection */
1993-05-21 18:52:16 +04:00
};
typedef struct scan_collstruct SCAN_COLLECTION;
1993-05-21 18:52:16 +04:00
/*********************************************
*** G L O B A L V A R I A B L E S ***
*********************************************/
int trace; /* -v flag */
int quiet; /* -q flag */
1993-05-21 18:52:16 +04:00
SCAN_COLLECTION *firstC; /* collection list pointer */
char *collname; /* collection name */
char *basedir; /* base directory name */
char *prefix; /* collection pathname prefix */
time_t lasttime = 0; /* time of last upgrade */
time_t scantime; /* time of this scan */
int newonly = FALSE; /* new files only */
jmp_buf sjbuf; /* jump location for errors */
1993-05-21 18:52:16 +04:00
TREELIST *listTL; /* list of all files specified by <coll>.list */
TREE *listT; /* final list of files in collection */
TREE *refuseT = NULL; /* list of all files specified by <coll>.list */
1993-05-21 18:52:16 +04:00
void usage(void);
void init(int, char **);
static SCAN_COLLECTION *getscancoll(char *, char *, char *);
int localhost(char *);
int main(int, char **);
1993-05-21 18:52:16 +04:00
/*************************************
*** M A I N R O U T I N E ***
*************************************/
int
main(int argc, char **argv)
1993-05-21 18:52:16 +04:00
{
SCAN_COLLECTION * volatile c; /* Avoid longjmp clobbering */
#ifdef RLIMIT_DATA
struct rlimit dlim;
if (getrlimit(RLIMIT_DATA, &dlim) == -1)
goaway("Error getting resource limit (%s)",
strerror(errno));
if (dlim.rlim_cur != dlim.rlim_max) {
dlim.rlim_cur = dlim.rlim_max;
if (setrlimit(RLIMIT_DATA, &dlim) == -1)
goaway("Error setting resource limit (%s)",
strerror(errno));
}
#endif
init(argc, argv); /* process arguments */
1993-05-21 18:52:16 +04:00
for (c = firstC; c; c = c->Cnext) {
collname = c->Cname;
basedir = c->Cbase;
prefix = c->Cprefix;
(void) chdir(basedir);
2011-08-31 20:24:54 +04:00
scantime = time(NULL);
1997-08-05 02:03:52 +04:00
if (!quiet)
printf("SUP Scan for %s starting at %s", collname,
ctime(&scantime));
(void) fflush(stdout);
if (!setjmp(sjbuf)) {
makescanlists(); /* record names in scan files */
2011-08-31 20:24:54 +04:00
scantime = time(NULL);
1997-08-05 02:03:52 +04:00
if (!quiet)
printf("SUP Scan for %s completed at %s",
collname, ctime(&scantime));
1993-05-21 18:52:16 +04:00
} else
fprintf(stderr,
"SUP: Scan for %s aborted at %s", collname,
ctime(&scantime));
1997-08-05 02:03:52 +04:00
if (!quiet)
(void) fflush(stdout);
1993-05-21 18:52:16 +04:00
}
while ((c = firstC) != NULL) {
1993-05-21 18:52:16 +04:00
firstC = firstC->Cnext;
free(c->Cname);
free(c->Cbase);
if (c->Cprefix)
free(c->Cprefix);
2002-07-11 01:28:13 +04:00
free(c);
1993-05-21 18:52:16 +04:00
}
exit(0);
1993-05-21 18:52:16 +04:00
}
/*****************************************
*** I N I T I A L I Z A T I O N ***
*****************************************/
void
usage(void)
1993-05-21 18:52:16 +04:00
{
fprintf(stderr, "Usage: supscan [ -vq ] collection [ basedir ]\n");
fprintf(stderr, " supscan [ -vq ] -f dirfile\n");
fprintf(stderr, " supscan [ -vq ] -s\n");
exit(1);
1993-05-21 18:52:16 +04:00
}
void
init(int argc, char **argv)
1993-05-21 18:52:16 +04:00
{
char buf[STRINGLENGTH], fbuf[STRINGLENGTH], *p, *q;
1993-05-21 18:52:16 +04:00
FILE *f;
SCAN_COLLECTION **c;
int fflag, sflag;
char *filename = NULL;
1993-05-21 18:52:16 +04:00
1997-08-05 02:03:52 +04:00
quiet = FALSE;
1993-05-21 18:52:16 +04:00
trace = FALSE;
fflag = FALSE;
sflag = FALSE;
while (argc > 1 && argv[1][0] == '-') {
switch (argv[1][1]) {
case 'f':
fflag = TRUE;
if (argc == 2)
usage();
1993-05-21 18:52:16 +04:00
--argc;
argv++;
filename = argv[1];
break;
1997-08-05 02:03:52 +04:00
case 'q':
quiet = TRUE;
break;
1993-05-21 18:52:16 +04:00
case 'v':
trace = TRUE;
break;
case 's':
sflag = TRUE;
break;
default:
fprintf(stderr, "supscan: Invalid flag %s ignored\n",
argv[1]);
(void) fflush(stderr);
1993-05-21 18:52:16 +04:00
}
--argc;
argv++;
}
if (!fflag) {
(void) sprintf(fbuf, FILEDIRS, DEFDIR);
1993-05-21 18:52:16 +04:00
filename = fbuf;
}
if (sflag) {
if (argc != 1)
usage();
1993-05-21 18:52:16 +04:00
firstC = NULL;
c = &firstC;
(void) sprintf(buf, FILEHOSTS, DEFDIR);
if ((f = fopen(buf, "r")) == NULL)
quit(1, "supscan: Unable to open %s\n", buf);
while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
2009-10-18 00:46:03 +04:00
q = strchr(p, '\n');
if (q)
*q = 0;
2009-10-18 00:46:03 +04:00
if (strchr("#;:", *p))
continue;
collname = nxtarg(&p, " \t=");
p = skipover(p, " \t=");
if (!localhost(p))
continue;
2011-08-31 20:24:54 +04:00
*c = getscancoll(filename, estrdup(collname), NULL);
if (*c)
c = &((*c)->Cnext);
1993-05-21 18:52:16 +04:00
}
(void) fclose(f);
1993-05-21 18:52:16 +04:00
return;
}
if (argc < 2 && fflag) {
firstC = NULL;
c = &firstC;
if ((f = fopen(filename, "r")) == NULL)
quit(1, "supscan: Unable to open %s\n", filename);
while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
2009-10-18 00:46:03 +04:00
q = strchr(p, '\n');
if (q)
*q = 0;
2009-10-18 00:46:03 +04:00
if (strchr("#;:", *p))
continue;
q = nxtarg(&p, " \t=");
p = skipover(p, " \t=");
2006-04-02 05:39:48 +04:00
*c = getscancoll(filename, estrdup(q), estrdup(p));
if (*c)
c = &((*c)->Cnext);
1993-05-21 18:52:16 +04:00
}
(void) fclose(f);
1993-05-21 18:52:16 +04:00
return;
}
if (argc < 2 || argc > 3)
usage();
2006-04-02 05:39:48 +04:00
firstC = getscancoll(filename, estrdup(argv[1]),
2011-08-31 20:24:54 +04:00
argc > 2 ? estrdup(argv[2]) : NULL);
1993-05-21 18:52:16 +04:00
}
static SCAN_COLLECTION *
getscancoll(char *filename, char *collname, char *basedir)
1993-05-21 18:52:16 +04:00
{
char buf[STRINGLENGTH], *p, *q;
1993-05-21 18:52:16 +04:00
FILE *f;
SCAN_COLLECTION *c;
1993-05-21 18:52:16 +04:00
if (basedir == NULL) {
if ((f = fopen(filename, "r")) != NULL) {
while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
2009-10-18 00:46:03 +04:00
q = strchr(p, '\n');
if (q)
*q = 0;
2009-10-18 00:46:03 +04:00
if (strchr("#;:", *p))
continue;
q = nxtarg(&p, " \t=");
if (strcmp(q, collname) == 0) {
p = skipover(p, " \t=");
2006-04-02 05:39:48 +04:00
basedir = estrdup(p);
1993-05-21 18:52:16 +04:00
break;
}
}
(void) fclose(f);
1993-05-21 18:52:16 +04:00
}
if (basedir == NULL) {
(void) sprintf(buf, FILEBASEDEFAULT, collname);
2006-04-02 05:39:48 +04:00
basedir = estrdup(buf);
1993-05-21 18:52:16 +04:00
}
}
if (chdir(basedir) < 0) {
fprintf(stderr, "supscan: Can't chdir to base directory %s for %s\n",
basedir, collname);
1993-05-21 18:52:16 +04:00
return (NULL);
}
prefix = NULL;
(void) sprintf(buf, FILEPREFIX, collname);
if ((f = fopen(buf, "r")) != NULL) {
while ((p = fgets(buf, STRINGLENGTH, f)) != NULL) {
2009-10-18 00:46:03 +04:00
q = strchr(p, '\n');
if (q)
*q = 0;
2009-10-18 00:46:03 +04:00
if (strchr("#;:", *p))
continue;
2006-04-02 05:39:48 +04:00
prefix = estrdup(p);
1993-05-21 18:52:16 +04:00
if (chdir(prefix) < 0) {
fprintf(stderr, "supscan: can't chdir to %s from base directory %s for %s\n",
prefix, basedir, collname);
fclose(f);
free(prefix);
1993-05-21 18:52:16 +04:00
return (NULL);
}
break;
}
(void) fclose(f);
1993-05-21 18:52:16 +04:00
}
if ((c = (SCAN_COLLECTION *) malloc(sizeof(SCAN_COLLECTION))) == NULL)
quit(1, "supscan: can't malloc collection structure\n");
1993-05-21 18:52:16 +04:00
c->Cname = collname;
c->Cbase = basedir;
c->Cprefix = prefix;
c->Cnext = NULL;
return (c);
}
void
goaway(const char *fmt, ...)
1993-05-21 18:52:16 +04:00
{
va_list ap;
va_start(ap, fmt);
1993-05-21 18:52:16 +04:00
vfprintf(stderr, fmt, ap);
va_end(ap);
(void) putc('\n', stderr);
(void) fflush(stderr);
longjmp(sjbuf, TRUE);
1993-05-21 18:52:16 +04:00
}
int
localhost(char *host)
1993-05-21 18:52:16 +04:00
{
static char myhost[MAXHOSTNAMELEN + 1];
2002-07-11 01:28:13 +04:00
static unsigned int myhostlen;
unsigned int hostlen;
1993-05-21 18:52:16 +04:00
if (*myhost == '\0') {
/*
* We assume that the host name in the kernel is the
* fully qualified form.
*/
if (gethostname(myhost, sizeof(myhost)) < 0) {
quit(1, "supscan: can't get kernel host name\n");
1993-05-21 18:52:16 +04:00
}
myhost[sizeof(myhost) - 1] = '\0';
1993-05-21 18:52:16 +04:00
myhostlen = strlen(myhost);
}
/*
* Here, we assume that the 'host' parameter from the
* coll.host file is at least a prefix of the fully qualified
* host name of some machine. This will be true when modcoll(8)
* (and related scripts) maintain the relevant files, but if
* a person makes a manual change, problems could result. In
* particular, if a nicname, such as "Y" for "GANDALF.CS.CMU.EDU"
* is present in the coll.host file, things will not work as
* expected.
*/
hostlen = strlen(host);
return (strncasecmp(myhost, host,
hostlen < myhostlen ? hostlen : myhostlen) == 0);
1993-05-21 18:52:16 +04:00
}