2121use OCA \Libresign \Handler \CertificateEngine \CfsslHandler ;
2222use OCA \Libresign \Handler \CertificateEngine \IEngineHandler ;
2323use OCA \Libresign \Service \CaIdentifierService ;
24+ use OCA \Libresign \Service \Process \ProcessManager ;
2425use OCA \Libresign \Vendor \LibreSign \WhatOSAmI \OperatingSystem ;
26+ use OCA \Libresign \Vendor \Symfony \Component \Process \Process ;
2527use OCP \Files \AppData \IAppDataFactory ;
2628use OCP \Files \IAppData ;
2729use OCP \Files \NotFoundException ;
3739use RuntimeException ;
3840use Symfony \Component \Console \Helper \ProgressBar ;
3941use Symfony \Component \Console \Output \OutputInterface ;
40- use Symfony \Component \Process \Process ;
4142
4243class InstallService {
4344 use TSimpleFile {
@@ -52,6 +53,7 @@ class InstallService {
5253 public const JSIGNPDF_VERSION = '2.3.0 ' ; /** @todo When update, verify the hash **/
5354 private const JSIGNPDF_HASH = 'd239658ea50a39eb35169d8392feaffb ' ;
5455 public const CFSSL_VERSION = '1.6.5 ' ;
56+ private const PROCESS_SOURCE = 'install ' ;
5557
5658 private ICache $ cache ;
5759 private ?OutputInterface $ output = null ;
@@ -77,6 +79,7 @@ public function __construct(
7779 private SignSetupService $ signSetupService ,
7880 protected IAppDataFactory $ appDataFactory ,
7981 private CaIdentifierService $ caIdentifierService ,
82+ private ProcessManager $ processManager ,
8083 ) {
8184 $ this ->cache = $ cacheFactory ->createDistributed ('libresign-setup ' );
8285 $ this ->appData = $ appDataFactory ->get ('libresign ' );
@@ -145,18 +148,28 @@ private function getDataDir(): string {
145148
146149 private function runAsync (): void {
147150 $ resource = $ this ->resource ;
148- $ process = new Process ([OC ::$ SERVERROOT . '/occ ' , 'libresign:install ' , '-- ' . $ resource ]);
151+ $ process = $ this -> createProcess ([OC ::$ SERVERROOT . '/occ ' , 'libresign:install ' , '-- ' . $ resource ]);
149152 $ process ->setOptions (['create_new_console ' => true ]);
150153 $ process ->setTimeout (null );
151154 $ process ->start ();
152155 $ data ['pid ' ] = $ process ->getPid ();
153156 if ($ data ['pid ' ]) {
157+ $ this ->processManager ->register (self ::PROCESS_SOURCE , (int )$ data ['pid ' ], [
158+ 'resource ' => $ resource ,
159+ ]);
154160 $ this ->setCache ($ resource , $ data );
155161 } else {
156162 $ this ->logger ->error ('Error to get PID of background install proccess. Command: ' . OC ::$ SERVERROOT . '/occ libresign:install -- ' . $ resource );
157163 }
158164 }
159165
166+ /**
167+ * @param string[] $command
168+ */
169+ protected function createProcess (array $ command ): Process {
170+ return new Process ($ command );
171+ }
172+
160173 private function progressToDatabase (int $ downloadSize , int $ downloaded ): void {
161174 $ data = $ this ->getProressData ();
162175 $ data ['download_size ' ] = $ downloadSize ;
@@ -295,27 +308,27 @@ public function isDownloadWip(): bool {
295308 }
296309
297310 private function getInstallPid (int $ pid = 0 ): int {
311+ $ resource = $ this ->resource ;
298312 if ($ pid > 0 ) {
299- if (shell_exec ('which ps ' ) === null ) {
300- if (is_dir ('/proc/ ' . $ pid )) {
301- return $ pid ;
302- }
303- return 0 ;
313+ $ registeredPid = $ this ->processManager ->findRunningPid (
314+ self ::PROCESS_SOURCE ,
315+ fn (array $ entry ): bool
316+ => $ entry ['pid ' ] === $ pid
317+ && ($ entry ['context ' ]['resource ' ] ?? '' ) === $ resource ,
318+ );
319+
320+ if ($ registeredPid > 0 ) {
321+ return $ registeredPid ;
304322 }
305- $ cmd = 'ps -p ' . $ pid . ' -o pid,command| ' ;
306- } else {
307- $ cmd = 'ps -eo pid,command| ' ;
308- }
309- $ cmd .= 'grep "libresign:install -- ' . $ this ->resource . '"| '
310- . 'grep -v grep| '
311- . 'grep -v defunct| '
312- . 'sed -e "s/^[[:space:]]*//"|cut -d" " -f1 ' ;
313- $ output = shell_exec ($ cmd );
314- if (!is_string ($ output )) {
323+
324+ $ this ->processManager ->unregister (self ::PROCESS_SOURCE , $ pid );
315325 return 0 ;
316326 }
317- $ pid = trim ($ output );
318- return (int )$ pid ;
327+
328+ return $ this ->processManager ->findRunningPid (
329+ self ::PROCESS_SOURCE ,
330+ fn (array $ entry ): bool => ($ entry ['context ' ]['resource ' ] ?? '' ) === $ resource ,
331+ );
319332 }
320333
321334 public function setResource (string $ resource ): self {
0 commit comments