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.6 by pajs, Sun Mar 2 16:43:48 2003 UTC vs.
Revision 1.29 by tdb, Sat Feb 20 17:55:04 2010 UTC

# Line 1 | Line 1
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
# Line 25 | Line 28
28   #include <stdlib.h>
29   #include <stdio.h>
30   #include <string.h>
28 #include <utmp.h>
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 + #ifndef WIN32
43 + #ifdef HAVE_UTMPX
44 + #include <utmpx.h>
45 + #else
46 + #include <utmp.h>
47 + #endif
48 + #endif
49 + #ifdef CYGWIN
50 + #include <sys/unistd.h>
51 + #endif
52 + #ifdef HPUX
53 + #include <utmp.h>
54 + #endif
55 + #ifdef WIN32
56 + #include <windows.h>
57 + #include <lm.h>
58 + #endif
59  
60 < #define START_VAL (5*(1+MAX_LOGIN_NAME_SIZE))
60 > sg_user_stats *sg_get_user_stats(){
61 >        int num_users = 0, pos = 0, new_pos;
62 >        VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
63 >        static sg_user_stats user_stats;
64 > #if defined(ALLBSD) && !defined(HAVE_UTMPX)
65 >        struct utmp entry;
66 >        FILE *f;
67  
68 < user_stat_t *get_user_stats(){
69 <        int num_users=0;
68 >        if ((f=fopen(_PATH_UTMP, "r")) == NULL){
69 >                sg_set_error_with_errno(SG_ERROR_OPEN, _PATH_UTMP);
70 >                return NULL;
71 >        }
72 >        while((fread(&entry, sizeof(entry),1,f)) != 0){
73 >                if (entry.ut_name[0] == '\0') continue;
74  
75 <        static user_stat_t user_stats;
76 <        static int size_of_namelist=-1;
38 <        char *tmp;
39 <        struct utmp *entry;
40 <
41 <        /* First case call */
42 <        if (size_of_namelist==-1){
43 <                user_stats.name_list=malloc(START_VAL);
44 <                if(user_stats.name_list==NULL){
75 >                new_pos = pos + strlen(entry.ut_name) + 1;
76 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
77                          return NULL;
78                  }
47                size_of_namelist=START_VAL;
48        }      
79  
80 <        /* Essentially blank the list, or give it a inital starting string */
81 <        strcpy(user_stats.name_list, "");
82 <        setutent();
83 <        while((entry=getutent()) != NULL) {
84 <                if(entry->ut_type==USER_PROCESS) {
85 <                        if((strlen(user_stats.name_list)+MAX_LOGIN_NAME_SIZE+2) > size_of_namelist){
86 <                                tmp=user_stats.name_list;
87 <                                user_stats.name_list=realloc(user_stats.name_list, 1+(size_of_namelist*2));
88 <                                if(user_stats.name_list==NULL){
89 <                                        user_stats.name_list=tmp;
80 >                strcpy(name_list + pos, entry.ut_name);
81 >                name_list[new_pos - 1] = ' ';
82 >                pos = new_pos;
83 >                num_users++;
84 >        }
85 >        fclose(f);
86 > #elif defined (WIN32)
87 >        LPWKSTA_USER_INFO_0 buf = NULL;
88 >        LPWKSTA_USER_INFO_0 tmp_buf;
89 >        unsigned long entries_read = 0;
90 >        unsigned long entries_tot = 0;
91 >        unsigned long resumehandle = 0;
92 >        NET_API_STATUS nStatus;
93 >        int i;
94 >        char name[256];
95 >
96 >        do {
97 >                nStatus = NetWkstaUserEnum(NULL, 0, (LPBYTE*)&buf,
98 >                                MAX_PREFERRED_LENGTH, &entries_read,
99 >                                &entries_tot, &resumehandle);
100 >                if((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA)) {
101 >                        if((tmp_buf = buf) == NULL) {
102 >                                continue;
103 >                        }
104 >                        for (i=0; i<entries_read; i++) {
105 >                                //assert(tmp_buf != NULL);
106 >                                if (tmp_buf == NULL) {
107 >                                        sg_set_error(SG_ERROR_PERMISSION, "User list");
108 >                                        break;
109 >                                }
110 >                                /* It's in unicode. We are not. Convert */
111 >                                WideCharToMultiByte(CP_ACP, 0, tmp_buf->wkui0_username, -1, name, sizeof(name), NULL, NULL);
112 >
113 >                                new_pos = pos + strlen(name) + 1;
114 >                                if(VECTOR_RESIZE(name_list, new_pos) < 0) {
115 >                                        NetApiBufferFree(buf);
116                                          return NULL;
117                                  }
118 <                                size_of_namelist=1+(size_of_namelist*2);
118 >                                strcpy(name_list + pos, name);
119 >                                name_list[new_pos - 1] = ' ';
120 >                                pos = new_pos;
121 >
122 >                                tmp_buf++;
123 >                                num_users++;
124                          }
125 +                } else {
126 +                        sg_set_error(SG_ERROR_PERMISSION, "User enum");
127 +                        return NULL;
128 +                }
129 +                if (buf != NULL) {
130 +                        NetApiBufferFree(buf);
131 +                        buf=NULL;
132 +                }
133 +        } while (nStatus == ERROR_MORE_DATA);
134 +        if (buf != NULL) {
135 +                NetApiBufferFree(buf);
136 +        }
137 + #else
138 +        /* This works on everything else. */
139 + #ifdef HAVE_UTMPX
140 +        struct utmpx *entry;
141 +        setutxent();
142 +        while((entry=getutxent()) != NULL) {
143 + #else
144 +        struct utmp *entry;
145 +        setutent();
146 +        while((entry=getutent()) != NULL) {
147 + #endif
148 +                if (entry->ut_type != USER_PROCESS) continue;
149  
150 <                        strncat(user_stats.name_list, entry->ut_user, MAX_LOGIN_NAME_SIZE);
151 <                        strcat(user_stats.name_list, " ");
152 <                        num_users++;
150 >                new_pos = pos + strlen(entry->ut_user) + 1;
151 >                if (VECTOR_RESIZE(name_list, new_pos) < 0) {
152 >                        return NULL;
153                  }
154 +
155 +                strcpy(name_list + pos, entry->ut_user);
156 +                name_list[new_pos - 1] = ' ';
157 +                pos = new_pos;
158 +                num_users++;
159          }
160 + #ifdef HAVE_UTMPX
161 +        endutxent();
162 + #else
163          endutent();
164 + #endif
165 + #endif
166  
167 <        /* We want to remove the last " " */
168 <        tmp=strrchr(user_stats.name_list, ' ');
169 <        *tmp='\0';
170 <        user_stats.num_entries=num_users;
167 >        /* Remove the extra space at the end, and append a \0. */
168 >        if (num_users != 0) {
169 >                pos--;
170 >        }
171 >        if (VECTOR_RESIZE(name_list, pos + 1) < 0) {
172 >                return NULL;
173 >        }
174 >        name_list[pos] = '\0';
175  
176 +        user_stats.num_entries = num_users;
177 +        user_stats.name_list = name_list;
178          return &user_stats;
78
179   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines