forked from OwnGoalStudio/TrollVNC
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTRWatchDog.h
More file actions
159 lines (114 loc) · 5.55 KB
/
TRWatchDog.h
File metadata and controls
159 lines (114 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
This file is part of TrollVNC
Copyright (c) 2025 82Flex <82flex@gmail.com> and contributors
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef TRWatchDog_h
#define TRWatchDog_h
#import <Foundation/Foundation.h>
// Error domain
FOUNDATION_EXPORT NSString *const TRWatchDogErrorDomain;
// Error codes
typedef NS_ENUM(NSInteger, TRWatchDogErrorCode) {
// Configuration errors (1000-1099)
TRWatchDogErrorCodeMissingLabel = 1001,
TRWatchDogErrorCodeMissingProgram = 1002,
TRWatchDogErrorCodeInvalidExecutable = 1003,
TRWatchDogErrorCodeInvalidWorkingDirectory = 1004,
// Runtime errors (1100-1199)
TRWatchDogErrorCodeTaskLaunchFailed = 1101,
TRWatchDogErrorCodeInvalidState = 1102
};
typedef NS_ENUM(NSInteger, TRWatchDogTerminationReason) {
TRWatchDogTerminationReasonExit = 0, // Process exited normally
TRWatchDogTerminationReasonUncaughtSignal = 1 // Process terminated by signal
};
typedef NS_ENUM(NSInteger, TRWatchDogState) {
TRWatchDogStateStopped = 0, // Initial state
TRWatchDogStateStarting, // Starting up
TRWatchDogStateRunning, // Running normally
TRWatchDogStateStopping, // Shutting down
TRWatchDogStateCrashed, // Process crashed
TRWatchDogStateThrottled // Throttled, waiting to restart
};
@interface TRWatchDog : NSObject
/// Service label identifier
@property(nonatomic, copy) NSString *label;
/// Program arguments (executable path and arguments)
@property(nonatomic, strong) NSArray<NSString *> *programArguments;
/// Environment variables
@property(nonatomic, strong) NSDictionary<NSString *, NSString *> *environmentVariables;
/// Working directory
@property(nonatomic, copy) NSString *workingDirectory;
/// Standard input file path
@property(nonatomic, copy) NSString *standardInputPath;
/// Standard output file path
@property(nonatomic, copy) NSString *standardOutputPath;
/// Standard error file path
@property(nonatomic, copy) NSString *standardErrorPath;
/// User name
@property(nonatomic, copy) NSString *userName;
/// Group name
@property(nonatomic, copy) NSString *groupName;
/// Process group identifier (-1 = not set, 0 = use default process group, >0 = specific group)
@property(nonatomic, assign) pid_t processGroupIdentifier;
/// Exit timeout in seconds
@property(nonatomic, assign) NSTimeInterval exitTimeOut;
/// Throttle interval in seconds (minimum time between successive starts)
@property(nonatomic, assign) NSTimeInterval throttleInterval;
/// Keep alive configuration (can be BOOL or NSDictionary)
@property(nonatomic, strong) id keepAlive;
/// Current state (thread-safe)
@property(nonatomic, assign, readonly) TRWatchDogState state;
#pragma mark - Public Methods
/// Start the watchdog service
/// @return YES if start initiated successfully, NO if already starting/running or configuration invalid
- (BOOL)start;
/// Stop the watchdog service
/// @return YES if stop initiated successfully, NO if already stopping/stopped
- (BOOL)stop;
/// Restart the watchdog service
/// @return YES if restart initiated successfully, NO if configuration invalid
- (BOOL)restart;
/// Send a signal to the current running task
/// @param signal The signal number to send (e.g., SIGTERM, SIGUSR1, etc.)
/// @return YES if signal was sent successfully, NO if no task is running or signal failed
- (BOOL)sendSignal:(int)signal;
/// Check if the watchdog is currently active (starting, running, or stopping)
@property(nonatomic, assign, readonly) BOOL isActive;
/// Check if the watchdog is currently running
@property(nonatomic, assign, readonly) BOOL isRunning;
/// Check if the watchdog is currently throttled (waiting to restart)
@property(nonatomic, assign, readonly) BOOL isThrottled;
/// Current process identifier (0 if not running)
@property(nonatomic, assign, readonly) pid_t processIdentifier;
/// Start time of current process (nil if not running)
@property(nonatomic, strong, readonly) NSDate *processStartTime;
/// Total number of restarts since watchdog creation
@property(nonatomic, assign, readonly) NSUInteger restartCount;
/// Time when the last process exit occurred (nil if never exited)
@property(nonatomic, strong, readonly) NSDate *lastExitTime;
/// Last exit status (valid only if lastExitTime is not nil)
@property(nonatomic, assign, readonly) int lastExitStatus;
/// Last uncaught signal (valid only if lastTerminationReason indicates signal termination)
@property(nonatomic, assign, readonly) int lastUncaughtSignal;
/// Last termination reason (valid only if lastExitTime is not nil)
@property(nonatomic, assign, readonly) TRWatchDogTerminationReason lastTerminationReason;
/// Time remaining until next restart attempt (0 if not throttled)
@property(nonatomic, assign, readonly) NSTimeInterval timeUntilNextRestart;
/// Total uptime of all processes managed by this watchdog
@property(nonatomic, assign, readonly) NSTimeInterval totalUptime;
/// Validate the current configuration
/// @param error Error details if validation fails
/// @return YES if configuration is valid, NO otherwise
- (BOOL)validateConfigurationWithError:(NSError **)error;
@end
#endif /* TRWatchDog_h */