| 1 |
/* |
| 2 |
* i-scream central monitoring system |
| 3 |
* http://www.i-scream.org.uk |
| 4 |
* Copyright (C) 2000-2002 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. |
| 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 |
#include "ukcprog.h" |
| 22 |
#include <stdlib.h> |
| 23 |
#include <stdio.h> |
| 24 |
#include <string.h> |
| 25 |
#ifdef FREEBSD |
| 26 |
#include <sys/types.h> |
| 27 |
#endif |
| 28 |
#include <utmp.h> |
| 29 |
|
| 30 |
char *get_user_stats(){ |
| 31 |
char *xml_user_data; |
| 32 |
int nousers=0; |
| 33 |
char *user_list=NULL; |
| 34 |
char *user_tmp; |
| 35 |
#if defined(LINUX) || defined(SOLARIS) |
| 36 |
struct utmp *entry; |
| 37 |
#endif |
| 38 |
#ifdef FREEBSD |
| 39 |
struct utmp entry; |
| 40 |
FILE *f; |
| 41 |
#endif |
| 42 |
|
| 43 |
#if defined(LINUX) || defined(SOLARIS) |
| 44 |
setutent(); |
| 45 |
while((entry=getutent()) != NULL) { |
| 46 |
if(entry->ut_type==USER_PROCESS) { |
| 47 |
user_tmp=user_list; |
| 48 |
if(user_list==NULL){ |
| 49 |
user_list=strdup(entry->ut_user); |
| 50 |
}else{ |
| 51 |
user_list=strf("%s %s", user_list, entry->ut_user); |
| 52 |
} |
| 53 |
if(user_tmp!=NULL) free(user_tmp); |
| 54 |
nousers++; |
| 55 |
} |
| 56 |
} |
| 57 |
endutent(); |
| 58 |
#endif |
| 59 |
|
| 60 |
#ifdef FREEBSD |
| 61 |
if ((f=fopen(_PATH_UTMP, "r")) == NULL){ |
| 62 |
errf("Failed to open UTMP file (%m)"); |
| 63 |
return NULL; |
| 64 |
} |
| 65 |
|
| 66 |
while((fread(&entry, sizeof(entry),1,f)) != 0){ |
| 67 |
if (entry.ut_name[0] == '\0') continue; |
| 68 |
user_tmp=user_list; |
| 69 |
if(user_list==NULL){ |
| 70 |
user_list=strdup(entry.ut_name); |
| 71 |
}else{ |
| 72 |
user_list=strf("%s %s", user_list, entry.ut_name); |
| 73 |
} |
| 74 |
if(user_tmp!=NULL) free(user_tmp); |
| 75 |
nousers++; |
| 76 |
} |
| 77 |
|
| 78 |
if ((fclose(f)) != 0) { |
| 79 |
errf("Failed to close file (%m)"); |
| 80 |
return NULL; |
| 81 |
} |
| 82 |
|
| 83 |
#endif |
| 84 |
if (user_list==NULL){ |
| 85 |
if((user_list=strdup("")) == NULL){ |
| 86 |
errf("strdup failed (%m)"); |
| 87 |
return NULL; |
| 88 |
} |
| 89 |
} |
| 90 |
if((xml_user_data=strf("<users><list>%s</list><count>%d</count></users>",user_list,nousers)) == NULL){ |
| 91 |
errf("strf failed (%m)"); |
| 92 |
return NULL; |
| 93 |
} |
| 94 |
if (user_list!=NULL) free(user_list); |
| 95 |
return xml_user_data; |
| 96 |
} |