src/jdk.jpackage/share/native/libapplauncher/PosixPlatform.cpp
author herrick
Fri, 07 Dec 2018 13:26:45 -0500
branchJDK-8200758-branch
changeset 57064 a7fdadf67a92
child 57099 9a85a7a076ad
permissions -rw-r--r--
8214899: rename papplauncher and it's library and move src to appropriate places Reviewed-by: almatvee, erikj
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57064
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     1
/*
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     2
 * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     4
 *
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    10
 *
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    15
 * accompanied this code).
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    16
 *
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    20
 *
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    23
 * questions.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    24
 */
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    25
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    26
#include "PosixPlatform.h"
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    27
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    28
#ifdef POSIX
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    29
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    30
#include "PlatformString.h"
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    31
#include "FilePath.h"
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    32
#include "Helpers.h"
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    33
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    34
#include <assert.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    35
#include <stdbool.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    36
#include <sys/types.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    37
#include <unistd.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    38
#include <sys/sysctl.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    39
#include <sys/file.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    40
#include <sys/stat.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    41
#ifdef LINUX
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    42
#include <sys/wait.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    43
#endif
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    44
#include <errno.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    45
#include <limits.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    46
#include <pwd.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    47
#include <iostream>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    48
#include <algorithm>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    49
#include <dlfcn.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    50
#include <signal.h>
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    51
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    52
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    53
PosixPlatform::PosixPlatform(void) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    54
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    55
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    56
PosixPlatform::~PosixPlatform(void) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    57
    if (!SingleInstanceFile.empty()) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    58
        unlink(SingleInstanceFile.c_str());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    59
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    60
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    61
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    62
TString PosixPlatform::GetTempDirectory() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    63
    struct passwd* pw = getpwuid(getuid());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    64
    TString homedir(pw->pw_dir);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    65
    homedir += getTmpDirString();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    66
    if (!FilePath::DirectoryExists(homedir)) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    67
        if (!FilePath::CreateDirectory(homedir, false)) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    68
            homedir.clear();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    69
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    70
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    71
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    72
    return homedir;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    73
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    74
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    75
TString PosixPlatform::fixName(const TString& name) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    76
    TString fixedName(name);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    77
    const TString chars("?:*<>/\\");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    78
    for (TString::const_iterator it = chars.begin(); it != chars.end(); it++) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    79
        fixedName.erase(std::remove(fixedName.begin(),
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    80
                fixedName.end(), *it), fixedName.end());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    81
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    82
    return fixedName;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    83
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    84
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    85
// returns true if another instance is already running.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    86
// if false, we need to continue regular launch.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    87
bool PosixPlatform::CheckForSingleInstance(TString appName) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    88
    TString tmpDir = GetTempDirectory();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    89
    if (tmpDir.empty()) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    90
        printf("Unable to check for single instance.\n");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    91
        return false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    92
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    93
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    94
    TString lockFile = tmpDir + "/" + fixName(appName);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    95
    SingleInstanceFile = lockFile;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    96
    int pid_file = open(lockFile.c_str(), O_CREAT | O_RDWR, 0666);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    97
    int rc = flock(pid_file, LOCK_EX | LOCK_NB);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    98
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
    99
    if (rc) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   100
        if (EWOULDBLOCK == errno) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   101
            // another instance is running
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   102
            pid_t pid = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   103
            read(pid_file, (void*)&pid, sizeof(pid_t));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   104
            printf("Another instance is running PID: %d\n", pid);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   105
            if (pid != 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   106
                singleInstanceProcessId = pid;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   107
                SingleInstanceFile.clear();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   108
                return true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   109
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   110
        } else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   111
            printf("Unable to check for single instance.\n");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   112
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   113
    } else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   114
        // It is the first instance.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   115
        pid_t pid = getpid();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   116
        write(pid_file, (void*)&pid, sizeof(pid_t));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   117
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   118
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   119
    return false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   120
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   121
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   122
MessageResponse PosixPlatform::ShowResponseMessage(TString title,
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   123
        TString description) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   124
    MessageResponse result = mrCancel;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   125
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   126
    printf("%s %s (Y/N)\n", PlatformString(title).toPlatformString(),
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   127
            PlatformString(description).toPlatformString());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   128
    fflush(stdout);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   129
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   130
    std::string input;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   131
    std::cin >> input;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   132
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   133
    if (input == "Y") {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   134
        result = mrOK;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   135
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   136
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   137
    return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   138
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   139
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   140
void PosixPlatform::SetCurrentDirectory(TString Value) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   141
    chdir(StringToFileSystemString(Value));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   142
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   143
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   144
Module PosixPlatform::LoadLibrary(TString FileName) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   145
    return dlopen(StringToFileSystemString(FileName), RTLD_LAZY);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   146
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   147
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   148
void PosixPlatform::FreeLibrary(Module AModule) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   149
    dlclose(AModule);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   150
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   151
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   152
Procedure PosixPlatform::GetProcAddress(Module AModule,
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   153
        std::string MethodName) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   154
    return dlsym(AModule, PlatformString(MethodName));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   155
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   156
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   157
std::vector<std::string> PosixPlatform::GetLibraryImports(
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   158
       const TString FileName) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   159
 std::vector<TString> result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   160
 return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   161
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   162
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   163
std::vector<TString> PosixPlatform::FilterOutRuntimeDependenciesForPlatform(
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   164
       std::vector<TString> Imports) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   165
 std::vector<TString> result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   166
 return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   167
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   168
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   169
Process* PosixPlatform::CreateProcess() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   170
    return new PosixProcess();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   171
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   172
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   173
PosixProcess::PosixProcess() : Process() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   174
    FChildPID = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   175
    FRunning = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   176
    FOutputHandle = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   177
    FInputHandle = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   178
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   179
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   180
PosixProcess::~PosixProcess() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   181
    Terminate();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   182
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   183
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   184
void PosixProcess::Cleanup() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   185
    if (FOutputHandle != 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   186
        close(FOutputHandle);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   187
        FOutputHandle = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   188
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   189
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   190
    if (FInputHandle != 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   191
        close(FInputHandle);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   192
        FInputHandle = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   193
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   194
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   195
#ifdef MAC
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   196
    sigaction(SIGINT, &savintr, (struct sigaction *)0);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   197
    sigaction(SIGQUIT, &savequit, (struct sigaction *)0);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   198
    sigprocmask(SIG_SETMASK, &saveblock, (sigset_t *)0);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   199
#endif //MAC
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   200
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   201
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   202
bool PosixProcess::ReadOutput() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   203
    bool result = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   204
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   205
    if (FOutputHandle != 0 && IsRunning() == true) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   206
        char buffer[4096] = {0};
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   207
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   208
        ssize_t count = read(FOutputHandle, buffer, sizeof(buffer));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   209
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   210
        if (count == -1) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   211
            if (errno == EINTR) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   212
                // continue;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   213
            } else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   214
                perror("read");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   215
                exit(1);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   216
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   217
        } else if (count == 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   218
            // break;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   219
        } else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   220
            if (buffer[count - 1] == EOF) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   221
                buffer[count - 1] = '\0';
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   222
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   223
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   224
            std::list<TString> output = Helpers::StringToArray(buffer);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   225
            FOutput.splice(FOutput.end(), output, output.begin(), output.end());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   226
            result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   227
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   228
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   229
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   230
    return false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   231
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   232
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   233
bool PosixProcess::IsRunning() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   234
    bool result = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   235
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   236
    if (kill(FChildPID, 0) == 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   237
        result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   238
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   239
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   240
    return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   241
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   242
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   243
bool PosixProcess::Terminate() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   244
    bool result = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   245
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   246
    if (IsRunning() == true && FRunning == true) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   247
        FRunning = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   248
        Cleanup();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   249
        int status = kill(FChildPID, SIGTERM);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   250
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   251
        if (status == 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   252
            result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   253
        } else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   254
#ifdef DEBUG
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   255
            if (errno == EINVAL) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   256
                printf("Kill error: The value of the sig argument is an invalid or unsupported signal number.");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   257
            } else if (errno == EPERM) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   258
                printf("Kill error: The process does not have permission to send the signal to any receiving process.");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   259
            } else if (errno == ESRCH) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   260
                printf("Kill error: No process or process group can be found corresponding to that specified by pid.");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   261
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   262
#endif // DEBUG
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   263
            if (IsRunning() == true) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   264
                status = kill(FChildPID, SIGKILL);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   265
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   266
                if (status == 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   267
                    result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   268
                }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   269
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   270
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   271
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   272
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   273
    return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   274
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   275
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   276
#define PIPE_READ 0
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   277
#define PIPE_WRITE 1
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   278
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   279
bool PosixProcess::Execute(const TString Application,
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   280
        const std::vector<TString> Arguments, bool AWait) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   281
    bool result = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   282
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   283
    if (FRunning == false) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   284
        FRunning = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   285
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   286
        int handles[2];
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   287
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   288
        if (pipe(handles) == -1) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   289
            return false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   290
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   291
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   292
        struct sigaction sa;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   293
        sa.sa_handler = SIG_IGN;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   294
        sigemptyset(&sa.sa_mask);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   295
        sa.sa_flags = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   296
#ifdef MAC
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   297
        sigemptyset(&savintr.sa_mask);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   298
        sigemptyset(&savequit.sa_mask);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   299
        sigaction(SIGINT, &sa, &savintr);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   300
        sigaction(SIGQUIT, &sa, &savequit);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   301
        sigaddset(&sa.sa_mask, SIGCHLD);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   302
        sigprocmask(SIG_BLOCK, &sa.sa_mask, &saveblock);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   303
#endif // MAC
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   304
        FChildPID = fork();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   305
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   306
        // PID returned by vfork is 0 for the child process and the
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   307
        // PID of the child process for the parent.
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   308
        if (FChildPID == -1) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   309
            // Error
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   310
            TString message = PlatformString::Format(
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   311
                    _T("Error: Unable to create process %s"),
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   312
                    Application.data());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   313
            throw Exception(message);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   314
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   315
        else if (FChildPID == 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   316
            Cleanup();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   317
            TString command = Application;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   318
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   319
            for (std::vector<TString>::const_iterator iterator =
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   320
                    Arguments.begin(); iterator != Arguments.end();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   321
                    iterator++) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   322
                command += TString(_T(" ")) + *iterator;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   323
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   324
#ifdef DEBUG
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   325
            printf("%s\n", command.data());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   326
#endif // DEBUG
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   327
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   328
            dup2(handles[PIPE_READ], STDIN_FILENO);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   329
            dup2(handles[PIPE_WRITE], STDOUT_FILENO);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   330
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   331
            close(handles[PIPE_READ]);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   332
            close(handles[PIPE_WRITE]);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   333
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   334
            execl("/bin/sh", "sh", "-c", command.data(), (char *)0);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   335
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   336
            _exit(127);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   337
        } else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   338
            FOutputHandle = handles[PIPE_READ];
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   339
            FInputHandle = handles[PIPE_WRITE];
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   340
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   341
            if (AWait == true) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   342
                ReadOutput();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   343
                Wait();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   344
                Cleanup();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   345
                FRunning = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   346
                result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   347
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   348
            else {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   349
                result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   350
            }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   351
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   352
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   353
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   354
    return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   355
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   356
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   357
bool PosixProcess::Wait() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   358
    bool result = false;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   359
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   360
    int status = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   361
    pid_t wpid = 0;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   362
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   363
#ifdef LINUX
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   364
    wpid = wait(&status);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   365
#endif
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   366
#ifdef MAC
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   367
    wpid = wait(&status);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   368
#endif
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   369
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   370
    if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   371
        if (errno != EINTR){
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   372
            status = -1;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   373
        }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   374
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   375
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   376
#ifdef DEBUG
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   377
    if (WIFEXITED(status)) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   378
        printf("child exited, status=%d\n", WEXITSTATUS(status));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   379
    } else if (WIFSIGNALED(status)) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   380
        printf("child killed (signal %d)\n", WTERMSIG(status));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   381
    } else if (WIFSTOPPED(status)) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   382
        printf("child stopped (signal %d)\n", WSTOPSIG(status));
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   383
#ifdef WIFCONTINUED // Not all implementations support this
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   384
    } else if (WIFCONTINUED(status)) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   385
        printf("child continued\n");
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   386
#endif // WIFCONTINUED
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   387
    } else { // Non-standard case -- may never happen
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   388
        printf("Unexpected status (0x%x)\n", status);
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   389
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   390
#endif // DEBUG
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   391
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   392
    if (wpid != -1) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   393
        result = true;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   394
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   395
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   396
    return result;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   397
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   398
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   399
TProcessID PosixProcess::GetProcessID() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   400
    return FChildPID;
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   401
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   402
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   403
void PosixProcess::SetInput(TString Value) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   404
    if (FInputHandle != 0) {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   405
        write(FInputHandle, Value.data(), Value.size());
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   406
    }
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   407
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   408
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   409
std::list<TString> PosixProcess::GetOutput() {
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   410
    ReadOutput();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   411
    return Process::GetOutput();
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   412
}
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   413
a7fdadf67a92 8214899: rename papplauncher and it's library and move src to appropriate places
herrick
parents:
diff changeset
   414
#endif // POSIX