Skip to content

Commit

Permalink
Cygwin: implement setproctitle
Browse files Browse the repository at this point in the history
Make sure to create commandline according to setting of setproctitle.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
  • Loading branch information
github-cygwin committed Jan 31, 2024
1 parent 61fd870 commit 2e7f7b9
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 20 deletions.
8 changes: 8 additions & 0 deletions newlib/libc/include/sys/unistd.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,14 @@ int setpgid (pid_t __pid, pid_t __pgid);
#if __SVID_VISIBLE || __XSI_VISIBLE >= 500
int setpgrp (void);
#endif
#if defined(__CYGWIN__) && __BSD_VISIBLE
/* Stub for Linux libbsd compatibility. */
#define initsetproctitle(c, a, e) setproctitle_init((c), (a), (e))
static inline void setproctitle_init (int, char *[], char *[]) {}

void setproctitle (const char *, ...)
_ATTRIBUTE ((__format__ (__printf__, 1, 2)));
#endif
#if __BSD_VISIBLE || __XSI_VISIBLE >= 4
int setregid (gid_t __rgid, gid_t __egid);
int setreuid (uid_t __ruid, uid_t __euid);
Expand Down
1 change: 1 addition & 0 deletions winsup/cygwin/cygwin.din
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,7 @@ setpassent NOSIGFE
setpgid SIGFE
setpgrp SIGFE
setpriority SIGFE
setproctitle SIGFE
setprogname NOSIGFE
setprotoent = cygwin_setprotoent SIGFE
setpwent NOSIGFE
Expand Down
2 changes: 2 additions & 0 deletions winsup/cygwin/globals.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ int NO_COPY __isthreaded = 0;
int __argc_safe;
int __argc;
char **__argv;
/* Set via setproctitle */
char *__argv0_orig;

_cygtls NO_COPY *_main_tls /* !globals.h */;

Expand Down
3 changes: 2 additions & 1 deletion winsup/cygwin/include/cygwin/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -489,12 +489,13 @@ details. */
352: Implement dirent.d_reclen.
353: Implement fdclosedir.
354: Implement posix_getdents.
355: Implement setproctitle.
Note that we forgot to bump the api for ualarm, strtoll, strtoull,
sigaltstack, sethostname. */

#define CYGWIN_VERSION_API_MAJOR 0
#define CYGWIN_VERSION_API_MINOR 354
#define CYGWIN_VERSION_API_MINOR 355

/* There is also a compatibity version number associated with the shared memory
regions. It is incremented when incompatible changes are made to the shared
Expand Down
34 changes: 34 additions & 0 deletions winsup/cygwin/libc/bsdlib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,40 @@ setprogname (const char *newprogname)
__endtry
}

extern "C" void
setproctitle (const char *fmt, ...)
{
char buf[MAX_PATH]; /* Max len of a new proc title */
va_list ap;
int len;
char *tmp;

if (__argv0_orig)
{
tmp = __argv[0];
__argv[0] = __argv0_orig;
__argv0_orig = NULL;
cfree (tmp);
}
if (fmt)
{
if (fmt[0] == '-') /* Skip program name prefix. */
{
fmt++;
len = 0;
}
else /* Print program name heading for grep. */
len = snprintf (buf, sizeof buf, "%s: ", getprogname ());
va_start (ap, fmt);
vsnprintf (buf + len, sizeof buf - len, fmt, ap);
va_end (ap);

tmp = (char *) cstrdup1 (buf);
__argv0_orig = __argv[0];
__argv[0] = tmp;
}
}

extern "C" void
logwtmp (const char *line, const char *user, const char *host)
{
Expand Down
37 changes: 18 additions & 19 deletions winsup/cygwin/pinfo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,27 +642,24 @@ commune_process (void *arg)
{
sigproc_printf ("processing PICOM_CMDLINE");
unsigned n = 0;
const char *argv[__argc_safe + 1];
int argc = __argv0_orig ? 1 : __argc_safe;
const char *argv[argc + 1];

for (int i = 0; i < __argc_safe; i++)
for (int i = 0; i < argc; i++)
{
argv[i] = __argv[i] ?: "";
n += strlen (argv[i]) + 1;
}
argv[__argc_safe] = NULL;
argv[argc] = NULL;
if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L))
{
/*__seterrno ();*/ // this is run from the signal thread, so don't set errno
sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E");
}
else
for (const char **a = argv; *a; a++)
if (!WritePipeOverlapped (tothem, *a, strlen (*a) + 1, &nr, 1000L))
{
sigproc_printf ("WritePipeOverlapped arg %d failed, %E",
a - argv);
break;
}
sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E");
else for (int i = 0; i < argc; i++)
if (!WritePipeOverlapped (tothem, __argv[i],
strlen (__argv[i]) + 1, &nr, 1000L))
{
sigproc_printf ("WritePipeOverlapped arg %d failed, %E", i);
break;
}
break;
}
case PICOM_CWD:
Expand Down Expand Up @@ -1162,12 +1159,14 @@ _pinfo::cmdline (size_t& n)
else
{
n = 0;
for (char **a = __argv; *a; a++)
n += strlen (*a) + 1;
int argc = __argv0_orig ? 1 : __argc_safe;

for (int i = 0; i < argc; ++i)
n += strlen (__argv[i]) + 1;
char *p;
p = s = (char *) cmalloc_abort (HEAP_COMMUNE, n);
for (char **a = __argv; *a; a++)
p = stpcpy (p, *a) + 1;
for (int i = 0; i < argc; ++i)
p = stpcpy (p, __argv[i]) + 1;
}
return s;
}
Expand Down
2 changes: 2 additions & 0 deletions winsup/cygwin/release/3.6.0
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ What's new:

- New API calls: fdclosedir, posix_getdents.

- New API call: setproctitle.


What changed:
-------------
Expand Down

0 comments on commit 2e7f7b9

Please sign in to comment.