Is there any C function that can check whether a Linux kernel configuration option (CONFIG_AAAA option in /boot/config- file) is set?
Or I have to develop it myself.
After a long search with no result, I developed a function myself. Here is the code:
static int is_kconfig_set(const char *config) {
int ret = 0;
struct utsname utsname;
char pattern[BUFSIZ], buf[BUFSIZ];
FILE *fp = NULL;
if (uname(&utsname) == -1)
return 0;
memset(pattern, 0, sizeof(pattern));
memset(buf, 0, sizeof(buf));
sprintf(pattern, "%s=y", config);
sprintf(buf, "/boot/config-%s", utsname.release);
fp = fopen(buf, "r");
if (fp == NULL)
return 0;
while(fgets(buf, sizeof(buf), fp) != NULL) {
if (strncmp(buf, pattern, strlen(pattern)) == 0) {
ret = 1;
break;
}
}
fclose(fp);
return ret;
}
To check whether CONFIG_CPU_FREQ is set:
if (is_kconfig_set("CONFIG_CPU_FREQ"))
return 1;
return 0;
The only way I can think is to create your own following the approach in kernel/configs.c (this is the code that creates /proc/config.gz).
Related
I need to check if file is PE file or not. I need to check first two byte is MZ or not and I did this.
This is my task: When verifying the PE format, not only according to the MZ expression, but also using the conditions that the IMAGE_NT_HEADERS structure is read and the Signature field is verified by reading the IMAGE_FILE_HEADER field and the Machine field is equal to the Th value IMAGE_FILE_MACHINE_I386 or IMAGE_FILE_MACHINE_AMD64.
I cannot figure how can do the rest of them. I hope you can help me.
int checkPE(char *file){
int fd=open(file,READ_FLAGS,0777);
char buffer[TWOBYTE+1] = {'\0'};
size_t bytes_read;
char ch;
if(fd==-1){ //if file cannot be opened give a error message.
perror("The file cannot be opened.\n");
return -1;
}
bytes_read = read(fd,buffer,TWOBYTE);
if(bytes_read==-1){
perror("Error while reading file\n");
return -1;
}
if(strcmp(buffer,MZ)!=0){
return -1;
}
int closeFlag = close(fd);
if(closeFlag==-1){
perror("The file cannot be closed.\n");
return -1;
}
}
There is nothing more than just parsing some structures. You have already the algorithm. I assume that you just need the implementation. Consider the following example utility.
PS: For further details, just comment it below.
#include <stdio.h>
#include <windows.h>
BOOL CheckValidity(BYTE* baseAddress);
int main(int argc, char* argv[]) {
if (argc != 2)
{
printf("You didn't specified a PE file.\n");
printf("Usage: CheckPEImage.exe <Full path of PE File>\n");
return -1;
}
HANDLE hFile = CreateFileA(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile == INVALID_HANDLE_VALUE)
return -1;
HANDLE hMemoryMap = CreateFileMappingA(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (!hMemoryMap)
return -2;
PBYTE baseAddress = (PBYTE)MapViewOfFile(hMemoryMap, FILE_MAP_READ, 0, 0, 0);
if (!baseAddress)
return -3;
printf("PE Image is %s.\n", CheckValidity(baseAddress) ? "valid" : "invalid");
getchar();
return 0;
}
BOOL CheckValidity(BYTE* baseAddress)
{
PIMAGE_DOS_HEADER lpDosHeader;
PIMAGE_FILE_HEADER lpFileHeader;
PIMAGE_NT_HEADERS lpNtHeaders;
PIMAGE_OPTIONAL_HEADER lpOptionalHeader;
lpDosHeader = (PIMAGE_DOS_HEADER)baseAddress;
lpNtHeaders = (PIMAGE_NT_HEADERS)(baseAddress + lpDosHeader->e_lfanew);
if (lpDosHeader->e_magic != IMAGE_DOS_SIGNATURE)
return FALSE;
if (lpNtHeaders->Signature != IMAGE_NT_SIGNATURE)
return FALSE;
if (lpNtHeaders->FileHeader.Machine != IMAGE_FILE_MACHINE_I386 && lpNtHeaders->FileHeader.Machine != IMAGE_FILE_MACHINE_AMD64)
return FALSE;
return TRUE;
}
My question is simple, I am a beginner in c language, I am currently developing the hangman game on code blocks. To do this a restriction was imposed on me, the file in which my word dictionary is located, I must pass it as an argument in my program. I saw in my research that in the project -> Set program's arguments .. tab, we can add arguments, but I cannot pass my text file as a parameter. Anyone have a solution please? thank you so much
I pass it like this :
But when I build my program it does not detect my file.
this is the code that tries to read the file
char *read_file(int ac, char **av, int *lifes, char **word)
{
int fd = -1;
int nb_caractere = 0;
char *buffer = calloc(1,sizeof(char));
if((fd = open(av[1], O_RDONLY)) == -1)
{
printf("Aucun fichier ou dossier. \n");
return (NULL);
}
lseek(fd, 0, SEEK_SET);
while(read(fd, buffer, 1) > 0)
{
nb_caractere++;
}
lseek(fd, 0, SEEK_SET);
buffer = calloc(nb_caractere+1, sizeof(char));
memset(buffer, 0, nb_caractere+1);
read(fd, buffer, nb_caractere);
close(fd);
return (buffer);
}
and the main how call this funtion is this :
int main(int ac, char **av)
{
int lifes = 10;
char *word = NULL;
char *hide_word = NULL;
srand(time(NULL));
if(ac < 2 || ac > 3)
{
printf("Nombres d'arguments incorrect !\n");
return (84);
}
if(ac == 3)
{
lifes = atoi(av[2]);
}
if(init_game(lifes, &word, &hide_word, read_file(ac, av, &lifes,
&word)) == 84 )
{
free(word);
return (84);
}
game_loop(lifes, word, hide_word);
free(word);
free(hide_word);
return 0;
}
the output of this code is "aucun fichier ou dossier"
I want to run shell command in C program and get stdout output.
I did it in this function:
int run_shell_cmd_nout(const char* cmd)
{
FILE *fp;
char out[4096] = {0};
char str[256] = {0};
char full_cmd[1024] = {0};
int result = 0;
// Compose full shell command
if (!sprintf(full_cmd, "/system/bin/%s", cmd))
{
printf("Failed to compose full shell command\n");
return -1;
}
// Open the command for reading.
fp = popen(full_cmd, "r");
if (fp == NULL)
{
printf("Failed to run command\n");
return -1;
}
// Read the output a line at a time - output it.
while(!feof(fp))
{
if(fgets(str, 256, fp) != NULL)
{
result = -1;
strcat(out, str);
}
}
pclose(fp);
if (result != 0)
{
printf("%s\n", out);
return -1;
}
return 0;
}
But it doesn't work with insmod.
Is there any way to intercept all outputs when invoke insmod?
The loop was supposed to break but it keeps stuck on the fgets. process_P1 talks to inputHandler through a pipe. The problem is that inputHandler doesn't realize when process_P1 stops writing...the 'lalala' printf is never reached.
void process_P1(char *argv[], int fd[2], pid_t child)
{
int bytes = 0;
static char bufferIn[BUFFER_SIZE];
static char bufferOut[BUFFER_SIZE];
char line[BUFFER_SIZE];
// close reading end of pipe
close(fd[0]);
FILE *in = fopen(getInput(argv), "r");
FILE *out = fdopen(fd[1], "w");
if (in == NULL) {
sys_err("fopen(r) error (P1)");
}
int ret = setvbuf(in, bufferIn, _IOLBF, BUFFER_SIZE);
if (ret != 0) {
sys_err("setvbuf error (P1)");
}
if (out == NULL) {
sys_err("fdopen(w) error (P1)");
}
ret = setvbuf(out, bufferOut, _IOLBF, BUFFER_SIZE);
if (ret != 0) {
sys_err("setvbuf error (P1)");
}
while (fgets(line, BUFFER_SIZE, in) != NULL)
{
fprintf(out, "%s", line);
bytes += count(line) * sizeof(char);
}
// alert P2 to stop reading
//fprintf(out, "%s", STOP);
fclose(in);
fflush(out);
fclose(out);
printf("P1: file %s, bytes %d\n", getInput(argv), bytes);
// wait P2 ends
if (waitpid(child, NULL, 0) < 0) {
sys_err("waitpid error (P1)");
}
}
void *inputHandler(void *args)
{
int ret;
static char bufferIn[BUFFER_SIZE];
char line[BUFFER_SIZE];
struct node *iterator;
int *fd = (int*)args;
close(fd[1]);
FILE *in = fdopen(fd[0], "r");
if (in == NULL) {
sys_err("fdopen(r) error (P2)");
}
ret = setvbuf(in, bufferIn, _IOLBF, BUFFER_SIZE);
if (ret != 0) {
sys_err("setvbuf(in) erro (P2)");
}
while (fgets(line, BUFFER_SIZE, in) != NULL)
{
// printf("%s", line);
iterator = firstArg;
while (iterator->next != NULL)
{
ret = sem_wait(&(iterator->sem));
if (ret == 0) {
strcat(iterator->buffer, line);
} else {
sys_err("sem_wait error");
}
ret = sem_post(&(iterator->sem));
if (ret != 0) {
sys_err("sem_post error");
}
iterator = iterator->next;
}
line[0] = '\0';
}
printf("lalala\n");
iterator = firstArg;
while (iterator->next != NULL)
{
ret = sem_wait(&(iterator->sem));
if (ret != 0) {
sys_err("sem_wait error");
}
iterator->shouldStop = 1;
ret = sem_post(&(iterator->sem));
if (ret != 0) {
sys_err("sem_post error");
}
iterator = iterator->next;
}
fclose(in);
return NULL;
}
The problem is probably not in the code you show. Since you mention a pipe, your problem is probably in the plumbing related to that — and most likely, you did a dup2() on one end of the pipe to make it into standard input or standard output, but you forgot to close the file descriptor that you duplicated, or you forgot to close the other end. The fgets() won't terminate until there's no process that could write to the pipe that it is reading from. If the process that is reading still has the write end of the pipe open, it will stay stuck in the read, waiting for it to write something.
So, look hard at your piping code. Make sure you've closed both the values returned by pipe() after you've duplicated one end to standard input or standard output.
I'm working on doing file uploads using the libs3 library found here: http://libs3.ischo.com/dox/index.html
I'm getting back an error of S3StatusConnectionFailed though. Can someone point me to how this situation could arise? This is my function for uploading files into S3.
int putFileIntoS3 (char *fileName, char *s3ObjName) {
S3Status status;
char *key;
struct stat statBuf;
uint64_t fileSize;
FILE *fd;
char *accessKeyId;
char *secretAccessKey;
put_object_callback_data data;
accessKeyId = S3_ACCESS_KEY;
secretAccessKey = S3_SECRET_ACCESS_KEY;
key = (char*) strchr(s3ObjName, '/');
if (key == NULL) {
printf("S3 Key not defined!!!!");
return (-1);
}
*key = '\0';
key++;
if (stat(fileName, &statBuf) == -1) {
printf("Unknown input file");
return(-1);
}
fileSize = statBuf.st_size;
fd = fopen(fileName, "r");
if (fd == NULL) {
printf("Unable to open input file");
return(-1);
}
data.infile = fd;
S3BucketContext bucketContext =
{s3ObjName, S3ProtocolHTTP, S3UriStylePath, accessKeyId, secretAccessKey}
S3PutObjectHandler putObjectHandler = {
{ &responsePropertiesCallback, &responseCompleteCallback },
&putObjectDataCallback
};
if ((status = S3_initialize("s3", S3_INIT_ALL)) != S3StatusOK) {
printf("Failed to initialize libs3: %s\n",S3_get_status_name(status));
return(-1);
}
S3_put_object(&bucketContext, key, fileSize, NULL, 0, &putObjectHandler, &data);
if (statusG != S3StatusOK) {
printf("Put failed: %i\n", statusG);
S3_deinitialize();
return(-1);
}
S3_deinitialize();
fclose(fd);
return(0);
}
I get back "Put failed: 46", which I'm pretty sure means that it's an S3StatusConnectionFailed error.
Any help would be great, or even pointers to a boto-like library that I can use instead of the drudgery that is doing this in C++.
Thanks!
Ok, i tried putting non-null value but i was getting same error. i found out the reason for this : you must set the contentLength of "data" (of type put_object_callback_data) as below
fileSize = statBuf.st_size;
//viren+
data.contentLength = fileSize;
//viren-
//..
data.infile = fd;
Oh, and also make sure you have correct permissions set on your bucket.
use NULL for first param in S3_initialize. S3StatusConnectionFailed is indicative that "s3" url maybe wrong. i assume you are providing correct S3_ACCESS_KEY and S3_SECRET_ACCESS_KEY.