Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 12 Feb 2001 18:00:15 -0600
From:      Lucas Bergman <lucas@slb.to>
To:        Doug Young <dougy@bryden.apana.org.au>
Cc:        freebsd-questions@freebsd.org
Subject:   Re: Scripting issue - pppd
Message-ID:  <20010212180015.A16376@billygoat.slb.to>
In-Reply-To: <02a601c09432$54a5c6c0$847e03cb@apana.org.au>; from dougy@bryden.apana.org.au on Sun, Feb 11, 2001 at 11:55:40PM %2B1000
References:  <02a601c09432$54a5c6c0$847e03cb@apana.org.au>

next in thread | previous in thread | raw e-mail | index | archive | help
> I need to setup a script to disconnect dialin (pppd) users after
> they have been online for more than 8 hours in any one 24 hour
> period but haven't a clue where to start. 

Finding out how long a process has been running from the shell is the
hard part of this task.  The following C program will tell you how
long (in seconds) a process has been running.

For example, if I do

  % gcc -Wall -O -o howlong howlong.c
  % ./howlong 1
  2592578

I get that `init' has been running 2.59 million seconds, or just a
smidge over 30 days.  This makes sense, since

  % uptime
   5:57PM  up 30 days, 6 mins, 1 user, load averages: 0.07, 0.02, 0.00

Note that this program is FreeBSD-specific, since it uses /proc
in an essential way.

The rest of your problem should be easy, if not trivial, shell
scripting; good luck.

Lucas

-------------- begin howlong.c --------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

void usage();

int
main(int argc, char **av)
{
  unsigned long pid, start;
  char   *end;
  char    procdir[80];   /* never overflows, if av[1] is valid */
  char    starttime[20]; /* never overflows */
  FILE   *f;
  int     c, field, i = 0;

  if (argc != 2) { usage(); exit(1); }
  pid = strtoul(av[1], &end, 10);
  if (*end) { usage(); exit(1); }
  
  strcpy(procdir, "/proc/");
  strcat(procdir, av[1]);
  if (chdir(procdir) == -1) {
    fprintf(stderr, "howlong: fatal: could not change to %s\n", procdir);
    fprintf(stderr, "Probably, the process ID %s is not running.\n", av[1]);
    exit(2);
  }

  f = fopen("status", "r");
  if (!f) {
    fprintf(stderr, "howlong: panic: could not open %s/status\n", procdir);
    exit(3);
  }

  field = 1;
  while (field < 8 && (c = fgetc(f)) >= 0) if ((char) c == ' ') ++field;
  if (c == EOF) {
    fclose(f);
    fprintf(stderr, "howlong: panic: could not get time field from status\n");
    exit(4);
  }

  while ((c = fgetc(f)) >= 0 && ((char) c != ',')) {
    if (i > 18) {
      /* we would overflow starttime */
      fclose(f);
      fprintf(stderr, "howlong: panic: bad start time in status\n");
      exit(5);
    }
    starttime[i++] = (char) c;
  }
  fclose(f);
  starttime[i] = 0;

  start = strtoul(starttime, &end, 10);
  if (*end) {
    fprintf(stderr, "howlong: panic: bad start time in status\n");
    exit(6);
  }

  printf("%ld\n", time(0) - start);
  return 0;
}

void
usage()
{
  fprintf(stderr, "usage: howlong pid\n");
}
-------------- end howlong.c ----------------------


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-questions" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20010212180015.A16376>