Skip to content

Commit 4b429df

Browse files
committed
Fix registration, running and removal
1 parent 1714d5c commit 4b429df

File tree

7 files changed

+61
-23
lines changed

7 files changed

+61
-23
lines changed

ant/windows/windows-launcher.nsi.in

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,14 @@ Section
6666
StrCpy $command '"$R2" /s /k "$command"'
6767
${EndIf}
6868

69-
Exec $command
69+
; Check for "--wait" parameter
70+
${StrLoc} $R3 "$params" "--wait" "<"
71+
${If} $R3 != ""
72+
ExecWait $command
73+
${Else}
74+
Exec $command
75+
${EndIf}
76+
7077
${If} ${RunningX64}
7178
${EnableX64FSRedirection}
7279
${EndIf}

src/qz/installer/Installer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,11 @@ public static boolean preinstall() {
9595
public static void install() throws Exception {
9696
getInstance();
9797
log.info("Installing to {}", instance.getDestination());
98-
instance.removeLibs()
98+
instance.removeServiceRegistration()
99+
.removeLibs()
99100
.deployApp()
100101
.removeLegacyStartup()
101102
.removeLegacyFiles()
102-
.removeServiceRegistration()
103103
.addSharedDirectory()
104104
.addAppLauncher()
105105
.addStartupEntry()

src/qz/installer/WindowsInstaller.java

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,22 +206,28 @@ public void spawn(List<String> args) throws Exception {
206206

207207
@Override
208208
public Installer addServiceRegistration(String user) {
209+
log.warn("Registering system service: {}", PROPS_FILE);
209210
if(!SystemUtilities.isAdmin()) {
210211
throw new UnsupportedOperationException("Installing a service requires elevation");
211212
}
212213

214+
if(WindowsUtilities.serviceExists(PROPS_FILE)) {
215+
log.warn("System service is already registered, removing.");
216+
removeServiceRegistration();
217+
}
218+
213219
Path nssm = SystemUtilities.getJarParentPath().resolve("utils/nssm.exe");
214220
Path qz = SystemUtilities.getJarParentPath().resolve(PROPS_FILE + ".exe");
215-
String servicePath = String.format("\"" + qz.toString() + "\" %s %s %s",
216-
ArgValue.WAIT.getMatches()[0],
217-
ArgValue.STEAL.getMatches()[0],
218-
ArgValue.HEADLESS.getMatches()[0]);
219221

220222
// Install the service
221-
if(ShellUtilities.execute(nssm.toString(), "install", PROPS_FILE, servicePath)) {
222-
ShellUtilities.execute(nssm.toString(), "set", "DisplayName", ABOUT_TITLE);
223-
ShellUtilities.execute(nssm.toString(), "set", "Description", ABOUT_DESCRIPTION);
224-
ShellUtilities.execute(nssm.toString(), "set", "DependOnService", "Spooler");
223+
if(ShellUtilities.execute(nssm.toString(), "install", PROPS_FILE,
224+
qz.toString(),
225+
ArgValue.WAIT.getMatches()[0],
226+
ArgValue.STEAL.getMatches()[0],
227+
ArgValue.HEADLESS.getMatches()[0])) {
228+
ShellUtilities.execute(nssm.toString(), "set", PROPS_FILE, "DisplayName", ABOUT_TITLE);
229+
ShellUtilities.execute(nssm.toString(), "set", PROPS_FILE, "Description", ABOUT_DESCRIPTION);
230+
ShellUtilities.execute(nssm.toString(), "set", PROPS_FILE, "DependOnService", "Spooler");
225231
log.info("Successfully registered system service: {}", PROPS_FILE);
226232
if(user != null && !user.trim().isEmpty()) {
227233
log.info("Setting service to run as {}", user);
@@ -233,7 +239,9 @@ public Installer addServiceRegistration(String user) {
233239
TaskKiller.killAll();
234240
// Instruct autostart to be ignored
235241
FileUtilities.disableGlobalAutoStart();
242+
log.info("Starting system service: {}", PROPS_FILE);
236243
if(WindowsUtilities.startService(PROPS_FILE)) {
244+
log.info("System system service started successfully.", PROPS_FILE);
237245
return this;
238246
}
239247
}
@@ -242,20 +250,25 @@ public Installer addServiceRegistration(String user) {
242250

243251
@Override
244252
public Installer removeServiceRegistration() {
253+
log.info("Removing system service: {}", PROPS_FILE);
245254
if(!SystemUtilities.isAdmin()) {
246255
throw new UnsupportedOperationException("Removing a service requires elevation");
247256
}
248257

249-
WindowsUtilities.stopService(PROPS_FILE);
250-
Path nssm = SystemUtilities.getJarParentPath().resolve("utils/nssm.exe");
251-
if(ShellUtilities.execute(nssm.toString(), "remove", PROPS_FILE, "confirm")) {
252-
// Old tutorials used "QZ Tray" as the service name
253-
ShellUtilities.execute(nssm.toString(), "remove", ABOUT_TITLE, "confirm");
254-
// Restore default autostart settings by deleting the preference file
255-
FileUtils.deleteQuietly(FileUtilities.SHARED_DIR.resolve(AUTOSTART_FILE).toFile());
256-
log.info("System service successfully removed: {}", PROPS_FILE);
258+
if(WindowsUtilities.serviceExists(PROPS_FILE)) {
259+
WindowsUtilities.stopService(PROPS_FILE);
260+
Path nssm = SystemUtilities.getJarParentPath().resolve("utils/nssm.exe");
261+
if(ShellUtilities.execute(nssm.toString(), "remove", PROPS_FILE, "confirm")) {
262+
// Old tutorials used "QZ Tray" as the service name
263+
ShellUtilities.execute(nssm.toString(), "remove", ABOUT_TITLE, "confirm");
264+
// Restore default autostart settings by deleting the preference file
265+
FileUtils.deleteQuietly(FileUtilities.SHARED_DIR.resolve(AUTOSTART_FILE).toFile());
266+
log.info("System service successfully removed: {}", PROPS_FILE);
267+
} else {
268+
log.error("An error occurred removing system service: {}", PROPS_FILE);
269+
}
257270
} else {
258-
log.error("An error occurred removing system service: {}, please try to remove manually using 'sc ", PROPS_FILE);
271+
log.info("System service was not found, skipping.");
259272
}
260273

261274
return this;

src/qz/utils/ArgParser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,11 @@ public ExitStatus processInstallerArgs(ArgValue argValue, List<String> args) {
183183
Installer.uninstall();
184184
return SUCCESS;
185185
case SERVICE:
186-
Installer.getInstance().addServiceRegistration(valueOf(RUNAS));
186+
if(hasFlag(REMOVE)) {
187+
Installer.getInstance().removeServiceRegistration();
188+
} else {
189+
Installer.getInstance().addServiceRegistration(valueOf(RUNAS));
190+
}
187191
return SUCCESS;
188192
case SPAWN:
189193
args.remove(0); // first argument is "spawn", remove it

src/qz/utils/ArgValue.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ public enum ArgValueOption {
130130

131131
// service
132132
RUNAS(ArgValue.SERVICE, "Username to run the system service as (Windows only)",
133-
"--runas", "--user", "-u");
133+
"--runas", "--user", "-u"),
134+
REMOVE(ArgValue.SERVICE, "Remove the system service as (Windows only)",
135+
"--remove", "-r");
134136

135137
ArgValue parent;
136138
String description;

src/qz/utils/FileUtilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static Path inheritParentPermissions(Path filePath) {
164164
public static boolean disableGlobalAutoStart() {
165165
Path autostart = SHARED_DIR.resolve(AUTOSTART_FILE);
166166
try {
167-
Files.write(autostart, "0".getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
167+
Files.write(autostart, "0".getBytes(StandardCharsets.UTF_8), StandardOpenOption.CREATE_NEW, StandardOpenOption.TRUNCATE_EXISTING);
168168
return true;
169169
}
170170
catch(IOException e) {

src/qz/utils/WindowsUtilities.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,18 @@ public static boolean isWow64() {
345345
return isWow64;
346346
}
347347

348+
public static boolean serviceExists(String serviceName) {
349+
try {
350+
W32ServiceManager serviceManager = new W32ServiceManager();
351+
serviceManager.open(Winsvc.SC_MANAGER_ALL_ACCESS);
352+
W32Service service = serviceManager.openService(serviceName, Winsvc.SC_MANAGER_ALL_ACCESS);
353+
return true;
354+
} catch(Win32Exception e) {
355+
return false;
356+
} catch(Throwable t) {}
357+
return ShellUtilities.execute("sc", "query", serviceName);
358+
}
359+
348360
public static boolean stopService(String serviceName) {
349361
try {
350362
W32ServiceManager serviceManager = new W32ServiceManager();

0 commit comments

Comments
 (0)