Commit Graph

6145 Commits

Author SHA1 Message Date
Michael Kerrisk 6b621d05b3 _exit.2, capget.2, fcntl.2, futex.2, listen.2, memfd_create.2, modify_ldt.2, move_pages.2, open.2, perf_event_open.2, ptrace.2, set_thread_area.2, stime.2, syscall.2, sysctl.2, userfaultfd.2, cmsg.3, exit.3, ftime.3, getpt.3, malloc.3, console_codes.4, loop.4, inotify.7, netlink.7, packet.7, rtnetlink.7, tcp.7, unix.7, vsock.7, ldconfig.8: tstamp
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-09 11:53:28 +01:00
Michael Kerrisk 9ec6412745 malloc.3: Minor tweaks to Vegard Nossum's patch
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-08 23:37:21 +01:00
Vegard Nossum 813d606b00 malloc.3: realloc() return value
One might be tempted to think that realloc() always requests a new
allocation before moving the contents over (at least in the case
where the new size is bigger than the original). This is not the
case; for example, on my system the following program:

	#include <stdlib.h>
	#include <stdio.h>
	#include <unistd.h>

	int main(int argc, char *argv[])
	{
		void *x = malloc(15);
		void *y = malloc(32);

		printf("x = %p\n", x);
		printf("y = %p\n", y);
		printf("usable_size(x) = %lu\n", malloc_usable_size(x));

		void *z = realloc(x, 24);
		printf("z = %p\n", z);

		return 0;
	}

prints:

	x = 0x1b3a010
	y = 0x1b3a030
	usable_size(x) = 24
	z = 0x1b3a010

Signed-off-by: Vegard Nossum <vegard.nossum@oracle.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-08 23:37:10 +01:00
Benjamin Peterson f3491e47ba exit.3: Use hex for the status mask
Admittedly, the POSIX specification for exit() also uses octal.
However, 0xFF immediately indicates the lowest 8 bits to me
whereas I had to think a bit about the octal mask.

Cowritten-by: Mike Frysinger <vapier@gentoo.org>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-08 11:59:37 +01:00
Dmitry V. Levin 0482a85341 cmsg.3: ffix
Signed-off-by: Dmitry V. Levin <ldv@altlinux.org>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-07 16:16:34 +01:00
Michael Kerrisk e4c98f64d1 cmsg.3: Modify CMSG_DATA() example to use memcpy()
See previous patch to this page for rationale

Reported-by: Rich Felker <dalias@libc.org>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-05 01:21:17 +01:00
Rich Felker 36d25246b4 cmsg.3: Clarify alignment issues and correct method of accessing CMSG_DATA()
From an email by Rich Felker:

It came to my attention while reviewing possible breakage with
move to 64-bit time_t that some applications are dereferencing
data in socket control messages (particularly SCM_TIMESTAMP*)
in-place as the message type, rather than memcpy'ing it to
appropriate storage. This necessarily does not work and is not
supportable if the message contains data with greater alignment
requirement than the header. In particular, on 32-bit archs,
cmsghdr has size 12 and alignment 4, but struct timeval and
timespec may have alignment requirement 8.

I found at least ptpd, socat, and ssmping doing this via Debian
Code Search:

https://sources.debian.org/src/ptpd/2.3.1-debian1-4/src/dep/net.c/?hl=1578#L1578
https://sources.debian.org/src/socat/1.7.3.3-2/xio-socket.c/?hl=1839#L1839
https://sources.debian.org/src/ssmping/0.9.1-3/ssmpngcl.c/?hl=307#L307

and I suspect there are a good deal more out there. On most archs
they won't break, or will visibly break with SIGBUS, but in theory
it's possible that they silently read wrong data and this might
happen on some older and more tiny-embedded-oriented archs.

I think it's clear to someone who understands alignment and who's
thought about it that applications just can't do this, but it
doesn't seem to be documented, and an example in cmsg(3) even
shows access to int payload via *(int *)CMSG_DATA(cmsg) (of course
int is safe because its alignment is <= header alignment, but this
is not mentioned).

Could we add text, and perhaps change the example, to indicate
that in general memcpy needs to be used to copy the payload
to/from a suitable object?

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-05 01:16:10 +01:00
Michael Kerrisk 477b8f4b6f exec.3: tfix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-02-04 00:42:34 +01:00
Mike Salvatore 4207253f85 getcwd.3: wfix
This patch is a minor wording fix in getcwd.3 that changes "In the case getcwd()" to "In the case of getcwd()". This patch should apply cleanly to the master branch of the git repository.

Regards,
Mike Salvatore

From 3b68ad225dbaada2b1b55153dc57807b04531cd6 Mon Sep 17 00:00:00 2001
From: Mike Salvatore <mike.salvatore@canonical.com>
Date: Thu, 16 Jan 2020 16:08:08 -0500
Subject: [PATCH] getcwd.3: wfix

Signed-off-by: Mike Salvatore <mike.salvatore@canonical.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-01-21 20:13:56 +01:00
Petr Vorel d1715bcd15 malloc.3: Remove duplicate _GNU_SOURCE
Fixes: d8d701003 ("malloc.3: Since glibc 2.29, realloc() is exposed by
defining _DEFAULT_SOURCE")

Signed-off-by: Petr Vorel <pvorel@suse.cz>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-01-10 20:57:14 +01:00
Samuel Thibault 378e5745a4 getpt.3: Remove mention of O_NOCTTY
The glibc implementation of getpt has actually never been setting
O_NOCTTY when opening /dev/ptmx or BSD ptys.

Signed-off-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2020-01-01 08:26:31 +01:00
Michael Kerrisk 5afb9373df nl_langinfo.3: tfix
See https://sourceware.org/bugzilla/show_bug.cgi?id=25255

Reported-by: Weitian LI <liweitianux@live.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-12-11 23:01:08 +01:00
Michael Kerrisk 0f18a233a3 ftime.3: Note that this function is deprecated
As noted in glibc 2.31 feature notes.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-11-23 10:38:28 +01:00
Jashank Jeremy 96605dde98 strtok.3: tfix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-11-21 09:31:27 +01:00
Jakub Wilk 0f2b59f5ca strftime.3: wfix
Fix comma splice.

Signed-off-by: Jakub Wilk <jwilk@jwilk.net>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-11-13 20:35:53 +01:00
Michael Kerrisk 576b9ea7ae abort.3: wfix
Reported-by: Mikael Magnusson <mikachu@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-16 21:47:20 +02:00
Michael Kerrisk 0b1348ba35 getauxval.3: Clarify that AT_BASE_PLATFORM and AT_EXECFN return pointers to strings
See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=942207

Reported-by: Witold Baryluk <witold.baryluk@gmail.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-12 14:23:15 +02:00
Jakub Wilk 22bff75859 strptime.3: tfix
Escape hyphens.

Signed-off-by: Jakub Wilk <jwilk@jwilk.net>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-11 23:45:02 +02:00
Michael Kerrisk 867c9b3408 localedef.1, close.2, copy_file_range.2, execve.2, get_robust_list.2, getdomainname.2, gethostname.2, inotify_add_watch.2, io_submit.2, ioctl_fideduperange.2, kcmp.2, kill.2, mmap.2, move_pages.2, perf_event_open.2, ptrace.2, rt_sigqueueinfo.2, sched_setaffinity.2, sched_setparam.2, setns.2, sigaction.2, signalfd.2, statx.2, syscall.2, syscalls.2, uname.2, write.2, errno.3, fexecve.3, getauxval.3, printf.3, pthread_mutex_consistent.3, pthread_mutexattr_init.3, pthread_mutexattr_setrobust.3, pthread_setcancelstate.3, regex.3, strtok.3, strtol.3, ttyname.3, smartpqi.4, core.5, resolv.conf.5, man-pages.7, mq_overview.7, operator.7, pthreads.7, signal-safety.7, sysvipc.7: Update timestamp
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-11 10:45:02 +02:00
Michael Kerrisk 0b6cf5d26e pthreads.7: Minor tweaks to Carlos O'Donell's patch
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-05 14:54:24 +03:00
Michael Kerrisk 50639a2a18 pthread_setcancelstate.3, pthreads.7: srcfix: wrap source lines at sentence boundaries
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-05 14:54:15 +03:00
Carlos O'Donell dbb01cbbdb pthread_setcancelstate.3, pthreads.7, signal-safety.7: Describe issues with cancellation points in signal handlers
In a recent conversation with Mathieu Desnoyers I was reminded
that we haven't written up anything about how deferred
cancellation and asynchronous signal handlers interact. Mathieu
ran into some of this behaviour and I promised to improve the
documentation in this area to point out the potential pitfall.

Thoughts?

8< --- 8< --- 8<
In pthread_setcancelstate.3, pthreads.7, and signal-safety.7 we
describe that if you have an asynchronous signal nesting over a
deferred cancellation region that any cancellation point in the
signal handler may trigger a cancellation that will behave
as-if it was an asynchronous cancellation. This asynchronous
cancellation may have unexpected effects on the consistency of
the application. Therefore care should be taken with asynchronous
signals and deferred cancellation.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-10-05 14:54:02 +03:00
Michael Kerrisk 27f942adbc sched_setparam.2, pthread_mutexattr_init.3, pthread_mutexattr_setrobust.3, pthread_mutex_consistent.3, strtol.3, sched.7, uts_namespaces.7: SEE ALSO: correct list order
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-27 14:18:46 +02:00
Michael Kerrisk 1c6f266407 res_nclose.3: Add NEW link to resolver.3
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-27 14:18:46 +02:00
Michael Kerrisk 8f4f2de329 getlogin.3: grfix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-27 14:18:46 +02:00
Michael Kerrisk 49a2a1052b copy_file_range.2, fanotify_mark.2, inotify_add_watch.2, ioctl_fideduperange.2, kcmp.2, prctl.2, get_robust_list.2, tkill.2, ttyname.3: ERRORS: correct alphabetical order
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-27 14:18:08 +02:00
Michael Kerrisk e72ae85154 strtok.3: Add portability note for strtok_r() '*saveptr' value
On some implementations, '*saveptr' must be NULL on first call
to strtok_r().

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-26 08:01:52 +02:00
Michael Kerrisk 85987f9818 strtok.3: The caller should not modify 'saveptr' between strtok_r() calls
Reported-by: eponymous alias <eponymousalias@yahoo.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-26 07:53:24 +02:00
Michael Kerrisk 618b1e7eca getauxval.3: ffix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-24 22:02:22 +02:00
Michael Kerrisk be2e899b49 getauxval.3: srcfix: rewrap source lines
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-24 22:01:19 +02:00
Raphael Moreira Zinsly a81869d0e6 getauxval.3: Add new cache geometry entries
Add entries for the new cache geometry values of the auxiliary
vector that got included in the kernel.

Signed-off-by: Raphael Moreira Zinsly <rzinsly@linux.vnet.ibm.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-24 21:57:51 +02:00
Michael Kerrisk 6c578de29d strtok.3: Correct description of use of 'saveptr' argument in strtok_r()
Reported-by: eponymous alias <eponymousalias@yahoo.com>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-23 15:57:11 +02:00
Vincent Lefevre 986d5d0048 printf.3: Add detail on the first digit with the %e format
This requirement on the first digit with the %e format comes from
the ISO C standard. It ensures that all the digits in the output are
significant and forbids output with a precision less than requested.

Signed-off-by: Vincent Lefevre <vincent@vinc17.net>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 14:57:53 +02:00
Michael Kerrisk 5a7f305a18 fexecve.3: ERRORS: add ENOENT
Reported-by: Simone Piccardi <piccardi@truelite.it>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 14:57:53 +02:00
Michael Kerrisk 56b242b1b6 fexecve.3: ENOSYS occurs only if the kernel provides no execveat() syscall
Reported-by: Simone Piccardi <piccardi@truelite.it>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 14:57:53 +02:00
Michael Kerrisk 2896c55242 regex.3: Minor rewording
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 13:48:18 +02:00
Michael Kerrisk ce0fd56bd7 regex.3: ffix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 12:47:32 +02:00
Michael Kerrisk e7a1bf89a2 regex.3: srcfix: Wrap source lines at sentence boundaries
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 12:44:34 +02:00
Rob Landley f8c3a92763 regex.3: Document REG_STARTEND
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-13 12:43:06 +02:00
Michael Kerrisk 07b973b2f3 errno.3: Minor tweaks to Rasmus Villemoes's patch
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-01 16:23:48 +02:00
Rasmus Villemoes 7a008ecff1 errno.3: Add some comments on EAGAIN/EWOULDBLOCK and EDEADLK/EDEADLOCK
Signed-off-by: Rasmus Villemoes <rv@rasmusvillemoes.dk>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-09-01 16:14:43 +02:00
Michael Kerrisk 63121bd499 pldd.1, bpf.2, chdir.2, clone.2, fanotify_init.2, fanotify_mark.2, intro.2, ipc.2, mount.2, mprotect.2, msgctl.2, msgget.2, msgop.2, pivot_root.2, pkey_alloc.2, poll.2, prctl.2, semctl.2, semget.2, semop.2, setxattr.2, shmctl.2, shmget.2, shmop.2, tkill.2, dlopen.3, exec.3, ftok.3, getutent.3, on_exit.3, strcat.3, cpuid.4, proc.5, capabilities.7, cgroup_namespaces.7, credentials.7, fanotify.7, mount_namespaces.7, namespaces.7, sched.7, signal.7, socket.7, unix.7, user_namespaces.7, vdso.7, xattr.7, ld.so.8: tstamp
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-08-02 08:34:32 +02:00
Michael Kerrisk ed9e645a84 getutent.3: Fix missing include file in EXAMPLE
See https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=932382

Reported-by: Thorsten Glaser <tg@mirbsd.de>
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-20 15:56:34 +02:00
Michael Kerrisk f642cc55db dlopen.3: wfix: consistently use "object" rather than "library"
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-03 13:59:36 +02:00
Michael Kerrisk d031ca920e dlopen.3: Make it clear that RTLD_NODELETE also affects global variables
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-03 11:46:16 +02:00
Michael Kerrisk 9bdbaa8ab2 dlopen.3: An object opened with RTLD_LOCAL can be promoted to RTLD_GLOBAL
Verified by experiment:

$ cat prog.c
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

static int
callback(struct dl_phdr_info *info, size_t size, void *data)
{
    printf("\tName = %s\n", info->dlpi_name);

    return 0;
}

int
main(int argc, char *argv[])
{
    void *x1Handle, *x2Handle, *yHandle;
    void (*funcp)(void);
    char *err;

    x1Handle = dlopen("./lib_x1.so", RTLD_NOW | RTLD_LOCAL);
    if (x1Handle == NULL) {
        fprintf(stderr, "dlopen: %s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    if (argc > 1) {
        x2Handle = dlopen("./lib_x2.so", RTLD_NOW | RTLD_GLOBAL);
        if (x2Handle == NULL) {
            fprintf(stderr, "dlopen: %s\n", dlerror());
            exit(EXIT_FAILURE);
        }
    }

    yHandle = dlopen("./lib_y1.so", RTLD_NOW | RTLD_LOCAL);
    if (yHandle == NULL) {
        fprintf(stderr, "dlopen: %s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    (void) dlerror();                           /* Clear dlerror() */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
    funcp = (void (*)(void)) dlsym(yHandle, "y1_enter");
#pragma GCC diagnostic pop
    err = dlerror();
    if (err != NULL) {
        fprintf(stderr, "dlsym: %s", err);
        exit(EXIT_FAILURE);
    }

    (*funcp)();

    exit(EXIT_SUCCESS);
}

$ cat lib_x1.c
#include <stdio.h>

void
x1_enter(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

$ cat lib_x2.c
#include <stdio.h>

void
testfunc(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

$ cat lib_y1.c
#include <stdio.h>

void
testfunc(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

void
y1_enter(void)
{
    extern void y2(void);

    printf("Called %s\n\n", __func__);

    testfunc();
}

$ cat Build.sh
#!/bin/sh

CFLAGS="-Wno-implicit-function-declaration -Wl,--no-as-needed"

cc $CFLAGS -g -fPIC -shared -o lib_x2.so lib_x2.c
cc $CFLAGS -g -fPIC -shared -o lib_x1.so lib_x1.c ./lib_x2.so
cc $CFLAGS -g -fPIC -shared -o lib_y1.so lib_y1.c

cc $CFLAGS -o prog prog.c -ldl

$ sh Build.sh

$ ./prog
Called y1_enter

Called lib_y1.c::testfunc
$ ./prog x
Called y1_enter

Called lib_x2.c::testfunc

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-03 11:15:12 +02:00
Michael Kerrisk da6b9a6172 dlopen.3: Minor wording fix
Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-03 10:55:49 +02:00
Michael Kerrisk 58a4ac49d4 dlopen.3: Clarify when an executable's symbols can be used for symbol resolution
The --export-dynamic linker option is not the only way that main's
global symbols may end up in the dynamic symbol table and thus be
used to satisfy symbol reference in a shared object. A symbol
may also be placed into the dynamic symbol table if ld(1)
notices a dependency in another object during the static link.

Verified by experiment; see previous commit.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-03 10:06:36 +02:00
Michael Kerrisk 43898de488 dlopen.3: Clarify the rules for symbol resolution in a dlopen'ed object
The existing text wrongly implied that symbol look up first
occurred in the object and then in main, and did not mention
whether dependencies of main where used for symbol resolution.

Verified by experiment:

$ cat prog.c
#define _GNU_SOURCE
#include <link.h>
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void            /* A function defined in both main and lib_x1 */
prog_x1(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

/* The following function is forced into prog's dynamic symbol table
   because of the static link-time reference in lib_m1.so */

void            /* A function defined in both main and lib_y1 */
prog_y1_exp(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

/* The following function is not forced into prog's dynamic symbol table */

void            /* A function defined in both main and lib_y1 */
prog_y1_noexp(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

static int
callback(struct dl_phdr_info *info, size_t size, void *data)
{
    printf("\tName = %s\n", info->dlpi_name);

    return 0;
}

int
main(int argc, char *argv[])
{
    void *xHandle, *yHandle;
    void (*funcp)(void);
    char *err;

    xHandle = dlopen("./lib_x1.so", RTLD_NOW | RTLD_GLOBAL);
    if (xHandle == NULL) {
        fprintf(stderr, "dlopen: %s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    yHandle = dlopen("./lib_y1.so", RTLD_NOW | RTLD_GLOBAL);
    if (yHandle == NULL) {
        fprintf(stderr, "dlopen: %s\n", dlerror());
        exit(EXIT_FAILURE);
    }

    /* Optionally display the link map() */

    if (argc > 1) {
        printf("Link map as shown from dl_iterate_phdr() callbacks:\n");
        dl_iterate_phdr(callback, NULL);
        printf("\n");
    }

    (void) dlerror();                           /* Clear dlerror() */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
    funcp = (void (*)(void)) dlsym(yHandle, "y1_enter");
#pragma GCC diagnostic pop
    err = dlerror();
    if (err != NULL) {
        fprintf(stderr, "dlsym: %s", err);
        exit(EXIT_FAILURE);
    }

    (*funcp)();

    exit(EXIT_SUCCESS);
}

$ cat lib_m1.c
#include <stdio.h>

void		/* A function defined in both lib_m1 and lib_y1 */
m1_y1(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

#if 1
void
dummy(void)
{
    extern void prog_y1_exp(void);

    prog_y1_exp();	/* Forces prog_y1_exp into prog's dynamic symbol table,
			   so that it will be visible also to lib_y1.so */
}
#endif

$ cat lib_x1.c
#include <stdio.h>

void		/* A function defined in both main and lib_x1 */
prog_x1(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

void		/* A function defined in both lib_x1 and lib_y1 */
x1_y1(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

$ cat lib_y1.c
#include <stdio.h>

void		/* A function defined in both lib_x1 and lib_y1 */
x1_y1(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

void		/* A function defined in both main and lib_y1 */
prog_y1_exp(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

void		/* A function defined in both lib_m1 and lib_y1 */
m1_y1(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

void		/* A function defined in both main and lib_y1 */
prog_y1_noexp(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

void
y1_enter(void)
{
    extern void y2(void);

    printf("Called %s\n\n", __func__);

    prog_x1();
    prog_y1_exp();
    prog_y1_noexp();
    x1_y1();
    m1_y1();
    y2();
}

$ cat lib_y2.c
#include <stdio.h>

void
y2(void)
{
    printf("Called %s::%s\n", __FILE__, __func__);
}

$ cat Build.sh
#!/bin/sh

CFLAGS="-Wno-implicit-function-declaration -Wl,--no-as-needed"

cc $CFLAGS -g -fPIC -shared -o lib_x1.so lib_x1.c
cc $CFLAGS -g -fPIC -shared -o lib_y2.so lib_y2.c
cc $CFLAGS -g -fPIC -shared -o lib_y1.so lib_y1.c ./lib_y2.so
cc $CFLAGS -g -fPIC -shared -o lib_m1.so lib_m1.c

#ED="-Wl,--export-dynamic"
cc $CFLAGS $ED -Wl,--rpath,$PWD -o prog prog.c -ldl lib_m1.so

$ sh Build.sh

$ ./prog x
Link map as shown from dl_iterate_phdr() callbacks:
	Name =
	Name = linux-vdso.so.1
	Name = /lib64/libdl.so.2
	Name = /home/mtk/tlpi/code/shlibs/dlopen_sym_res_expt/lib_m1.so
	Name = /lib64/libc.so.6
	Name = /lib64/ld-linux-x86-64.so.2
	Name = ./lib_x1.so
	Name = ./lib_y1.so
	Name = ./lib_y2.so

Called y1_enter

Called lib_x1.c::prog_x1
Called prog.c::prog_y1_exp
Called lib_y1.c::prog_y1_noexp
Called lib_x1.c::x1_y1
Called lib_m1.c::m1_y1
Called lib_y2.c::y2

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-03 09:45:55 +02:00
Michael Kerrisk ba20328e27 dlopen.3: Note that symbol use might keep a dlclose'd object in memory
My earlier commit was in error:

    commit 4a1af09bd1
    Author: Michael Kerrisk <mtk.manpages@gmail.com>
    Date:   Sat Mar 14 21:40:35 2015 +0100

        dlopen.3: Amend error in description of dlclose() behavior

    -If the reference count drops to zero and no other loaded libraries use
    -symbols in it, then the dynamic library is unloaded.
    +If the reference count drops to zero,
    +then the dynamic library is unloaded.

I doubted the removed text, because it provide little clue about
the scenario. The POSIX dlclose(3) specification actually details
the scenario sufficiently:

       Although  a dlclose() operation is not required to remove
       any functions or data objects  from  the  address  space,
       neither  is  an  implementation prohibited from doing so.
       The only restriction on such a removal is that  no  func‐
       tion nor data object shall be removed to which references
       have been relocated, until or unless all such  references
       are removed. For instance, an executable object file that
       had been loaded with a dlopen() operation specifying  the
       RTLD_GLOBAL flag might provide a target for dynamic relo‐
       cations performed in the processing of other  relocatable
       objects—in  such  environments, an application may assume
       that no relocation, once made, shall be undone or  remade
       unless  the  executable  object file containing the relo‐
       cated object has itself been removed.

Verified by experiment:

$ cat openlibs.c       # Test program

int
main(int argc, char *argv[])
{
    void *libHandle[MAX_LIBS];
    int lcnt;

    if (argc < 2) {
        fprintf(stderr, "Usage: %s lib-path...\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    lcnt = 0;

    for (int j = 1; j < argc; j++) {
        if (argv[j][0] != '-') {
            if (lcnt >= MAX_LIBS) {
                fprintf(stderr, "Too many libraries (limit: %d)\n", MAX_LIBS);
                exit(EXIT_FAILURE);
            }

            printf("[%d] Opening %s\n", lcnt, argv[j]);
            libHandle[lcnt] = dlopen(argv[j], RTLD_NOW | RTLD_GLOBAL);
            if (libHandle[lcnt] == NULL) {
                fprintf(stderr, "dlopen: %s\n", dlerror());
                exit(EXIT_FAILURE);
            }
            lcnt++;

        } else {    /* "-N" closes the Nth handle */

            int i = atoi(&argv[j][1]);
            printf("Closing handle %d\n", i);
            dlclose(libHandle[i]);
        }

        sleep(1);
        printf("\n");
    }

    printf("Program about to exit\n");

    exit(EXIT_SUCCESS);
}

$ cat lib_x1.c

void x1_func(void) { printf("Hello world\n"); }

__attribute__((constructor)) void x1_cstor(void)
{ printf("Called %s\n", __FUNCTION__); }

__attribute__((destructor)) void x1_dstor(void)
{ printf("Called %s\n", __FUNCTION__); }

$ cat lib_y1.c

void y1_func(void) { printf("Hello world\n"); }

__attribute__((constructor)) void y1_cstor(void)
{ printf("Called %s\n", __FUNCTION__); }

__attribute__((destructor)) void y1_dstor(void)
{ printf("Called %s\n", __FUNCTION__); }

static void testref(void) {
    /* The following reference, to a symbol in lib_x1.so shows that
       RTLD_GLOBAL may pin a library when it might otherwise have been
       released with dlclose() */
    extern void x1_func(void);
    x1_func();
}

$ cc -shared -fPIC -o lib_x1.so lib_x1.c
$ cc -shared -fPIC -o lib_y1.so lib_y1.c
$ cc -o openlibs openlibs.c -ldl

$ LD_LIBRARY_PATH=. ./openlibs lib_x1.so lib_y1.so -0 -1
[0] Opening lib_x1.so
Called x1_cstor

[1] Opening lib_y1.so
Called y1_cstor

Closing handle 0

Closing handle 1
Called y1_dstor
Called x1_dstor

Program about to exit
<end program output>

Note that x1_dstor was called only when handle 1 (lib_y1.so) was closed.
But, if we edit lib_y1 to remove the reference to x1_func(), things are
different:

$ cat lib_y1.c      # After editing

void y1_func(void) { printf("Hello world\n"); }

__attribute__((constructor)) void y1_cstor(void)
{ printf("Called %s\n", __FUNCTION__); }

__attribute__((destructor)) void y1_dstor(void)
{ printf("Called %s\n", __FUNCTION__); }

static void testref(void) {
    // extern void x1_func(void);
    // x1_func();
}

$ cc -shared -fPIC -o lib_y1.so lib_y1.c
$ LD_LIBRARY_PATH=. ./openlibs lib_x1.so lib_y1.so -0 -1
[0] Opening lib_x1.so
Called x1_cstor

[1] Opening lib_y1.so
Called y1_cstor

Closing handle 0
Called x1_dstor

Closing handle 1
Called y1_dstor

Program about to exit
<end program output>

This time, x1_dstor was called when handle 0 (lib_x1.so) was closed.

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
2019-07-02 15:55:09 +02:00