Logo Search packages:      
Sourcecode: ud version File versions  Download package

uptime.c

/* 
   Copyright (C) 2002 Manuel Menal

   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, 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 
*/

#include <sys/time.h>
#include <time.h>
#include <stdlib.h>

#include <errno.h>

#include <ps.h>
#include <hurd.h>
#include <error.h>

float
get_uptime (void)
{
  struct timeval now;

  struct ps_context *context;
  struct proc_stat *ps;

  float uptime;
  error_t err;
  
  if (gettimeofday(&now, 0) < 0) 
    error (EXIT_FAILURE, errno, "gettimeofday");

  err = ps_context_create (getproc(), &context);
  if (err)
    error (EXIT_FAILURE, err, "ps_context_create");
    
  err = ps_context_find_proc_stat (context, 1, &ps);
  if (err)
    error (EXIT_FAILURE, err, "ps_context_find_proc_stat");

  err = proc_stat_set_flags (ps, PSTAT_TASK_BASIC);    
  if (!err && !(ps->flags & PSTAT_TASK_BASIC))
    err = EGRATUITOUS;
  if (err)
    error (EXIT_FAILURE, err, "cannot find boot time");
  else 
    {
      struct timeval boot_time, up;
      time_value_t *const tv = (&proc_stat_task_basic_info (ps)->creation_time);
      
      boot_time.tv_sec = tv->seconds;
      boot_time.tv_usec = tv->microseconds;

      timersub(&now, &boot_time, &up);
      
      uptime = up.tv_sec+(up.tv_usec/1000000);
    }
  ps_context_free (context);
  
  return uptime;
}

Generated by  Doxygen 1.6.0   Back to index