Compare commits

...

2 Commits

Author SHA1 Message Date
6c8c2f7192 log socket path 2023-10-04 17:24:48 +02:00
7d445ee27b Override configuration with env variables 2023-10-04 17:15:15 +02:00
4 changed files with 33 additions and 32 deletions

View File

@ -35,10 +35,9 @@ pthread_mutex_t tasks_lock = PTHREAD_MUTEX_INITIALIZER; // locks tasks and has_c
void* inotify_check_changes(void* arg) {
(void)arg;
void* inotify_check_changes(void* sk_path) {
char* socket_path = (char*)sk_path;
char buffer[EVENT_BUF_LEN];
char* socket_path = get_socket_path();
while (1) { // Regarde si /tmp/takl.$USER a changé (donc si la BDD a changé et doit être rechargée)
int fd = inotify_init();
@ -67,14 +66,11 @@ void* inotify_check_changes(void* arg) {
inotify_rm_watch(fd, wd);
close(fd);
}
free(socket_path);
return NULL;
}
int existing_takl_daemon() {
char* socket_path = get_socket_path();
int existing_takl_daemon(char* socket_path) {
if (!access(socket_path, F_OK)) { // tmp file exists
FILE* fp = fopen(socket_path, "r+");
int daemon_pid;
@ -90,15 +86,16 @@ int existing_takl_daemon() {
fprintf(fp, "%d", getpid());
fclose(fp);
free(socket_path);
return false;
}
int main() {
bool notified_no_change = false;
char* socket_path = get_socket_path();
log_debug("Socket path:%s", socket_path);
if (!existing_takl_daemon()) {
if (!existing_takl_daemon(socket_path)) {
log_info("TaKl " VERSION " -- Daemon started");
} else {
log_info("TaKl Daemon déjà en cours d'exécution. Arrêt");
@ -108,7 +105,7 @@ int main() {
tasks = get_task_list(false, false);
pthread_t inotify_id = 0; // Lancement d'inotify dans un autre fil
pthread_create(&inotify_id, NULL, inotify_check_changes, NULL);
pthread_create(&inotify_id, NULL, inotify_check_changes, (void*)socket_path);
while (1) {
pthread_mutex_lock(&tasks_lock);
@ -169,5 +166,6 @@ int main() {
pthread_join(inotify_id, NULL);
free_task_list(tasks);
free(socket_path);
return 0;
}

View File

@ -25,8 +25,17 @@ void sqlAssert(int ret, sqlite3* db, char* file, int line) {
char* get_db_path() {
#ifndef DB_FILE
//! We should check that $HOME/.config already exists even if it should be the case
/*
Checks if $TAKL_DB env variable is set
else, the DB is located at $HOME/.config/takl.sqlite3
*/
char* env_db_path = getenv("TAKL_DB");
if (env_db_path) {
char* db_path = malloc(sizeof(char)*(strlen(env_db_path)+1));
strcpy(db_path, env_db_path);
return db_path;
} else {
char* base_path = ".config/takl.sqlite3";
char* home_dir = getenv("HOME");
@ -36,12 +45,7 @@ char* get_db_path() {
sprintf(db_path, "%s/%s", home_dir, base_path);
return db_path;
#else
char* db_path = malloc(sizeof(char)*(strlen(DB_FILE)+1));
memcpy(db_path, DB_FILE, sizeof(char)*(strlen(DB_FILE)+1));
return db_path;
#endif
}
}

View File

@ -1,13 +1,7 @@
#ifndef DEF_CONFIG_H
#define DEF_CONFIG_H
#define VERSION "1.3.0"
// By default, $HOME/.config/takl.sqlite3 is used. You can change this behaviour here
//#define DB_FILE "takl.sqlite3"
// By default, /tmp/takl.$USER is used. You can change this behaviour here
//#define SOCKET_FILE "takl.sock"
#define VERSION "1.3.2"
#define MAX_TASK_ID 10000 // max is set to MAX_TASK_ID-1
#define NEW_TASK_ID_MAX_RETRIES 10000 // number of retries before giving up

View File

@ -8,7 +8,17 @@
#include "include/struct.h"
char* get_socket_path() {
#ifndef SOCKET_FILE
/*
Checks if $TAKL_SOCKET env variable is set
else, the socket is located at /tmp/takl
*/
char* env_socket_path = getenv("TAKL_SOCKET");
if (env_socket_path) {
char* socket_path = malloc(sizeof(char)*(strlen(env_socket_path)+1));
strcpy(socket_path, env_socket_path);
return socket_path;
} else {
char* base_path = "/tmp/takl";
char* username = getenv("USER");
@ -18,12 +28,7 @@ char* get_socket_path() {
sprintf(socket_path, "%s.%s", base_path, username);
return socket_path;
#else
char* socket_path = malloc(sizeof(char)*(strlen(SOCKET_FILE)+1));
memcpy(socket_path, SOCKET_FILE, sizeof(char)*(strlen(SOCKET_FILE)+1));
return socket_path;
#endif
}
}