View difference between Paste ID: QbJz550x and CY3ZCPW2
SHOW: | | - or go back to the newest paste.
1
#include <sys/param.h>
2
#include <sys/sysctl.h>
3
#if defined(__DragonFly__) || defined(__FreeBSD__)
4
#include <sys/user.h>
5
#endif
6
7
#include <stdio.h>
8
#include <stdlib.h>
9
#include <unistd.h>
10
11
int main(int argc, char *argv[])
12
{
13
#if defined(__NetBSD__)
14
  int mib[6] = { CTL_KERN, KERN_PROC2, KERN_PROC_PID, getpid(), sizeof(struct kinfo_proc2), 1 };
15
#elif defined(__OpenBSD__)
16
  int mib[6] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid(), sizeof(struct kinfo_proc), 1 };
17
#else
18
  int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
19
#endif
20
  size_t buffer_size;
21
22
#if defined(__NetBSD__) || defined(__OpenBSD__)
23
  if (sysctl(mib, 6, NULL, &buffer_size, NULL, 0))
24
#else
25
  if (sysctl(mib, 4, NULL, &buffer_size, NULL, 0))
26
#endif
27
    return 0;
28
29
#if defined(__NetBSD__)
30
  struct kinfo_proc2 *proc = (struct kinfo_proc2 *) malloc(buffer_size);
31
#else
32
  struct kinfo_proc *proc = (struct kinfo_proc *) malloc(buffer_size);
33
#endif
34
35
#if defined(__NetBSD__) || defined(__OpenBSD__)
36
  if (sysctl(mib, 6, proc, &buffer_size, NULL, 0)) {
37
#else
38
  if (sysctl(mib, 4, proc, &buffer_size, NULL, 0)) {
39
#endif
40
    free(proc);
41
    return 0;
42
  }
43
44
#if defined(__APPLE__)
45
  printf("%lds %ldus\n", proc->kp_proc.p_un.__p_starttime.tv_sec, proc->kp_proc.p_un.__p_starttime.tv_usec);
46
#elif defined(__DragonFly__)
47
  printf("%lds %ldus\n", proc->kp_start.tv_sec, proc->kp_start.tv_usec);
48
#elif defined(__FreeBSD__)
49
  printf("%lds %ldus\n", proc->ki_start.tv_sec, proc->ki_start.tv_usec);
50
#else
51
  printf("%lds %ldus\n", proc->p_ustart_sec, proc->p_ustart_usec);
52
#endif
53
54
  return 0;
55
}