Linux check which libraries are loaded

How to determine if a specific module is loaded in linux kernel

I am just curious is there any way to determine if a particular module is loaded/installed. $ lsmod lists all modules (device driver loaded). Is there any way to check or a command that returns true/false boolean output if a module name is polled. For eg. if keyboard.o exists return true else false. I need this tip to complete my driver auto refresh program. PS: tried modinfo. I am using busybox client in my test DUT so can you give some alternatives other than modinfo?

The question is a bit ambiguous. Are you trying to check if the driver is loaded into memory or installed on the system? modinfo would help with the latter but not the former.

10 Answers 10

The modinfo module method does not work well for me. I prefer this method that is similar to the alternative method proposed:

#!/bin/sh MODULE="$1" if lsmod | grep -wq "$MODULE"; then echo "$MODULE is loaded!" exit 0 else echo "$MODULE is not loaded!" exit 1 fi 

not sure if modinfo modname and checking $? will work for you, just a suggestion.

/tmp$ sudo modinfo e1000 /tmp$ echo $? 0 /tmp$ sudo modinfo keyboard ERROR: modinfo: could not find module keyboard /tmp$ echo $? 1 

alternatively you also grep /proc/modules

Just a note: the modinfo approach doesn’t seem to actually work for the loaded check (as in the question title). It shows info regardless of whether the module is loaded.

This is not a good solution. modinfo shows information of a kernel module installed on the rootfs, but it doesn’t check if the module is loaded into the kernel or not. So this solution should not be used to solve the issue.

The —first-time flag causes modprobe to fail if the module is already loaded. That in conjunction with the —dry-run (or the shorthand -n ) flag makes a nice test:

modprobe -n --first-time $MODULE && echo "Not loaded" || echo "Loaded" 

Edit 1: As @Nobody pointed out this also prints Loaded if the module does not exist. We can fix this by combining it with modinfo :

modinfo $MODULE >/dev/null 2>/dev/null && ! modprobe -n --first-time $MODULE 2>/dev/null && echo "Loaded" || echo "Not loaded" 

Edit 2: On some systems modprobe lives in /usr/sbin , which is not in the $PATH unless you are root. In that case you have to substitute modprobe for /usr/sbin/modprobe in the above.

Читайте также:  Arch linux install firefox

Oh, sorry, I was only half-right (or maybe quarter-right. Shame on you comment-up-voters). On Debian, modprobe is not in $PATH for normal users, so just copy&pasting your command only works as root. But when calling modprobe with its full path, it’s executable for normal users and only the actual inserting operation fails, so your solution works in principle. I still think querying /proc/modules is more elegant, but that’s a matter of taste. Had I downvoted your answer, I would have removed the vote now (but I didn’t).

MODULE=snd_aloop # for example test -n "$(grep -e "^$MODULE " /proc/modules)" && echo "Loaded" || echo "Not loaded" 

It checks in /proc/modules . If the module is mentioned there, it’s assumed to be loaded, otherwise not.

The others seemed too long to me (the other short one requires root, this does not). Of course it’s just written out what was already mentioned as «alternatives».

Caution: modprobe accepts some variants of module names other than the primary listed in /proc/modules . For example loading snd-aloop works, but the module is named snd_aloop and is listed as such in /proc/modules and when using rmmod that’s also the only name that will work.

Источник

How to see the currently loaded shared objects in Linux?

You can do both with lsof . To see what processes have a library open or mapped do:

and to see what files (including shared libraries) a process has open and/or mapped, do:

That only helps if you know WHICH instance of a .so file is loaded. Is it possible to list all th e .so files actually used by an app to find the disk path to the one I need?

Читайте также:  Команда права файла линукс

Another way to see what’s loaded in a process is by looking at the /proc/PID/maps file. This shows everything mapped into your address space, including shared objects mapped in.

Worked fine on my embedded ARM platform. While the BusyBox implementation of lsof did not have the needed functionality.

sudo grep libcairo.so /proc/*/maps 

is a nice way to explore all /proc/PID/maps mentioned by Rich at once. Sample output:

/proc/8390/maps:7f0a9afae000-7f0a9b0bc000 r-xp 00000000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8390/maps:7f0a9b0bc000-7f0a9b2bc000 ---p 0010e000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8390/maps:7f0a9b2bc000-7f0a9b2bf000 r--p 0010e000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8390/maps:7f0a9b2bf000-7f0a9b2c0000 rw-p 00111000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8466/maps:7f0a9afae000-7f0a9b0bc000 r-xp 00000000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8466/maps:7f0a9b0bc000-7f0a9b2bc000 ---p 0010e000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8466/maps:7f0a9b2bc000-7f0a9b2bf000 r--p 0010e000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 /proc/8466/maps:7f0a9b2bf000-7f0a9b2c0000 rw-p 00111000 fc:00 274690 /usr/lib/x86_64-linux-gnu/libcairo.so.2.11400.6 

Further awk and bash-fu can refine the output further.

This method also shows libraries opened with dlopen , tested with this minimal setup hacked up with a sleep(1000) on Ubuntu 18.04.

Источник

How to find out where a program is looking for libraries

The other day I came across a linux command that let me see where a program is expecting to find its libraries. It is very useful to solve library dependency problems for not so popular or proprietary software. I used ldd , it was very informative, but missed one crucial piece of information for me: ldd -v ./my_executable gave good information for libraries that my_executable can link to. But for those it can not link/find, ldd only gave information like: => not found What I want is, instead of «not found», I want to see not found at /path/to/ .

3 Answers 3

on linux you can use the LD_DEBUG. This link is helpful.

Probably you need the strace command Take a look here http://www.thegeekstuff.com/2011/11/strace-examples/

strace -e open ./my_executable shows pretty much what I want to see. I had the impression that the command I was looking for gives more concise output though. I accept this as the answer anyway.

The information about library paths is stored in /etc/ld.so.conf :

/usr/local/lib64 /usr/local/lib include /etc/ld.so.conf.d/*.conf # /lib64, /lib, /usr/lib64 and /usr/lib gets added # automatically by ldconfig after parsing this file. # So, they do not need to be listed. 

See man ldconfig for more information.

Читайте также:  Linux привилегии и parsec привилегии

I am aware of this. However, some programs will look for library from /path/to/installation/dir/libraries . What worse, this path can be hard-coded, and it is different from the actual installation path.

But why should ldd list all directories where it is looking? You can get this info from /etc/ld.so.conf and you can also look if LD_LIBRARY_PATH is set. See this documentation.

Источник

How to check what libraries are used by a program or process on Linux

Question: I would like to know which shared libraries are loaded at run-time when I invoke a particular executable. Is there any way to identify shared library dependencies of a program executable or a running process on Linux?

You can use the following methods to identiy which shared librariies a given program executable (e.g., /path/to/program ) or a given running process (e.g., PID 1149 ) depends on.

Check Shared Library Dependencies of a Program Executable

To find out what libraries a particular executable depends on, you can use ldd command. This command invokes dynamic linker to find out library dependencies of an executable.

Note that it is not recommended to run ldd with any untrusted third-party executable because some versions of ldd may directly invoke the executable to identify its library dependencies, which can be security risk.

Instead, a safer way to show library dependencies of an unknown application binary is to use the following command.

$ objdump -p /path/to/program | grep NEEDED

Check Shared Library Dependencies of a Running Process

If you want to find out what shared libraries are loaded by a running process, you can use pldd command, which shows all shared objects loaded into a process at run-time.

Note that you need root privilege to run pldd command.

Alternatively, a command line utility called pmap , which reports memory map of a process, can also show shared library dependencies of a running process.

Support Xmodulo

This website is made possible by minimal ads and your gracious donation via PayPal or credit card

Please note that this article is published by Xmodulo.com under a Creative Commons Attribution-ShareAlike 3.0 Unported License. If you would like to use the whole or any part of this article, you need to cite this web page at Xmodulo.com as the original source.

Источник

Оцените статью
Adblock
detector