{"action":"created","comment":{"url":"https://api.github.com/repos/apple/swift-package-manager/pulls/comments/92442908","pull_request_review_id":12954791,"id":92442908,"diff_hunk":"@@ -0,0 +1,295 @@\n+/*\n+ This source file is part of the Swift.org open source project\n+ \n+ Copyright 2016 Apple Inc. and the Swift project authors\n+ Licensed under Apache License v2.0 with Runtime Library Exception\n+ \n+ See http://swift.org/LICENSE.txt for license information\n+ See http://swift.org/CONTRIBUTORS.txt for Swift project authors\n+*/\n+\n+import class Foundation.ProcessInfo\n+\n+import enum POSIX.SystemError\n+import libc\n+import Basic\n+\n+/// Process allows spawning new subprocesses and working with them.\n+public final class Process {\n+    /// Typealias for process id type.\n+    public typealias ProcessID = pid_t\n+\n+    /// The arguments to execute.\n+    public let arguments: [String]\n+\n+    /// The enviornment with which the process was executed.\n+    public let environment: [String: String]\n+\n+    /// The return code of the process, available once process has finished execution.\n+    public private(set) var returnCode: Int32 = 0\n+\n+    /// The process id of the spawned process, available after the process is launched.\n+    public private(set) var processID = ProcessID()\n+\n+    /// The stream containing output of the process, data is available only after execution.\n+    public private(set) var outputStream: OutputByteStream? = nil\n+\n+    /// If the subprocess has launched.\n+    public private(set) var launched = false\n+\n+    /// If the subprocess received a signal.\n+    public private(set) var signalled = false\n+\n+    /// The pipes to redirect and save the output of the subprocess.\n+    private var saveOutputPipe: [Int32] = [0, 0]\n+\n+    /// Create a new process instance.\n+    ///\n+    /// - Parameters:\n+    ///   - arguments: The arguments for the subprocess.\n+    ///   - environment: The environment to pass to subprocess. By default the current process environment\n+    ///     will be inherited.\n+    ///   - outputStream: Redirect and store stdout/stderr output (of subprocess) to this stream, instead of printing on\n+    ///     the standard streams.\n+    public init(arguments: [String], environment: [String: String] = env(), outputStream: OutputByteStream? = nil) {\n+        self.arguments = arguments\n+        self.environment = environment\n+        self.outputStream = outputStream\n+    }\n+\n+    /// Launch the subprocess.\n+    ///\n+    /// - Throws: Process.Error\n+    public func launch() throws {\n+        assert(!launched, \"It is not allowed to launch the same process object again.\")\n+        launched = true\n+\n+        // Initialize the spawn attributes.\n+      #if os(macOS)\n+        var attributes: posix_spawnattr_t? = nil\n+      #else\n+        var attributes = posix_spawnattr_t()\n+      #endif\n+        posix_spawnattr_init(&attributes)\n+\n+        // Unmask all signals.\n+        var noSignals = sigset_t()\n+        sigemptyset(&noSignals)\n+        posix_spawnattr_setsigmask(&attributes, &noSignals)\n+\n+        // Reset all signals to default behavior.\n+      #if os(macOS)\n+        var mostSignals = sigset_t()\n+        sigfillset(&mostSignals)\n+        sigdelset(&mostSignals, SIGKILL)\n+        sigdelset(&mostSignals, SIGSTOP)\n+        posix_spawnattr_setsigdefault(&attributes, &mostSignals)\n+      #else\n+        // On Linux, this can only be used to reset signals that are legal to\n+        // modify, so we have to take care about the set we use.\n+        var mostSignals = sigset_t()\n+        sigemptyset(&mostSignals);\n+        for i in 1 ..< SIGUNUSED {\n+            if i == SIGKILL || i == SIGSTOP {\n+                continue\n+            }\n+            sigaddset(&mostSignals, i)\n+        }\n+        posix_spawnattr_setsigdefault(&attributes, &mostSignals)\n+      #endif\n+\n+        // Establish a separate process group.\n+        posix_spawnattr_setpgroup(&attributes, 0)\n+\n+        // Set the attribute flags.\n+        var flags = POSIX_SPAWN_SETSIGMASK | POSIX_SPAWN_SETSIGDEF\n+        flags |= POSIX_SPAWN_SETPGROUP\n+\n+        // Close all other files by default.\n+        //\n+        // FIXME: Note that this is an Apple-specific extension, and we will have to\n+        // do something else on other platforms (and unfortunately, there isn't\n+        // really an easy answer other than using a stub executable).\n+      #if os(macOS)\n+        flags |= POSIX_SPAWN_CLOEXEC_DEFAULT\n+      #endif\n+\n+        posix_spawnattr_setflags(&attributes, Int16(flags))\n+\n+        // Setup the file actions.\n+      #if os(macOS)\n+        var fileActions: posix_spawn_file_actions_t? = nil\n+      #else\n+        var fileActions = posix_spawn_file_actions_t()\n+      #endif\n+        posix_spawn_file_actions_init(&fileActions)\n+\n+        // Workaround for https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=89e435f3559c53084498e9baad22172b64429362\n+        let devNull = strdup(\"/dev/null\")\n+        defer { free(devNull) }\n+        // Open /dev/null as stdin.\n+        posix_spawn_file_actions_addopen(&fileActions, 0, devNull, O_RDONLY, 0)\n+\n+        var rv = libc.pipe(&saveOutputPipe)\n+        guard rv == 0 else {\n+            throw SystemError.pipe(rv)\n+        }\n+\n+        if outputStream != nil {\n+            // Open the write end of the pipe as stdout and stderr, if desired.\n+            posix_spawn_file_actions_adddup2(&fileActions, saveOutputPipe[1], 1)\n+            posix_spawn_file_actions_adddup2(&fileActions, saveOutputPipe[1], 2)\n+\n+            // Close the other ends of the pipe.\n+            posix_spawn_file_actions_addclose(&fileActions, saveOutputPipe[0])\n+            posix_spawn_file_actions_addclose(&fileActions, saveOutputPipe[1])\n+        } else {\n+            posix_spawn_file_actions_adddup2(&fileActions, 1, 1)\n+            posix_spawn_file_actions_adddup2(&fileActions, 2, 2)\n+        }\n+\n+        let argv: [UnsafeMutablePointer<CChar>?] = arguments.map{ $0.withCString(strdup) }\n+        defer { for case let arg? in argv { free(arg) } }\n+        \n+        let env: [UnsafeMutablePointer<CChar>?] = environment.map{ \"\\($0.0)=\\($0.1)\".withCString(strdup) }\n+        defer { for case let arg? in env { free(arg) } }","path":"Sources/Utility/Process.swift","position":155,"original_position":155,"commit_id":"d2c441291c50430957b001a9c2c74080784c165c","original_commit_id":"d2c441291c50430957b001a9c2c74080784c165c","user":{"login":"ddunbar","id":106625,"avatar_url":"https://avatars.githubusercontent.com/u/106625?v=3","gravatar_id":"","url":"https://api.github.com/users/ddunbar","html_url":"https://github.com/ddunbar","followers_url":"https://api.github.com/users/ddunbar/followers","following_url":"https://api.github.com/users/ddunbar/following{/other_user}","gists_url":"https://api.github.com/users/ddunbar/gists{/gist_id}","starred_url":"https://api.github.com/users/ddunbar/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ddunbar/subscriptions","organizations_url":"https://api.github.com/users/ddunbar/orgs","repos_url":"https://api.github.com/users/ddunbar/repos","events_url":"https://api.github.com/users/ddunbar/events{/privacy}","received_events_url":"https://api.github.com/users/ddunbar/received_events","type":"User","site_admin":false},"body":"`var argv` + argv.append(nil) would be mildly more efficient than our `argv + [nil]` (used later)","created_at":"2016-12-14T17:16:52Z","updated_at":"2016-12-14T17:16:58Z","html_url":"https://github.com/apple/swift-package-manager/pull/849#discussion_r92442908","pull_request_url":"https://api.github.com/repos/apple/swift-package-manager/pulls/849","_links":{"self":{"href":"https://api.github.com/repos/apple/swift-package-manager/pulls/comments/92442908"},"html":{"href":"https://github.com/apple/swift-package-manager/pull/849#discussion_r92442908"},"pull_request":{"href":"https://api.github.com/repos/apple/swift-package-manager/pulls/849"}}},"pull_request":{"url":"https://api.github.com/repos/apple/swift-package-manager/pulls/849","id":97941904,"html_url":"https://github.com/apple/swift-package-manager/pull/849","diff_url":"https://github.com/apple/swift-package-manager/pull/849.diff","patch_url":"https://github.com/apple/swift-package-manager/pull/849.patch","issue_url":"https://api.github.com/repos/apple/swift-package-manager/issues/849","number":849,"state":"open","locked":false,"title":"[Utility] Add Process class","user":{"login":"aciidb0mb3r","id":4136295,"avatar_url":"https://avatars.githubusercontent.com/u/4136295?v=3","gravatar_id":"","url":"https://api.github.com/users/aciidb0mb3r","html_url":"https://github.com/aciidb0mb3r","followers_url":"https://api.github.com/users/aciidb0mb3r/followers","following_url":"https://api.github.com/users/aciidb0mb3r/following{/other_user}","gists_url":"https://api.github.com/users/aciidb0mb3r/gists{/gist_id}","starred_url":"https://api.github.com/users/aciidb0mb3r/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aciidb0mb3r/subscriptions","organizations_url":"https://api.github.com/users/aciidb0mb3r/orgs","repos_url":"https://api.github.com/users/aciidb0mb3r/repos","events_url":"https://api.github.com/users/aciidb0mb3r/events{/privacy}","received_events_url":"https://api.github.com/users/aciidb0mb3r/received_events","type":"User","site_admin":false},"body":"This class aims to be replacement to current system() and popen() methods and a wrapper around posix_spawn","created_at":"2016-12-14T12:21:45Z","updated_at":"2016-12-14T17:26:16Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d48bf005cb07b12d151c9ad3ca7550a8bc59bfb3","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/apple/swift-package-manager/pulls/849/commits","review_comments_url":"https://api.github.com/repos/apple/swift-package-manager/pulls/849/comments","review_comment_url":"https://api.github.com/repos/apple/swift-package-manager/pulls/comments{/number}","comments_url":"https://api.github.com/repos/apple/swift-package-manager/issues/849/comments","statuses_url":"https://api.github.com/repos/apple/swift-package-manager/statuses/d2c441291c50430957b001a9c2c74080784c165c","head":{"label":"aciidb0mb3r:process","ref":"process","sha":"d2c441291c50430957b001a9c2c74080784c165c","user":{"login":"aciidb0mb3r","id":4136295,"avatar_url":"https://avatars.githubusercontent.com/u/4136295?v=3","gravatar_id":"","url":"https://api.github.com/users/aciidb0mb3r","html_url":"https://github.com/aciidb0mb3r","followers_url":"https://api.github.com/users/aciidb0mb3r/followers","following_url":"https://api.github.com/users/aciidb0mb3r/following{/other_user}","gists_url":"https://api.github.com/users/aciidb0mb3r/gists{/gist_id}","starred_url":"https://api.github.com/users/aciidb0mb3r/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aciidb0mb3r/subscriptions","organizations_url":"https://api.github.com/users/aciidb0mb3r/orgs","repos_url":"https://api.github.com/users/aciidb0mb3r/repos","events_url":"https://api.github.com/users/aciidb0mb3r/events{/privacy}","received_events_url":"https://api.github.com/users/aciidb0mb3r/received_events","type":"User","site_admin":false},"repo":{"id":47417816,"name":"swift-package-manager","full_name":"aciidb0mb3r/swift-package-manager","owner":{"login":"aciidb0mb3r","id":4136295,"avatar_url":"https://avatars.githubusercontent.com/u/4136295?v=3","gravatar_id":"","url":"https://api.github.com/users/aciidb0mb3r","html_url":"https://github.com/aciidb0mb3r","followers_url":"https://api.github.com/users/aciidb0mb3r/followers","following_url":"https://api.github.com/users/aciidb0mb3r/following{/other_user}","gists_url":"https://api.github.com/users/aciidb0mb3r/gists{/gist_id}","starred_url":"https://api.github.com/users/aciidb0mb3r/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aciidb0mb3r/subscriptions","organizations_url":"https://api.github.com/users/aciidb0mb3r/orgs","repos_url":"https://api.github.com/users/aciidb0mb3r/repos","events_url":"https://api.github.com/users/aciidb0mb3r/events{/privacy}","received_events_url":"https://api.github.com/users/aciidb0mb3r/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/aciidb0mb3r/swift-package-manager","description":"The Package Manager for the Swift Programming Language","fork":true,"url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager","forks_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/forks","keys_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/teams","hooks_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/hooks","issue_events_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/issues/events{/number}","events_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/events","assignees_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/assignees{/user}","branches_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/branches{/branch}","tags_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/tags","blobs_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/languages","stargazers_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/stargazers","contributors_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/contributors","subscribers_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/subscribers","subscription_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/subscription","commits_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/contents/{+path}","compare_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/merges","archive_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/downloads","issues_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/issues{/number}","pulls_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/labels{/name}","releases_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/releases{/id}","deployments_url":"https://api.github.com/repos/aciidb0mb3r/swift-package-manager/deployments","created_at":"2015-12-04T17:05:51Z","updated_at":"2016-02-04T13:02:44Z","pushed_at":"2016-12-14T14:48:52Z","git_url":"git://github.com/aciidb0mb3r/swift-package-manager.git","ssh_url":"git@github.com:aciidb0mb3r/swift-package-manager.git","clone_url":"https://github.com/aciidb0mb3r/swift-package-manager.git","svn_url":"https://github.com/aciidb0mb3r/swift-package-manager","homepage":"","size":3932,"stargazers_count":0,"watchers_count":0,"language":"Swift","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"apple:master","ref":"master","sha":"bb86eb957bd3bb211e88c38c21573a73ca293e57","user":{"login":"apple","id":10639145,"avatar_url":"https://avatars.githubusercontent.com/u/10639145?v=3","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"repo":{"id":44840041,"name":"swift-package-manager","full_name":"apple/swift-package-manager","owner":{"login":"apple","id":10639145,"avatar_url":"https://avatars.githubusercontent.com/u/10639145?v=3","gravatar_id":"","url":"https://api.github.com/users/apple","html_url":"https://github.com/apple","followers_url":"https://api.github.com/users/apple/followers","following_url":"https://api.github.com/users/apple/following{/other_user}","gists_url":"https://api.github.com/users/apple/gists{/gist_id}","starred_url":"https://api.github.com/users/apple/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/apple/subscriptions","organizations_url":"https://api.github.com/users/apple/orgs","repos_url":"https://api.github.com/users/apple/repos","events_url":"https://api.github.com/users/apple/events{/privacy}","received_events_url":"https://api.github.com/users/apple/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/apple/swift-package-manager","description":"The Package Manager for the Swift Programming Language","fork":false,"url":"https://api.github.com/repos/apple/swift-package-manager","forks_url":"https://api.github.com/repos/apple/swift-package-manager/forks","keys_url":"https://api.github.com/repos/apple/swift-package-manager/keys{/key_id}","collaborators_url":"https://api.github.com/repos/apple/swift-package-manager/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/apple/swift-package-manager/teams","hooks_url":"https://api.github.com/repos/apple/swift-package-manager/hooks","issue_events_url":"https://api.github.com/repos/apple/swift-package-manager/issues/events{/number}","events_url":"https://api.github.com/repos/apple/swift-package-manager/events","assignees_url":"https://api.github.com/repos/apple/swift-package-manager/assignees{/user}","branches_url":"https://api.github.com/repos/apple/swift-package-manager/branches{/branch}","tags_url":"https://api.github.com/repos/apple/swift-package-manager/tags","blobs_url":"https://api.github.com/repos/apple/swift-package-manager/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/apple/swift-package-manager/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/apple/swift-package-manager/git/refs{/sha}","trees_url":"https://api.github.com/repos/apple/swift-package-manager/git/trees{/sha}","statuses_url":"https://api.github.com/repos/apple/swift-package-manager/statuses/{sha}","languages_url":"https://api.github.com/repos/apple/swift-package-manager/languages","stargazers_url":"https://api.github.com/repos/apple/swift-package-manager/stargazers","contributors_url":"https://api.github.com/repos/apple/swift-package-manager/contributors","subscribers_url":"https://api.github.com/repos/apple/swift-package-manager/subscribers","subscription_url":"https://api.github.com/repos/apple/swift-package-manager/subscription","commits_url":"https://api.github.com/repos/apple/swift-package-manager/commits{/sha}","git_commits_url":"https://api.github.com/repos/apple/swift-package-manager/git/commits{/sha}","comments_url":"https://api.github.com/repos/apple/swift-package-manager/comments{/number}","issue_comment_url":"https://api.github.com/repos/apple/swift-package-manager/issues/comments{/number}","contents_url":"https://api.github.com/repos/apple/swift-package-manager/contents/{+path}","compare_url":"https://api.github.com/repos/apple/swift-package-manager/compare/{base}...{head}","merges_url":"https://api.github.com/repos/apple/swift-package-manager/merges","archive_url":"https://api.github.com/repos/apple/swift-package-manager/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/apple/swift-package-manager/downloads","issues_url":"https://api.github.com/repos/apple/swift-package-manager/issues{/number}","pulls_url":"https://api.github.com/repos/apple/swift-package-manager/pulls{/number}","milestones_url":"https://api.github.com/repos/apple/swift-package-manager/milestones{/number}","notifications_url":"https://api.github.com/repos/apple/swift-package-manager/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/apple/swift-package-manager/labels{/name}","releases_url":"https://api.github.com/repos/apple/swift-package-manager/releases{/id}","deployments_url":"https://api.github.com/repos/apple/swift-package-manager/deployments","created_at":"2015-10-23T21:41:12Z","updated_at":"2016-12-14T14:18:45Z","pushed_at":"2016-12-14T14:48:52Z","git_url":"git://github.com/apple/swift-package-manager.git","ssh_url":"git@github.com:apple/swift-package-manager.git","clone_url":"https://github.com/apple/swift-package-manager.git","svn_url":"https://github.com/apple/swift-package-manager","homepage":"","size":3920,"stargazers_count":5011,"watchers_count":5011,"language":"Swift","has_issues":false,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":468,"mirror_url":null,"open_issues_count":9,"forks":468,"open_issues":9,"watchers":5011,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/apple/swift-package-manager/pulls/849"},"html":{"href":"https://github.com/apple/swift-package-manager/pull/849"},"issue":{"href":"https://api.github.com/repos/apple/swift-package-manager/issues/849"},"comments":{"href":"https://api.github.com/repos/apple/swift-package-manager/issues/849/comments"},"review_comments":{"href":"https://api.github.com/repos/apple/swift-package-manager/pulls/849/comments"},"review_comment":{"href":"https://api.github.com/repos/apple/swift-package-manager/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/apple/swift-package-manager/pulls/849/commits"},"statuses":{"href":"https://api.github.com/repos/apple/swift-package-manager/statuses/d2c441291c50430957b001a9c2c74080784c165c"}}}}
{"actor":{"display_login":"ddunbar"}}