1 |
< |
/* |
2 |
< |
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
1 |
> |
/* |
2 |
> |
* i-scream libstatgrab |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2004 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. |
6 |
> |
* This library is free software; you can redistribute it and/or |
7 |
> |
* modify it under the terms of the GNU Lesser General Public |
8 |
> |
* License as published by the Free Software Foundation; either |
9 |
> |
* version 2.1 of the License, or (at your option) any later version. |
10 |
|
* |
11 |
< |
* This program is distributed in the hope that it will be useful, |
11 |
> |
* This library 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. |
13 |
> |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
14 |
> |
* Lesser 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. |
16 |
> |
* You should have received a copy of the GNU Lesser General Public |
17 |
> |
* License along with this library; if not, write to the Free Software |
18 |
> |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
19 |
> |
* 02111-1307 USA |
20 |
> |
* |
21 |
> |
* $Id$ |
22 |
|
*/ |
23 |
|
|
24 |
|
#ifdef HAVE_CONFIG_H |
25 |
|
#include "config.h" |
26 |
|
#endif |
27 |
< |
#include "statgrab.h" |
25 |
< |
#include "ukcprog.h" |
27 |
> |
|
28 |
|
#include <stdlib.h> |
29 |
|
#include <stdio.h> |
30 |
|
#include <string.h> |
31 |
< |
#ifdef FREEBSD |
31 |
> |
#include "statgrab.h" |
32 |
> |
#include "vector.h" |
33 |
> |
#ifdef ALLBSD |
34 |
|
#include <sys/types.h> |
35 |
|
#endif |
36 |
+ |
#if defined(NETBSD) || defined(OPENBSD) |
37 |
+ |
#include <limits.h> |
38 |
+ |
#endif |
39 |
+ |
#ifdef OPENBSD |
40 |
+ |
#include <sys/param.h> |
41 |
+ |
#endif |
42 |
|
#include <utmp.h> |
43 |
+ |
#ifdef CYGWIN |
44 |
+ |
#include <sys/unistd.h> |
45 |
+ |
#endif |
46 |
|
|
47 |
< |
#define START_VAL 5 |
48 |
< |
|
49 |
< |
user_stat_t *get_user_stats(){ |
50 |
< |
int num_users=0; |
51 |
< |
|
39 |
< |
static user_stat_t user_stat; |
40 |
< |
static int watermark=-1; |
47 |
> |
sg_user_stats *sg_get_user_stats(){ |
48 |
> |
int num_users = 0, pos = 0, new_pos; |
49 |
> |
VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL); |
50 |
> |
static sg_user_stats user_stats; |
51 |
> |
#if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN) |
52 |
|
struct utmp *entry; |
53 |
+ |
#endif |
54 |
+ |
#ifdef ALLBSD |
55 |
+ |
struct utmp entry; |
56 |
+ |
FILE *f; |
57 |
+ |
#endif |
58 |
|
|
59 |
< |
name *name_ptr; |
59 |
> |
#if defined(SOLARIS) || defined(LINUX) || defined(CYGWIN) |
60 |
> |
setutent(); |
61 |
> |
while((entry=getutent()) != NULL) { |
62 |
> |
if (entry->ut_type != USER_PROCESS) continue; |
63 |
|
|
64 |
< |
/* First case call */ |
65 |
< |
if (watermark==-1){ |
47 |
< |
user_stat.name_list=malloc(START_VAL * sizeof *user_stat.name_list); |
48 |
< |
if(user_stat.name_list==NULL){ |
64 |
> |
new_pos = pos + strlen(entry->ut_user) + 1; |
65 |
> |
if (VECTOR_RESIZE(name_list, new_pos) < 0) { |
66 |
|
return NULL; |
67 |
|
} |
51 |
– |
watermark=START_VAL; |
52 |
– |
} |
68 |
|
|
69 |
< |
setutent(); |
70 |
< |
while((entry=getutent()) != NULL) { |
71 |
< |
if(entry->ut_type==USER_PROCESS) { |
72 |
< |
if(num_users>watermark-1){ |
73 |
< |
name_ptr=user_stat.name_list; |
74 |
< |
if((user_stat.name_list=realloc(user_stat.name_list, (watermark*2* sizeof *user_stat.name_list)))==NULL){ |
75 |
< |
user_stat.name_list=name_ptr; |
76 |
< |
return NULL; |
77 |
< |
} |
78 |
< |
watermark=watermark*2; |
79 |
< |
} |
69 |
> |
strcpy(name_list + pos, entry->ut_user); |
70 |
> |
name_list[new_pos - 1] = ' '; |
71 |
> |
pos = new_pos; |
72 |
> |
num_users++; |
73 |
> |
} |
74 |
> |
endutent(); |
75 |
> |
#endif |
76 |
> |
#ifdef ALLBSD |
77 |
> |
if ((f=fopen(_PATH_UTMP, "r")) == NULL){ |
78 |
> |
sg_set_error(SG_ERROR_OPEN, _PATH_UTMP); |
79 |
> |
return NULL; |
80 |
> |
} |
81 |
> |
while((fread(&entry, sizeof(entry),1,f)) != 0){ |
82 |
> |
if (entry.ut_name[0] == '\0') continue; |
83 |
|
|
84 |
< |
strncpy(user_stat.name_list[num_users], entry->ut_user, MAX_LOGIN_NAME_SIZE); |
85 |
< |
/* NULL terminate just in case , the size of user_stat.name_list is MAX_LOGIN_NAME_SIZE+1 */ |
86 |
< |
|
69 |
< |
user_stat.name_list[num_users][MAX_LOGIN_NAME_SIZE]='\0'; |
70 |
< |
|
71 |
< |
num_users++; |
84 |
> |
new_pos = pos + strlen(entry.ut_name) + 1; |
85 |
> |
if (VECTOR_RESIZE(name_list, new_pos) < 0) { |
86 |
> |
return NULL; |
87 |
|
} |
88 |
+ |
|
89 |
+ |
strcpy(name_list + pos, entry.ut_name); |
90 |
+ |
name_list[new_pos - 1] = ' '; |
91 |
+ |
pos = new_pos; |
92 |
+ |
num_users++; |
93 |
|
} |
94 |
< |
endutent(); |
95 |
< |
user_stat.num_entries=num_users; |
94 |
> |
fclose(f); |
95 |
> |
#endif |
96 |
|
|
97 |
< |
return &user_stat; |
97 |
> |
/* Remove the extra space at the end, and append a \0. */ |
98 |
> |
if (num_users != 0) { |
99 |
> |
pos--; |
100 |
> |
} |
101 |
> |
if (VECTOR_RESIZE(name_list, pos + 1) < 0) { |
102 |
> |
return NULL; |
103 |
> |
} |
104 |
> |
name_list[pos] = '\0'; |
105 |
|
|
106 |
+ |
user_stats.num_entries = num_users; |
107 |
+ |
user_stats.name_list = name_list; |
108 |
+ |
return &user_stats; |
109 |
|
} |