• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Joyent, Inc. and other Node contributors.
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a
4 // copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to permit
8 // persons to whom the Software is furnished to do so, subject to the
9 // following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included
12 // in all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17 // NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 // DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 
22 #include "node.h"
23 #include <cstdio>
24 
25 #ifdef _WIN32
26 #include <windows.h>
27 #include <VersionHelpers.h>
28 #include <WinError.h>
29 
30 #define SKIP_CHECK_VAR "NODE_SKIP_PLATFORM_CHECK"
31 #define SKIP_CHECK_SIZE 1
32 #define SKIP_CHECK_VALUE "1"
33 
wmain(int argc,wchar_t * wargv[])34 int wmain(int argc, wchar_t* wargv[]) {
35   // Windows Server 2012 (not R2) is supported until 10/10/2023, so we allow it
36   // to run in the experimental support tier.
37   char buf[SKIP_CHECK_SIZE + 1];
38   if (!IsWindows8Point1OrGreater() &&
39       !(IsWindowsServer() && IsWindows8OrGreater()) &&
40       (GetEnvironmentVariableA(SKIP_CHECK_VAR, buf, sizeof(buf)) !=
41        SKIP_CHECK_SIZE ||
42        strncmp(buf, SKIP_CHECK_VALUE, SKIP_CHECK_SIZE + 1) != 0)) {
43     fprintf(stderr, "Node.js is only supported on Windows 8.1, Windows "
44                     "Server 2012 R2, or higher.\n"
45                     "Setting the " SKIP_CHECK_VAR " environment variable "
46                     "to 1 skips this\ncheck, but Node.js might not execute "
47                     "correctly. Any issues encountered on\nunsupported "
48                     "platforms will not be fixed.");
49     exit(ERROR_EXE_MACHINE_TYPE_MISMATCH);
50   }
51 
52   // Convert argv to UTF8
53   char** argv = new char*[argc + 1];
54   for (int i = 0; i < argc; i++) {
55     // Compute the size of the required buffer
56     DWORD size = WideCharToMultiByte(CP_UTF8,
57                                      0,
58                                      wargv[i],
59                                      -1,
60                                      nullptr,
61                                      0,
62                                      nullptr,
63                                      nullptr);
64     if (size == 0) {
65       // This should never happen.
66       fprintf(stderr, "Could not convert arguments to utf8.");
67       exit(1);
68     }
69     // Do the actual conversion
70     argv[i] = new char[size];
71     DWORD result = WideCharToMultiByte(CP_UTF8,
72                                        0,
73                                        wargv[i],
74                                        -1,
75                                        argv[i],
76                                        size,
77                                        nullptr,
78                                        nullptr);
79     if (result == 0) {
80       // This should never happen.
81       fprintf(stderr, "Could not convert arguments to utf8.");
82       exit(1);
83     }
84   }
85   argv[argc] = nullptr;
86   // Now that conversion is done, we can finally start.
87   return node::Start(argc, argv);
88 }
89 #else
90 // UNIX
91 #ifdef __linux__
92 #include <elf.h>
93 #ifdef __LP64__
94 #define Elf_auxv_t Elf64_auxv_t
95 #else
96 #define Elf_auxv_t Elf32_auxv_t
97 #endif  // __LP64__
98 extern char** environ;
99 #endif  // __linux__
100 #if defined(__POSIX__) && defined(NODE_SHARED_MODE)
101 #include <string.h>
102 #include <signal.h>
103 #endif
104 
105 namespace node {
106 namespace per_process {
107 extern bool linux_at_secure;
108 }  // namespace per_process
109 }  // namespace node
110 
main(int argc,char * argv[])111 int main(int argc, char* argv[]) {
112 #if defined(__POSIX__) && defined(NODE_SHARED_MODE)
113   // In node::PlatformInit(), we squash all signal handlers for non-shared lib
114   // build. In order to run test cases against shared lib build, we also need
115   // to do the same thing for shared lib build here, but only for SIGPIPE for
116   // now. If node::PlatformInit() is moved to here, then this section could be
117   // removed.
118   {
119     struct sigaction act;
120     memset(&act, 0, sizeof(act));
121     act.sa_handler = SIG_IGN;
122     sigaction(SIGPIPE, &act, nullptr);
123   }
124 #endif
125 
126 #if defined(__linux__)
127   char** envp = environ;
128   while (*envp++ != nullptr) {}
129   Elf_auxv_t* auxv = reinterpret_cast<Elf_auxv_t*>(envp);
130   for (; auxv->a_type != AT_NULL; auxv++) {
131     if (auxv->a_type == AT_SECURE) {
132       node::per_process::linux_at_secure = auxv->a_un.a_val;
133       break;
134     }
135   }
136 #endif
137   // Disable stdio buffering, it interacts poorly with printf()
138   // calls elsewhere in the program (e.g., any logging from V8.)
139   setvbuf(stdout, nullptr, _IONBF, 0);
140   setvbuf(stderr, nullptr, _IONBF, 0);
141   return node::Start(argc, argv);
142 }
143 #endif
144