15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
5ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch#include "base/process/process_info.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/sysctl.h>
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <sys/time.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h"
13eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch#include "base/time/time.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
15ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochnamespace base {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
17ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch//static
18ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdochconst Time CurrentProcessInfo::CreationTime() {
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid() };
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t len = 0;
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (sysctl(mib, arraysize(mib), NULL, &len, NULL, 0) < 0)
22ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    return Time();
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  scoped_ptr<struct kinfo_proc, base::FreeDeleter>
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      proc(static_cast<struct kinfo_proc*>(malloc(len)));
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (sysctl(mib, arraysize(mib), proc.get(), &len, NULL, 0) < 0)
27ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch    return Time();
28ba5b9a6411cb1792fd21f0a078d7a25cd1ceec16Ben Murdoch  return Time::FromTimeVal(proc->kp_proc.p_un.__p_starttime);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace base
32