Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

common: Get program name based on executable path if possible #307

Merged
merged 1 commit into from
Aug 10, 2020

Conversation

ueno
Copy link
Member

@ueno ueno commented Aug 2, 2020

Some programs (e.g., Chromium) pack command line arguments into argv[0]. Check if it is the case by reading /proc/self/exe and extract the program name.

Logic borrowed from:
https:/mesa3d/mesa/commit/759b94038987bb983398cd4b1d2cb1c8f79817a9.

Fixes #263.

@packit-as-a-service
Copy link

Congratulations! One of the builds has completed. 🍾

You can install the built RPMs by following these steps:

  • sudo yum install -y dnf-plugins-core on RHEL 8
  • sudo dnf install -y dnf-plugins-core on Fedora
  • dnf copr enable packit/p11-glue-p11-kit-307
  • And now you can install the packages.

Please note that the RPMs should be used only in a testing environment.

@@ -112,7 +112,28 @@ getprogname (void)
if (p != NULL)
name = p + 1;
#elif defined (HAVE_PROGRAM_INVOCATION_SHORT_NAME)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this workaround only needed in this particular branch, or should it cover all of them?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other branches are basically for other OSes, which likely don't have /proc/self/exe I suspect. For the meantime I'd skip the effect to Linux only.

common/compat.c Outdated Show resolved Hide resolved
common/compat.c Outdated Show resolved Hide resolved
if (!buf)
buf = realpath ("/proc/self/exe", NULL);

if (buf && strncmp (buf, name, strlen (buf)) == 0)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This won't work if name isn't a realpath itself, e.g. a relative path or a symlinked path. Is there a need to cover those cases as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed; however, it would bring too much complexity in this exceptional path IMO. Let's skip it until we find an actual use-case that require such checks.

@ueno ueno force-pushed the wip/dueno/progname branch 7 times, most recently from 71500a0 to b8fb0e4 Compare August 9, 2020 18:14
}
if (nread == 0)
break;
if (sizeof(buffer) - offset < nread) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since read(..., nbytes) <= nbytes, and thus nread <= sizeof(buffer) - offset, I don't think this condition is ever true. Maybe <= or ==?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, let's remove the check then.

assert (WIFEXITED (status));
assert (WEXITSTATUS (status) == 0);

p = strchr (buffer, '\n');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what exactly are you anticipating here, so that a string would be missing a newline, but I suspect it has a chance of also not being null-terminated in this case.

I propose folding this check into a strncmp ("frob-getprogname\n", buffer, sizeof(buffer)) or, alternatively, null-terminating the buffer after reading it (and maybe not bothering with \n altogether).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I replaced strchr with memchr so it also handles the non null-terminated case.

Some programs (e.g., Chromium) pack command line arguments into argv[0].
Check if it is the case by reading /proc/self/exe and extract the
program name.

Logic borrowed from:
<https:/mesa3d/mesa/commit/759b94038987bb983398cd4b1d2cb1c8f79817a9>.
Copy link
Contributor

@t184256 t184256 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM now

@ueno
Copy link
Member Author

ueno commented Aug 10, 2020

Thank you for the detailed review!

@ueno ueno merged commit 15f1554 into p11-glue:master Aug 10, 2020
@ueno ueno added this to the 0.23.21 milestone Aug 18, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve handling of progname
2 participants