process_info_mac.cc 895 Bytes
Newer Older
gejun's avatar
gejun committed
1 2 3 4
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "butil/process/process_info.h"
gejun's avatar
gejun committed
6 7 8 9 10

#include <sys/sysctl.h>
#include <sys/time.h>
#include <unistd.h>

11 12 13
#include "butil/basictypes.h"
#include "butil/memory/scoped_ptr.h"
#include "butil/time/time.h"
gejun's avatar
gejun committed
14

15
namespace butil {
gejun's avatar
gejun committed
16 17 18 19 20 21 22 23

//static
const Time CurrentProcessInfo::CreationTime() {
  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
  size_t len = 0;
  if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0)
    return Time();

24
  scoped_ptr<struct kinfo_proc, butil::FreeDeleter>
gejun's avatar
gejun committed
25 26 27 28 29 30
      proc(static_cast<struct kinfo_proc*>(malloc(len)));
  if (sysctl(mib, arraysize(mib), proc.get(), &len, NULL, 0) < 0)
    return Time();
  return Time::FromTimeVal(proc->kp_proc.p_un.__p_starttime);
}

31
}  // namespace butil