• 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 
wmain(int argc,wchar_t * wargv[])30 int wmain(int argc, wchar_t* wargv[]) {
31   if (!IsWindows7OrGreater()) {
32     fprintf(stderr, "This application is only supported on Windows 7, "
33                     "Windows Server 2008 R2, or higher.");
34     exit(ERROR_EXE_MACHINE_TYPE_MISMATCH);
35   }
36 
37   // Convert argv to UTF8
38   char** argv = new char*[argc + 1];
39   for (int i = 0; i < argc; i++) {
40     // Compute the size of the required buffer
41     DWORD size = WideCharToMultiByte(CP_UTF8,
42                                      0,
43                                      wargv[i],
44                                      -1,
45                                      nullptr,
46                                      0,
47                                      nullptr,
48                                      nullptr);
49     if (size == 0) {
50       // This should never happen.
51       fprintf(stderr, "Could not convert arguments to utf8.");
52       exit(1);
53     }
54     // Do the actual conversion
55     argv[i] = new char[size];
56     DWORD result = WideCharToMultiByte(CP_UTF8,
57                                        0,
58                                        wargv[i],
59                                        -1,
60                                        argv[i],
61                                        size,
62                                        nullptr,
63                                        nullptr);
64     if (result == 0) {
65       // This should never happen.
66       fprintf(stderr, "Could not convert arguments to utf8.");
67       exit(1);
68     }
69   }
70   argv[argc] = nullptr;
71   // Now that conversion is done, we can finally start.
72   return node::Start(argc, argv);
73 }
74 #else
75 // UNIX
76 #ifdef __linux__
77 #include <elf.h>
78 #ifdef __LP64__
79 #define Elf_auxv_t Elf64_auxv_t
80 #else
81 #define Elf_auxv_t Elf32_auxv_t
82 #endif  // __LP64__
83 extern char** environ;
84 #endif  // __linux__
85 #if defined(__POSIX__) && defined(NODE_SHARED_MODE)
86 #include <string.h>
87 #include <signal.h>
88 #endif
89 
90 namespace node {
91 namespace per_process {
92 extern bool linux_at_secure;
93 }  // namespace per_process
94 }  // namespace node
95 
main(int argc,char * argv[])96 int main(int argc, char* argv[]) {
97 #if defined(__POSIX__) && defined(NODE_SHARED_MODE)
98   // In node::PlatformInit(), we squash all signal handlers for non-shared lib
99   // build. In order to run test cases against shared lib build, we also need
100   // to do the same thing for shared lib build here, but only for SIGPIPE for
101   // now. If node::PlatformInit() is moved to here, then this section could be
102   // removed.
103   {
104     struct sigaction act;
105     memset(&act, 0, sizeof(act));
106     act.sa_handler = SIG_IGN;
107     sigaction(SIGPIPE, &act, nullptr);
108   }
109 #endif
110 
111 #if defined(__linux__)
112   char** envp = environ;
113   while (*envp++ != nullptr) {}
114   Elf_auxv_t* auxv = reinterpret_cast<Elf_auxv_t*>(envp);
115   for (; auxv->a_type != AT_NULL; auxv++) {
116     if (auxv->a_type == AT_SECURE) {
117       node::per_process::linux_at_secure = auxv->a_un.a_val;
118       break;
119     }
120   }
121 #endif
122   // Disable stdio buffering, it interacts poorly with printf()
123   // calls elsewhere in the program (e.g., any logging from V8.)
124   setvbuf(stdout, nullptr, _IONBF, 0);
125   setvbuf(stderr, nullptr, _IONBF, 0);
126   return node::Start(argc, argv);
127 }
128 #endif
129