Help, no clue how to make this Makefile - linux

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.

Related

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)

Linux. SOL_NETLINK not defined

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;
}

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.

cmake include_directories order AFTER/BEFORE

I have a file in source tree which name is "time.h", exactly as system "time.h". This cannot be changed. I have encountered a problem with cmake that when I use include_library option it is translated to -I flag which means that my custom "time.h" takes prcedence over system time.h even for <> includes. This is a definiti no-no.
I tried using include_directories (AFTER dir1 dir2) but it still generate -I option instead of expected -idirafter.
I don't think this is a problem with CMake; I believe gcc will always find your "time.h" before the system one, regardless of whether you use quotes or brackets in the #include and regardless of the various options in include_directories. See the entries for -I and -isystem in the gcc documentation
The AFTER option of CMake's include_directories relates only to the order of the directories as listed in the gcc command, it doesn't relate to gcc's -idirafter flag.
It's not a great plan to have your own files with identical names to system files, but if your hands are tied, you could avoid this issue without renaming time.h by qualifying the path for your own includes more fully, so rather than e.g.
CMakeLists.txt: include_directories(${PROJECT_SOURCE_DIR}/src)
header file: #include <time.h> // we want but don't get system one
#include "time.h" // we want and get own custom one
something more like
CMakeLists.txt: include_directories(${PROJECT_SOURCE_DIR})
header file: #include <time.h> // we want and get system one
#include "src/time.h" // we want and get own custom one
An alternative option would be to stick with your current #include setup (using angle brackets for the system time.h and quotes for your own) and not use include_directories at all in the CMakeLists.txt. Instead I think you could replace it with something like:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -iquote ${PROJECT_SOURCE_DIR}/src")
Using -iquote is probably a better option than -idirafter since the directory specified by -idirafter is (in this case incorrectly) treated as a system directory, and as such has warnings suppressed, etc.
If you do go for this choice, it's probably worth commenting the CMakeLists.txt to explain why there is no include_directories to avoid future refactoring reverting back to use the more normal include_directories command.
All in all, your best option if at all possible would be to rename your "time.h" file though.

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