Skip to content

Commit

Permalink
Migrate to syslog.
Browse files Browse the repository at this point in the history
  • Loading branch information
rozhuk-im committed Mar 15, 2024
1 parent eba9a1e commit f3d4456
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 65 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ cmake_minimum_required(VERSION 3.10)
project(ssdpd C)

set(PACKAGE_VERSION_MAJOR 1)
set(PACKAGE_VERSION_MINOR 08)
set(PACKAGE_VERSION_MINOR 9)
set(PACKAGE_VERSION_PATCH 0)

set(PACKAGE_NAME "${PROJECT_NAME}")
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ set(SSDPD_BIN ssdpd.c
liblcb/src/utils/sys.c
liblcb/src/utils/buf_str.c
liblcb/src/utils/info.c
liblcb/src/utils/log.c
liblcb/src/utils/xml.c)

add_executable(ssdpd ${SSDPD_BIN})
Expand Down
104 changes: 45 additions & 59 deletions src/ssdpd.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2011 - 2020 Rozhuk Ivan <[email protected]>
* Copyright (c) 2011-2024 Rozhuk Ivan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -40,14 +40,14 @@
#include <unistd.h> /* close, write, sysconf */
#include <fcntl.h> /* open, fcntl */
#include <signal.h> /* SIGNAL constants. */
#include <syslog.h>

#include "utils/macro.h"
#include "utils/mem_utils.h"
#include "utils/str2num.h"
#include "utils/xml.h"
#include "utils/buf_str.h"
#include "utils/sys.h"
#include "utils/log.h"
#include "utils/cmd_line_daemon.h"
#include "net/socket_address.h"
#include "net/utils.h"
Expand Down Expand Up @@ -76,7 +76,6 @@ main(int argc, char *argv[]) {
int error;
tp_p tp = NULL;
upnp_ssdp_p upnp_ssdp = NULL; /* UPnP SSDP server. */
int log_fd = -1;
cmd_line_data_t cmd_line_data;


Expand All @@ -88,9 +87,16 @@ main(int argc, char *argv[]) {
}
if (0 != cmd_line_data.daemon) {
make_daemon();
openlog(PACKAGE_NAME,
(LOG_NDELAY | LOG_PID | ((0 != cmd_line_data.verbose) ? LOG_PERROR : 0)),
LOG_DAEMON);
} else {
openlog(PACKAGE_NAME,
(LOG_NDELAY | LOG_PID | LOG_PERROR), LOG_USER);
}
setlogmask(LOG_UPTO(cmd_line_data.log_level));

{ // process config file
{ /* Process config file. */
const uint8_t *data, *ptm, *cur_pos, *cur_svc_pos, *cur_if_pos;
const uint8_t *ann_data, *svc_data;
uint8_t *cfg_file_buf, *cfg_dev_buf;
Expand All @@ -104,71 +110,51 @@ main(int argc, char *argv[]) {
size_t if_name_size, url4_size, url6_size;
upnp_ssdp_settings_t ssdp_st;
uint8_t url4_buf[1024], url6_buf[1024];
char strbuf[1024];
struct sockaddr_storage addr;
tp_settings_t tp_s;


g_log_fd = (uintptr_t)open("/dev/stderr", (O_WRONLY | O_APPEND));
error = read_file(cmd_line_data.cfg_file_name, 0, 0, 0,
CFG_FILE_MAX_SIZE, &cfg_file_buf, &cfg_file_buf_size);
if (0 != error) {
LOG_ERR(error, "config read_file()");
SYSLOG_ERR(LOG_CRIT, error, "Config read_file().");
goto err_out;
}
if (0 != xml_get_val_args(cfg_file_buf, cfg_file_buf_size,
NULL, NULL, NULL, NULL, NULL,
(const uint8_t*)"ssdpd", NULL)) {
LOG_INFO("Config file XML format invalid.");
syslog(LOG_CRIT, "Config file XML format invalid.");
goto err_out;
}

/* Log file */
if (0 == cmd_line_data.verbose &&
0 == SSDPD_CFG_GET_VAL_DATA(NULL, &data, &data_size,
"log", "file", NULL)) {
if (sizeof(strbuf) > data_size) {
memcpy(strbuf, data, data_size);
strbuf[data_size] = 0;
log_fd = open(strbuf,
(O_WRONLY | O_APPEND | O_CREAT), 0644);
if (-1 == log_fd) {
LOG_ERR(errno, "Fail to open log file.");
}
} else {
LOG_ERR(EINVAL, "Log file name too long.");
}
} else if (0 != cmd_line_data.verbose) {
log_fd = open("/dev/stdout", (O_WRONLY | O_APPEND));
/* Log level. */
if (0 == SSDPD_CFG_GET_VAL_UINT(NULL, (uint32_t*)&cmd_line_data.log_level,
"log", "level", NULL)) {
setlogmask(LOG_UPTO(cmd_line_data.log_level));
}
close((int)g_log_fd);
g_log_fd = (uintptr_t)log_fd;
fd_set_nonblocking(g_log_fd, 1);
log_write("\n\n\n\n", 4);
LOG_INFO(PACKAGE_STRING": started");
syslog(LOG_NOTICE, PACKAGE_STRING": started!");
#ifdef DEBUG
LOG_INFO("Build: "__DATE__" "__TIME__", DEBUG");
syslog(LOG_INFO, "Build: "__DATE__" "__TIME__", DEBUG.");
#else
LOG_INFO("Build: "__DATE__" "__TIME__", Release");
syslog(LOG_INFO, "Build: "__DATE__" "__TIME__", Release.");
#endif
LOG_INFO_FMT("CPU count: %d", get_cpu_count());
LOG_INFO_FMT("descriptor table size: %d (max files)", getdtablesize());
syslog(LOG_INFO, "CPU count: %d.", get_cpu_count());
syslog(LOG_INFO, "Descriptor table size: %d (max files).", getdtablesize());

/* Thread pool settings. */
tp_settings_def(&tp_s);
tp_s.flags = 0;
tp_s.threads_max = 1;
error = tp_create(&tp_s, &tp);
if (0 != error) {
LOG_ERR(error, "tp_create()");
SYSLOG_ERR(LOG_CRIT, error, "tp_create().");
goto err_out;
}
tp_threads_create(tp, 1); /* XXX exit rewrite. */


/* SSDP receiver. */
if (0 == SSDPD_CFG_CALC_VAL_COUNT("announceList", "announce", NULL)) {
LOG_INFO("no announce devices specified, nothink to do...");
syslog(LOG_NOTICE, "No announce devices specified, nothink to do...");
goto err_out;
}
/* Default values. */
Expand Down Expand Up @@ -205,7 +191,7 @@ main(int argc, char *argv[]) {
/* Create SSDP receiver. */
error = upnp_ssdp_create(tp, &ssdp_st, &upnp_ssdp);
if (0 != error) {
LOG_ERR(error, "upnp_ssdp_create()");
SYSLOG_ERR(LOG_CRIT, error, "upnp_ssdp_create().");
return (error);
}

Expand All @@ -223,15 +209,15 @@ main(int argc, char *argv[]) {
error = read_file((const char*)data, data_size, 0, 0,
CFG_FILE_MAX_SIZE, &cfg_dev_buf, &cfg_dev_buf_size);
if (0 != error) {
LOG_ERR(error, "xmlDevDescr read_file()");
SYSLOG_ERR(LOG_ERR, error, "xmlDevDescr read_file().");
continue;
}
/* Load device options and add. */
if (0 != xml_get_val_args(cfg_dev_buf, cfg_dev_buf_size,
NULL, NULL, NULL, &uuid, &tm,
(const uint8_t*)"root", "device", "UDN", NULL) ||
(5 + 36) != tm) { /* 5 = "uuid:", 36 = UPNP_UUID_SIZE */
LOG_ERR(EINVAL, "Invalid device UUID");
syslog(LOG_ERR, "Invalid device UUID.");
free(cfg_dev_buf);
continue;
}
Expand All @@ -240,7 +226,7 @@ main(int argc, char *argv[]) {
NULL, NULL, NULL, &data, &data_size,
(const uint8_t*)"root", "device", "deviceType", NULL)) {
no_dev_type:
LOG_ERR(EINVAL, "No deviceType");
syslog(LOG_ERR, "No deviceType.");
free(cfg_dev_buf);
continue;
}
Expand Down Expand Up @@ -272,7 +258,7 @@ main(int argc, char *argv[]) {
(uint32_t)time(NULL), config_id, max_age,
ann_interval, &dev);
if (0 != error) {
LOG_ERR(error, "upnp_ssdp_dev_add()");
SYSLOG_ERR(LOG_ERR, error, "upnp_ssdp_dev_add().");
free(cfg_dev_buf);
continue;
}
Expand All @@ -285,7 +271,7 @@ main(int argc, char *argv[]) {
NULL, NULL, &data, &data_size,
(const uint8_t*)"serviceType", NULL)) {
no_svc_type:
LOG_ERR(EINVAL, "No serviceType");
syslog(LOG_ERR, "No serviceType.");
continue;
}
/* Parse: "urn:schemas-upnp-org:service:ContentDirectory:3". */
Expand All @@ -306,7 +292,7 @@ main(int argc, char *argv[]) {
(const char*)domain_name, domain_name_size,
(const char*)type, type_size, ver);
if (0 != error) {
LOG_ERR(error, "upnp_ssdp_svc_add()");
SYSLOG_ERR(LOG_ERR, error, "upnp_ssdp_svc_add().");
continue;
}
}
Expand All @@ -328,25 +314,25 @@ main(int argc, char *argv[]) {
&if_name, &if_name_size,
(const uint8_t*)"ifName", NULL) ||
(0 == url4_size && 0 == url6_size)) {
LOG_ERR(EINVAL, "Bad device network interface");
syslog(LOG_ERR, "Bad device network interface.");
continue;
}
/* Autoreplace NULL addr to if addr. */
if (14 < url4_size && /* http://0.0.0.0... */
0 == mem_cmp_cstr("0.0.0.0", (url4 + 7)) &&
(':' == url4[14] || '\\' == url4[14])) {
LOG_INFO("Autoreplace NULL IPv4 addr to if addr.");
syslog(LOG_INFO, "Autoreplace NULL IPv4 addr to if addr.");
error = get_if_addr_by_name((const char*)if_name,
if_name_size, AF_INET, &addr);
if (0 != error) {
LOG_ERR(error, "get_if_addr_by_name()");
SYSLOG_ERR(LOG_ERR, error, "get_if_addr_by_name().");
continue;
}
error = sa_addr_to_str(&addr,
(char*)(url4_buf + 7),
(sizeof(url4_buf) - 8), &tm);
if (0 != error) {
LOG_ERR(error, "sa_addr_to_str()");
SYSLOG_ERR(LOG_ERR, error, "sa_addr_to_str().");
continue;
}
if (sizeof(url4_buf) > (url4_size + (tm - 7))) {
Expand All @@ -357,28 +343,28 @@ main(int argc, char *argv[]) {
url4 = url4_buf;
url4_size += (tm - 7);
url4_buf[url4_size] = 0;
LOG_INFO((const char*)url4);
syslog(LOG_INFO, "%s", (const char*)url4);
} else {
LOG_EV("URL to long, not enough buf space.");
syslog(LOG_ERR, "URL to long, not enough buf space.");
url4 = NULL;
url4_size = 0;
}
}
if (13 < url6_size && /* http://[::]... */
0 == mem_cmp_cstr("[::]", (url6 + 7)) &&
(':' == url6[11] || '\\' == url6[11])) {
LOG_INFO("Autoreplace NULL IPv6 addr to if addr.");
syslog(LOG_INFO, "Autoreplace NULL IPv6 addr to if addr.");
error = get_if_addr_by_name((const char*)if_name,
if_name_size, AF_INET6, &addr);
if (0 != error) {
LOG_ERR(error, "get_if_addr_by_name()");
SYSLOG_ERR(LOG_ERR, error, "get_if_addr_by_name().");
continue;
}
error = sa_addr_to_str(&addr,
(char*)(url6_buf + 7),
(sizeof(url6_buf) - 8), &tm);
if (0 != error) {
LOG_ERR(error, "sa_addr_to_str()");
SYSLOG_ERR(LOG_ERR, error, "sa_addr_to_str().");
continue;
}
if (sizeof(url6_buf) > (url6_size + (tm - 7))) {
Expand All @@ -389,9 +375,9 @@ main(int argc, char *argv[]) {
url6 = url6_buf;
url6_size += (tm - 4);
url6_buf[url6_size] = 0;
LOG_INFO((const char*)url6);
syslog(LOG_INFO, "%s", (const char*)url6);
} else {
LOG_EV("URL to long, not enough buf space.");
syslog(LOG_ERR, "URL to long, not enough buf space.");
url6 = NULL;
url6_size = 0;
}
Expand All @@ -402,7 +388,7 @@ main(int argc, char *argv[]) {
(const char*)url4, url4_size,
(const char*)url6, url6_size);
if (0 != error) {
LOG_ERR(error, "upnp_ssdp_dev_if_add()");
SYSLOG_ERR(LOG_WARNING, error, "upnp_ssdp_dev_if_add().");
continue;
}
}
Expand All @@ -413,7 +399,7 @@ main(int argc, char *argv[]) {

if (0 == upnp_ssdp_root_dev_count(upnp_ssdp) ||
0 == upnp_ssdp_if_count(upnp_ssdp)) {
LOG_INFO("no announce devices specified or no network ifaces, nothink to do...");
syslog(LOG_NOTICE, "No announce devices specified or no network ifaces, nothink to do...");
goto err_out;
}

Expand All @@ -434,8 +420,8 @@ main(int argc, char *argv[]) {
unlink(cmd_line_data.pid_file_name); /* Remove pid file. */
}
tp_destroy(tp);
LOG_INFO("exiting.");
close((int)g_log_fd);
syslog(LOG_NOTICE, "Exiting.");
closelog();

return (error);
}
Expand Down
2 changes: 1 addition & 1 deletion ssdpd.conf
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
-->
<ssdpd>
<log>
<file>/dev/stdout</file>
<level>6</level> <!-- syslog Severity level: 0=emerg - 7=debug. -->
</log>

<skt>
Expand Down
44 changes: 44 additions & 0 deletions ssdpd.conf.debug
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="utf-8"?>

<!--
Sizes in kb, time in seconds
-->
<ssdpd>
<log>
<level>6</level> <!-- syslog Severity level: 0=emerg - 7=debug. -->
</log>

<skt>
<rcvBuf>64</rcvBuf>
<sndBuf>64</sndBuf>
<hopLimitUnicast>1</hopLimitUnicast> <!-- For unicast. -->
<hopLimitMulticast>1</hopLimitMulticast> <!-- For multicast. -->
<hopLimit>1</hopLimit> <!-- For unicast and multicast if ni specific values set. -->
</skt>

<fEnableIPv4>yes</fEnableIPv4> <!-- Enable IPv4: 239.255.255.250:1900 -->
<fEnableIPv6>yes</fEnableIPv6> <!-- Enable IPv6: [FF02::C]:1900, [FF05::C]:1900 -->
<fEnableByebye>yes</fEnableByebye><!-- Enable send byebye on interface delete, daemon stop. -->
<httpServer></httpServer> <!-- Default (if empty/not defined): 'OS/version UPnP/1.1 product/version' -->

<announceList>
<announce> <!-- Announce device. -->
<xmlDevDescr>/home/rim/docs/Progs/ssdpd/www/upnp/descr/root.xml</xmlDevDescr>
<maxAge>1800</maxAge> <!-- seconds, announces life time in clients cache. -->
<interval>10</interval> <!-- seconds, send announces to network. -->
<ifList> <!-- Network Ifaces list. -->
<if>
<ifName>lo0</ifName>
<DevDescrURL4><![CDATA[http://127.0.0.1:80/upnp/descr/root.xml]]></DevDescrURL4>
<DevDescrURL6><![CDATA[http://[::1]:80/upnp/descr/root.xml]]></DevDescrURL6>
</if>
<if>
<ifName>lan0</ifName>
<DevDescrURL4><![CDATA[http://0.0.0.0:80/upnp/descr/root.xml]]></DevDescrURL4> <!-- 0.0.0.0 will be replaced to IPv4 if addr. -->
<DevDescrURL6><![CDATA[http://[::]:80/upnp/descr/root.xml]]></DevDescrURL6> <!-- [::] will be replaced to IPv6 if addr. -->
</if>
</ifList>
</announce>
<!-- Announce next device... -->
</announceList>
</ssdpd>
3 changes: 1 addition & 2 deletions ssdpd.project
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
<File Name="src/liblcb/src/threadpool/threadpool_task.c"/>
</VirtualDirectory>
<VirtualDirectory Name="utils">
<File Name="src/liblcb/src/utils/log.c"/>
<File Name="src/liblcb/src/utils/xml.c"/>
<File Name="src/liblcb/src/utils/info.c"/>
<File Name="src/liblcb/src/utils/cmd_line_daemon.c"/>
Expand All @@ -77,7 +76,6 @@
<VirtualDirectory Name="utils">
<File Name="src/liblcb/include/utils/mem_utils.h"/>
<File Name="src/liblcb/include/utils/macro.h"/>
<File Name="src/liblcb/include/utils/log.h"/>
<File Name="src/liblcb/include/utils/io_buf.h"/>
<File Name="src/liblcb/include/utils/str2num.h"/>
<File Name="src/liblcb/include/utils/strh2num.h"/>
Expand Down Expand Up @@ -106,6 +104,7 @@
<Description/>
<Dependencies/>
<VirtualDirectory Name="root">
<File Name="ssdpd.conf.debug"/>
<File Name="readme.md"/>
<File Name="test.txt"/>
<File Name="ssdpd.conf"/>
Expand Down

0 comments on commit f3d4456

Please sign in to comment.