1 |
pajs |
1.1 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
|
|
* http://www.i-scream.org.uk |
4 |
|
|
* Copyright (C) 2000-2002 i-scream |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or |
7 |
|
|
* modify it under the terms of the GNU General Public License |
8 |
|
|
* as published by the Free Software Foundation; either version 2 |
9 |
|
|
* of the License, or (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
|
|
*/ |
20 |
|
|
|
21 |
|
|
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include "config.h" |
23 |
|
|
#endif |
24 |
|
|
#include "statgrab.h" |
25 |
|
|
#include <stdlib.h> |
26 |
|
|
#include <stdio.h> |
27 |
|
|
#include <string.h> |
28 |
|
|
#ifdef FREEBSD |
29 |
|
|
#include <sys/types.h> |
30 |
|
|
#endif |
31 |
|
|
#include <utmp.h> |
32 |
|
|
|
33 |
|
|
#define START_VAL 5 |
34 |
|
|
|
35 |
|
|
user_stat_t *get_user_stats(){ |
36 |
|
|
int num_users=0; |
37 |
|
|
|
38 |
|
|
static user_stat_t user_stat; |
39 |
|
|
static int watermark=-1; |
40 |
|
|
struct utmp *entry; |
41 |
|
|
|
42 |
|
|
name *name_ptr; |
43 |
|
|
|
44 |
|
|
/* First case call */ |
45 |
|
|
if (watermark==-1){ |
46 |
|
|
user_stat.name_list=malloc(START_VAL * sizeof *user_stat.name_list); |
47 |
|
|
if(user_stat.name_list==NULL){ |
48 |
|
|
return NULL; |
49 |
|
|
} |
50 |
|
|
watermark=START_VAL; |
51 |
|
|
} |
52 |
|
|
|
53 |
|
|
setutent(); |
54 |
|
|
while((entry=getutent()) != NULL) { |
55 |
|
|
if(entry->ut_type==USER_PROCESS) { |
56 |
|
|
if(num_users>watermark-1){ |
57 |
|
|
name_ptr=user_stat.name_list; |
58 |
|
|
if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof *user_stat.name_list)))==NULL){ |
59 |
|
|
user_stat.name_list=name_ptr; |
60 |
|
|
return NULL; |
61 |
|
|
} |
62 |
|
|
watermark=watermark*2; |
63 |
|
|
} |
64 |
|
|
|
65 |
|
|
strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE); |
66 |
|
|
/* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */ |
67 |
|
|
|
68 |
|
|
user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0'; |
69 |
|
|
|
70 |
|
|
num_users++; |
71 |
|
|
} |
72 |
|
|
} |
73 |
|
|
endutent(); |
74 |
|
|
user_stat.num_entries=num_users; |
75 |
|
|
|
76 |
|
|
return &user_stat; |
77 |
|
|
|
78 |
|
|
} |