Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process: Implement signal sending functions. #1578

Merged
merged 2 commits into from
Jun 15, 2018

Conversation

spevans
Copy link
Contributor

@spevans spevans commented Jun 4, 2018

  • Implements suspend(), resume(), interrupt() and terminate().

  • Add --signal-test to xdgTestHelper for testing signals.

@spevans
Copy link
Contributor Author

spevans commented Jun 4, 2018

@swift-ci please test

suspendCount += 1
if suspendCount == 1, processIdentifier > 0 {
kill(processIdentifier, SIGSTOP)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On Darwin we actually check the result of kill (for 0) before incrementing the count. Same for resume.

case "--signal-test": signalTest()
default: break
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're going to have to rename this helper tool at some point :)

(no need to do it now)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this logic look correct:

    open func suspend() -> Bool {
        guard isRunning && processIdentifier > 0 else {
            return false
        }

        if suspendCount == 0 {
            if kill(processIdentifier, SIGSTOP) == 0 {
                suspendCount += 1
            }
        } else {
            suspendCount += 1
        }
        return true
    }

    open func resume() -> Bool {
        guard isRunning && processIdentifier > 0 else {
            return true
        }

        if suspendCount == 1 {
            if kill(processIdentifier, SIGCONT) == 0 {
                suspendCount -= 1
            }
        } else {
            suspendCount -= 1
        }
        return true
    }

The tests ran OK on Linux/macOS and Darwin native so I think the counting is correct.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the code from Darwin, simplified slightly:

- (BOOL)suspend {
    BOOL success = YES;
    success = 0 == kill(_pid, SIGSTOP);
    if (success) {
        _suspendCount++;
    }
    return success;
}

- (BOOL)resume {
    BOOL success = YES;
    if (1 == _suspendCount) {
        success = 0 == kill(_pid, SIGCONT);
    }
    if (success) {
        _suspendCount--;
    }
    return success;
}

It doesn't look like we bother checking the pid, or isRunning. Those might be good improvements but I suspect if we were to change them on Darwin we would find someone who relies upon being able to send these before launching the app in some way.

@spevans
Copy link
Contributor Author

spevans commented Jun 6, 2018

@swift-ci please test

@spevans
Copy link
Contributor Author

spevans commented Jun 6, 2018

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor Author

spevans commented Jun 6, 2018

@swift-ci please test

@millenomi
Copy link
Contributor

@spevans I did some mangling around arguments in xdgTestHelper; can you rebase the patch on master once my FileManager patch finishes merging?

@spevans spevans force-pushed the pr_process_signals branch 2 times, most recently from 66432ca to 823c5da Compare June 12, 2018 20:57
@spevans
Copy link
Contributor Author

spevans commented Jun 12, 2018

@swift-ci please test

@spevans spevans force-pushed the pr_process_signals branch from 823c5da to 0940c2c Compare June 13, 2018 11:25
@spevans
Copy link
Contributor Author

spevans commented Jun 13, 2018

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor Author

spevans commented Jun 13, 2018

@swift-ci please test

spevans added 2 commits June 13, 2018 16:39
- Implements suspend(), resume(), interrupt() and terminate().

- Add --signal-test to xdgTestHelper for testing signals.
- .processIdentifier is 0 before launch and retains the PID value
  after termination.

- .terminationStatus, .terminationReason: Add precondition that the task
  has run and exited.

- interrupt(), termintate(): Add precondition that the task has started.

- suspend(), resume(): Remove checks that process is running and match
  Darwin's suspendCount increment/decrement.
@spevans spevans force-pushed the pr_process_signals branch from 0940c2c to 12c1638 Compare June 13, 2018 15:41
@spevans
Copy link
Contributor Author

spevans commented Jun 13, 2018

@swift-ci please test

1 similar comment
@spevans
Copy link
Contributor Author

spevans commented Jun 13, 2018

@swift-ci please test

@spevans
Copy link
Contributor Author

spevans commented Jun 14, 2018

@parkera This should match the behaviour of Darwin more closely now.

@spevans
Copy link
Contributor Author

spevans commented Jun 14, 2018

@swift-ci please test

@spevans
Copy link
Contributor Author

spevans commented Jun 15, 2018

@swift-ci please test and merge

@swift-ci swift-ci merged commit bf2cc1c into swiftlang:master Jun 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants