ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/cms/source/ihost/libstatgrab/user_stats.c
Revision: 1.6
Committed: Tue May 21 16:16:41 2002 UTC (22 years, 6 months ago) by pajs
Content type: text/plain
Branch: MAIN
Changes since 1.5: +10 -2 lines
Log Message:
Bug fixes

File Contents

# User Rev Content
1 tdb 1.3 /*
2     * i-scream central monitoring system
3     * Copyright (C) 2000-2002 i-scream
4     *
5     * This program is free software; you can redistribute it and/or
6     * modify it under the terms of the GNU General Public License
7     * as published by the Free Software Foundation; either version 2
8     * of the License, or (at your option) any later version.
9     *
10     * This program is distributed in the hope that it will be useful,
11     * but WITHOUT ANY WARRANTY; without even the implied warranty of
12     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13     * GNU General Public License for more details.
14     *
15     * You should have received a copy of the GNU General Public License
16     * along with this program; if not, write to the Free Software
17     * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18     */
19    
20 pajs 1.1 #include "ukcprog.h"
21     #include <stdlib.h>
22     #include <stdio.h>
23     #include <string.h>
24     #ifdef FREEBSD
25     #include <sys/types.h>
26     #endif
27     #include <utmp.h>
28    
29     char *get_user_stats(){
30     char *xml_user_data;
31     int nousers=0;
32     char *user_list=NULL;
33     char *user_tmp;
34     #if defined(LINUX) || defined(SOLARIS)
35     struct utmp *entry;
36     #endif
37     #ifdef FREEBSD
38     struct utmp entry;
39     FILE *f;
40     #endif
41    
42     #if defined(LINUX) || defined(SOLARIS)
43 pajs 1.5 setutent();
44 pajs 1.1 while((entry=getutent()) != NULL) {
45     if(entry->ut_type==USER_PROCESS) {
46     user_tmp=user_list;
47     if(user_list==NULL){
48     user_list=strdup(entry->ut_user);
49     }else{
50     user_list=strf("%s %s", user_list, entry->ut_user);
51     }
52     if(user_tmp!=NULL) free(user_tmp);
53     nousers++;
54     }
55     }
56 pajs 1.5 endutent();
57 pajs 1.1 #endif
58    
59     #ifdef FREEBSD
60     if ((f=fopen(_PATH_UTMP, "r")) == NULL){
61     errf("Failed to open UTMP file (%m)");
62     return NULL;
63     }
64    
65     while((fread(&entry, sizeof(entry),1,f)) != 0){
66     if (entry.ut_name[0] == '\0') continue;
67     user_tmp=user_list;
68     if(user_list==NULL){
69     user_list=strdup(entry.ut_name);
70     }else{
71     user_list=strf("%s %s", user_list, entry.ut_name);
72     }
73     if(user_tmp!=NULL) free(user_tmp);
74     nousers++;
75     }
76 pajs 1.4
77     if ((fclose(f)) != 0) {
78     errf("Failed to close file (%m)");
79     return NULL;
80     }
81    
82 pajs 1.1 #endif
83 pajs 1.6 if (user_list==NULL){
84     if((user_list=strdup("")) == NULL){
85     errf("strdup failed (%m)");
86     return NULL;
87     }
88     }
89     if((xml_user_data=strf("<users><list>%s</list><count>%d</count></users>",user_list,nousers)) == NULL){
90     errf("strf failed (%m)");
91     return NULL;
92     }
93 pajs 1.2 if (user_list!=NULL) free(user_list);
94 pajs 1.1 return xml_user_data;
95     }