ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/i-scream/projects/libstatgrab/src/libstatgrab/user_stats.c
Revision: 1.29
Committed: Sat Feb 20 17:55:04 2010 UTC (14 years, 2 months ago) by tdb
Content type: text/plain
Branch: MAIN
CVS Tags: LIBSTATGRAB_0_17, HEAD
Changes since 1.28: +16 -3 lines
Log Message:
Support utmpx in user_stats as well as utmp.

File Contents

# User Rev Content
1 tdb 1.14 /*
2 tdb 1.23 * i-scream libstatgrab
3 tdb 1.9 * http://www.i-scream.org
4 tdb 1.14 * Copyright (C) 2000-2004 i-scream
5 pajs 1.1 *
6 tdb 1.14 * 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 pajs 1.1 *
11 tdb 1.14 * This library is distributed in the hope that it will be useful,
12 pajs 1.1 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 tdb 1.14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14     * Lesser General Public License for more details.
15 pajs 1.1 *
16 tdb 1.14 * 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 tdb 1.15 *
21 tdb 1.29 * $Id: user_stats.c,v 1.28 2005/09/24 13:29:23 tdb Exp $
22 pajs 1.1 */
23    
24     #ifdef HAVE_CONFIG_H
25     #include "config.h"
26     #endif
27 tdb 1.3
28 pajs 1.1 #include <stdlib.h>
29     #include <stdio.h>
30     #include <string.h>
31 pajs 1.8 #include "statgrab.h"
32 ats 1.20 #include "vector.h"
33 ats 1.10 #ifdef ALLBSD
34 pajs 1.8 #include <sys/types.h>
35 ats 1.17 #endif
36 tdb 1.18 #if defined(NETBSD) || defined(OPENBSD)
37 ats 1.17 #include <limits.h>
38 pajs 1.8 #endif
39 tdb 1.18 #ifdef OPENBSD
40     #include <sys/param.h>
41     #endif
42 tdb 1.28 #ifndef WIN32
43 tdb 1.29 #ifdef HAVE_UTMPX
44     #include <utmpx.h>
45     #else
46 pajs 1.1 #include <utmp.h>
47 tdb 1.28 #endif
48 tdb 1.29 #endif
49 tdb 1.16 #ifdef CYGWIN
50     #include <sys/unistd.h>
51     #endif
52 tdb 1.27 #ifdef HPUX
53     #include <utmp.h>
54     #endif
55 tdb 1.28 #ifdef WIN32
56     #include <windows.h>
57     #include <lm.h>
58     #endif
59 pajs 1.1
60 ats 1.22 sg_user_stats *sg_get_user_stats(){
61 ats 1.20 int num_users = 0, pos = 0, new_pos;
62     VECTOR_DECLARE_STATIC(name_list, char, 128, NULL, NULL);
63 ats 1.22 static sg_user_stats user_stats;
64 tdb 1.29 #if defined(ALLBSD) && !defined(HAVE_UTMPX)
65 pajs 1.8 struct utmp entry;
66 tdb 1.24 FILE *f;
67 pajs 1.1
68 tdb 1.27 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 ats 1.20
75 tdb 1.27 new_pos = pos + strlen(entry.ut_name) + 1;
76 ats 1.20 if (VECTOR_RESIZE(name_list, new_pos) < 0) {
77     return NULL;
78 pajs 1.1 }
79 ats 1.20
80 tdb 1.27 strcpy(name_list + pos, entry.ut_name);
81 ats 1.20 name_list[new_pos - 1] = ' ';
82     pos = new_pos;
83     num_users++;
84 pajs 1.1 }
85 tdb 1.27 fclose(f);
86 tdb 1.28 #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     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 tdb 1.27 #else
138     /* This works on everything else. */
139 tdb 1.29 #ifdef HAVE_UTMPX
140     struct utmpx *entry;
141     setutxent();
142     while((entry=getutxent()) != NULL) {
143     #else
144 tdb 1.27 struct utmp *entry;
145     setutent();
146     while((entry=getutent()) != NULL) {
147 tdb 1.29 #endif
148 tdb 1.27 if (entry->ut_type != USER_PROCESS) continue;
149 ats 1.20
150 tdb 1.27 new_pos = pos + strlen(entry->ut_user) + 1;
151 ats 1.20 if (VECTOR_RESIZE(name_list, new_pos) < 0) {
152     return NULL;
153 pajs 1.8 }
154 ats 1.20
155 tdb 1.27 strcpy(name_list + pos, entry->ut_user);
156 ats 1.20 name_list[new_pos - 1] = ' ';
157     pos = new_pos;
158 pajs 1.8 num_users++;
159     }
160 tdb 1.29 #ifdef HAVE_UTMPX
161     endutxent();
162     #else
163 tdb 1.27 endutent();
164 ats 1.20 #endif
165 tdb 1.29 #endif
166 pajs 1.1
167 ats 1.20 /* Remove the extra space at the end, and append a \0. */
168     if (num_users != 0) {
169     pos--;
170 pajs 1.7 }
171 ats 1.21 if (VECTOR_RESIZE(name_list, pos + 1) < 0) {
172     return NULL;
173     }
174 ats 1.20 name_list[pos] = '\0';
175 pajs 1.5
176 ats 1.20 user_stats.num_entries = num_users;
177     user_stats.name_list = name_list;
178 pajs 1.5 return &user_stats;
179 pajs 1.1 }