ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
(Generate patch)

Comparing projects/libstatgrab/src/libstatgrab/user_stats.c (file contents):
Revision 1.10 by ats, Sun Oct 19 02:03:02 2003 UTC vs.
Revision 1.27 by tdb, Mon Nov 1 18:30:17 2004 UTC

# Line 1 | Line 1
1 < /*
2 < * i-scream central monitoring system
1 > /*
2 > * i-scream libstatgrab
3   * http://www.i-scream.org
4 < * Copyright (C) 2000-2003 i-scream
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
# Line 26 | Line 29
29   #include <stdio.h>
30   #include <string.h>
31   #include "statgrab.h"
32 + #include "vector.h"
33   #ifdef ALLBSD
34   #include <sys/types.h>
35   #endif
36 < #include <utmp.h>
33 < #ifdef NETBSD
36 > #if defined(NETBSD) || defined(OPENBSD)
37   #include <limits.h>
35 #undef MAX_LOGIN_NAME_SIZE
36 #define MAX_LOGIN_NAME_SIZE _POSIX_LOGIN_NAME_MAX
38   #endif
39 <
40 < #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
40 <
41 < user_stat_t *get_user_stats(){
42 <        int num_users=0;
43 <
44 <        static user_stat_t user_stats;
45 <        static int size_of_namelist=-1;
46 <        char *tmp;
47 < #if defined(SOLARIS) || defined(LINUX)  
48 <        struct utmp *entry;
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 + #ifdef HPUX
47 + #include <utmp.h>
48 + #endif
49 +
50 + sg_user_stats *sg_get_user_stats(){
51 +        int num_users = 0, pos = 0, new_pos;
52 +        VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
53 +        static sg_user_stats user_stats;
54   #ifdef ALLBSD
55          struct utmp entry;
56 <        FILE *f;
53 < #endif
56 >        FILE *f;
57  
58 <        /* First case call */
59 <        if (size_of_namelist==-1){
60 <                user_stats.name_list=malloc(START_VAL);
61 <                if(user_stats.name_list==NULL){
58 >        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
59 >                sg_set_error_with_errno(SG_ERROR_OPEN, _PATH_UTMP);
60 >                return NULL;
61 >        }
62 >        while((fread(&entry, sizeof(entry),1,f)) != 0){
63 >                if (entry.ut_name[0] == '\0') continue;
64 >
65 >                new_pos = pos + strlen(entry.ut_name) + 1;
66 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
67                          return NULL;
68                  }
61                size_of_namelist=START_VAL;
62        }      
69  
70 <        /* Essentially blank the list, or give it a inital starting string */
71 <        strcpy(user_stats.name_list, "");
70 >                strcpy(name_list + pos, entry.ut_name);
71 >                name_list[new_pos - 1] = ' ';
72 >                pos = new_pos;
73 >                num_users++;
74 >        }
75 >        fclose(f);
76 > #else
77 >        /* This works on everything else. */
78 >        struct utmp *entry;
79  
67 #if defined(SOLARIS) || defined(LINUX)  
80          setutent();
81          while((entry=getutent()) != NULL) {
82 <                if(entry->ut_type==USER_PROCESS) {
71 <                        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 <                                        return NULL;
77 <                                }
78 <                                size_of_namelist=1+(size_of_namelist*2);
79 <                        }
82 >                if (entry->ut_type != USER_PROCESS) continue;
83  
84 <                        strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
85 <                        strcat(user_stats.name_list, " ");
86 <                        num_users++;
84 >                new_pos = pos + strlen(entry->ut_user) + 1;
85 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
86 >                        return NULL;
87                  }
88 +
89 +                strcpy(name_list + pos, entry->ut_user);
90 +                name_list[new_pos - 1] = ' ';
91 +                pos = new_pos;
92 +                num_users++;
93          }
94          endutent();
95   #endif
96 < #ifdef ALLBSD
97 <        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
96 >
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 <        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);
104 >        name_list[pos] = '\0';
105  
106 < #endif  
107 <        /* We want to remove the last " " */
112 <        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 <
106 >        user_stats.num_entries = num_users;
107 >        user_stats.name_list = name_list;
108          return &user_stats;
121
109   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines