1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef SERVICES_SERVICE_MANAGER_ZYGOTE_COMMON_ZYGOTE_HANDLE_H_
6 #define SERVICES_SERVICE_MANAGER_ZYGOTE_COMMON_ZYGOTE_HANDLE_H_
7 
8 #include "base/callback.h"
9 #include "base/command_line.h"
10 #include "base/component_export.h"
11 #include "base/files/scoped_file.h"
12 #include "build/build_config.h"
13 #include "services/service_manager/zygote/common/zygote_buildflags.h"
14 
15 #if !BUILDFLAG(USE_ZYGOTE_HANDLE)
16 #error "Can not use zygote handles without USE_ZYGOTE_HANDLE"
17 #endif
18 
19 namespace service_manager {
20 
21 #if defined(OS_POSIX)
22 class ZygoteCommunication;
23 using ZygoteHandle = ZygoteCommunication*;
24 #else
25 // Perhaps other ports may USE_ZYGOTE_HANDLE here somdeday.
26 #error "Can not use zygote handles on this platform"
27 #endif  // defined(OS_POSIX)
28 
29 using ZygoteLaunchCallback =
30     base::OnceCallback<pid_t(base::CommandLine*, base::ScopedFD*)>;
31 
32 // Allocates and initializes the global generic zygote process, and returns the
33 // ZygoteHandle used to communicate with it. |launch_cb| is a callback that
34 // should actually launch the process, after adding additional command line
35 // switches to the ones composed by this function. It returns the pid created,
36 // and provides a control fd for it.
37 COMPONENT_EXPORT(SERVICE_MANAGER_ZYGOTE)
38 ZygoteHandle CreateGenericZygote(ZygoteLaunchCallback launch_cb);
39 COMPONENT_EXPORT(SERVICE_MANAGER_ZYGOTE) ZygoteHandle GetGenericZygote();
40 
41 // Similar to the above but for creating an unsandboxed zygote from which
42 // processes which need non-generic sandboxes can be derived.
43 COMPONENT_EXPORT(SERVICE_MANAGER_ZYGOTE)
44 ZygoteHandle CreateUnsandboxedZygote(ZygoteLaunchCallback launch_cb);
45 COMPONENT_EXPORT(SERVICE_MANAGER_ZYGOTE) ZygoteHandle GetUnsandboxedZygote();
46 
47 }  // namespace service_manager
48 
49 #endif  // SERVICES_SERVICE_MANAGER_ZYGOTE_COMMON_ZYGOTE_HANDLE_H_
50