1 |
tdb |
1.3 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.7 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.3 |
* 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 |
tdb |
1.8 |
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include "config.h" |
23 |
|
|
#endif |
24 |
|
|
|
25 |
pajs |
1.1 |
#include "ukcprog.h" |
26 |
|
|
#include <stdlib.h> |
27 |
|
|
#include <stdio.h> |
28 |
|
|
#include <string.h> |
29 |
|
|
#ifdef FREEBSD |
30 |
|
|
#include <sys/types.h> |
31 |
|
|
#endif |
32 |
|
|
#include <utmp.h> |
33 |
|
|
|
34 |
|
|
char *get_user_stats(){ |
35 |
|
|
char *xml_user_data; |
36 |
|
|
int nousers=0; |
37 |
|
|
char *user_list=NULL; |
38 |
|
|
char *user_tmp; |
39 |
|
|
#if defined(LINUX) || defined(SOLARIS) |
40 |
|
|
struct utmp *entry; |
41 |
|
|
#endif |
42 |
|
|
#ifdef FREEBSD |
43 |
|
|
struct utmp entry; |
44 |
|
|
FILE *f; |
45 |
|
|
#endif |
46 |
|
|
|
47 |
|
|
#if defined(LINUX) || defined(SOLARIS) |
48 |
pajs |
1.5 |
setutent(); |
49 |
pajs |
1.1 |
while((entry=getutent()) != NULL) { |
50 |
|
|
if(entry->ut_type==USER_PROCESS) { |
51 |
|
|
user_tmp=user_list; |
52 |
|
|
if(user_list==NULL){ |
53 |
|
|
user_list=strdup(entry->ut_user); |
54 |
|
|
}else{ |
55 |
|
|
user_list=strf("%s %s", user_list, entry->ut_user); |
56 |
|
|
} |
57 |
|
|
if(user_tmp!=NULL) free(user_tmp); |
58 |
|
|
nousers++; |
59 |
|
|
} |
60 |
|
|
} |
61 |
pajs |
1.5 |
endutent(); |
62 |
pajs |
1.1 |
#endif |
63 |
|
|
|
64 |
|
|
#ifdef FREEBSD |
65 |
|
|
if ((f=fopen(_PATH_UTMP, "r")) == NULL){ |
66 |
|
|
errf("Failed to open UTMP file (%m)"); |
67 |
|
|
return NULL; |
68 |
|
|
} |
69 |
|
|
|
70 |
|
|
while((fread(&entry, sizeof(entry),1,f)) != 0){ |
71 |
|
|
if (entry.ut_name[0] == '\0') continue; |
72 |
|
|
user_tmp=user_list; |
73 |
|
|
if(user_list==NULL){ |
74 |
|
|
user_list=strdup(entry.ut_name); |
75 |
|
|
}else{ |
76 |
|
|
user_list=strf("%s %s", user_list, entry.ut_name); |
77 |
|
|
} |
78 |
|
|
if(user_tmp!=NULL) free(user_tmp); |
79 |
|
|
nousers++; |
80 |
|
|
} |
81 |
pajs |
1.4 |
|
82 |
|
|
if ((fclose(f)) != 0) { |
83 |
|
|
errf("Failed to close file (%m)"); |
84 |
|
|
return NULL; |
85 |
|
|
} |
86 |
|
|
|
87 |
pajs |
1.1 |
#endif |
88 |
pajs |
1.6 |
if (user_list==NULL){ |
89 |
|
|
if((user_list=strdup("")) == NULL){ |
90 |
|
|
errf("strdup failed (%m)"); |
91 |
|
|
return NULL; |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
if((xml_user_data=strf("<users><list>%s</list><count>%d</count></users>",user_list,nousers)) == NULL){ |
95 |
|
|
errf("strf failed (%m)"); |
96 |
|
|
return NULL; |
97 |
|
|
} |
98 |
pajs |
1.2 |
if (user_list!=NULL) free(user_list); |
99 |
pajs |
1.1 |
return xml_user_data; |
100 |
|
|
} |