Skip to content

Commit

Permalink
bug fix (not turning off)
Browse files Browse the repository at this point in the history
  • Loading branch information
KelvinNovais committed Jun 22, 2023
1 parent 1d1bb8c commit 0b618f4
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 27 deletions.
2 changes: 0 additions & 2 deletions cli/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ install: all
@# Uninstaller
@install -v -o root -g root -m 555 uninstall.sh /opt/gawake/ # src destination
@install -v -o root -g root -m 644 gawake /etc/cron.d/ # src destination
@# Folders: /var/gawake for the database; /var/gawake/logs for logs
@mkdir -p -m 664 /var/gawake/logs/
@echo "Adding Gawake user"
-@useradd -M gawake
@echo "Gawake installed successfully!"
Expand Down
4 changes: 0 additions & 4 deletions cli/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,6 @@ echo "Installing the cron service to /etc/cron.d"
install -v -o root -g root -m 644 gawake /etc/cron.d/ # src destination
err_counter=$(($? + $err_counter))

# Folders: /var/gawake for the database; /var/gawake/logs for logs
mkdir -p -m 664 /var/gawake/logs/
err_counter=$(($? + $err_counter))

# Adding Gawake user
useradd -M gawake

Expand Down
13 changes: 9 additions & 4 deletions cli/src/cron_toff.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include "include/gawake.h"
#include "include/get_time.h"
#include "include/wday.h"

// Apdend with ">> path/turn_off.log 2>&1"
#define LOGS_OUTPUT " >> " LOGS "turn_off.log 2>&1"

int main (void) {
int rc, now, db_time = 1, id_match = -1, alloc = 192, cmd_stat = 0, gawake_stat = 0;
int rc, now, db_time = 1, id_match = -1, alloc = 230, cmd_stat = 0, gawake_stat = 0;

struct tm *timeinfo; // Default structure, see documentation
struct sqlite3_stmt *stmt;
Expand Down Expand Up @@ -75,7 +79,7 @@ int main (void) {

// DO NOTHING IF GAWAKE IS DISABLED
if (gawake_stat == 0) {
fprintf(stdout, "Gawake is disabled, exiting...");
fprintf(stdout, "Gawake is disabled, exiting...\n");
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -193,9 +197,10 @@ int main (void) {
if (stat != 0)
fprintf(stderr, "[!] >>>>>>>>> Command(set by user) exited with error\n");
}
snprintf(rtcwake_cmd, alloc, "rtcwake --date %s%s %s -m %s", date, time, options, mode);
snprintf(rtcwake_cmd, alloc, "sudo rtcwake --date %s%s %s -m %s", date, time, options, mode);
fprintf(stdout, "Running rtcwake: %s\n", rtcwake_cmd);
system(rtcwake_cmd);
strcat(rtcwake_cmd, LOGS_OUTPUT); // Sending rtcwake output to the log
stat = system(rtcwake_cmd);
if (stat != 0)
fprintf(stderr, "[!] >>>>>>>>> rtcwake failed scheduling\n");
return EXIT_SUCCESS;
Expand Down
14 changes: 6 additions & 8 deletions cli/src/db_checker.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(void) {
"VALUES ('Example', '10:00:00', 0, 0, 0, 0, 0, 0, 0);"\
"INSERT INTO rules_turnoff (rule_name, time, sun, mon, tue, wed, thu, fri, sat, command, mode)"\
"VALUES ('Example', '11:30:00', 0, 0, 0, 0, 0, 0, 0, 'dnf update -y', 'mem');"\
"INSERT INTO config (options, status, version, commands, localtime, def_mode, boot_time) VALUES ('-a', 1, '" VERSION "', 0, 1, 'off', 300);";
"INSERT INTO config (options, status, version, commands, localtime, def_mode, boot_time) VALUES ('-a -v', 1, '" VERSION "', 0, 1, 'off', 300);";

printf("Opening database...\n");
// If the database doesn't exist, create and configure it
Expand All @@ -88,8 +88,11 @@ int main(void) {
struct stat dir;
if (stat(DIR, &dir) == -1) {
// Directory doesn't exist, creating it
if (mkdir(DIR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH) == -1)
if (mkdir(DIR, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
return EXIT_FAILURE;

// Creating logs directory
mkdir(LOGS, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH);
}

printf("[2/5] Creating empty file for the database.\n");
Expand All @@ -102,16 +105,11 @@ int main(void) {

// Redundancy
printf("[3/5] Setting directory and file permissions.\n");
if (chown(DIR, 0, 0) == -1
|| chown(PATH, 0, 0) == -1
|| chmod(DIR, 0664) == -1
|| chmod(PATH, 0660) == -1
) {
if (chown(DIR, 0, 0) == -1 || chown(PATH, 0, 0) == -1) {
fprintf(stderr, ANSI_COLOR_RED "ERROR: %s\n" ANSI_COLOR_RESET, strerror(errno));
return EXIT_FAILURE;
}


printf("[4/5] Creating database.\n");
// Try to open it
rc = sqlite3_open_v2(PATH, &db, SQLITE_OPEN_READWRITE, NULL);
Expand Down
5 changes: 3 additions & 2 deletions cli/src/include/gawake.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@
*/
#define DIR "/var/gawake/"
#define PATH DIR "gawake-cli.db"
#define LOGS DIR "logs/"

#define VERSION "3.0" // Gawake version
#define CMD_LEN 129 // Allowed length for commands
#define VERSION "3.0.1" // Gawake version
#define CMD_LEN 129 // Allowed length for commands

#endif /* GAWAKE_H_ */

16 changes: 9 additions & 7 deletions cli/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>
*/

/*
* Possible implementations:
* Updater function
* Run commands as other user besides root
* Very input when receiveing rtwake options/arguments (on function config)
* Allow changing boot_time
/*! TODO:
* (Possible implementations)
* @Gawake updater function
* @Run commands as other users, besides root
* @Verify user input when receiveing rtwake options/arguments (on function config)
* @Allow changing boot_time
* @Fix output log
* @Add manual schedule output to logs
*/

#include <stdio.h>
Expand Down Expand Up @@ -561,7 +563,7 @@ int config(sqlite3 **db) {
// Computer time
struct tm *timeinfo;
get_time(&timeinfo);
printf("Time information:\n%-35s %d-%02d-%02d %02d:%d:%02d\n",
printf("Time information:\n%-35s %d-%02d-%02d %02d:%02d:%02d\n",
"Current local time and date:",
timeinfo -> tm_year + 1900, timeinfo -> tm_mon + 1, timeinfo -> tm_mday, timeinfo -> tm_hour, timeinfo -> tm_min, timeinfo -> tm_sec);

Expand Down

0 comments on commit 0b618f4

Please sign in to comment.