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)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CONTENT_PUBLIC_COMMON_ZYGOTE_FORK_DELEGATE_LINUX_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CONTENT_PUBLIC_COMMON_ZYGOTE_FORK_DELEGATE_LINUX_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <unistd.h>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
133551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// TODO(jln) base::TerminationStatus should be forward declared when switching
143551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)// to C++11.
153551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)#include "base/process/kill.h"
163551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace content {
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The ZygoteForkDelegate allows the Chrome Linux zygote to delegate
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// fork operations to another class that knows how to do some
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// specialized version of fork.
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ZygoteForkDelegate {
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A ZygoteForkDelegate is created during Chrome linux zygote
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // initialization, and provides "fork()" functionality as an
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // alternative to forking the zygote.  A new delegate is passed in
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // as an argument to ZygoteMain().
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ZygoteForkDelegate() {}
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Initialization happens in the zygote after it has been
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // started by ZygoteMain.
320529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // If |enable_layer1_sandbox| is true, the delegate must enable a
330529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // layer-1 sandbox such as the setuid sandbox.
340529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  virtual void Init(int sandboxdesc, bool enable_layer1_sandbox) = 0;
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
36cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // After Init, supply a UMA_HISTOGRAM_ENUMERATION the delegate would like
37cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // reported to the browser process.  (Note: Because these reports are
38cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // piggy-backed onto fork responses that don't otherwise contain UMA reports,
39cedac228d2dd51db4b79ea1e72c7f249408ee061Torne (Richard Coles)  // this method may not be called until much later.)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void InitialUMA(std::string* uma_name,
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          int* uma_sample,
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          int* uma_boundary_value) = 0;
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns 'true' if the delegate would like to handle a given fork
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // request.  Otherwise returns false.  Optionally, fills in uma_name et al
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // with a report the helper wants to make via UMA_HISTOGRAM_ENUMERATION.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual bool CanHelp(const std::string& process_type, std::string* uma_name,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                       int* uma_sample, int* uma_boundary_value) = 0;
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
504e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Indexes of FDs in the vector passed to Fork().
514e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  enum {
524e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    // Used to pass in the descriptor for talking to the Browser
534e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    kBrowserFDIndex,
54010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // The PID oracle is used in the protocol for discovering the
55010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // child process's real PID from within the SUID sandbox.
56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // The child process is required to write to the socket after
57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    // successfully forking.
58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)    kPIDOracleFDIndex,
594e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    kNumPassedFDs  // Number of FDs in the vector passed to Fork().
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  };
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Delegate forks, returning a -1 on failure. Outside the
633551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // suid sandbox, Fork() returns the Linux process ID.
643551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // This method is not aware of any potential pid namespaces, so it'll
653551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // return a raw pid just like fork() would.
660529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // Delegate is responsible for communicating the channel ID to the
670529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch  // newly created child process.
68e5d81f57cb97b3b6b7fccc9c5610d21eb81db09dBen Murdoch  virtual pid_t Fork(const std::string& process_type,
690529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                     const std::vector<int>& fds,
700529e5d033099cbfc42635f6f6183833b09dff6eBen Murdoch                     const std::string& channel_id) = 0;
713551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)
723551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // The fork delegate must also assume the role of waiting for its children
733551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // since the caller will not be their parents and cannot do it. |pid| here
743551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // should be a pid that has been returned by the Fork() method. i.e. This
753551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // method is completely unaware of eventual PID namespaces due to sandboxing.
763551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // |known_dead| indicates that the process is already dead and that a
773551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // blocking wait() should be performed. In this case, GetTerminationStatus()
783551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  // will send a SIGKILL to the target process first.
793551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)  virtual bool GetTerminationStatus(pid_t pid, bool known_dead,
803551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                    base::TerminationStatus* status,
813551c9c881056c480085172ff9840cab31610854Torne (Richard Coles)                                    int* exit_code) = 0;
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace content
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CONTENT_PUBLIC_COMMON_ZYGOTE_FORK_DELEGATE_LINUX_H_
87