-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add possibility to to autmatically restart the agent when it is updated
- Loading branch information
Showing
9 changed files
with
184 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "inotify.h" | ||
|
||
#ifdef __linux__ | ||
|
||
#include "memory.h" | ||
#include "oidc_error.h" | ||
#include "string/stringUtils.h" | ||
|
||
#define _XOPEN_SOURCE 500 | ||
#include <libgen.h> | ||
#include <sys/inotify.h> | ||
#include <unistd.h> | ||
|
||
#define EVENT_SIZE (sizeof(struct inotify_event)) | ||
#define EVENT_BUF_LEN (1024 * (EVENT_SIZE + 16)) | ||
|
||
int inotify_watch(const char* file_path) { | ||
if (file_path == NULL) { | ||
oidc_setArgNullFuncError(__func__); | ||
return -1; | ||
} | ||
|
||
char* fp_copy = oidc_strcopy(file_path); | ||
char* directory = oidc_strcopy(dirname(fp_copy)); // Get directory part | ||
secFree(fp_copy); | ||
fp_copy = oidc_strcopy(file_path); | ||
char* filename = oidc_strcopy(basename(fp_copy)); // Get filename part | ||
secFree(fp_copy); | ||
|
||
// Create inotify instance | ||
int fd = inotify_init(); | ||
if (fd < 0) { | ||
oidc_setErrnoError(); | ||
secFree(directory); | ||
secFree(filename); | ||
return -1; | ||
} | ||
|
||
// Add a watch for the directory | ||
int wd = inotify_add_watch(fd, directory, IN_MOVED_FROM | IN_MOVED_TO); | ||
if (wd == -1) { | ||
oidc_setErrnoError(); | ||
close(fd); | ||
secFree(directory); | ||
secFree(filename); | ||
return -1; | ||
} | ||
|
||
secFree(directory); | ||
secFree(filename); | ||
return fd; | ||
} | ||
|
||
oidc_error_t inotify_read(int fd, const char* filename, void (*callback)()) { | ||
char buffer[EVENT_BUF_LEN]; | ||
|
||
ssize_t length = read(fd, buffer, EVENT_BUF_LEN); | ||
if (length < 0) { | ||
oidc_setErrnoError(); | ||
return oidc_errno; | ||
} | ||
|
||
size_t i = 0; | ||
while (i < (size_t)length) { | ||
struct inotify_event* event = (struct inotify_event*)&buffer[i]; | ||
|
||
if ((event->mask & IN_MOVED_FROM || event->mask & IN_MOVED_TO) && | ||
strequal(event->name, filename)) { | ||
callback(); | ||
return OIDC_SUCCESS; | ||
} | ||
i += EVENT_SIZE + event->len; | ||
} | ||
return OIDC_SUCCESS; | ||
} | ||
|
||
#endif //__linux__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#ifndef OIDC_AGENT_INOTIFY_H | ||
#define OIDC_AGENT_INOTIFY_H | ||
|
||
#ifdef __linux__ | ||
|
||
#include "oidc_error.h" | ||
|
||
oidc_error_t inotify_read(int fd, const char* filename, void (*callback)()); | ||
int inotify_watch(const char* file_path); | ||
|
||
#endif // __linux__ | ||
#endif // OIDC_AGENT_INOTIFY_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#define _XOPEN_SOURCE 500 | ||
#include "restart.h" | ||
|
||
#include <unistd.h> | ||
|
||
#include "defines/settings.h" | ||
#include "utils/agentLogger.h" | ||
#include "utils/logger.h" | ||
#include "utils/oidc_error.h" | ||
|
||
static int restart_argc; | ||
static char** restart_argv; | ||
|
||
void set_restart_agent_args(int argc, char* argv[]) { | ||
restart_argc = argc; | ||
restart_argv = argv; | ||
} | ||
|
||
void restart_agent_with_set_args() { | ||
restart_agent(restart_argc, restart_argv); | ||
} | ||
|
||
void restart_agent(int argc, char* argv[]) { | ||
// Rebuild the arguments array, ensuring the last element is NULL as required | ||
// by execv | ||
char* new_argv[argc + 1]; | ||
for (int i = 0; i < argc; i++) { new_argv[i] = argv[i]; } | ||
new_argv[argc] = NULL; | ||
|
||
agent_log(INFO, "Restarting oidc-agent %s ...", AGENT_PATH); | ||
execv(AGENT_PATH, new_argv); | ||
|
||
// This is only reached in error case | ||
oidc_setErrnoError(); | ||
agent_log(ERROR, "%s", oidc_serror()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef OIDC_AGENT_RESTART_H | ||
#define OIDC_AGENT_RESTART_H | ||
|
||
void restart_agent(int argc, char* argv[]); | ||
void restart_agent_with_set_args(); | ||
void set_restart_agent_args(int argc, char* argv[]); | ||
|
||
#endif // OIDC_AGENT_RESTART_H |