Skip to content
Snippets Groups Projects
Verified Commit 9804451c authored by Nathaniel Russell's avatar Nathaniel Russell Committed by Newbyte
Browse files

Implement Slackware specific information for rc.ntpd (MR 13)

Closes #5
parent 1ff05f4f
No related branches found
No related tags found
1 merge request!13Implement Slackware specific information for rc.ntpd
Pipeline #208336 passed
......@@ -36,6 +36,10 @@
#include <rc.h>
#endif
#if TIME_STYLE_SLACKWARE
#include <sys/stat.h>
#endif
#include "copypaste/hwclock.h"
#include "timedated.h"
#include "timedate1-generated.h"
......@@ -186,26 +190,6 @@ set_timezone (const gchar *identifier,
return TRUE;
}
#if TIME_STYLE_SLACKWARE
// Function to check if the NTP service is running
bool ntp_is_running() {
FILE* fp = popen("pgrep ntpd", "r"); // Check if the ntpd process is running
if (fp == NULL) {
perror("popen");
return false; // Assume NTP is not running if an error occurs
}
char buf[128];
if (fgets(buf, sizeof(buf), fp) != NULL) {
pclose(fp);
return true; // NTP process found, so it is running
}
pclose(fp);
return false; // NTP process not found, so it is not running
}
#endif
/* Return the ntp rc service we will use; return value should NOT be freed */
static const gchar *
ntp_service ()
......@@ -233,11 +217,22 @@ ntp_service ()
return service;
#elif TIME_STYLE_SLACKWARE
// Check if NTP service is already running
// Add your logic here to check if the NTP program is running
if (ntp_is_running()) {
return "ntpd"; // Assuming "ntp" is the default service name when NTP is running
const char *rc_ntpd = "/etc/rc.d/rc.ntpd";
const char *ntpd_conf = "/etc/ntp.conf"; // Assuming this is the path for NTP configuration
struct stat st_ntpd, st_ntpd_conf;
if (stat(rc_ntpd, &st_ntpd) == 0 && (st_ntpd.st_mode & S_IFMT) == S_IFREG &&
stat(ntpd_conf, &st_ntpd_conf) == 0 && (st_ntpd_conf.st_mode & S_IFMT) == S_IFREG) {
if ((st_ntpd.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) == 0755) {
g_debug("Starting ntpd: rc.ntpd has appropriate permissions (0755).");
return "ntpd";
} else if ((st_ntpd.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) == 0644) {
g_debug("Unable to start ntpd: rc.ntpd has insufficient permissions (0644).");
return NULL;
}
}
g_warning("Unable to start ntpd: rc.ntpd is not a regular file or does not exist.");
return NULL;
#else
return NULL;
......@@ -260,6 +255,31 @@ service_started (const gchar *service,
state = rc_service_state (service);
return state == RC_SERVICE_STARTED || state == RC_SERVICE_STARTING || state == RC_SERVICE_INACTIVE;
#elif TIME_STYLE_SLACKWARE
const char *rc_ntpd = "/etc/rc.d/rc.ntpd";
struct stat st;
// Check the file status
if (stat(rc_ntpd, &st) == 0) {
// Check if it's a regular file and permissions are 755
if ((st.st_mode & S_IFMT) == S_IFREG &&
(st.st_mode & (S_IRWXU | S_IRWXG | S_IRWXO)) == 0755) {
// Check the NTP daemon status
int ret = system("/etc/rc.d/rc.ntpd status");
if (ret == 0) {
return TRUE; // NTP daemon is running
} else {
// Attempt to start the NTP daemon if not running
gchar *cmd = g_strconcat("/etc/rc.d/rc.ntpd start", NULL);
ret = system(cmd);
g_free(cmd);
return (ret == 0); // Return TRUE if start command succeeds
}
} else {
g_debug("Unable to start ntpd: rc.ntpd has insufficient permissions (not 755).");
}
}
return FALSE;
#else
return FALSE;
#endif
......@@ -313,6 +333,18 @@ service_disable (const gchar *service,
if (service_script != NULL)
free (service_script);
return ret;
#elif TIME_STYLE_SLACKWARE
int stop_ret = system("/etc/rc.d/rc.ntpd stop");
if (stop_ret != 0) {
g_set_error(error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to stop NTP service with /etc/rc.d/rc.ntpd.");
return FALSE;
} else {
use_ntp = FALSE;
if (timedate1 != NULL) {
openrc_settingsd_timedated_timedate1_set_ntp(timedate1, use_ntp);
}
return TRUE;
}
#else
return FALSE;
#endif
......@@ -366,6 +398,20 @@ service_enable (const gchar *service,
if (service_script != NULL)
free (service_script);
return ret;
#elif TIME_STYLE_SLACKWARE
int ret = system("/etc/rc.d/rc.ntpd start");
if (ret == 0) {
use_ntp = TRUE;
} else {
g_set_error(error, G_IO_ERROR, G_IO_ERROR_FAILED, "Failed to start NTP service with /etc/rc.d/rc.ntpd.");
return FALSE;
}
if (timedate1 != NULL) {
openrc_settingsd_timedated_timedate1_set_ntp(timedate1, use_ntp);
}
return TRUE;
#else
return FALSE;
#endif
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment