summaryrefslogtreecommitdiff
path: root/src/utils/process.cpp
blob: a037baef8c83e14973face7f6f33813194e6f588 (plain) (blame)
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
 *  The ManaPlus Client
 *  Copyright (C) 2011-2015  The ManaPlus Developers
 *
 *  This file is part of The ManaPlus Client.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "utils/process.h"

#include <unistd.h>

#include "localconsts.h"

#ifdef USE_SDL2
#ifdef ANDROID
#include <SDL_system.h>
#endif
#endif

#include "debug.h"

#ifndef __native_client__
const int timeOut = 10;
#endif

#ifdef WIN32

#include "utils/stringutils.h"

#include <windows.h>

int execFileWait(const std::string &pathName, const std::string &name A_UNUSED,
                 const std::string &arg1, const std::string &arg2,
                 const int waitTime A_UNUSED)
{
//    if (!waitTime)
//        waitTime = timeOut;

    STARTUPINFO siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);
    DWORD ret = -1;
    std::string args(std::string(pathName).append(" ").append(arg1));
    if (!arg2.empty())
        args.append(" ").append(arg2);

    if (CreateProcess(pathName.c_str(), const_cast<char*>(args.c_str()),
        nullptr, nullptr, false, CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr,
        &siStartupInfo, &piProcessInfo) != false)
    {
        if (!WaitForSingleObject(piProcessInfo.hProcess, timeOut * 1000))
        {
            if (GetExitCodeProcess(piProcessInfo.hProcess, &ret))
            {
                CloseHandle(piProcessInfo.hProcess);
                CloseHandle(piProcessInfo.hThread);
                return ret;
            }
        }
        TerminateProcess(piProcessInfo.hProcess, -1);
    }

    CloseHandle(piProcessInfo.hProcess);
    CloseHandle(piProcessInfo.hThread);
    return -1;
}

bool execFile(const std::string &pathName, const std::string &name A_UNUSED,
              const std::string &arg1, const std::string &arg2)
{
    STARTUPINFO siStartupInfo;
    PROCESS_INFORMATION piProcessInfo;
    memset(&siStartupInfo, 0, sizeof(siStartupInfo));
    memset(&piProcessInfo, 0, sizeof(piProcessInfo));
    siStartupInfo.cb = sizeof(siStartupInfo);
    std::string args(std::string(pathName).append(" ").append(arg1));
    if (!arg2.empty())
        args.append(" ").append(arg2);

    bool res = CreateProcess(pathName.c_str(), const_cast<char*>(
        args.c_str()), nullptr, nullptr, false,
        CREATE_DEFAULT_ERROR_MODE, nullptr, nullptr, &siStartupInfo,
        &piProcessInfo);

    CloseHandle(piProcessInfo.hProcess);
    CloseHandle(piProcessInfo.hThread);
    return res;
}


#elif defined __linux__ || defined __linux || defined __APPLE__

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>

int execFileWait(const std::string &pathName, const std::string &name,
                 const std::string &arg1, const std::string &arg2,
                 int waitTime)
{
    pid_t mon_pid;
    int status;

    if (!waitTime)
        waitTime = timeOut;

    if ((mon_pid = fork()) == -1)
    {   // fork error
        return -1;
    }
    else if (!mon_pid)
    {   // monitoring child
        pid_t pid;
        if ((pid = fork()) == -1)
        {   // fork error
            return -1;
        }
        else if (!pid)
        {   // work child
            if (arg2.empty())
            {
                execl(pathName.c_str(), name.c_str(),
                   arg1.c_str(), static_cast<char *>(nullptr));
            }
            else
            {
                execl(pathName.c_str(), name.c_str(), arg1.c_str(),
                    arg2.c_str(), static_cast<char *>(nullptr));
            }
            _exit(-1);
        }

        // monitoring process
        pid_t sleep_pid;
        if ((sleep_pid = fork()) == -1)
        {   // fork error
            return -1;
        }
        else if (!sleep_pid)
        {   // sleep pid
            sleep(waitTime);
            execl("/bin/true", "/bin/true", static_cast<char *>(nullptr));
            _exit(-1);
        }

        // monitoring process
        const pid_t exited_pid = wait(&status);
        int ret = -1;
        if (exited_pid == pid)
        {
            kill(sleep_pid, SIGKILL);
            if (WIFEXITED(status))
                ret = WEXITSTATUS(status);
        }
        else
        {
            kill(pid, SIGKILL);
            ret = -1;
        }
        wait(nullptr);
        execl("/bin/true", "/bin/true", static_cast<char *>(nullptr));
        _exit(ret);
    }

    // monitoring parent
    waitpid(mon_pid, &status, 0);
    if (WIFEXITED(status))
        return WEXITSTATUS(status);

    return -1;
}

bool execFile(const std::string &pathName, const std::string &name,
              const std::string &arg1, const std::string &arg2)
{
    struct stat statbuf;
    // file not exists
    if (stat(pathName.c_str(), &statbuf))
        return false;

    pid_t pid;
    if ((pid = fork()) == -1)
    {   // fork error
        return false;
    }
    else if (!pid)
    {   // work child
        if (arg2.empty())
        {
            execl(pathName.c_str(), name.c_str(),
                arg1.c_str(), static_cast<char *>(nullptr));
        }
        else
        {
            execl(pathName.c_str(), name.c_str(), arg1.c_str(),
                arg2.c_str(), static_cast<char *>(nullptr));
        }
        _exit(-1);
        return false;
    }
    return true;
}

#else

int execFileWait(const std::string &pathName A_UNUSED,
                 const std::string &name A_UNUSED,
                 const std::string &arg1 A_UNUSED,
                 const std::string &arg2 A_UNUSED,
                 int waitTime A_UNUSED)
{
    return -1;
}

bool execFile(const std::string &pathName A_UNUSED,
              const std::string &name A_UNUSED,
              const std::string &arg1 A_UNUSED,
              const std::string &arg2 A_UNUSED)
{
    return false;
}

#endif

#ifdef WIN32
bool openBrowser(std::string url)
{
    return reinterpret_cast<int>(ShellExecute(nullptr, "open",
        replaceAll(url, " ", "").c_str(),
        nullptr, nullptr, SW_SHOWNORMAL)) > 32;
}
#elif defined ANDROID
#include "utils/stringutils.h"
#ifndef USE_SDL2
#include <SDL_screenkeyboard.h>
#endif
bool openBrowser(std::string url)
{
#ifdef USE_SDL2
    SDL_OpenBrowser(replaceAll(url, " ", "").c_str());
#else
    SDL_ANDROID_OpenBrowser(replaceAll(url, " ", "").c_str());
#endif
    return true;
}
#elif defined __APPLE__
bool openBrowser(std::string url)
{
    return execFile("/usr/bin/open", "/usr/bin/open", url, "");
}
#elif defined __OpenBSD__ || defined __FreeBSD__ || defined __DragonFly__
bool openBrowser(std::string url)
{
    return execFile("/usr/local/bin/xdg-open",
        "/usr/local/bin/xdg-open", url, "");
}
#elif defined __linux__ || defined __linux
bool openBrowser(std::string url)
{
    return execFile("/usr/bin/xdg-open", "/usr/bin/xdg-open", url, "");
}
#elif defined __native_client__

#include "utils/naclmessages.h"

bool openBrowser(std::string url)
{
    naclPostMessage("open-browser", url);
    return true;
}
#else
bool openBrowser(std::string url)
{
    return false;
}

#endif

#ifdef WIN32
void setPriority(const bool big)
{
    HANDLE hCurrentProcess = GetCurrentProcess();
    if (big)
        SetPriorityClass(hCurrentProcess, ABOVE_NORMAL_PRIORITY_CLASS);
    else
        SetPriorityClass(hCurrentProcess, BELOW_NORMAL_PRIORITY_CLASS);
}
#else
void setPriority(const bool big A_UNUSED)
{
}
#endif