summaryrefslogtreecommitdiffstats
path: root/chrome/app/chrome_exe_main_mac.c
blob: 5c9be39ba3f3d043bfa73651e0ada11e8438505d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) 2015 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.

// The entry point for all Mac Chromium processes, including the outer app
// bundle (browser) and helper app (renderer, plugin, and friends).

#include <dlfcn.h>
#include <errno.h>
#include <libgen.h>
#include <mach-o/dyld.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "chrome/common/chrome_version.h"

typedef int (*ChromeMainPtr)(int, char**);

__attribute__((visibility("default"))) int main(int argc, char* argv[]) {
#if defined(HELPER_EXECUTABLE)
  const char* const rel_path =
      "../../../" PRODUCT_FULLNAME_STRING
      " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework";
#else
  const char* const rel_path =
      "../Versions/" CHROME_VERSION_STRING "/" PRODUCT_FULLNAME_STRING
      " Framework.framework/" PRODUCT_FULLNAME_STRING " Framework";
#endif  // defined(HELPER_EXECUTABLE)

  uint32_t exec_path_size = 0;
  int rv = _NSGetExecutablePath(NULL, &exec_path_size);
  if (rv != -1) {
    fprintf(stderr,
            "_NSGetExecutablePath: get [length|path] failed.\n");
    abort();
  }

  char* exec_path = malloc(exec_path_size);
  if (!exec_path) {
    fprintf(stderr, "malloc %u: %s\n", exec_path_size, strerror(errno));
    abort();
  }

  rv = _NSGetExecutablePath(exec_path, &exec_path_size);
  if (rv != 0) {
    fprintf(stderr,
            "_NSGetExecutablePath(): get [%d|path] failed.\n", exec_path_size);
    abort();
  }

  // Slice off the last part of the main executable path, and append the
  // version framework information.
  char* parent_dir = dirname(exec_path);
  if (!parent_dir) {
    fprintf(stderr, "dirname %s: %s\n", exec_path, strerror(errno));
    abort();
  }
  free(exec_path);

  size_t parent_path_len = strlen(parent_dir);
  size_t rel_path_len = strlen(rel_path);
  // 2 accounts for a trailing NUL byte and the '/' in the middle of the paths.
  size_t framework_path_size = parent_path_len + rel_path_len + 2;
  char* framework_path = malloc(framework_path_size);
  if (!framework_path) {
    fprintf(stderr, "malloc %zu: %s\n", framework_path_size, strerror(errno));
    abort();
  }
  snprintf(framework_path, framework_path_size, "%s/%s", parent_dir, rel_path);

  void* library = dlopen(framework_path, RTLD_LAZY | RTLD_LOCAL | RTLD_FIRST);
  if (!library) {
    fprintf(stderr, "dlopen %s: %s\n", framework_path, dlerror());
    abort();
  }
  free(framework_path);

  ChromeMainPtr chrome_main = dlsym(library, "ChromeMain");
  if (!chrome_main) {
    fprintf(stderr, "dlsym ChromeMain: %s\n", dlerror());
    abort();
  }
  rv = chrome_main(argc, argv);

  // exit, don't return from main, to avoid the apparent removal of main from
  // stack backtraces under tail call optimization.
  exit(rv);
}