| 1 |
/* |
| 2 |
* Copyright (c) 2009, Giampaolo Rodola'. All rights reserved. |
| 3 |
* Use of this source code is governed by a BSD-style license that can be |
| 4 |
* found in the LICENSE file. |
| 5 |
*/ |
| 6 |
|
| 7 |
#include <Python.h> |
| 8 |
|
| 9 |
#include <utmpx.h> |
| 10 |
|
| 11 |
#include "../../arch/all/init.h" |
| 12 |
|
| 13 |
|
| 14 |
PyObject * |
| 15 |
psutil_boot_time(PyObject *self, PyObject *args) { |
| 16 |
float boot_time = 0.0; |
| 17 |
struct utmpx *ut; |
| 18 |
|
| 19 |
UTXENT_MUTEX_LOCK(); |
| 20 |
setutxent(); |
| 21 |
while (NULL != (ut = getutxent())) { |
| 22 |
if (ut->ut_type == BOOT_TIME) { |
| 23 |
boot_time = (float)ut->ut_tv.tv_sec; |
| 24 |
break; |
| 25 |
} |
| 26 |
} |
| 27 |
endutxent(); |
| 28 |
UTXENT_MUTEX_UNLOCK(); |
| 29 |
if (fabs(boot_time) < 0.000001) { |
| 30 |
/* could not find BOOT_TIME in getutxent loop */ |
| 31 |
PyErr_SetString(PyExc_RuntimeError, "can't determine boot time"); |
| 32 |
return NULL; |
| 33 |
} |
| 34 |
return Py_BuildValue("f", boot_time); |
| 35 |
} |