What is the replacement for Linux init.h library - c

I just started to learn Linux kernel programming, and one of the tutorials use the linux/init.h and the linux/module.h. and init.h holds the printk function, but init.h does not exists.
#include <linux/module.h>
#include <linux/init.h> // source file does not exists
int main(int argc, char const *argv[])
{
printk(); // undefined
return 0;
}
// gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)
Is there a replacement for that?

Related

ESP-IDF on Eclipse IDE error with external Library

I am using esp-idf v4.1.1 with different compilers, I have used Visual Studio Code and Eclipse IDE with Espressif tool installed.
My intention is that I want to use an external library that, at the moment, only has a function that does a SHA256 hash for which the openssl sha library (<openssl/sha.h>) is used.
The problem is that I include the library as a component to my project and I call it from the main but I get the following error when building the project.
(https://i.stack.imgur.com/3EECj.png)
If I try it in the Eclipse IDE I get more information about the error and I get "undefined reference to SHA256_INIT()" as for the rest of the functions.
See main.c, dual.c and dual.h code:
main.c:
#include <stdio.h>
#include "dual.h"
void app_main(void)
{
printf("Empezamos");
char * data = "hola";
char * e = generateHashSHA256(data);
printf("%s",e);
}
Dual.c:
#include <stdio.h>
#include "dual.h"
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <openssl/sha.h>
char * generateHashSHA256(char *data){
SHA256_CTX ctx;
u_int8_t results[SHA256_DIGEST_LENGTH];
int n;
n = strlen(data);
SHA256_Init(&ctx);
SHA256_Update(&ctx, (u_int8_t *)data, n);
SHA256_Final(results, &ctx);
char *newString;
newString = malloc(sizeof(char)*SHA256_DIGEST_LENGTH*2);
memset(newString, 0, sizeof(char)*SHA256_DIGEST_LENGTH*2);
for(n=0;n<SHA256_DIGEST_LENGTH;n++)
{
printf(newString, "%s%02x", newString, results[n]);
}
return newString;
}
And Dual.h:
char * generateHashSHA256(char *data);
and CMake files:
CMake of component Dual:
idf_component_register(SRCS "dual.c"
INCLUDE_DIRS "include"
)
CMake of main folder:
idf_component_register(SRCS "main.c"
INCLUDE_DIRS ".")
CMake of project folder:
cmake_minimum_required(VERSION 3.5)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(template-app)
I compiled the library from the terminal with "gcc -o name main.c -lssl -lcrypto" and it works correctly but when compiling it in an esp-idf project nothing...
Please HELP!
I have tried everything, I have included the openssl libraries in all the esp-idf directories, I have put the paths in the CMake... etc.

macos Mojave `DYLD_INSERT_LIBRARIES` segmentation fault

When I'm trying to use the DYLD_INSERT_LIBRARIES environmental variable to insert a .dylib file to a running process on macOS Mojave, I'm encountering a segmentation fault.
System version:
macOS 14.4.4
Compiler version:
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/Library/Developer/CommandLineTools/SDKs/MacOSX10.14.sdk/usr/include/c++/4.2.1
Apple LLVM version 10.0.1 (clang-1001.0.46.4)
Target: x86_64-apple-darwin18.5.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin
debug_malloc.c (compiles to the dylib file):
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#define DYLD_INTERPOSE(_replacment,_replacee) \
__attribute__((used)) static struct{ const void* replacment; const void* replacee; } _interpose_##_replacee \
__attribute__ ((section ("__DATA,__interpose"))) = { (const void*)(unsigned long)&_replacment, (const void*)(unsigned long)&_replacee };
void* pMalloc(size_t size) //would be nice if I didn't have to rename my function..
{
printf("Allocated: %zu\n", size);
return malloc(size);
}
DYLD_INTERPOSE(pMalloc, malloc);
The running process is a test program written in C, which does nothing except calling malloc once:
test.c (compiles to test)
#include <memory.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
printf("before malloc\n");
void *a= malloc(900);
return 0;
}
Compile & execute commands
gcc -odbg.dylib -dynamiclib ./debug_malloc.c
gcc -otest ./test.c
DYLD_INSERT_LIBRARIES=./dbg.dylib ./test
Running the last command yields
Segmentation fault: 11
Whereas just running test without dylib preload works fine.

Library include with tkill

I tried use tkill ,to kill some thread with c code .
What are the library I need to include to use this function?
Running on linux ubuntu
int main(int argc , char* argv[])
{
tkill(123,9);
}
You neeed syscall(SYS_tkill, ThePid, TheSignal).
Example:
#include <unistd.h>
#include <sys/syscall.h>
#include <signal.h>
int main()
{
//kills self:
pid_t tid = syscall(SYS_gettid);
syscall(SYS_tkill,tid,SIGTERM);
return 0;
}
Glibc has had a policy of not including wrappers for non-POSIX syscalls.
They may be changing it in current/future versions but using the generic syscall syscall-making function should always work on Linux.

How to fix Segmentation fault in C

Hello i wrote my c program which will be run on linux.
I am trying to make my own shell for linux.
I have the following code below...
#include <limits.h>
#include <libgen.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#define MAX_LINE 80 /* 80 chars per line, per command, should be enough. */
int main(void){
int i = 0;
int k = 0;
int argsCount = 0;
char inputBuffer[MAX_LINE]; /*buffer to hold command entered */
int background; /* equals 1 if a command is followed by '&' */
char *args[MAX_LINE/2 + 1]; /*command line arguments */
pid_t tpid ;
pid_t child_pid;
int child_status;
char path[PATH_MAX+1];
char *progpath = strdup(args[0]);
char *prog = basename(progpath);
char temp[MAX_LINE];
}
It'is compiling well but when i try to run the code it gives me segmentation fault error
How can i fix it and why i take this error?
Your main has a wrong signature. You want
int main(int argsCount, char**args) {
and of course you should remove the internal declaration of argCount & args inside your main.
Perhaps you want instead your args & argCount to contain the parsed arguments of your own shell (but you still have to give a good signature to your main, conventionally and very often int main(int argc, char**argv).... you probably want your shell to accept the -c argument as most shells do, this would ease debugging with simplistic test cases). Then you should initialize them, and you should read some line (probably with getline) in a loop.
As I commented, you should compile with all warnings & debug info:
gcc -Wall -Wextra -g yoursource.c -o yourprog
Then use gdb ./yourprog to debug your program (see GDB documentation). valgrind should also be helpful. Of course, be sure to develop on a Linux system!
BTW, your program is not a convincing start for a shell. Use strace on some existing shell to understand what a shell needs to do. Study the source code of some existing free software shell (e.g. sash, fish, GNU bash ...). Read Advanced Linux Programming

Code compiles on AIX 5.3 but not AIX 7.1 something to do with struct shl_descriptor where is this defined?

I have some code that looks similar to the following:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/errno.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdarg.h>
#include <sys/ldr.h>
int main (int argc, char **argv)
{
int liRC = 0;
struct shl_descriptor *lstModDesc;
int liEach;
char lsBaseName[513];
char *lsTheName;
for( liEach = 0; liRC == 0; liEach++ )
{
liRC = shl_get( liEach, &lstModDesc );
if( liRC == 0 )
{
strcpy( lsBaseName, lstModDesc->filename );
lsTheName = (char *)basename( lsBaseName );
/* do more stuff */
}
}
return 0;
}
What it is doing is enumerating all the shared libraries attached to the binary. This compiles fine on AIX 5.3 but on AIX 7.1 I am getting the following concerning lstModDesc:
"modulename.c", line 2553.30: 1506-285
(S) The indirection operator cannot be
a pplied to a pointer to an incomplete
struct or union.
I cannot find where shl_get is defined on my aix 5.3 box nor can I find where struct shl_descriptor is defined either. I am stumped. I even tried outputing the preprocessed output with the -E flag to the compiler with no luck. I did a recursive grep in /usr/include. Is there somewhere else I should be searching? Where are those definitions?
Are you sure that bit of the code was included in the compilation on AIX 5.3? I just went Google-whacking with 'site:ibm.com shl_descriptor' and there is precisely one item found:
http://www-01.ibm.com/support/docview.wss?uid=swg21212239
It is pointing to a problem on HP-UX with WAS (WebSphere Application Server). There is sample code which uses <dl.h> (dynamic loader), and shows shl_descriptor and shl_gethandle() and shl_load().
Given the complete absence of hits for anything in AIX and the presence of the HP-UX platform, then you have a slightly different problem to resolve. The question is:
Why is the conditional compilation on AIX 5.3 excluding the section that uses shl_descriptor and not excluding it on AIX 7.1. You should look at the conditions wrapped around that code in the #ifdef line, and see what is used to trigger the HP-only compilation on AIX 5.3.

Resources