AqualinkD/utils.c

870 lines
19 KiB
C
Raw Normal View History

2017-12-30 20:12:01 +00:00
/*
* Copyright (c) 2017 Shaun Feakes - All rights reserved
*
* You may use redistribute and/or modify this code under the terms of
* the GNU General Public License version 2 as published by the
* Free Software Foundation. For the terms of this license,
* see <http://www.gnu.org/licenses/>.
*
* You are free to use this software under the terms of the GNU General
* Public License, but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* https://github.com/sfeakes/aqualinkd
*/
2023-06-04 21:17:48 +00:00
2017-12-30 20:12:01 +00:00
#include <stdio.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <syslog.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <ctype.h>
#include <fcntl.h>
2019-06-27 22:18:44 +00:00
#ifdef AD_DEBUG
#include <sys/time.h>
2019-06-27 22:18:44 +00:00
#endif
2017-12-30 20:12:01 +00:00
2023-06-23 20:16:30 +00:00
#ifdef AQ_MANAGER
#include <systemd/sd-journal.h>
#endif
2017-12-30 20:12:01 +00:00
#ifndef _UTILS_C_
#define _UTILS_C_
#endif
#include "utils.h"
2019-08-25 20:57:51 +00:00
#define DEFAULT_LOG_FILE "/tmp/aqualinkd-inline.log"
2017-12-30 20:12:01 +00:00
//#define MAXCFGLINE 265
#define TIMESTAMP_LENGTH 30
2023-06-04 21:17:48 +00:00
// Since this get's compiled without net_services for serial_logger
// pre-define this here rather than include netservices.h
2023-06-20 00:15:45 +00:00
void broadcast_log(char *msg);
2017-12-30 20:12:01 +00:00
static bool _daemonise = false;
2023-06-24 00:38:59 +00:00
2023-07-08 16:14:44 +00:00
#ifndef AQ_MANAGER
2017-12-30 20:12:01 +00:00
static bool _log2file = false;
static char *_log_filename = NULL;
2019-08-25 20:57:51 +00:00
static bool _cfg_log2file;
static int _cfg_log_level;
2023-06-24 00:38:59 +00:00
#endif
2023-07-08 16:14:44 +00:00
static int _log_level = LOG_WARNING;
2019-08-18 20:54:10 +00:00
static char *_loq_display_message = NULL;
2020-07-18 16:37:19 +00:00
int16_t _logforcemask = 0;
2017-12-30 20:12:01 +00:00
//static char _log_filename[256];
2023-07-08 16:14:44 +00:00
#ifdef AQ_MANAGER
void setLoggingPrms(int level , bool deamonized, char *error_messages)
{
_log_level = level;
_daemonise = deamonized;
_loq_display_message = error_messages;
//_cfg_log_level = _log_level;
}
#else
2019-08-18 20:54:10 +00:00
void setLoggingPrms(int level , bool deamonized, char* log_file, char *error_messages)
2017-12-30 20:12:01 +00:00
{
_log_level = level;
_daemonise = deamonized;
2019-08-18 20:54:10 +00:00
_loq_display_message = error_messages;
2019-08-25 20:57:51 +00:00
_cfg_log_level = _log_level;
_cfg_log2file = _log2file;
2017-12-30 20:12:01 +00:00
if (log_file == NULL || strlen(log_file) <= 0) {
_log2file = false;
} else {
_log2file = true;
_log_filename = log_file;
//strcpy(_log_filename, log_file);
}
}
2023-07-08 16:14:44 +00:00
#endif // AQ_MANAGER
2017-12-30 20:12:01 +00:00
2023-06-20 00:15:45 +00:00
void setSystemLogLevel( int level)
{
_log_level = level;
}
int getSystemLogLevel()
{
return _log_level;
}
2020-07-18 16:37:19 +00:00
int getLogLevel(int16_t from)
2017-12-30 20:12:01 +00:00
{
2020-08-28 19:12:38 +00:00
// RSSD_LOG should default to INFO unless the mask is explicitly set.
// IE Even if DEBUG is set, (Note ignored for the moment)
2023-05-30 23:14:04 +00:00
if ( from == RSSD_LOG && ((_logforcemask & from) == from ) && _log_level < LOG_DEBUG_SERIAL)
return LOG_DEBUG_SERIAL;
else if ( ((_logforcemask & from) == from ) && _log_level < LOG_DEBUG_SERIAL)
2020-07-18 16:37:19 +00:00
return LOG_DEBUG;
2017-12-30 20:12:01 +00:00
return _log_level;
}
2023-06-23 20:16:30 +00:00
#ifdef AQ_MANAGER
2023-06-24 00:38:59 +00:00
/*
2023-06-20 00:15:45 +00:00
void startInlineLog2File()
2020-06-01 00:35:17 +00:00
{
2023-06-20 00:15:45 +00:00
_log2file = true;
2020-06-01 00:35:17 +00:00
if (_log_filename == NULL)
_log_filename = DEFAULT_LOG_FILE;
}
2023-06-20 00:15:45 +00:00
void stopInlineLog2File()
2019-08-25 20:57:51 +00:00
{
_log2file = _cfg_log2file;
}
2023-06-23 20:16:30 +00:00
void cleanInlineLog2File() {
2023-06-20 00:15:45 +00:00
if (_log_filename != NULL) {
fclose(fopen(_log_filename, "w"));
}
}
2023-06-24 00:38:59 +00:00
*/
2023-06-23 20:16:30 +00:00
#else // AQ_MANAGER
2023-06-20 00:15:45 +00:00
void startInlineDebug()
{
_log_level = LOG_DEBUG;
_log2file = true;
if (_log_filename == NULL)
_log_filename = DEFAULT_LOG_FILE;
}
void startInlineSerialDebug()
{
_log_level = LOG_DEBUG_SERIAL;
_log2file = true;
if (_log_filename == NULL)
_log_filename = DEFAULT_LOG_FILE;
}
void stopInlineDebug()
{
_log_level = _cfg_log_level;
_log2file = _cfg_log2file;
}
2019-08-25 20:57:51 +00:00
void cleanInlineDebug() {
if (_log_filename != NULL) {
fclose(fopen(_log_filename, "w"));
}
}
2023-06-23 20:16:30 +00:00
char *getInlineLogFName()
{
return _log_filename;
}
bool islogFileReady()
{
if (_log_filename != NULL) {
struct stat st;
stat(_log_filename, &st);
if ( st.st_size > 0)
return true;
}
return false;
}
2023-06-24 00:38:59 +00:00
#endif // AQ_MANAGER
2023-06-20 00:15:45 +00:00
2019-08-25 20:57:51 +00:00
2017-12-30 20:12:01 +00:00
/*
2023-07-08 16:14:44 +00:00
* This function reports the last error
2017-12-30 20:12:01 +00:00
*/
2023-07-08 16:14:44 +00:00
void LOGSystemError (int errnum, int16_t from, const char *on_what)
{
fputs (strerror (errno), stderr);
fputs (": ", stderr);
fputs (on_what, stderr);
fputc ('\n', stderr);
if (_daemonise == TRUE)
{
//logMessage (LOG_ERR, "%d : %s", errno, on_what);
LOG(AQUA_LOG, LOG_ERR, "%s (%d) : %s\n", strerror (errno), errno, on_what);
closelog ();
}
}
2017-12-30 20:12:01 +00:00
void displayLastSystemError (const char *on_what)
{
2023-07-08 16:14:44 +00:00
LOGSystemError(errno, AQUA_LOG, on_what);
/*
2017-12-30 20:12:01 +00:00
fputs (strerror (errno), stderr);
fputs (": ", stderr);
fputs (on_what, stderr);
fputc ('\n', stderr);
if (_daemonise == TRUE)
{
2020-07-26 19:28:58 +00:00
//logMessage (LOG_ERR, "%d : %s", errno, on_what);
2023-07-08 16:14:44 +00:00
LOG(AQUA_LOG, LOG_ERR, "%s (%d) : %s", strerror (errno), errno, on_what);
2017-12-30 20:12:01 +00:00
closelog ();
}
2023-07-08 16:14:44 +00:00
*/
2017-12-30 20:12:01 +00:00
}
/*
From -- syslog.h --
#define LOG_EMERG 0 // system is unusable
#define LOG_ALERT 1 // action must be taken immediately
#define LOG_CRIT 2 // critical conditions
#define LOG_ERR 3 // error conditions
#define LOG_WARNING 4 // warning conditions
#define LOG_NOTICE 5 // normal but significant condition
#define LOG_INFO 6 // informational
#define LOG_DEBUG 7 // debug-level messages
*/
char *elevel2text(int level)
{
switch(level) {
case LOG_ERR:
return "Error:";
break;
case LOG_WARNING:
return "Warning:";
break;
case LOG_NOTICE:
return "Notice:";
break;
case LOG_INFO:
return "Info:";
break;
case LOG_DEBUG:
default:
return "Debug:";
break;
}
return "";
}
int text2elevel(char* level)
{
2018-03-05 23:52:42 +00:00
if (strcmp(level, "DEBUG_SERIAL") == 0) {
return LOG_DEBUG_SERIAL;
}
2017-12-30 20:12:01 +00:00
if (strcmp(level, "DEBUG") == 0) {
return LOG_DEBUG;
}
else if (strcmp(level, "INFO") == 0) {
return LOG_INFO;
}
else if (strcmp(level, "WARNING") == 0) {
return LOG_WARNING;
}
else if (strcmp(level, "NOTICE") == 0) {
return LOG_NOTICE;
}
2020-08-28 19:12:38 +00:00
return LOG_ERR;
2017-12-30 20:12:01 +00:00
}
2023-06-20 00:15:45 +00:00
const char* loglevel2name(int level)
{
if (level == LOG_DEBUG_SERIAL)
return "Debug Serial:";
return elevel2text(level);
}
2020-07-26 19:28:58 +00:00
const char* logmask2name(int16_t from)
{
switch (from) {
case NET_LOG:
return "NetService:";
break;
case AQRS_LOG:
return "RS Allbtn: ";
break;
2020-08-28 19:12:38 +00:00
case RSSA_LOG:
return "RS SAdptr: ";
break;
2020-07-26 19:28:58 +00:00
case ONET_LOG:
return "One Touch: ";
break;
case IAQT_LOG:
return "iAQ Touch: ";
break;
case PDA_LOG:
return "PDA: ";
break;
case DJAN_LOG:
return "JandyDvce: ";
break;
case DPEN_LOG:
return "PentaDvce: ";
break;
case RSSD_LOG:
return "RS Serial: ";
break;
2020-08-28 19:12:38 +00:00
case PROG_LOG:
return "Panl Prog: ";
break;
2020-07-26 19:28:58 +00:00
case DBGT_LOG:
return "AQ Timing: ";
break;
2023-05-14 21:35:13 +00:00
case TIMR_LOG:
return "Schd/Timer:";
break;
2024-04-19 19:06:00 +00:00
case SIM_LOG:
return "Simulator: ";
break;
2020-07-26 19:28:58 +00:00
case AQUA_LOG:
default:
return "AqualinkD: ";
break;
}
}
2017-12-30 20:12:01 +00:00
void timestamp(char* time_string)
{
time_t now;
struct tm *tmptr;
time(&now);
tmptr = localtime(&now);
strftime(time_string, TIMESTAMP_LENGTH, "%b-%d-%y %H:%M:%S %p ", tmptr);
}
2018-03-16 17:18:15 +00:00
//Move existing pointer
2017-12-30 20:12:01 +00:00
char *cleanwhitespace(char *str)
{
char *end;
2023-05-14 21:35:13 +00:00
if (str == NULL)
return str;
2017-12-30 20:12:01 +00:00
// Trim leading space
while(isspace(*str)) str++;
if(*str == 0) // All spaces?
return str;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
2018-03-16 17:18:15 +00:00
// Return new pointer
char *stripwhitespace(char *str)
{
2019-06-05 16:41:38 +00:00
// Should probably just call Trim and Chop functions.
2018-03-16 17:18:15 +00:00
char *end;
char *start = str;
2020-07-18 16:37:19 +00:00
if(*start == 0 || strlen(str) <= 0 ) // All spaces?
return str;
2018-03-16 17:18:15 +00:00
// Trim leading space
while(isspace(*start)) start++;
if(*start == 0) // All spaces?
return start;
// Trim trailing space
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return start;
}
2019-06-05 16:41:38 +00:00
// Trim whispace (return new pointer) leave trailing whitespace
char *trimwhitespace(char *str)
{
char *start = str;
// Trim leading space
while(isspace(*start)) start++;
if(*start == 0) // All spaces?
return start;
return start;
}
char *chopwhitespace(char *str)
{
char *end;
end = str + strlen(str) - 1;
while(end > str && isspace(*end)) end--;
// Write new null terminator
*(end+1) = 0;
return str;
}
2017-12-30 20:12:01 +00:00
/*
char *cleanquotes(char *str)
{
char *end;
// Trim leading whitespace
//while(isspace(*str)) str++;
//if(*str == 0) // All spaces?
// return str;
syslog(LOG_INFO, "String to clean %s\n", str);
while(*str=='"' || *str== '\'' || *str==' ') str++;
if(*str == 0) // All spaces
return str;
end = str + strlen(str) - 1;
while(end > str && (*end=='"' || *end== '\'' || *end==' ')) end--;
// Write new null terminator
*(end+1) = 0;
syslog(LOG_INFO, "String cleaned %s\n", str);
return str;
}
*/
int cleanint(char*str)
{
if (str == NULL)
return 0;
str = cleanwhitespace(str);
return atoi(str);
}
2020-08-28 19:12:38 +00:00
/*
2017-12-30 20:12:01 +00:00
void test(int msg_level, char *msg)
{
char buffer[256];
sprintf(buffer,"Level %d | MsgLvl %d | Dmn %d | LF %d | %s - %s",_log_level,msg_level,_daemonise,_log2file,_log_filename,msg);
if ( buffer[strlen(buffer)-1] != '\n') {
strcat(buffer, "\n");
}
int fp = open("/var/log/aqualink.log", O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fp != -1) {
write(fp, buffer, strlen(buffer) );
close(fp);
} else {
2020-08-28 19:12:38 +00:00
syslog(LOG_ERR, "Can't open file /var/log/aqualink.log/n");
2017-12-30 20:12:01 +00:00
}
}
2020-08-28 19:12:38 +00:00
*/
2019-08-23 23:14:14 +00:00
2020-07-18 16:37:19 +00:00
void addDebugLogMask(int16_t flag)
{
_logforcemask |= flag;
}
2020-08-28 19:12:38 +00:00
void removeDebugLogMask(int16_t flag)
{
_logforcemask &= ~flag;
}
2023-06-20 00:15:45 +00:00
void clearDebugLogMask()
{
_logforcemask = 0;
}
bool isDebugLogMaskSet(int16_t flag)
{
return _logforcemask & flag;
}
2020-07-18 16:37:19 +00:00
void _LOG(int16_t from, int msg_level, char * message);
2020-07-26 19:28:58 +00:00
/*
2020-07-18 16:37:19 +00:00
void logMessage(int msg_level, const char *format, ...)
2017-12-30 20:12:01 +00:00
{
2019-08-23 23:14:14 +00:00
if (msg_level > _log_level) {
return;
}
2019-06-07 23:41:48 +00:00
char buffer[1024];
2017-12-30 20:12:01 +00:00
va_list args;
va_start(args, format);
2020-07-18 16:37:19 +00:00
strncpy(buffer, " ", 20);
vsprintf (&buffer[20], format, args);
2017-12-30 20:12:01 +00:00
va_end(args);
2020-06-01 00:35:17 +00:00
2020-07-18 16:37:19 +00:00
_LOG(AQUA_LOG, msg_level, buffer);
}
2020-07-26 19:28:58 +00:00
*/
2023-06-04 21:17:48 +00:00
2020-07-18 16:37:19 +00:00
void LOG(int16_t from, int msg_level, const char * format, ...)
{
2020-08-28 19:12:38 +00:00
//printf("msg_level=%d _log_level=%d mask=%d\n",msg_level,_log_level,(_logforcemask & from));
/*
2020-07-18 16:37:19 +00:00
if (msg_level > _log_level && ((_logforcemask & from) == 0) ) { // from NOT set in _logforcemask
return;
2020-06-06 16:36:04 +00:00
}
2020-08-28 19:12:38 +00:00
*/
if ( msg_level > getLogLevel(from))
return;
2020-06-06 16:36:04 +00:00
2023-05-14 21:35:13 +00:00
char buffer[LOGBUFFER];
2020-07-18 16:37:19 +00:00
va_list args;
va_start(args, format);
strncpy(buffer, " ", 20);
2023-05-16 22:27:45 +00:00
//vsprintf (&buffer[20], format, args);
int size = vsnprintf (&buffer[20], LOGBUFFER-30, format, args);
2020-07-18 16:37:19 +00:00
va_end(args);
2023-05-16 22:27:45 +00:00
if (size >= LOGBUFFER-30 ) {
sprintf(&buffer[LOGBUFFER-11], ".........\n");
}
2020-07-18 16:37:19 +00:00
_LOG(from, msg_level, buffer);
}
2020-07-26 19:28:58 +00:00
void _LOG(int16_t from, int msg_level, char *message)
2020-07-18 16:37:19 +00:00
{
int i;
2020-06-06 16:36:04 +00:00
2020-07-18 16:37:19 +00:00
// Make all printable chars
2023-05-14 21:35:13 +00:00
for(i = 8; i+8 < strlen(&message[8]) && i < LOGBUFFER; i++) {
2020-07-18 16:37:19 +00:00
if ( (message[i] < 32 || message[i] > 125) && message[i] != 10 ) {
//printf ("Change %c to %c in %s\n",message[i], ' ', message);
message[i] = ' ';
}
}
2023-05-17 17:51:00 +00:00
// Add return to end of string if not already their.
// NSF need to come back to this, doesn;t always work
2020-07-18 16:37:19 +00:00
if (message[i] != '\n') {
message[i] = '\n';
message[i+1] = '\0';
}
2020-07-26 19:28:58 +00:00
//strncpy(&message[9], logmask2name(from), 11); // Will give compiller warning as doesn;t realise we are copying into middle of string.
memcpy(&message[9], logmask2name(from), 11);
2019-08-23 23:14:14 +00:00
// Logging has not been setup yet, so STD error & syslog
2017-12-30 20:12:01 +00:00
if (_log_level == -1) {
2023-05-14 21:35:13 +00:00
fprintf (stderr, "%s\n", message);
2020-07-18 16:37:19 +00:00
syslog (msg_level, "%s", &message[9]);
2017-12-30 20:12:01 +00:00
closelog ();
}
2023-07-08 16:14:44 +00:00
#ifdef AQ_MANAGER // Always use systemd journel with aqmanager
2023-06-23 20:16:30 +00:00
//sd_journal_print()
//openlog("aqualinkd", 0, LOG_DAEMON);
if (msg_level > LOG_DEBUG) // Let's not confuse syslog with custom levels
sd_journal_print (LOG_DEBUG, "%s", &message[9]);
//sd_journal_print_with_location(LOG_DEBUG, "aqualinkd", "%s", &message[9]);
else
sd_journal_print (msg_level, "%s", &message[9]);
//sd_journal_print_with_location(msg_level, "aqualinkd", "%s", &message[9]);
//closelog ();
#else
2017-12-30 20:12:01 +00:00
if (_daemonise == TRUE)
{
if (msg_level > LOG_DEBUG) // Let's not confuse syslog with custom levels
2020-07-18 16:37:19 +00:00
syslog (LOG_DEBUG, "%s", &message[9]);
else
2020-07-18 16:37:19 +00:00
syslog (msg_level, "%s", &message[9]);
2017-12-30 20:12:01 +00:00
closelog ();
//return;
}
2023-07-08 16:14:44 +00:00
#endif //AQ_MANAGER
2017-12-30 20:12:01 +00:00
2020-06-06 16:36:04 +00:00
//int len;
2020-07-18 16:37:19 +00:00
message[8] = ' ';
2019-08-23 23:14:14 +00:00
char *strLevel = elevel2text(msg_level);
2023-05-17 17:51:00 +00:00
//strncpy(message, strLevel, strlen(strLevel)); // Will give compiler warning, so use memcpy instead
memcpy(message, strLevel, strlen(strLevel));
2020-07-18 16:37:19 +00:00
//len = strlen(message);
2020-06-06 16:36:04 +00:00
/*
2020-07-18 16:37:19 +00:00
if ( message[len-1] != '\n') {
strcat(message, "\n");
2019-08-23 23:14:14 +00:00
}
2020-06-06 16:36:04 +00:00
*/
2019-08-18 20:54:10 +00:00
2023-06-23 20:16:30 +00:00
// Superceded systemd/sd-journal
//with Send logs to any websocket that's interested.
//broadcast_log(message);
2023-06-04 21:17:48 +00:00
2019-08-23 23:14:14 +00:00
// Sent the log to the UI if configured.
2020-06-06 16:36:04 +00:00
if (msg_level <= LOG_ERR && _loq_display_message != NULL) {
2023-05-14 21:35:13 +00:00
snprintf(_loq_display_message, 127, "%s\n",message);
2023-06-04 21:17:48 +00:00
}
2019-08-18 20:54:10 +00:00
2023-07-08 16:14:44 +00:00
#ifndef AQ_MANAGER
2019-08-23 23:14:14 +00:00
if (_log2file == TRUE && _log_filename != NULL) {
2017-12-30 20:12:01 +00:00
char time[TIMESTAMP_LENGTH];
int fp = open(_log_filename, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if (fp != -1) {
timestamp(time);
2023-05-17 17:51:00 +00:00
if ( write(fp, time, strlen(time) ) == -1 ||
2023-05-23 02:12:12 +00:00
write(fp, message, strlen(message) ) == -1 )
2023-05-17 17:51:00 +00:00
{
syslog(LOG_ERR, "Can't write to log file %s\n %s", _log_filename, message);
fprintf (stderr, "Can't write to log file %s\n %s", _log_filename, message);
}
//write(fp, time, strlen(time) );
//write(fp, message, strlen(message) );
2017-12-30 20:12:01 +00:00
close(fp);
} else {
if (_daemonise == TRUE)
2023-05-17 17:51:00 +00:00
syslog(LOG_ERR, "Can't open log file %s\n %s", _log_filename, message);
2017-12-30 20:12:01 +00:00
else
2023-05-17 17:51:00 +00:00
fprintf (stderr, "Can't open debug log %s\n %s", _log_filename, message);
2017-12-30 20:12:01 +00:00
}
}
2023-07-08 16:14:44 +00:00
#endif //AQ_MANAGER
2017-12-30 20:12:01 +00:00
if (_daemonise == FALSE) {
if (msg_level == LOG_ERR) {
2020-07-18 16:37:19 +00:00
fprintf(stderr, "%s", message);
2017-12-30 20:12:01 +00:00
} else {
2019-06-27 22:18:44 +00:00
#ifndef AD_DEBUG
2020-07-18 16:37:19 +00:00
printf("%s", message);
2019-06-27 22:18:44 +00:00
#else
struct timespec tspec;
struct tm localtm;
clock_gettime(CLOCK_REALTIME, &tspec);
char timeStr[TIMESTAMP_LENGTH];
strftime(timeStr, sizeof(timeStr), "%H:%M:%S", localtime_r(&tspec.tv_sec, &localtm));
2020-07-18 16:37:19 +00:00
printf("%s.%03ld %s", timeStr, tspec.tv_nsec / 1000000L, message);
2019-06-27 22:18:44 +00:00
#endif
2017-12-30 20:12:01 +00:00
}
}
}
void daemonise (char *pidFile, void (*main_function) (void))
{
FILE *fp = NULL;
pid_t process_id = 0;
pid_t sid = 0;
_daemonise = true;
/* Check we are root */
if (getuid() != 0)
{
2020-07-26 19:28:58 +00:00
LOG(AQUA_LOG, LOG_ERR,"Can only be run as root\n");
2017-12-30 20:12:01 +00:00
exit(EXIT_FAILURE);
}
int pid_file = open (pidFile, O_CREAT | O_RDWR, 0666);
int rc = flock (pid_file, LOCK_EX | LOCK_NB);
if (rc)
{
2019-06-05 16:41:38 +00:00
//if (EWOULDBLOCK == errno)
//; // another instance is running
2017-12-30 20:12:01 +00:00
//fputs ("\nAnother instance is already running\n", stderr);
2020-07-26 19:28:58 +00:00
LOG(AQUA_LOG, LOG_ERR,"\nAnother instance is already running\n");
2017-12-30 20:12:01 +00:00
exit (EXIT_FAILURE);
}
process_id = fork ();
// Indication of fork() failure
if (process_id < 0)
{
displayLastSystemError ("fork failed!");
// Return failure in exit status
exit (EXIT_FAILURE);
}
// PARENT PROCESS. Need to kill it.
if (process_id > 0)
{
fp = fopen (pidFile, "w");
if (fp == NULL)
2023-06-24 00:38:59 +00:00
LOG(AQUA_LOG, LOG_ERR,"can't write to PID file %s",pidFile);
2017-12-30 20:12:01 +00:00
else
2023-06-24 00:38:59 +00:00
fprintf(fp, "%d", process_id);
2017-12-30 20:12:01 +00:00
fclose (fp);
2020-07-26 19:28:58 +00:00
LOG(AQUA_LOG, LOG_DEBUG, "process_id of child process %d \n", process_id);
2017-12-30 20:12:01 +00:00
// return success in exit status
exit (EXIT_SUCCESS);
}
//unmask the file mode
umask (0);
//set new session
sid = setsid ();
if (sid < 0)
{
// Return failure
displayLastSystemError("Failed to fork process");
exit (EXIT_FAILURE);
}
// Change the current working directory to root.
2023-05-17 17:51:00 +00:00
if ( chdir ("/") == -1) {
LOG(AQUA_LOG, LOG_ERR,"Can't set working dir to /");
}
2017-12-30 20:12:01 +00:00
// Close stdin. stdout and stderr
close (STDIN_FILENO);
close (STDOUT_FILENO);
close (STDERR_FILENO);
// this is the first instance
(*main_function) ();
return;
}
int count_characters(const char *str, char character)
{
const char *p = str;
int count = 0;
do {
if (*p == character)
count++;
} while (*(p++));
return count;
}
bool text2bool(char *str)
{
str = cleanwhitespace(str);
if (strcasecmp (str, "YES") == 0 || strcasecmp (str, "ON") == 0)
return TRUE;
else
return FALSE;
}
2019-10-13 15:07:14 +00:00
bool request2bool(char *str)
{
str = cleanwhitespace(str);
if (strcasecmp (str, "YES") == 0 || strcasecmp (str, "ON") == 0 || atoi(str) == 1)
return TRUE;
else
return FALSE;
}
2017-12-30 20:12:01 +00:00
char *bool2text(bool val)
{
if(val == TRUE)
return "YES";
else
return "NO";
}
// (50°F - 32) x .5556 = 10°C
float degFtoC(float degF)
{
return ((degF-32) / 1.8);
}
// 30°C x 1.8 + 32 = 86°F
float degCtoF(float degC)
{
return (degC * 1.8 + 32);
}
#include <time.h>
2020-08-28 19:12:38 +00:00
void delay (unsigned int howLong) // Microseconds (1000000 = 1 second) miliseconds
2017-12-30 20:12:01 +00:00
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)(howLong / 1000) ;
sleeper.tv_nsec = (long)(howLong % 1000) * 1000000 ;
nanosleep (&sleeper, &dummy) ;
}
2020-08-28 19:12:38 +00:00
/*
// Same as above but can pass 0.5
void ndelay (float howLong) // Microseconds (1000000 = 1 second)
{
struct timespec sleeper, dummy ;
sleeper.tv_sec = (time_t)((howLong*10) / 10000) ;
sleeper.tv_nsec = (long)( ((int)(howLong*10)) % 10000) * 1000000 ;
2018-02-18 14:07:53 +00:00
2020-08-28 19:12:38 +00:00
nanosleep (&sleeper, &dummy) ;
}
*/
2018-02-18 14:07:53 +00:00
char* stristr(const char* haystack, const char* needle) {
do {
const char* h = haystack;
const char* n = needle;
while (tolower((unsigned char) *h) == tolower((unsigned char ) *n) && *n) {
h++;
n++;
}
if (*n == 0) {
return (char *) haystack;
}
} while (*haystack++);
return 0;
2018-07-29 14:31:35 +00:00
}
2020-07-18 16:37:19 +00:00
/*
2018-07-29 14:31:35 +00:00
int ascii(char *destination, char *source) {
2019-06-05 16:41:38 +00:00
unsigned int i;
2018-07-29 14:31:35 +00:00
for(i = 0; i < strlen(source); i++) {
2020-06-06 16:36:04 +00:00
//if ( source[i] >= 0 && source[i] < 128 )
if ( source[i] >= 0 && source[i] < 127 )
2018-07-29 14:31:35 +00:00
destination[i] = source[i];
else
destination[i] = ' ';
if ( source[i]==126 ) {
destination[i-1] = '<';
destination[i] = '>';
}
}
destination[i] = '\0';
return i;
2019-05-25 16:52:36 +00:00
}
2020-07-18 16:37:19 +00:00
*/
2019-05-25 16:52:36 +00:00
char *prittyString(char *str)
{
char *ptr = str;
char *end;
bool lastspace=true;
end = str + strlen(str) - 1;
while(end >= ptr){
//printf("%d %s ", *ptr, ptr);
if (lastspace && *ptr > 96 && *ptr < 123) {
*ptr = *ptr - 32;
lastspace=false;
//printf("to upper\n");
} else if (lastspace == false && *ptr > 54 && *ptr < 91) {
*ptr = *ptr + 32;
lastspace=false;
//printf("to lower\n");
} else if (*ptr == 32) {
lastspace=true;
//printf("space\n");
} else {
lastspace=false;
//printf("leave\n");
}
ptr++;
}
//printf("-- %s --\n", str);
return str;
}