getprogname() and setproctitle() on Linux.

This commit is contained in:
Nicholas Marriott 2016-05-27 18:04:25 +01:00
parent 20d97eb849
commit cfef0c6658
6 changed files with 154 additions and 4 deletions

View File

@ -26,7 +26,7 @@ if IS_GCC
CFLAGS += -std=gnu99 -O2
if IS_DEBUG
CFLAGS += -g
CFLAGS += -Wno-long-long -Wall -W -Wnested-externs -Wformat=2
CFLAGS += -Wno-long-long -Wall -W -Wformat=2
CFLAGS += -Wmissing-prototypes -Wstrict-prototypes -Wmissing-declarations
CFLAGS += -Wwrite-strings -Wshadow -Wpointer-arith -Wsign-compare
CFLAGS += -Wundef -Wbad-function-cast -Winline -Wcast-align
@ -194,6 +194,15 @@ endif
if NO_DAEMON
nodist_tmux_SOURCES += compat/daemon.c
endif
if NO_DAEMON
nodist_tmux_SOURCES += compat/daemon.c
endif
if NO_GETPROGNAME
nodist_tmux_SOURCES += compat/getprogname.c
endif
if NO_SETPROCTITLE
nodist_tmux_SOURCES += compat/setproctitle.c
endif
if NO_SETENV
nodist_tmux_SOURCES += compat/setenv.c
endif

View File

@ -223,6 +223,16 @@ size_t strlcat(char *, const char *, size_t);
int daemon(int, int);
#endif
#ifndef HAVE_GETPROGNAME
/* getprogname.c */
const char *getprogname(void);
#endif
#ifndef HAVE_SETPROCTITLE
/* setproctitle.c */
void setproctitle(const char *, ...);
#endif
#ifndef HAVE_B64_NTOP
/* b64_ntop.c */
#undef b64_ntop /* for Cygwin */

43
compat/getprogname.c Normal file
View File

@ -0,0 +1,43 @@
/*
* Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include "tmux.h"
#if defined(HAVE_PROGRAM_INVOCATION_SHORT_NAME)
const char *
getprogname(void)
{
extern char *program_invocation_short_name;
return (program_invocation_short_name);
}
#elif defined(HAVE___PROGNAME)
const char *
getprogname(void)
{
extern char *__progname;
return (__progname);
}
#else
const char *
getprogname(void)
{
return ("tmux");
}
#endif

51
compat/setproctitle.c Normal file
View File

@ -0,0 +1,51 @@
/*
* Copyright (c) 2016 Nicholas Marriott <nicholas.marriott@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER
* IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
* OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <string.h>
#include "tmux.h"
#if defined(HAVE_PRCTL) && defined(HAVE_PR_SET_NAME)
#include <sys/prctl.h>
void
setproctitle(const char *fmt, ...)
{
char title[16], name[16], *cp;
va_list ap;
int used;
va_start(ap, fmt);
vsnprintf(title, sizeof title, fmt, ap);
va_end(ap);
used = snprintf(name, sizeof name, "%s: %s", getprogname(), title);
if (used >= (int)sizeof name) {
cp = strrchr(name, ' ');
if (cp != NULL)
*cp = '\0';
}
prctl(PR_SET_NAME, name);
}
#else
void
setproctitle(const char *fmt, ...)
{
}
#endif

View File

@ -113,7 +113,7 @@ AC_CHECK_FUNCS(
[ \
dirfd \
flock \
setproctitle \
prctl \
sysconf \
cfmakeraw \
]
@ -298,6 +298,20 @@ if test "x$found_daemon" = xyes; then
fi
AM_CONDITIONAL(NO_DAEMON, [test "x$found_daemon" = xno])
# Look for getprogname, compat/getprogname.c used if missing.
AC_CHECK_FUNC(getprogname, found_getprogname=yes, found_getprogname=no)
if test "x$found_getprogname" = xyes; then
AC_DEFINE(HAVE_GETPROGNAME)
fi
AM_CONDITIONAL(NO_GETPROGNAME, [test "x$found_getprogname" = xno])
# Look for setproctitle, compat/setproctitle.c used if missing.
AC_CHECK_FUNC(setproctitle, found_setproctitle=yes, found_setproctitle=no)
if test "x$found_setproctitle" = xyes; then
AC_DEFINE(HAVE_SETPROCTITLE)
fi
AM_CONDITIONAL(NO_SETPROCTITLE, [test "x$found_setproctitle" = xno])
# Look for setenv, compat/setenv.c used if missing.
AC_CHECK_FUNC(setenv, found_setenv=yes, found_setenv=no)
if test "x$found_setenv" = xyes; then
@ -475,6 +489,31 @@ AC_LINK_IFELSE([AC_LANG_SOURCE(
AC_MSG_RESULT(no)
)
# Look for program_invocation_short_name.
AC_MSG_CHECKING(for program_invocation_short_name)
AC_LINK_IFELSE([AC_LANG_SOURCE(
[
#include <stdio.h>
#include <stdlib.h>
extern char *program_invocation_short_name;
int main(void) {
const char *cp = program_invocation_short_name;
printf("%s\n", cp);
exit(0);
}
])],
[AC_DEFINE(HAVE_PROGRAM_INVOCATION_SHORT_NAME) AC_MSG_RESULT(yes)],
AC_MSG_RESULT(no)
)
# Look for prctl(PR_SET_NAME).
AC_CHECK_DECL(
PR_SET_NAME,
AC_DEFINE(HAVE_PR_SET_NAME),
,
[#include <sys/prctl.h>]
)
# Look for fcntl(F_CLOSEM).
AC_CHECK_DECL(
F_CLOSEM,

2
proc.c
View File

@ -191,9 +191,7 @@ proc_start(const char *name, struct event_base *base, int forkflag,
log_open(name);
#ifdef HAVE_SETPROCTITLE
setproctitle("%s (%s)", name, socket_path);
#endif
if (uname(&u) < 0)
memset(&u, 0, sizeof u);