[pulseaudio-discuss] [PATCH] util: Try finding out application name using dladdr if available

David Henningsson david.henningsson at canonical.com
Wed Sep 10 00:37:08 PDT 2014



On 2014-08-30 02:00, Felipe Sateler wrote:
> This fixes getting the binary name in the Hurd, or any other port using
> the GNU C library, but only in the case where the library is directly
> linked to. Opening with dlopen will not work.

Hi Felipe,

Do you recognise this code would help something else than hurd? If not, 
I think it would be safer to other OSes and compilers to have everything 
you add inside an "ifdef hurd" section.

If it would help other OSes, at least the

#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif

and

#include <dlfcn.h>

...should be inside "#if defined(HAVE_DLADDR) && 
defined(PA_GCC_WEAKREF)" quotes just like the rest of code you add, 
preferable all in one section, like this:


#if defined(HAVE_DLADDR) && defined(PA_GCC_WEAKREF)
  /* This makes pa_get_binary_name work on Hurd */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <dlfcn.h>
static int _main() PA_GCC_WEAKREF(main);
#endif

>
> Change since last version: use a weak reference to main, so that we
> don't crash when main cannot be found.
> ---
>   configure.ac     |  2 ++
>   src/pulse/util.c | 24 ++++++++++++++++++++++++
>   2 files changed, 26 insertions(+)
>
> diff --git a/configure.ac b/configure.ac
> index 7b56210..e8e2034 100644
> --- a/configure.ac
> +++ b/configure.ac
> @@ -580,6 +580,8 @@ AC_SYS_LARGEFILE
>   # Check for open64 to know if the current system does have open64() and similar functions
>   AC_CHECK_FUNCS_ONCE([open64])
>
> +AC_SEARCH_LIBS([dladdr], [dl], [HAVE_DLADDR=1], [HAVE_DLADDR=0])
> +AC_DEFINE(HAVE_DLADDR, [1], [Have dladdr?])
>
>   ###################################
>   #      External libraries         #
> diff --git a/src/pulse/util.c b/src/pulse/util.c
> index ace698c..9b76661 100644
> --- a/src/pulse/util.c
> +++ b/src/pulse/util.c
> @@ -24,6 +24,10 @@
>   #include <config.h>
>   #endif
>
> +#ifndef _GNU_SOURCE
> +#define _GNU_SOURCE 1
> +#endif
> +
>   #include <errno.h>
>   #include <limits.h>
>   #include <stdio.h>
> @@ -32,6 +36,7 @@
>   #include <time.h>
>   #include <unistd.h>
>   #include <sys/types.h>
> +#include <dlfcn.h>
>
>   #ifdef HAVE_PWD_H
>   #include <pwd.h>
> @@ -64,6 +69,10 @@
>
>   #include "util.h"
>
> +#if defined(HAVE_DLADDR) && defined(PA_GCC_WEAKREF)
> +static int _main() PA_GCC_WEAKREF(main);
> +#endif
> +
>   char *pa_get_user_name(char *s, size_t l) {
>       const char *p;
>       char *name = NULL;
> @@ -218,6 +227,21 @@ char *pa_get_binary_name(char *s, size_t l) {
>       }
>   #endif
>
> +#if defined(HAVE_DLADDR) && defined(PA_GCC_WEAKREF)
> +    {
> +        Dl_info info;
> +        if(_main) {
> +            int err = dladdr(&_main, &info);
> +            if (err != 0) {
> +                char *p = pa_realpath(info.dli_fname);
> +                if (p) {
> +                    return p;
> +                }

Unnecessary parenthesis around "return p".

> +            }
> +        }
> +    }
> +#endif
> +
>   #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_NAME)
>       {
>
>

-- 
David Henningsson, Canonical Ltd.
https://launchpad.net/~diwic


More information about the pulseaudio-discuss mailing list