Skip to content

Commit

Permalink
Merge pull request #2771 from jimklimov/issue-2764
Browse files Browse the repository at this point in the history
Report full path to driver socket (when we listen or fail to connect)
  • Loading branch information
jimklimov authored Jan 19, 2025
2 parents 2dbb8b0 + 8979560 commit cb499ae
Show file tree
Hide file tree
Showing 32 changed files with 147 additions and 98 deletions.
6 changes: 6 additions & 0 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ https://github.com/networkupstools/nut/milestone/11
prevent the tool name and NUT version banner from being unilaterally
printed out when NUT programs start. [issues #1789 vs. #316; #2573]
- Drivers would now report the socket they are listening on, and server
would report full path to the driver socket it tries to connect to.
A new `NUT_QUIET_INIT_LISTENER` envvar (presence or "true" value) can
prevent the socket name from being unilaterally printed out when NUT
drivers start. [#2764]
- The `upsdrvctl` should now warn if executed on systems where NUT was
built with support for service management frameworks like systemd or SMF,
so nut-driver service units prepared by `nut-driver-enumerator` would
Expand Down
2 changes: 1 addition & 1 deletion clients/cgilib.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ static void cgilib_err(const char *errmsg)

int checkhost(const char *host, char **desc)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
PCONF_CTX_t ctx;

if (!host)
Expand Down
8 changes: 4 additions & 4 deletions clients/upssched.c
Original file line number Diff line number Diff line change
Expand Up @@ -970,14 +970,14 @@ static void start_daemon(TYPE_FD lockfd)
DWORD timeout_ms;
HANDLE rfds[32];

char module[MAX_PATH];
char module[NUT_PATH_MAX];
STARTUPINFO sinfo;
PROCESS_INFORMATION pinfo;
if( !GetModuleFileName(NULL,module,MAX_PATH) ) {
if (!GetModuleFileName(NULL, module, NUT_PATH_MAX)) {
fatal_with_errno(EXIT_FAILURE, "Can't retrieve module name");
}
memset(&sinfo,0,sizeof(sinfo));
if(!CreateProcess(module, NULL, NULL,NULL,FALSE,0,NULL,NULL,&sinfo,&pinfo)) {
if (!CreateProcess(module, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sinfo, &pinfo)) {
fatal_with_errno(EXIT_FAILURE, "Can't create child process");
}
pipefd = open_sock();
Expand Down Expand Up @@ -1439,7 +1439,7 @@ static void upssched_err(const char *errmsg)

static void checkconf(void)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
PCONF_CTX_t ctx;
int numerrors = 0;

Expand Down
4 changes: 2 additions & 2 deletions clients/upsset.c
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ static void upsset_hosts_err(const char *errmsg)
/* this defaults to wherever we are now, ups and function-wise */
static void do_pickups(const char *currfunc)
{
char hostfn[SMALLBUF];
char hostfn[NUT_PATH_MAX];
PCONF_CTX_t ctx;

snprintf(hostfn, sizeof(hostfn), "%s/hosts.conf", confpath());
Expand Down Expand Up @@ -1020,7 +1020,7 @@ static void upsset_conf_err(const char *errmsg)
/* see if the user has confirmed their cgi directory's secure state */
static void check_conf(void)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
PCONF_CTX_t ctx;

snprintf(fn, sizeof(fn), "%s/upsset.conf", confpath());
Expand Down
4 changes: 2 additions & 2 deletions clients/upsstats.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ static void parse_line(const char *buf)

static void display_template(const char *tfn)
{
char fn[SMALLBUF], buf[LARGEBUF];
char fn[NUT_PATH_MAX], buf[LARGEBUF];

snprintf(fn, sizeof(fn), "%s/%s", confpath(), tfn);

Expand Down Expand Up @@ -965,7 +965,7 @@ static void upsstats_hosts_err(const char *errmsg)

static void load_hosts_conf(void)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
PCONF_CTX_t ctx;

snprintf(fn, sizeof(fn), "%s/hosts.conf", CONFPATH);
Expand Down
32 changes: 11 additions & 21 deletions common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,7 @@ char * getprocname(pid_t pid)
*/
char *procname = NULL;
size_t procnamelen = 0;
#ifdef UNIX_PATH_MAX
char pathname[UNIX_PATH_MAX];
#else
char pathname[PATH_MAX];
#endif
char pathname[NUT_PATH_MAX];
struct stat st;

#ifdef WIN32
Expand Down Expand Up @@ -1473,11 +1469,7 @@ int compareprocname(pid_t pid, const char *procname, const char *progname)
size_t procbasenamelen = 0, progbasenamelen = 0;
/* Track where the last dot is in the basename; 0 means none */
size_t procbasenamedot = 0, progbasenamedot = 0;
#ifdef UNIX_PATH_MAX
char procbasename[UNIX_PATH_MAX], progbasename[UNIX_PATH_MAX];
#else
char procbasename[PATH_MAX], progbasename[PATH_MAX];
#endif
char procbasename[NUT_PATH_MAX], progbasename[NUT_PATH_MAX];

if (checkprocname_ignored(__func__)) {
ret = -3;
Expand Down Expand Up @@ -1669,7 +1661,7 @@ int checkprocname(pid_t pid, const char *progname)
depending on the .exe path */
char * getfullpath(char * relative_path)
{
char buf[MAX_PATH];
char buf[NUT_PATH_MAX];
if ( GetModuleFileName(NULL, buf, sizeof(buf)) == 0 ) {
return NULL;
}
Expand All @@ -1690,7 +1682,7 @@ char * getfullpath(char * relative_path)
void writepid(const char *name)
{
#ifndef WIN32
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
FILE *pidf;
mode_t mask;

Expand Down Expand Up @@ -2069,7 +2061,7 @@ int snprintfcat(char *dst, size_t size, const char *fmt, ...)
#ifndef WIN32
int sendsignal(const char *progname, int sig, int check_current_progname)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];

snprintf(fn, sizeof(fn), "%s/%s.pid", rootpidpath(), progname);

Expand Down Expand Up @@ -3088,11 +3080,7 @@ const char * rootpidpath(void)
/* Die with a standard message if socket filename is too long */
void check_unix_socket_filename(const char *fn) {
size_t len = strlen(fn);
#ifdef UNIX_PATH_MAX
size_t max = UNIX_PATH_MAX;
#else
size_t max = PATH_MAX;
#endif
size_t max = NUT_PATH_MAX;
#ifndef WIN32
struct sockaddr_un ssaddr;
max = sizeof(ssaddr.sun_path);
Expand All @@ -3108,6 +3096,8 @@ void check_unix_socket_filename(const char *fn) {
* varying 104-108 bytes (UNIX_PATH_MAX)
* as opposed to PATH_MAX or MAXPATHLEN
* typically of a kilobyte range.
* We define NUT_PATH_MAX as the greatest
* value of them all.
*/
fatalx(EXIT_FAILURE,
"Can't create a unix domain socket: pathname '%s' "
Expand Down Expand Up @@ -3890,12 +3880,12 @@ static char * get_libname_in_dir(const char* base_libname, size_t base_libname_l
DIR *dp;
struct dirent *dirp;
char *libname_path = NULL, *libname_alias = NULL;
char current_test_path[LARGEBUF];
char current_test_path[NUT_PATH_MAX];

upsdebugx(3, "%s('%s', %" PRIuSIZE ", '%s', %i): Entering method...",
__func__, base_libname, base_libname_length, dirname, index);

memset(current_test_path, 0, LARGEBUF);
memset(current_test_path, 0, sizeof(current_test_path));

if ((dp = opendir(dirname)) == NULL) {
if (index >= 0) {
Expand Down Expand Up @@ -3938,7 +3928,7 @@ static char * get_libname_in_dir(const char* base_libname, size_t base_libname_l
continue;
}

snprintf(current_test_path, LARGEBUF, "%s/%s", dirname, dirp->d_name);
snprintf(current_test_path, sizeof(current_test_path), "%s/%s", dirname, dirp->d_name);
#if HAVE_DECL_REALPATH
libname_path = realpath(current_test_path, NULL);
#else
Expand Down
6 changes: 3 additions & 3 deletions common/nutstream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ static const char* getTmpDirPath() {

#ifdef WIN32
/* Suggestions from https://sourceforge.net/p/mingw/bugs/666/ */
static char pathbuf[MAX_PATH];
static char pathbuf[NUT_PATH_MAX];
int i;
#endif

Expand All @@ -255,7 +255,7 @@ static const char* getTmpDirPath() {

#ifdef WIN32
i = GetTempPathA(sizeof(pathbuf), pathbuf);
if ((i > 0) && (i < MAX_PATH) && checkExistsWritableDir(pathbuf))
if ((i > 0) && (i < NUT_PATH_MAX) && checkExistsWritableDir(pathbuf))
return (const char *)pathbuf;
#endif

Expand Down Expand Up @@ -328,7 +328,7 @@ NutFile::NutFile(anonymous_t):
/* Suggestions from https://sourceforge.net/p/mingw/bugs/666/ because
* msvcrt tmpfile() uses C: root dir and lacks permissions to actually
* use it, and mingw tends to call that OS method so far */
char filename[MAX_PATH];
char filename[NUT_PATH_MAX];
memset(filename, 0, sizeof(filename));

GetTempFileNameA(m_tmp_dir.c_str(), "nuttemp", 0, filename);
Expand Down
2 changes: 1 addition & 1 deletion common/upsconf.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ static void upsconf_err(const char *errmsg)
*/
int read_upsconf(int fatal_errors)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
PCONF_CTX_t ctx;

ups_section = NULL;
Expand Down
6 changes: 3 additions & 3 deletions common/wincompat.c
Original file line number Diff line number Diff line change
Expand Up @@ -498,11 +498,11 @@ static const char *named_pipe_name=NULL;
void pipe_create(const char * pipe_name)
{
BOOL ret;
char pipe_full_name[SMALLBUF];
char pipe_full_name[NUT_PATH_MAX];

/* save pipe name for further use in pipe_connect */
if( pipe_name == NULL ) {
if( named_pipe_name == NULL ) {
if (pipe_name == NULL) {
if (named_pipe_name == NULL) {
return;
}
}
Expand Down
4 changes: 4 additions & 0 deletions conf/nut.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,7 @@ MODE=none
# be helpful in shell profiles or scripts which process NUT tool outputs.
#NUT_QUIET_INIT_BANNER=true
#export NUT_QUIET_INIT_BANNER

# Optionally suppress reporting of sockets the driver programs listen at.
#NUT_QUIET_INIT_LISTENER=true
#export NUT_QUIET_INIT_LISTENER
15 changes: 15 additions & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,21 @@ CFLAGS=${CFLAGS-"-O"}
dnl # Not so far a legacy, added for consistency in 2024 :)
CXXFLAGS=${CXXFLAGS-"-O"}

dnl Check for various integer types
AC_TYPE_SSIZE_T
AC_TYPE_INTMAX_T
AC_TYPE_INT8_T
AC_TYPE_INT16_T
AC_TYPE_INT32_T
AC_TYPE_INT64_T

AC_TYPE_SIZE_T
AC_TYPE_UINTMAX_T
AC_TYPE_UINT8_T
AC_TYPE_UINT16_T
AC_TYPE_UINT32_T
AC_TYPE_UINT64_T

dnl +-------------------------------------------------------------------

dnl Use "./configure --enable-maintainer-mode" to keep Makefile.in and Makefile
Expand Down
2 changes: 1 addition & 1 deletion drivers/clone-outlet.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ static TYPE_FD sstate_connect(void)

/* continued below... */
#else /* WIN32 */
char pipename[SMALLBUF];
char pipename[NUT_PATH_MAX];
BOOL result = FALSE;

snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\%s/%s", dflt_statepath(), device_path);
Expand Down
2 changes: 1 addition & 1 deletion drivers/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ static TYPE_FD sstate_connect(void)

/* continued below... */
#else /* WIN32 */
char pipename[SMALLBUF];
char pipename[NUT_PATH_MAX];
BOOL result = FALSE;

snprintf(pipename, sizeof(pipename), "\\\\.\\pipe\\%s/%s", dflt_statepath(), device_path);
Expand Down
17 changes: 12 additions & 5 deletions drivers/dstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ static TYPE_FD sock_open(const char *fn)
fatal_with_errno(EXIT_FAILURE, "listen(%d, %d) failed", fd, DS_LISTEN_BACKLOG);
}

if (!getenv("NUT_QUIET_INIT_LISTENER"))
upslogx(LOG_INFO, "Listening on socket %s", sockfn);

#else /* WIN32 */

fd = CreateNamedPipe(
Expand All @@ -187,17 +190,21 @@ static TYPE_FD sock_open(const char *fn)
}

/* Prepare an async wait on a connection on the pipe */
memset(&connect_overlapped,0,sizeof(connect_overlapped));
memset(&connect_overlapped, 0, sizeof(connect_overlapped));
connect_overlapped.hEvent = CreateEvent(NULL, /*Security*/
FALSE, /* auto-reset*/
FALSE, /* inital state = non signaled*/
NULL /* no name*/);
if(connect_overlapped.hEvent == NULL ) {
if (connect_overlapped.hEvent == NULL) {
fatal_with_errno(EXIT_FAILURE, "Can't create event");
}

/* Wait for a connection */
ConnectNamedPipe(fd,&connect_overlapped);
ConnectNamedPipe(fd, &connect_overlapped);

if (!getenv("NUT_QUIET_INIT_LISTENER"))
upslogx(LOG_INFO, "Listening on named pipe %s", fn);

#endif

return fd;
Expand All @@ -210,7 +217,7 @@ static void sock_disconnect(conn_t *conn)
close(conn->fd);
#else
/* FIXME not sure if this is the right way to close a connection */
if( conn->read_overlapped.hEvent != INVALID_HANDLE_VALUE) {
if (conn->read_overlapped.hEvent != INVALID_HANDLE_VALUE) {
CloseHandle(conn->read_overlapped.hEvent);
conn->read_overlapped.hEvent = INVALID_HANDLE_VALUE;
}
Expand Down Expand Up @@ -1074,7 +1081,7 @@ static void sock_close(void)

char * dstate_init(const char *prog, const char *devname)
{
char sockname[SMALLBUF];
char sockname[NUT_PATH_MAX];

#ifndef WIN32
/* do this here for now */
Expand Down
6 changes: 3 additions & 3 deletions drivers/dummy-ups.c
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ void upsdrv_updateinfo(void)
/* less stress on the sys */
if (ctx == NULL && next_update == -1) {
struct stat fs;
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];

prepare_filepath(fn, sizeof(fn));

Expand Down Expand Up @@ -446,7 +446,7 @@ void upsdrv_initups(void)
}
else
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
mode = MODE_NONE;

if (val) {
Expand Down Expand Up @@ -747,7 +747,7 @@ static void upsconf_err(const char *errmsg)
*/
static int parse_data_file(TYPE_FD arg_upsfd)
{
char fn[SMALLBUF];
char fn[NUT_PATH_MAX];
char *ptr, var_value[MAX_STRING_SIZE];
size_t value_args = 0, counter;
time_t now;
Expand Down
Loading

0 comments on commit cb499ae

Please sign in to comment.