Subject: bin/6395: Enhancement to newsyslog - HUP process other than syslogd
To: None <gnats-bugs@gnats.netbsd.org>
From: Paul Goyette <paul@whooppee.com>
List: netbsd-bugs
Date: 11/02/1998 17:29:08
>Number:         6395
>Category:       bin
>Synopsis:       Enhancement to newsyslog - HUP process other than syslogd
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          change-request
>Submitter-Id:   net
>Arrival-Date:   Mon Nov  2 17:35:01 1998
>Last-Modified:
>Originator:     Paul Goyette
>Organization:
-----------------------------------------------------------------------------
| Paul Goyette      | PGP DSS Key fingerprint:   | E-mail addresses:        |
| Network Engineer  |   BCD7 5301 9513 58A6 0DBC |  paul@whooppee.com       |
| and kernel hacker |   91EB ADB1 A280 3B79 9221 |  paul.goyette@ascend.com |
-----------------------------------------------------------------------------
>Release:        1998-Oct-20
>Environment:
	
System: NetBSD pc1.whooppee.com 1.3H NetBSD 1.3H (PC1) #31: Sun Sep 27 10:57:52 PDT 1998 paul@pc1.whooppee.com:/home/paul/src/sys/arch/i386/compile/PC1 i386


>Description:
	The functionality of newsyslog should not be restricted to 
	files maintained by syslogd.  There are other programs, such 
	as httpd (Apache web server) that maintain log files which
	grow rather large over time, and that are kept open for the
	duration of the program.

	The attached patch modified /usr/bin/newsyslog to take an 
	optional argument in the newsyslog.conf file that specifies 
	the name of a proces to which the HUP signal should be sent.
	The default remains syslog.  The patch permits building in
	this new functionality conditionally, by defining two new
	macros, PIDDIR (the directory in which xxx.pid files should
	be found, presumably /var/run/) and the suffix to attach to
	the process name (by default, .pid).
>How-To-Repeat:
	N/A
>Fix:

*** /usr/src/usr.bin/newsyslog/newsyslog.c	Wed Jul 29 04:11:21 1998
--- ./newsyslog.c	Mon Nov  2 17:14:20 1998
***************
*** 83,88 ****
--- 83,91 ----
          int     permissions;    /* File permissions on the log */
          int     flags;          /* Flags (CE_COMPACT & CE_BINARY)  */
          struct conf_entry       *next; /* Linked list pointer */
+ #ifdef PIDDIR
+ 	int	pid;		/* pind of process to HUP */
+ #endif
  };
  
  char    *progname;              /* contains argv[0] */
***************
*** 101,107 ****
--- 104,115 ----
  void	PRS __P((int, char **));
  int	age_old_log __P((char *));
  void	compress_log __P((char *));
+ #ifdef	PIDDIR
+ int	get_the_pid(char *);
+ void	dotrim __P((char *, int, int, int, int, int, int));
+ #else
  void	dotrim __P((char *, int, int, int, int, int));
+ #endif
  void	do_entry __P((struct conf_entry *));
  int	isnumber __P((char *));
  int	log_trim __P((char *));
***************
*** 170,177 ****
--- 178,190 ----
                                          printf("%s <%d>: trimming",
                                                 ent->log,ent->numlogs);
                          }
+ #ifndef PIDDIR
                          dotrim(ent->log, ent->numlogs, ent->flags,
                                 ent->permissions, ent->uid, ent->gid);
+ #else
+ 			dotrim(ent->log, ent->numlogs, ent->flags,
+ 				ent->permissions, ent->uid, ent->gid, ent->pid);
+ #endif
                  } else {
                          if (verbose)
                                  printf("--> skipping\n");
***************
*** 356,361 ****
--- 369,383 ----
                                  working->flags |= CE_COMPACT;
                          else if ((*q == 'B') || (*q == 'b'))
                                  working->flags |= CE_BINARY;
+ #ifdef PIDDIR
+ 			else if ((*q == '*')) {
+ 				/*
+ 				 * placeholder  - required if optional 
+ 				 * process name is supplied without 
+ 				 * any other flags
+ 				 */
+ 			}
+ #endif
                          else {
                                  fprintf(stderr,
                                          "Illegal flag in config file -- %c\n",
***************
*** 364,369 ****
--- 386,399 ----
                          }
                          q++;
                  }
+ #ifdef PIDDIR
+ 		q = parse = sob(++parse); /* Optional field - process name */
+ 		*(parse = son(parse)) = '\0';
+ 		if (*q)
+ 			working->pid = get_the_pid(q);
+ 		else
+ 			working->pid = syslog_pid;
+ #endif PIDDIR
                  
                  free(errline);
          }
***************
*** 386,398 ****
--- 416,435 ----
  }
  
  void
+ #ifndef PIDDIR
  dotrim(log,numdays,flags,perm,owner_uid,group_gid)
+ #else
+ dotrim(log,numdays,flags,perm,owner_uid,group_gid,pid)
+ #endif
          char    *log;
          int     numdays;
          int     flags;
          int     perm;
          int     owner_uid;
          int     group_gid;
+ #ifdef PIDDIR
+ 	int	pid;
+ #endif
  {
          char    file1[128], file2[128];
          char    zfile1[128], zfile2[128];
***************
*** 481,486 ****
--- 518,524 ----
          else
                  (void) chmod(log,perm);
          if (noaction)
+ #ifndef PIDDIR
                  printf("kill -HUP %d (syslogd)\n",syslog_pid);
          else
  	if (syslog_pid < MIN_PID || syslog_pid > MAX_PID) {
***************
*** 490,495 ****
--- 528,544 ----
                          fprintf(stderr,"%s: ",progname);
                          perror("warning - could not restart syslogd");
                  }
+ #else
+ 		printf("kill -HUP %d (logging program)\n", pid);
+ 	else
+ 	if (pid < MIN_PID || pid > MAX_PID) {
+ 		fprintf(stderr, "%s: preposterous process number: %d\n",
+ 				progname, pid);
+ 	} else if (kill(pid, SIGHUP)) {
+ 			fprintf(stderr, "%s: ",progname);
+ 			perror("warning - could not restart logging program");
+ 		}
+ #endif
          if (flags & CE_COMPACT) {
                  if (noaction)
                          printf("Compress %s.0\n",log);
***************
*** 595,598 ****
--- 644,668 ----
              string++;
          }
          return(1);
+ }
+ 
+ /* get pid of a logging process */
+ 
+ int
+ get_the_pid(procname)
+ 	char *procname;
+ {
+ 	FILE	*f;
+ 	char	line[BUFSIZ];
+ 	int	name_len;
+ 	int	the_pid = 0;
+ 
+ 	name_len = snprintf(line, BUFSIZ, "%s%s%s", PIDDIR, procname, PIDSUFFIX);
+ 	f = fopen(line, "r");
+ 	if (f) {
+ 		if (fgets(line, BUFSIZ, f))
+ 			the_pid = atoi(line);
+ 		(void)fclose(f);
+ 	}
+ 	return (the_pid);
  }
*** /usr/src/usr.bin/newsyslog/Makefile	Wed Jul 29 04:11:20 1998
--- ./Makefile	Mon Nov  2 15:57:04 1998
***************
*** 6,11 ****
--- 6,13 ----
  CPPFLAGS+= -DPIDFILE=\"/var/run/syslog.pid\"
  CPPFLAGS+= -DCOMPRESS=\"/usr/bin/gzip\"
  CPPFLAGS+= -DCOMPRESS_POSTFIX=\".gz\"
+ CPPFLAGS+= -DPIDDIR=\"/var/run/\"
+ CPPFLAGS+= -DPIDSUFFIX=\".pid\"
  
  MAN=	newsyslog.8
  
>Audit-Trail:
>Unformatted: