/soc/2012/sanket/statscollector-2.x.y: e57a693d6a03: Make the pl...

Sanket Agarwal sanket at soc.pidgin.im
Tue Jul 10 00:36:23 EDT 2012


Changeset: e57a693d6a03ea572d89c1e534744ba78bd9a9d1
Author:	 Sanket Agarwal <sanket at soc.pidgin.im>
Date:	 2012-05-21 06:46 +0000
Branch:	 soc.2012.statscollector
URL: http://hg.pidgin.im/soc/2012/sanket/statscollector-2.x.y/rev/e57a693d6a03

Description:

Make the plugin finch based; add OS handling for APPLE

There are major changes with this commit.

* The plugin type is changed to libpurple so that testing could be done
on UI-less systems
* I have added a *LOT* of handling for major OS such as APPLE and POSIX
compliants. With APPLE we can now use Gestalt functions (using dlopen and
dlsym) and get away with having to compile with -frameowork CoreServices
* To understand the nature of OS bitness, we have decided to use ``uname''
as the best understood standard. So if uname -m returns either of ``*64''
type architectures we will assume them to be 64 bit OS and 32 otherwise.
Note that bitness of OS, application and hardware capabilities are 3
completely different things (and could fundamentally all be varying)

There's a need to test the plugin on various falvor of UNIX and APPLE platforms.
Besides also testing various hardware platforms such as PPC or AMD etc.

diffstat:

 libpurple/plugins/statscollector.c |  725 +++++++++++++++++++++++++++++++++++++
 1 files changed, 725 insertions(+), 0 deletions(-)

diffs (truncated from 730 to 300 lines):

diff --git a/libpurple/plugins/statscollector.c b/libpurple/plugins/statscollector.c
new file mode 100644
--- /dev/null
+++ b/libpurple/plugins/statscollector.c
@@ -0,0 +1,725 @@
+/* Log the activity of users */
+
+#include "internal.h"
+/* #include "pidgin.h" */
+
+#include "debug.h"
+#include "log.h"
+#include "notify.h"
+#include "signals.h"
+#include "util.h"
+#include "version.h"
+#include "cipher.h"
+#include "prpl.h"
+#include "core.h"
+
+#include <glib.h>
+/* #include <gtkplugin.h> */
+
+#ifdef _WIN32
+#include<windows.h>
+
+#elif defined __APPLE__
+#include<CoreServices/CoreServices.h>
+#include<sys/types.h>
+#include<sys/sysctl.h>
+#include<sys/utsname.h>
+#include<dlfcn.h>
+
+#endif
+
+#define STATS_MAJOR_VERSION 1
+#define STATS_MINOR_VERSION 0
+
+xmlnode *root_stats, *cpuinfo_xml;
+GHashTable *stats_acc_ht, *stats_plugins_ht;
+int save_timer = 0;
+
+/* Types of Operating Systems */
+enum OS_TYPES {WINDOWS, APPLE, UNIX};
+enum BIT_32_64 {BIT_32, BIT_64};
+
+static void
+save_xml(){
+
+  /* Uses the Hash tables and other XML nodes
+   * that have been saved/modified and create
+   * final modified XML out of it to be flushed
+   */
+
+  GHashTableIter iter;
+  gpointer key, value;
+  xmlnode *nroot, *nacc, *nplugins, *node;
+  char *data;
+
+  nroot = xmlnode_new("stats");
+
+  /* Load CPUinfo strucutre */
+  xmlnode_insert_child(nroot, cpuinfo_xml);
+
+  nacc = xmlnode_new_child(nroot, "accounts");
+  nplugins = xmlnode_new_child(nroot, "plugins");
+
+  /* Use the hash tables to populate acc and plugins */
+
+  g_hash_table_iter_init(&iter, stats_acc_ht);
+
+  while(g_hash_table_iter_next(&iter, &key, &value)){
+
+    node = xmlnode_from_str(value, -1);
+    xmlnode_insert_child(nacc, node);
+
+  }
+
+
+  g_hash_table_iter_init(&iter, stats_plugins_ht);
+
+  while(g_hash_table_iter_next(&iter, &key, &value)){
+
+    node = xmlnode_from_str(value, -1);
+    xmlnode_insert_child(nplugins, node);
+
+  }
+
+  data = xmlnode_to_formatted_str(nroot, NULL);
+  purple_util_write_data_to_file("stats.xml", data, -1);
+
+}
+
+static gboolean
+save_cb(gpointer data){
+  save_xml();
+  save_timer = 0;
+  return FALSE;
+}
+
+static void
+schedule_stats_save(void){
+  if(save_timer == 0)
+    save_timer = purple_timeout_add_seconds(5, save_cb, NULL);
+}
+
+static xmlnode *
+get_app_32_64(){
+
+  /* Determines if the application is running in 32 or 64 bit mode */
+  int pt_size;
+  xmlnode *bit_size;
+
+  pt_size = sizeof(int)*8;
+  bit_size = xmlnode_new("app-bit");
+  xmlnode_insert_data(bit_size, g_strdup_printf("%d",pt_size), -1);
+  return bit_size;
+
+}
+
+static xmlnode *
+get_os_32_64(){
+
+  /* Determines whether the kernel we are running is 32 or 64 bit */
+  int bit_size=-1;
+  xmlnode *bit_size_xml;
+#ifdef _WIN32
+  BOOL bIsWow64;
+  typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
+  LPFN_ISWOW64PROCESS fnIsWow64Process;
+#elif (defined __USE_POSIX) || (defined __APPLE__) /* What to use for APPLE */
+  struct utsname os_utsname;
+  char *m_name;
+#endif
+
+  bit_size_xml = xmlnode_new("os-bit");
+
+#ifdef _WIN64
+  bit_size = 64;
+
+#elif defined _WIN32
+  /* Check if we are running as wow64 process */
+  bIsWow64 = FALSE;
+  fnIsWow64Process = (LPFN_ISWOW64PROCESS) GetProcAddress( \
+        GetModuleHandle(TEXT("kernel32")),"IsWow64Process");
+
+  if(NULL != fnIsWow64Process)
+  {
+      if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))
+      {
+        bit_size = -1; /* Cannot say! */
+      }
+  }
+  if(bIsWow64) bit_size= 64;
+  else bit_size = 32;
+
+#elif (defined __USE_POSIX) || (defined __APPLE__)
+  /* Use uname to find kernel architecture and infer bit-size */
+  uname(&os_utsname);
+  m_name = os_utsname.machine;
+
+  /* Identifying 64 bit OS is tricky. We use the following identifiers
+   * on basis of experience with uname(2) call.
+   */
+
+  if(!g_strcmp0(m_name, "x86_64") || !g_strcmp0(m_name, "ia64") \
+      || !g_strcmp0(m_name, "amd64") || !g_strcmp0(m_name, "ppc64")) \
+    bit_size = 64;
+  else bit_size = 32;
+#endif
+
+  xmlnode_insert_data(bit_size_xml, g_strdup_printf("%d", bit_size), -1);
+
+  return bit_size_xml;
+
+}
+
+static xmlnode *
+get_arch(){
+
+  /* Obtains the architecture type */
+
+  xmlnode *arch_xml;
+  char *arch_str;
+#if (defined __USE_POSIX) || (defined __APPLE__)
+  struct utsname os_utsname;
+#elif defined _WIN32
+  SYSTEM_INFO sys_info;
+  typedef void (WINAPI *PGNSI)(LPSYSTEM_INFO);
+  typedef BOOL (WINAPI *PGPI)(DWORD, DWORD, DWORD, DWORD, PDWORD);
+  PGNSI pGNSI;
+#endif
+
+  arch_xml = xmlnode_new("arch");
+
+
+#if (defined __USE_POSIX) || (defined __APPLE__)
+  uname(&os_utsname);
+  arch_str = os_utsname.machine;
+
+#elif defined _WIN32
+  pGNSI = (PGNSI) GetProcAddress(
+    GetModuleHandle(TEXT("kernel32.dll")), "GetNativeSystemInfo");
+
+  if(NULL != pGNSI)
+      pGNSI(&sys_info);
+  else GetSystemInfo(&sys_info);
+
+  switch(sys_info.wProcessorArchitecture){
+
+    case 9:
+      arch_str = "AMD64";
+      break;
+    case 6:
+      arch_str = "IA64";
+      break;
+    case 0:
+      arch_str = "INTEL";
+      break;
+    default:
+      arch_str = "UNKNOWN";
+  }
+#endif
+
+  xmlnode_set_attrib(arch_xml, "id", arch_str);
+
+  return arch_xml;
+
+}
+
+static xmlnode *
+get_os_name(){
+
+  /* Determines the name of the operating system as <os-info id="...">
+   * we could specialize it's name further in this xml
+   */
+
+  xmlnode *os_name_xml;
+  char *name_attrib;
+#ifdef _WIN32
+  xmlnode *major_version, *minor_version;
+  OSVERSIONINFO osvi;
+
+#elif defined __USE_POSIX
+  xmlnode *os_release, *os_version;
+  struct utsname os_utsname;
+
+#elif defined __APPLE__
+  int mib[2];
+  size_t length;
+  SInt32 major_version,minor_version,bug_fix_version;
+
+  char sys_ver_file[] = "/System/Library/CoreServices/SystemVersion.plist";
+  char *sys_ver_contents, *key_str, *value_str;
+
+  GHashTable *sys_ver_ht;
+  GError *err;
+  xmlnode *product_version_xml;
+  xmlnode *dict_sys_ver_xml, *key_sys_ver_xml, *value_sys_ver_xml;
+  xmlnode *major_version_xml, *minor_version_xml, *bug_version_xml;
+  OSErr err1, err2, err3;
+
+  /* Get handle for Gestalt by dlopen */
+  int (*gestalt_ext)(int, int *);
+  void *sdl_library;
+
+  sdl_library = dlopen("/System/Library/Frameworks/CoreServices.framework/CoreServices", RTLD_LAZY); // we assume standard location for framework
+  gestalt_ext = dlsym(sdl_library, "Gestalt");
+
+#endif
+
+
+  os_name_xml = xmlnode_new("os-info");
+
+#ifdef _WIN32
+  name_attrib = "windows";
+#elif defined __linux__
+  name_attrib = "linux";
+#elif defined __APPLE__
+  name_attrib = "apple";
+#endif
+
+  xmlnode_set_attrib(os_name_xml, "id", name_attrib);
+
+  /* Get additional information */
+#ifdef _WIN32
+  ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
+  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+  GetVersionEx(&osvi);
+  major_version = xmlnode_new("major-version");
+  minor_version = xmlnode_new("minor-version");
+
+  xmlnode_insert_data(major_version, \
+      g_strdup_printf("%d",osvi.dwMajorVersion),-1);
+  xmlnode_insert_data(minor_version, \
+      g_strdup_printf("%d",osvi.dwMinorVersion),-1);
+
+  xmlnode_insert_child(os_name_xml, major_version);
+  xmlnode_insert_child(os_name_xml, minor_version);
+



More information about the Commits mailing list