Linux. SOL_NETLINK not defined - linux

I was trying to use SOL_NETLINK in setsockopt in Linux, and got an error saying that SOL_NETLINK is not defined although in included the socket.h file.
Googled for some answers and saw people redefine SOL_NETLINK in their own files due to "Linux header file confusion".
Any explanation for that?

This may be a workaround, but if you see the Linux kernel SOL_NETLINK is always defined as 270:
#define SOL_NETLINK 270
You can just replace the constant with the value and it should work without any issue.

I just compile-checked this code on a raspbian PI, and it compiles without any warnings.
#include <sys/socket.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
printf ("SOL_SOCKET=%d\n", SOL_SOCKET);
return 0;
}

Related

How do I find out if my linux platform supports strong aliases?

The GCC manual describes -fabi-compat-version=n, which is used to handle variations in C++ name mangling in the slightly variant C++ ABIs of GCC 3.4 to 9.2, and probably later. It has an important caveat:
On targets that support strong aliases, G++ works around mangling changes by
creating an alias with the correct mangled name when defining a symbol with
an incorrect mangled name. This switch specifies which ABI version to use for
the alias.
However, it's not immediately obvious how you find out if your platform does support strong aliases.
The easy way to find out is to write a small program that uses __attribute__ to create an alias, and then use nm to see if the alias exists. Here's sample code, which is a variant on the classic "Hello, World" program:
/* strong_alias.c
compile with 'gcc strong_alias.c'
run ./a.out to check it works, and then run 'nm ./a.out'
to check that strong_alias() has the same address as main() */
#include <stdio.h>
int main( int argc, char *argv[])
{
printf( "Hello, World\n");
return 0;
}
int strong_alias( int argc, char *argv[]) __attribute__ (( alias ("main")));
Compile it and check that it runs, then use nm strong_alias to look at its symbol table. Here's a version that was compiled on CentOS 7 for x86-64:
nm ./a.out | egrep ' (main|strong_alias)'
000000000040052d T main
000000000040052d T strong_alias
We can see that main and strong_alias have the same address, and are thus aliases.

how to get process descriptor given a pid in user space Linux

how to get process descriptor given a pid in user space Linux.
We are doing a porting project from OS900 to Linux, converting all system calls.
I tried using find_task_by_pid() in a test code
#include <stdio.h>
#include <stdlib.h>
//#include <include/linux/sched.h>
//#include <Linux/include/linux/pid.h>
//find_task_by_pid(getpid());
pid_task(find_vpid(getpid()), PIDTYPE_PID);
but it is not compiling
error: ‘PIDTYPE_PID’ undeclared (first use in this function)

Printk not printing in spite of properly set loglevel

my problem is, I am trying to build a driver into the kernel. I decided to test my code with a simple Hello World program. The code looks like:
#include <linux/kernel.h>
#include <linux/err.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/printk.h>
int __init my_init(void)
{
printk(KERN_ALERT "Hello world\n");
return 0;
}
device_initcall(my_init);
//subsys_initcall(my_init);
Also, cat /proc/sys/kernel/printk shows
7 4 1 7
From the .config file, I find "CONFIG_DEFAULT_MESSAGE_LOGLEVEL=4"
I am making the file using obj-y += in the Makefile. I find that 'make' can build the module, but no printk outputs are appearing in dmesg or under /var/log/ after boot.
I am wondering if the driver is not being built at all into the kernel. Is there any way of checking that?
Thanks,
D.
You can use
lsmod command
OR
cat /proc/modules
command to check if your driver is loaded.
Looking at your code I feel __init should be replaced with module_init or __initcall.
These kernel signatures ensure that the module init is called at insertion time.
You can test your driver by using insmod and check if it logs the message by calling dmesg.
I solved the problem. I found out that the printk statements were actually getting printed on the console, but got wiped out from dmesg due to the large number of other messages getting printed. I increased the size of dmesg, and now it stores the printk statements.

Help, no clue how to make this Makefile

I have read several tutorials but I still do not have any clue :-) I have a c File "liboratidy.c" this file includes some oder libraries:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <tidy.h>
#include <buffio.h>
#include <oci.h>
#include <ociextp.h>
The needed files are located in /user/lib/libtidy.so and header files in /usr/include/tidy/ and /usr/lib/oracle/xe/app/oracle/product/10.2.0/server/rdbms/public/
I try to compile my code as shared library but every way I invoke my gcc compiler I will get a "xy.h file not found" error. But the files are existing. I have never done something with c,c++ ... how do I make a Makefile for compiling this source?
Thanks
Christian
The -I options should point to the directories (e.g. -I /usr/include/tidy) that contain the header files you need to compile against. The -L options should point to the directories (e.g. -L /usr/lib/opracle/.....) that contain the libraries you need to build against. The -l options should contain the shortened name (e.g. libfoo.so -> -lfoo) of the libraries you want to build against.

Hook file saving in Linux

How can i hook file saving in Linux systems (to show my programm dialog, opearting with them then)?
Just use the inotify interface to get notification of file system changes. See: http://linux.die.net/man/7/inotify
You can try FILE_PRELOAD utility which generate C++ code with hooks, compile and LD_PRELOAD it. After short look at it you can feel how easy to hook linux. Start point is this tutorial.
For example, if you want to change 'open call' of file /tmp/some with /tmp/replace_with:
#: FILE_PRELOAD -C "A+f:/tmp/some:/tmp/replace_with" -- bash
#: echo "HaHa" >> /tmp/some
#: ll /tmp/some
ls: cannot access /tmp/some: No such file or directory
#: cat /tmp/replace_with
HaHa
If you want to see the source of generated code just add "-p" to options.
#: FILE_PRELOAD -p -C "A+f:/tmp/some:/tmp/replace_with" -- bash
In additional all generated.cpp files you can find in /tmp/$USER/FILE_PRELOAD/cpp.
Have a nice play with linux hooks)
Generated code looks like this:
#include <sys/types.h>
#include <dlfcn.h>
#include <stdio.h>
#include <map>
#include <string>
#define I int
#define C char
#define S string
#define P printf
#define R return
using std::map;
using std::string;
typedef map<S,S> MAP;
static I (*old_open)(const C *p, I flags, mode_t mode);
extern "C"
I open (const C *p, I flags, mode_t mode){
old_open = dlsym(RTLD_NEXT, "open");
P("open hook\n");
MAP files;
files[p]=p;
files["/tmp/some"]="/tmp/replace_with";
S newpath = files[S(p)];
R old_open(newpath.c_str(), flags, mode);
}
# &compile
gcc -w -fpermissive -fPIC -c -Wall file.cpp
gcc -shared file.o -ldl -lstdc++ -o wrap_loadfile.so
LD_PRELOAD=./wrap_loadfile.so bash
nm -D /lib/libc.so.6 | grep open # we hook this syscall
If you can compile them you can link first against a custom library that provides open().
There's a stock way of doing it.
If you can't compile it, this works most of the time:
Write function _open_posthook that does syscall(NR_OPEN, ...)
Provide shared library libopenhook that provides your new open. Rembember you renamed open to _open_posthook() here unless you want recursion. Don't forget to also provide creat().
Load this library with LD_PRELOAD.
EDIT: if you're trying for security this won't work. You might be able to get away with using strace() but unless you are very careful a determined programmer can overcome that too.

Resources