Merge branch 'master' of github.com:mono/mono
[mono.git] / mono / utils / mono-proclib.c
index c311f06052672a5e6ed064fa45b60955fd5f073d..b7062c33f1e4d6c6cd2ead32493fa27685ce1d90 100644 (file)
 #include <windows.h>
 #endif
 
-/* FIXME: bsds untested */
 #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
 #include <sys/param.h>
 #include <sys/types.h>
 #include <sys/sysctl.h>
 #include <sys/proc.h>
+#if defined(__APPLE__)
 #include <mach/mach.h>
+#endif
 #ifdef HAVE_SYS_USER_H
 #include <sys/user.h>
 #endif
 #ifdef HAVE_STRUCT_KINFO_PROC_KP_PROC
-#  ifdef KERN_PROC2
-#    define kinfo_pid_member p_pid
-#    define kinfo_name_member p_comm
-#  else
 #    define kinfo_pid_member kp_proc.p_pid
 #    define kinfo_name_member kp_proc.p_comm
-#  endif
+#elif defined(__OpenBSD__)
+#    define kinfo_pid_member p_pid
+#    define kinfo_name_member p_comm
 #else
 #define kinfo_pid_member ki_pid
 #define kinfo_name_member ki_comm
@@ -265,22 +264,25 @@ get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
 {
 #if defined(__APPLE__) 
        double process_user_time = 0, process_system_time = 0;//, process_percent = 0;
-
        task_t task;
+
        if (task_for_pid(mach_task_self(), pid, &task) != KERN_SUCCESS)
                RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
        
        struct task_basic_info t_info;
        mach_msg_type_number_t t_info_count = TASK_BASIC_INFO_COUNT, th_count;
-       if (task_info(task,
-                                       TASK_BASIC_INFO,
-                                       (task_info_t)&t_info,
-                                       &t_info_count) != KERN_SUCCESS)
+
+       if (task_info(task, TASK_BASIC_INFO, (task_info_t)&t_info, &t_info_count) != KERN_SUCCESS) {
+               mach_port_deallocate (mach_task_self (), task);
                RET_ERROR (MONO_PROCESS_ERROR_OTHER);
+       }
        
        thread_array_t th_array;
-       if (task_threads(task, &th_array, &th_count) != KERN_SUCCESS)
+
+       if (task_threads(task, &th_array, &th_count) != KERN_SUCCESS) {
+               mach_port_deallocate (mach_task_self (), task);
                RET_ERROR (MONO_PROCESS_ERROR_OTHER);
+       }
                
        size_t i;
 
@@ -303,6 +305,8 @@ get_process_stat_item (int pid, int pos, int sum, MonoProcessError *error)
        for (i = 0; i < th_count; i++)
                mach_port_deallocate(task, th_array[i]);
 
+       mach_port_deallocate (mach_task_self (), task);
+
        process_user_time += t_info.user_time.seconds + t_info.user_time.microseconds / 1e6;
        process_system_time += t_info.system_time.seconds + t_info.system_time.microseconds / 1e6;
     
@@ -395,6 +399,7 @@ get_pid_status_item (int pid, const char *item, MonoProcessError *error, int mul
 #if defined(__APPLE__)
        // ignore the multiplier
        
+       gint64 ret;
        task_t task;
        if (task_for_pid (mach_task_self (), pid, &task) != KERN_SUCCESS)
                RET_ERROR (MONO_PROCESS_ERROR_NOT_FOUND);
@@ -402,23 +407,28 @@ get_pid_status_item (int pid, const char *item, MonoProcessError *error, int mul
        struct task_basic_info t_info;
        mach_msg_type_number_t th_count = TASK_BASIC_INFO_COUNT;
        
-       if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count) != KERN_SUCCESS)
+       if (task_info (task, TASK_BASIC_INFO, (task_info_t)&t_info, &th_count) != KERN_SUCCESS) {
+               mach_port_deallocate (mach_task_self (), task);
                RET_ERROR (MONO_PROCESS_ERROR_OTHER);
+       }
 
        if (strcmp (item, "VmRSS") == 0 || strcmp (item, "VmHWM") == 0)
-               return t_info.resident_size;
+               ret = t_info.resident_size;
        else if (strcmp (item, "VmSize") == 0 || strcmp (item, "VmPeak") == 0)
-               return t_info.virtual_size;
+               ret = t_info.virtual_size;
        else if (strcmp (item, "Threads") == 0)
-               return th_count;
-       return 0;
+               ret = th_count;
+
+       mach_port_deallocate (mach_task_self (), task);
+       
+       return ret;
 #else
        char buf [64];
        char *s;
 
        s = get_pid_status_item_buf (pid, item, buf, sizeof (buf), error);
        if (s)
-               return atoi (s) * multiplier;
+               return ((gint64) atol (s)) * multiplier;
        return 0;
 #endif
 }
@@ -545,6 +555,7 @@ get_cpu_times (int cpu_id, gint64 *user, gint64 *systemt, gint64 *irq, gint64 *s
                        continue;
                }
                sscanf (data, "%Lu %Lu %Lu %Lu %Lu %Lu %Lu", &user_ticks, &nice_ticks, &system_ticks, &idle_ticks, &iowait_ticks, &irq_ticks, &sirq_ticks);
+               break;
        }
        fclose (f);