ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.10
Committed: Sun Oct 19 02:03:02 2003 UTC (20 years, 7 months ago) by ats
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_7
Changes since 1.9: +8 -3 lines
Log Message:
Initial support for NetBSD. This adds NetBSD support for everything
except diskio stats (since they're even more disturbingly complex to get
at on NetBSD than the three OSs we already support). Tested against
NetBSD 1.6 on i386.

File Contents

# User Rev Content
1 pajs 1.1 /*
2     * i-scream central monitoring system
3 tdb 1.9 * http://www.i-scream.org
4     * Copyright (C) 2000-2003 i-scream
5 pajs 1.1 *
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 tdb 1.3
25 pajs 1.1 #include <stdlib.h>
26     #include <stdio.h>
27     #include <string.h>
28 pajs 1.8 #include "statgrab.h"
29 ats 1.10 #ifdef ALLBSD
30 pajs 1.8 #include <sys/types.h>
31     #endif
32 pajs 1.1 #include <utmp.h>
33 ats 1.10 #ifdef NETBSD
34     #include <limits.h>
35     #undef MAX_LOGIN_NAME_SIZE
36     #define MAX_LOGIN_NAME_SIZE _POSIX_LOGIN_NAME_MAX
37     #endif
38 pajs 1.1
39 pajs 1.5 #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
40 pajs 1.1
41     user_stat_t *get_user_stats(){
42     int num_users=0;
43    
44 pajs 1.5 static user_stat_t user_stats;
45     static int size_of_namelist=-1;
46     char *tmp;
47 pajs 1.8 #if defined(SOLARIS) || defined(LINUX)
48 pajs 1.1 struct utmp *entry;
49 pajs 1.8 #endif
50 ats 1.10 #ifdef ALLBSD
51 pajs 1.8 struct utmp entry;
52     FILE *f;
53     #endif
54 pajs 1.1
55     /* First case call */
56 pajs 1.5 if (size_of_namelist==-1){
57     user_stats.name_list=malloc(START_VAL);
58     if(user_stats.name_list==NULL){
59 pajs 1.1 return NULL;
60     }
61 pajs 1.5 size_of_namelist=START_VAL;
62 pajs 1.1 }
63    
64 pajs 1.5 /* Essentially blank the list, or give it a inital starting string */
65     strcpy(user_stats.name_list, "");
66 pajs 1.8
67     #if defined(SOLARIS) || defined(LINUX)
68 pajs 1.1 setutent();
69     while((entry=getutent()) != NULL) {
70     if(entry->ut_type==USER_PROCESS) {
71 pajs 1.5 if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
72     tmp=user_stats.name_list;
73     user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
74     if(user_stats.name_list==NULL){
75     user_stats.name_list=tmp;
76 pajs 1.1 return NULL;
77     }
78 pajs 1.5 size_of_namelist=1+(size_of_namelist*2);
79 pajs 1.1 }
80    
81 pajs 1.5 strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
82     strcat(user_stats.name_list, " ");
83 pajs 1.1 num_users++;
84     }
85     }
86     endutent();
87 pajs 1.8 #endif
88 ats 1.10 #ifdef ALLBSD
89 pajs 1.8 if ((f=fopen(_PATH_UTMP, "r")) == NULL){
90     return NULL;
91     }
92     while((fread(&entry, sizeof(entry),1,f)) != 0){
93     if (entry.ut_name[0] == '\0') continue;
94     if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
95     tmp=user_stats.name_list;
96     user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
97     if(user_stats.name_list==NULL){
98     user_stats.name_list=tmp;
99     return NULL;
100     }
101     size_of_namelist=1+(size_of_namelist*2);
102    
103     }
104     strncat(user_stats.name_list, entry.ut_name, MAX_LOGIN_NAME_SIZE);
105     strcat(user_stats.name_list, " ");
106     num_users++;
107     }
108     fclose(f);
109 pajs 1.1
110 pajs 1.8 #endif
111 pajs 1.5 /* We want to remove the last " " */
112 pajs 1.7 if(num_users!=0){
113     tmp=strrchr(user_stats.name_list, ' ');
114     if(tmp!=NULL){
115     *tmp='\0';
116     user_stats.num_entries=num_users;
117     }
118     }
119 pajs 1.5
120     return &user_stats;
121 pajs 1.1
122     }