From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2155) id D9F4938582B4; Thu, 1 Feb 2024 11:40:11 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D9F4938582B4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1706787611; bh=Zg3EUKqMdSM8ZxbowLztAX/NsALzuHS3Cuo9ruKV3Fk=; h=From:To:Subject:Date:From; b=xz3caue851ap5R8prtO4Y/ozJuAjAMRzvFkLbxyjzfA1FRpG8IrWDXY02OwohKx4y lcQPzNQLkvfQikHGGm0jiB0T+OHF9LcJ1a767jg7TxcElPu8w48cKzDXrBXHJzW+1m 23RpGznO/c1wapyGWnxGU+qggm0JWW7Hvxd1duiI= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Corinna Vinschen To: cygwin-cvs@sourceware.org, newlib-cvs@sourceware.org Subject: [newlib-cygwin/main] Cygwin: implement setproctitle X-Act-Checkin: newlib-cygwin X-Git-Author: Corinna Vinschen X-Git-Refname: refs/heads/main X-Git-Oldrev: 61fd8702969da82a8f086e9b9aaa26fc068926c0 X-Git-Newrev: 2e7f7b96e5f1ca788e3f4589afaaa705b20051c0 Message-Id: <20240201114011.D9F4938582B4@sourceware.org> Date: Thu, 1 Feb 2024 11:40:11 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dnewlib-cygwin.git;h=3D2e7f7b96e5f= 1ca788e3f4589afaaa705b20051c0 commit 2e7f7b96e5f1ca788e3f4589afaaa705b20051c0 Author: Corinna Vinschen AuthorDate: Wed Jan 31 19:54:13 2024 +0100 Commit: Corinna Vinschen CommitDate: Wed Jan 31 20:11:58 2024 +0100 Cygwin: implement setproctitle =20 Make sure to create commandline according to setting of setproctitle. =20 Signed-off-by: Corinna Vinschen Diff: --- newlib/libc/include/sys/unistd.h | 8 ++++++++ winsup/cygwin/cygwin.din | 1 + winsup/cygwin/globals.cc | 2 ++ winsup/cygwin/include/cygwin/version.h | 3 ++- winsup/cygwin/libc/bsdlib.cc | 34 +++++++++++++++++++++++++++++++ winsup/cygwin/pinfo.cc | 37 +++++++++++++++++-------------= ---- winsup/cygwin/release/3.6.0 | 2 ++ 7 files changed, 67 insertions(+), 20 deletions(-) diff --git a/newlib/libc/include/sys/unistd.h b/newlib/libc/include/sys/uni= std.h index 00901540fc5f..13b0c166f1a7 100644 --- a/newlib/libc/include/sys/unistd.h +++ b/newlib/libc/include/sys/unistd.h @@ -209,6 +209,14 @@ int setpgid (pid_t __pid, pid_t __pgid); #if __SVID_VISIBLE || __XSI_VISIBLE >=3D 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 >=3D 4 int setregid (gid_t __rgid, gid_t __egid); int setreuid (uid_t __ruid, uid_t __euid); diff --git a/winsup/cygwin/cygwin.din b/winsup/cygwin/cygwin.din index 6bac40cc066f..be9efdc00f44 100644 --- a/winsup/cygwin/cygwin.din +++ b/winsup/cygwin/cygwin.din @@ -1353,6 +1353,7 @@ setpassent NOSIGFE setpgid SIGFE setpgrp SIGFE setpriority SIGFE +setproctitle SIGFE setprogname NOSIGFE setprotoent =3D cygwin_setprotoent SIGFE setpwent NOSIGFE diff --git a/winsup/cygwin/globals.cc b/winsup/cygwin/globals.cc index 654b836c2890..885ada85e7b8 100644 --- a/winsup/cygwin/globals.cc +++ b/winsup/cygwin/globals.cc @@ -83,6 +83,8 @@ int NO_COPY __isthreaded =3D 0; int __argc_safe; int __argc; char **__argv; +/* Set via setproctitle */ +char *__argv0_orig; =20 _cygtls NO_COPY *_main_tls /* !globals.h */; =20 diff --git a/winsup/cygwin/include/cygwin/version.h b/winsup/cygwin/include= /cygwin/version.h index 6679eedd9505..d12ea9c89d62 100644 --- a/winsup/cygwin/include/cygwin/version.h +++ b/winsup/cygwin/include/cygwin/version.h @@ -489,12 +489,13 @@ details. */ 352: Implement dirent.d_reclen. 353: Implement fdclosedir. 354: Implement posix_getdents. + 355: Implement setproctitle. =20 Note that we forgot to bump the api for ualarm, strtoll, strtoull, sigaltstack, sethostname. */ =20 #define CYGWIN_VERSION_API_MAJOR 0 -#define CYGWIN_VERSION_API_MINOR 354 +#define CYGWIN_VERSION_API_MINOR 355 =20 /* There is also a compatibity version number associated with the shared m= emory regions. It is incremented when incompatible changes are made to the s= hared diff --git a/winsup/cygwin/libc/bsdlib.cc b/winsup/cygwin/libc/bsdlib.cc index 8e108272417f..14df03335363 100644 --- a/winsup/cygwin/libc/bsdlib.cc +++ b/winsup/cygwin/libc/bsdlib.cc @@ -257,6 +257,40 @@ setprogname (const char *newprogname) __endtry } =20 +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 =3D __argv[0]; + __argv[0] =3D __argv0_orig; + __argv0_orig =3D NULL; + cfree (tmp); + } + if (fmt) + { + if (fmt[0] =3D=3D '-') /* Skip program name prefix. */ + { + fmt++; + len =3D 0; + } + else /* Print program name heading for grep. */ + len =3D snprintf (buf, sizeof buf, "%s: ", getprogname ()); + va_start (ap, fmt); + vsnprintf (buf + len, sizeof buf - len, fmt, ap); + va_end (ap); + + tmp =3D (char *) cstrdup1 (buf); + __argv0_orig =3D __argv[0]; + __argv[0] =3D tmp; + } +} + extern "C" void logwtmp (const char *line, const char *user, const char *host) { diff --git a/winsup/cygwin/pinfo.cc b/winsup/cygwin/pinfo.cc index 5fd11f570c3c..7f1f41d79177 100644 --- a/winsup/cygwin/pinfo.cc +++ b/winsup/cygwin/pinfo.cc @@ -642,27 +642,24 @@ commune_process (void *arg) { sigproc_printf ("processing PICOM_CMDLINE"); unsigned n =3D 0; - const char *argv[__argc_safe + 1]; + int argc =3D __argv0_orig ? 1 : __argc_safe; + const char *argv[argc + 1]; =20 - for (int i =3D 0; i < __argc_safe; i++) + for (int i =3D 0; i < argc; i++) { argv[i] =3D __argv[i] ?: ""; n +=3D strlen (argv[i]) + 1; } - argv[__argc_safe] =3D NULL; + argv[argc] =3D NULL; if (!WritePipeOverlapped (tothem, &n, sizeof n, &nr, 1000L)) - { - /*__seterrno ();*/ // this is run from the signal thread, so don't se= t errno - sigproc_printf ("WritePipeOverlapped sizeof argv failed, %E"); - } - else - for (const char **a =3D 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 =3D 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: @@ -1162,12 +1159,14 @@ _pinfo::cmdline (size_t& n) else { n =3D 0; - for (char **a =3D __argv; *a; a++) - n +=3D strlen (*a) + 1; + int argc =3D __argv0_orig ? 1 : __argc_safe; + + for (int i =3D 0; i < argc; ++i) + n +=3D strlen (__argv[i]) + 1; char *p; p =3D s =3D (char *) cmalloc_abort (HEAP_COMMUNE, n); - for (char **a =3D __argv; *a; a++) - p =3D stpcpy (p, *a) + 1; + for (int i =3D 0; i < argc; ++i) + p =3D stpcpy (p, __argv[i]) + 1; } return s; } diff --git a/winsup/cygwin/release/3.6.0 b/winsup/cygwin/release/3.6.0 index 5536d39a3eff..561637c397e6 100644 --- a/winsup/cygwin/release/3.6.0 +++ b/winsup/cygwin/release/3.6.0 @@ -5,6 +5,8 @@ What's new: =20 - New API calls: fdclosedir, posix_getdents. =20 +- New API call: setproctitle. + =20 What changed: -------------