added HAWK_SKAD_TO_OOCSTR_UNIXAT. this changes the behaivor of skad to string conversion.

updated string to skad converter to handle a unix socket starting with a slash
This commit is contained in:
hyung-hwan 2023-06-29 17:51:14 +09:00
parent 05509d8e96
commit b6e6e96578
3 changed files with 75 additions and 61 deletions

View File

@ -31,10 +31,13 @@
#define HAWK_SKAD_TO_OOCSTR_ADDR (1 << 0) #define HAWK_SKAD_TO_OOCSTR_ADDR (1 << 0)
#define HAWK_SKAD_TO_OOCSTR_PORT (1 << 1) #define HAWK_SKAD_TO_OOCSTR_PORT (1 << 1)
#define HAWK_SKAD_TO_UCSTR_ADDR HAWK_SKAD_TO_OOCSTR_ADDR #define HAWK_SKAD_TO_OOCSTR_UNIXAT (1 << 2) /* prefix the unix address string with @ */
#define HAWK_SKAD_TO_UCSTR_PORT HAWK_SKAD_TO_OOCSTR_PORT #define HAWK_SKAD_TO_UCSTR_ADDR HAWK_SKAD_TO_OOCSTR_ADDR
#define HAWK_SKAD_TO_BCSTR_ADDR HAWK_SKAD_TO_OOCSTR_ADDR #define HAWK_SKAD_TO_UCSTR_PORT HAWK_SKAD_TO_OOCSTR_PORT
#define HAWK_SKAD_TO_BCSTR_PORT HAWK_SKAD_TO_OOCSTR_PORT #define HAWK_SKAD_TO_UCSTR_UNIXAT HAWK_SKAD_TO_OOCSTR_UNIXAT
#define HAWK_SKAD_TO_BCSTR_ADDR HAWK_SKAD_TO_OOCSTR_ADDR
#define HAWK_SKAD_TO_BCSTR_PORT HAWK_SKAD_TO_OOCSTR_PORT
#define HAWK_SKAD_TO_BCSTR_UNIXAT HAWK_SKAD_TO_OOCSTR_UNIXAT
typedef struct hawk_ifcfg_t hawk_ifcfg_t; typedef struct hawk_ifcfg_t hawk_ifcfg_t;

View File

@ -4502,8 +4502,9 @@ local://xxx opens the local syslog using the openlog() call in the libc library.
openlog() affects the entire process. openlog() affects the entire process.
to open /dev/log on the local system for the current running context, to open /dev/log on the local system for the current running context,
you can specify the remote:// with /dev/log. you can specify the remote:// with /dev/log or @/dev/log.
sys::openlog("remote:///dev/log/xxx", sys::LOG_OPT_PID | sys::LOG_OPT_NDELAY, sys::LOG_FAC_LOCAL0); sys::openlog("remote:///dev/log/xxx", sys::LOG_OPT_PID | sys::LOG_OPT_NDELAY, sys::LOG_FAC_LOCAL0);
sys::openlog("remote://@/dev/log/xxx", sys::LOG_OPT_PID | sys::LOG_OPT_NDELAY, sys::LOG_FAC_LOCAL0);
*/ */
static void open_remote_log_socket (hawk_rtx_t* rtx, rtx_data_t* rdp) static void open_remote_log_socket (hawk_rtx_t* rtx, rtx_data_t* rdp)
{ {
@ -4586,7 +4587,7 @@ static int fnc_openlog (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
hawk_comp_oocstr_limited(ident, HAWK_T("sock://"), (pfxlen = 7), 0) == 0) hawk_comp_oocstr_limited(ident, HAWK_T("sock://"), (pfxlen = 7), 0) == 0)
{ {
hawk_ooch_t* slash; hawk_ooch_t* slash;
/* "remote://<target-address>/syslog-identifier" /* "remote://<target-address>/syslog-identifier"
* "sock://<target-address>/syslog-identifier" * "sock://<target-address>/syslog-identifier"
*/ */
@ -4726,7 +4727,6 @@ static int fnc_closelog (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
return 0; return 0;
} }
static int fnc_writelog (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi) static int fnc_writelog (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
{ {
hawk_int_t rx = ERRNUM_TO_RC(HAWK_EOTHER); hawk_int_t rx = ERRNUM_TO_RC(HAWK_EOTHER);
@ -4780,62 +4780,73 @@ static int fnc_writelog (hawk_rtx_t* rtx, const hawk_fnc_info_t* fi)
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
}; };
if (rdp->log.sck <= -1) open_remote_log_socket (rtx, rdp); hawk_ntime_t now;
struct tm tm, * tmx;
time_t t;
if (rdp->log.sck >= 0) if (rdp->log.sck <= -1)
{ {
hawk_ntime_t now; open_remote_log_socket (rtx, rdp);
struct tm tm, * tmx; if (rdp->log.sck <= -1)
time_t t;
if (!rdp->log.dmsgbuf)
{ {
rdp->log.dmsgbuf = hawk_becs_open(hawk_rtx_getgem(rtx), 0, 0); rx = set_error_on_sys_list_with_errno(rtx, sys_list, HAWK_T("unable to open log socket"));
if (!rdp->log.dmsgbuf) goto fail;
}
if (hawk_get_ntime(&now) <= -1)
{
rx = set_error_on_sys_list(rtx, sys_list, HAWK_ESYSERR, HAWK_T("unable to get time"));
goto done; goto done;
} }
}
t = now.sec; if (!rdp->log.dmsgbuf)
#if defined(HAVE_LOCALTIME_R) {
tmx = localtime_r(&t, &tm); rdp->log.dmsgbuf = hawk_becs_open(hawk_rtx_getgem(rtx), 0, 0);
#else if (!rdp->log.dmsgbuf) goto fail;
tmx = localtime(&t); }
#endif
if (!tmx)
{
rx = set_error_on_sys_list_with_errno(rtx, sys_list, HAWK_T("unable to get local time"));
goto done;
}
if (hawk_becs_fmt( if (hawk_get_ntime(&now) <= -1)
rdp->log.dmsgbuf, HAWK_BT("<%d>%hs %02d %02d:%02d:%02d "), {
(int)(rdp->log.fac | pri), rx = set_error_on_sys_list(rtx, sys_list, HAWK_ESYSERR, HAWK_T("unable to get time"));
__syslog_month_names[tmx->tm_mon], tmx->tm_mday, goto done;
tmx->tm_hour, tmx->tm_min, tmx->tm_sec) == (hawk_oow_t)-1) goto fail; }
if (rdp->log.ident || (rdp->log.opt & LOG_PID)) t = now.sec;
{ #if defined(HAVE_LOCALTIME_R)
/* if the identifier is set or LOG_PID is set, the produced tag won't be empty. tmx = localtime_r(&t, &tm);
* so appending ':' is kind of ok */ #else
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("%hs"), (rdp->log.ident? rdp->log.ident: HAWK_BT(""))) == (hawk_oow_t)-1) goto fail; tmx = localtime(&t);
if ((rdp->log.opt & LOG_PID) && hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("[%d]"), (int)HAWK_GETPID()) == (hawk_oow_t)-1) goto fail; #endif
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT(": ")) == (hawk_oow_t)-1) goto fail; if (!tmx)
} {
rx = set_error_on_sys_list_with_errno(rtx, sys_list, HAWK_T("unable to get local time"));
goto done;
}
#if defined(HAWK_OOCH_IS_BCH) if (hawk_becs_fmt(
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("%hs"), msg) == (hawk_oow_t)-1) goto fail; rdp->log.dmsgbuf, HAWK_BT("<%d>%hs %02d %02d:%02d:%02d "),
#else (int)(rdp->log.fac | pri),
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("%ls"), msg) == (hawk_oow_t)-1) goto fail; __syslog_month_names[tmx->tm_mon], tmx->tm_mday,
#endif tmx->tm_hour, tmx->tm_min, tmx->tm_sec) == (hawk_oow_t)-1) goto fail;
/* don't care about output failure */ if (rdp->log.ident || (rdp->log.opt & LOG_PID))
sendto (rdp->log.sck, HAWK_BECS_PTR(rdp->log.dmsgbuf), HAWK_BECS_LEN(rdp->log.dmsgbuf), {
0, (struct sockaddr*)&rdp->log.skad, hawk_skad_get_size(&rdp->log.skad)); /* if the identifier is set or LOG_PID is set, the produced tag won't be empty.
* so appending ':' is kind of ok */
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("%hs"), (rdp->log.ident? rdp->log.ident: HAWK_BT(""))) == (hawk_oow_t)-1) goto fail;
if ((rdp->log.opt & LOG_PID) && hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("[%d]"), (int)HAWK_GETPID()) == (hawk_oow_t)-1) goto fail;
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT(": ")) == (hawk_oow_t)-1) goto fail;
}
#if defined(HAWK_OOCH_IS_BCH)
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("%hs"), msg) == (hawk_oow_t)-1) goto fail;
#else
if (hawk_becs_fcat(rdp->log.dmsgbuf, HAWK_BT("%ls"), msg) == (hawk_oow_t)-1) goto fail;
#endif
/* don't care about output failure */
if (sendto(rdp->log.sck, HAWK_BECS_PTR(rdp->log.dmsgbuf), HAWK_BECS_LEN(rdp->log.dmsgbuf),
0, (struct sockaddr*)&rdp->log.skad, hawk_skad_get_size(&rdp->log.skad)) <= -1)
{
hawk_ooch_t msg[256] = HAWK_T("unable to write to log socket ");
hawk_gem_skadtooocstr(hawk_rtx_getgem(rtx), &rdp->log.skad, &msg[30], HAWK_COUNTOF(msg) - 30, HAWK_SKAD_TO_OOCSTR_ADDR | HAWK_SKAD_TO_OOCSTR_PORT);
rx = set_error_on_sys_list_with_errno(rtx, sys_list, msg);
goto done;
} }
#endif #endif
} }

View File

@ -344,14 +344,14 @@ int hawk_gem_ucharstoskad (hawk_gem_t* gem, const hawk_uch_t* str, hawk_oow_t le
/* use HAWK_SIZEOF(*_skad) instead of HAWK_SIZEOF(*skad) in case they are different */ /* use HAWK_SIZEOF(*_skad) instead of HAWK_SIZEOF(*skad) in case they are different */
HAWK_MEMSET (skad, 0, HAWK_SIZEOF(*_skad)); HAWK_MEMSET (skad, 0, HAWK_SIZEOF(*_skad));
if (*p == '@') if (*p == '@' || *p == '/')
{ {
#if defined(AF_UNIX) && (HAWK_SIZEOF_STRUCT_SOCKADDR_UN > 0) #if defined(AF_UNIX) && (HAWK_SIZEOF_STRUCT_SOCKADDR_UN > 0)
/* @aaa, @/tmp/aaa ... */ /* @aaa, @/tmp/aaa ... */
hawk_oow_t srclen, dstlen; hawk_oow_t srclen, dstlen;
dstlen = HAWK_COUNTOF(skad->un.sun_path) - 1; dstlen = HAWK_COUNTOF(skad->un.sun_path) - 1;
srclen = len - 1; srclen = len - (*p == '@');
if (hawk_gem_convutobchars(gem, p + 1, &srclen, skad->un.sun_path, &dstlen) <= -1) return -1; if (hawk_gem_convutobchars(gem, p + (*p == '@'), &srclen, skad->un.sun_path, &dstlen) <= -1) return -1;
skad->un.sun_path[dstlen] = '\0'; skad->un.sun_path[dstlen] = '\0';
skad->un.sun_family = AF_UNIX; skad->un.sun_family = AF_UNIX;
return 0; return 0;
@ -564,11 +564,11 @@ int hawk_gem_bcharstoskad (hawk_gem_t* gem, const hawk_bch_t* str, hawk_oow_t le
/* use HAWK_SIZEOF(*_skad) instead of HAWK_SIZEOF(*skad) in case they are different */ /* use HAWK_SIZEOF(*_skad) instead of HAWK_SIZEOF(*skad) in case they are different */
HAWK_MEMSET (skad, 0, HAWK_SIZEOF(*_skad)); HAWK_MEMSET (skad, 0, HAWK_SIZEOF(*_skad));
if (*p == '@') if (*p == '@' || *p == '/')
{ {
#if defined(AF_UNIX) && (HAWK_SIZEOF_STRUCT_SOCKADDR_UN > 0) #if defined(AF_UNIX) && (HAWK_SIZEOF_STRUCT_SOCKADDR_UN > 0)
/* @aaa, @/tmp/aaa ... */ /* @aaa, @/tmp/aaa /tmp/aaa ... */
hawk_copy_bchars_to_bcstr (skad->un.sun_path, HAWK_COUNTOF(skad->un.sun_path), str + 1, len - 1); hawk_copy_bchars_to_bcstr (skad->un.sun_path, HAWK_COUNTOF(skad->un.sun_path), str + (*p == '@'), len - (*p == '@'));
skad->un.sun_family = AF_UNIX; skad->un.sun_family = AF_UNIX;
return 0; return 0;
#else #else
@ -1015,7 +1015,7 @@ hawk_oow_t hawk_gem_skadtoucstr (hawk_gem_t* gem, const hawk_skad_t* _skad, hawk
if (flags & HAWK_SKAD_TO_BCSTR_ADDR) if (flags & HAWK_SKAD_TO_BCSTR_ADDR)
{ {
if (xlen + 1 >= len) goto done; if (xlen + 1 >= len) goto done;
buf[xlen++] = '@'; if (flags & HAWK_SKAD_TO_BCSTR_UNIXAT) buf[xlen++] = '@';
if (xlen + 1 >= len) goto done; if (xlen + 1 >= len) goto done;
else else
@ -1265,7 +1265,7 @@ hawk_oow_t hawk_gem_skadtobcstr (hawk_gem_t* gem, const hawk_skad_t* _skad, hawk
if (flags & HAWK_SKAD_TO_BCSTR_ADDR) if (flags & HAWK_SKAD_TO_BCSTR_ADDR)
{ {
if (xlen + 1 >= len) goto done; if (xlen + 1 >= len) goto done;
buf[xlen++] = '@'; if (flags & HAWK_SKAD_TO_BCSTR_UNIXAT) buf[xlen++] = '@';
if (xlen + 1 >= len) goto done; if (xlen + 1 >= len) goto done;
xlen += hawk_copy_bcstr(&buf[xlen], len - xlen, skad->un.sun_path); xlen += hawk_copy_bcstr(&buf[xlen], len - xlen, skad->un.sun_path);