I need an explanation with File I/O I am recieving warnings I do not understand - linux

#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
struct string_count_struct{
char fname;
char str;
long long count;
};
void* filesearch(void* arg)
{
//get the file name
struct string_count_struct *arg_ptr = (struct string_count_struct*) arg;
int line_num = 1;
int find_result = 0;
char temp[512];
//create a file pointer
FILE *fp;
fp = fopen(arg_ptr -> fname, "r");
//dont forget error handling
if (fp == NULL){
printf("File could not be opened");
return(-1);
}
while (fgets(temp, 512, fp) != NULL) {
if ((strstr(temp, arg_ptr -> str)) != NULL) {
find_result++;
}
line_num++;
}
if(find_result = 0) {
printf("\nSorry, couldn't find a match.\n");
}
arg_ptr -> count = find_result;
//close the file
if (fp){
fclose(fp);
}
pthread_exit(0);
}
int main (int argc, char **argv)
{
if (argc < 3) {
printf("Usage: <file> <string> <arg1> <arg2>...<argN>\n", argv[0]);
exit(-1);
}
int num_args = argc - 2;
struct string_count_struct args[num_args];
//Thread Creation:
pthread_t tids[num_args];
for(int i = 0; i < num_args; i++) {
args[i].fname = atoll(argv[i + 2]);
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(&tids[i], &attr, filesearch, &args[i]);
}
//Wait until work is completed
for (int i = 0; i < num_args; i ++){
pthread_join(tids[i], NULL);
printf("blah is blah %lld\n", args[i].count);
}
return 0;
}
Here are my warnings
root#kali:~/Desktop# gcc prog2.c -lbthread
prog2.c: In function ‘filesearch’:
prog2.c:29:13: warning: passing argument 1 of ‘fopen’ makes pointer from integer without a cast [-Wint-conversion]
fp = fopen(arg_ptr -> fname, "r");
^~~~~~~
In file included from prog2.c:1:0:
/usr/include/stdio.h:274:14: note: expected ‘const char * restrict’ but argument is of type ‘char’
extern FILE *fopen (const char *__restrict __filename,
^~~~~
prog2.c:34:8: warning: return makes pointer from integer without a cast [-Wint-conversion]
return(-1);
^
prog2.c:38:21: warning: passing argument 2 of ‘strstr’ makes pointer from integer without a cast [-Wint-conversion]
if ((strstr(temp, arg_ptr -> str)) != NULL) {
^~~~~~~
In file included from prog2.c:4:0:
/usr/include/string.h:337:14: note: expected ‘const char *’ but argument is of type ‘char’
extern char *strstr (const char *__haystack, const char *__needle)
^~~~~~
prog2.c: In function ‘main’:
prog2.c:78:17: error: assignment of read-only member ‘fname’
args[i].fname = atoll(argv[i + 2]);
I am unsure of what I am doing wrong, these errors are preventing my program from correctly reading through the desired files and calculating the # of occurrences of a particular string that the user will select. I have fixed my error but not the warnings.
The program will take a command line argument, create a separate thread for each file to be searched through, search through each file, and then give the results. I plan on using a Mutex for further refinement, but right now I am just trying to solve my I/O issues.

Just addressing some of the warnings, I'm not at all sure if this will make the code work:
In line 29, fopen expects a filename (char *) but fname is just a char in string_count_struct. Make it a char*. In the main function you convert one of the arguments from ASCII to long long and assign it to fname, which shall later be used as a file name for fopen(). That's probably not what you want to do.
In line 34, you return -1 which is not a pointer. You declared the function to be returning a void pointer. Make it return(0) (or return(NULL)).
Same as in line 29 happens in line 38: str in the struct string_count_struct is just a char, but strstr expects a char*. Make it a char*, too.
Your "Usage" is missing a "%s" format string to actually print the argument argv[0].

Related

Getting an error while trying to access struct file private_data

The problem:
I have a pointer to struct file called flip, an int called cmd and an unsigned long called arg.
The private_data field in struct file points
the private_data structure is defined as follows:
typedef struct {
unsigned char key;
int read_state;
} my_private_data;
and ioctl function is defined as follows:
int my_ioctl(struct inode *inode, struct file *filp, unsigned int cmd, unsigned long arg)
{
switch (cmd) {
case CMD_CHANGE_KEY:
filp->private_data->key = (char)cmd;
filp->private_data->read_state = (int)arg;
break;
case CMD_SET_READ_STATE:
filp->private_data->key = (char)cmd;
filp->private_data->read_state = (int)arg;
break;
case CMD_ZERO:
kfree(buff_caesar);
break;
}
return 0;
}
However, when I try to compile the code, I get the following warning/error:
Warning: dereferencing 'void *' pointer.
Requesting for Member 'key' in something not a struct or union.
Warning: dereferencing 'void *' pointer.
Requesting for Member 'read_state' in something not a struct or union.
What can I do to fix this?
Here's the open method:
int my_open(struct inode *inode, struct file *filp)
{
int minor = MINOR(inode->i_rdev);
if(minor == ONE) {
/* Caesar*/
filp->f_op = &fops_caesar;
}
else if(minor == TWO){
/*Xor*/
filp->f_op = &fops_xor;
}
else return -ENODEV;
my_private_data* privateData = NULL;
privateData = kmalloc(sizeof(my_private_date),GFP_KERNEL);
if (privateData==NULL){
return -1;
}
filp->private_data = privateData;
return 0;
}
The struct file member .private_data is defined as a void *, so filp->private_data itself has no members. If you want to store data items to your structure, you could access them with a local of that type; eg, within my_ioctl() :
my_private_data *info;
. . .
if (filp->private_data == NULL) {
error handling
}
info = filp->private_data;
. . .
info->key = (char) cmd;
info->read_state = (int) arg;

serialport access in kernel space

I want to use serial port in kernel space, I have found some code which was in user space, I tried to convert the codes to work in kernel space...
This is my code
#include <linux/termios.h>
#include <linux/unistd.h>
#include <linux/signal.h>
#include <linux/fcntl.h>
#include <linux/types.h>
#include <linux/socket.h>
#include <linux/in.h>
struct file * fp;
...
struct termios termAttr;
struct sigaction saio;
oldfs = get_fs();
set_fs(KERNEL_DS);
fp = filp_open("/dev/ttymxc0", O_RDWR | O_NOCTTY | O_NDELAY,0);
if(fp == NULL)
printk(KERN_ALERT "Serial openning error!!.\n");
else{
saio.sa_handler = signal_handler_IO;
saio.sa_flags = 0;
saio.sa_restorer = NULL;
sigaction(SIGIO,&saio,NULL);
fcntl(fp, F_SETFL, O_NDELAY|FASYNC);
fcntl(fp, F_SETOWN, THIS_MODULE);
tcgetattr(fp,&termAttr);
cfsetispeed(&termAttr,B115200);
cfsetospeed(&termAttr,B115200);
termAttr.c_cflag &= ~PARENB;
termAttr.c_cflag &= ~CSTOPB;
termAttr.c_cflag &= ~CSIZE;
termAttr.c_cflag |= CS8;
termAttr.c_cflag |= (CLOCAL | CREAD);
termAttr.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
termAttr.c_iflag &= ~(IXON | IXOFF | IXANY);
termAttr.c_oflag &= ~OPOST;
tcsetattr(fp,TCSANOW,&termAttr);
printk(KERN_ALERT "Serial configured....\n");
vfs_write(fp, "HI",2, NULL);
filp_close(fp, NULL);
set_fs(oldfs);
}
while compiling, I got following errors:
note: each undeclared identifier is reported only once for each function it appears in
error: implicit declaration of function 'sigaction' [-Werror=implicit-function-declaration]
sigaction(SIGIO,&saio,NULL);
^
error: implicit declaration of function 'fcntl' [-Werror=implicit-function-declaration]
fcntl(fp, F_SETFL, O_NDELAY|FASYNC);
^
error: implicit declaration of function 'tcgetattr' [-Werror=implicit-function-declaration]
tcgetattr(fp,&termAttr);
^
error: implicit declaration of function 'cfsetispeed' [-Werror=implicit-function-declaration]
cfsetispeed(&termAttr,B115200);
^
error: implicit declaration of function 'cfsetospeed' [-Werror=implicit-function-declaration]
cfsetospeed(&termAttr,B115200);
^
error: implicit declaration of function 'tcsetattr' [-Werror=implicit-function-declaration]
I am cross compiling this driver and I already compiled Linux source, I have searched for this functions in my Linux source code but I did not find any of this functions! what should I use instead of this functions?
Edit 1:
I have changed my code into this:
//serial
struct ktermios termAttr;
struct sigaction saio;
loff_t pos =0;
struct tty_struct *tty;
serialfp = file_open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY,0);
if(serialfp == NULL)
printk(KERN_ALERT "ARIO RMG Serial openning error!!.\n");
else{
tty = (struct tty_struct *)serialfp->private_data;
tty_termios_encode_baud_rate(&tty->termios,B115200,B115200 );
printk(KERN_ALERT "ARIO RMG Serial configured....\n");
pos = serialfp->f_pos;
file_write(serialfp, "\n\n\n\n\nThis is first test of sending serial data from kernel module\n\n\n\n\n",70,&pos);
serialfp->f_pos=pos;
serial_thread_condition = 1;
mutex_init(&serial_mutex);
task1 = kthread_create(&thread_function, (void *)&pid1, "pradeep");
wake_up_process(task1);
printk(KERN_ALERT "data received:%s\n\n\n\n\n\n\n\n",rmg_drvstruct[0].RxSerial);
}
I am able to send data into serial port now, I also created a thread to read data from serial port. with following code:
static int thread_function(void *data){
loff_t pos;
while(serial_thread_condition){
mutex_lock(&serial_mutex);
if (IS_ERR(serialfp)) {
mutex_unlock(&serial_mutex);
serial_thread_condition=0;
return 0;
}
pos = serialfp->f_pos;
printk(KERN_INFO "try to read from serial\r\n");
if(file_read(serialfp, rmg_drvstruct[0].RxSerial, 100, &pos)>0)
{
printk(KERN_INFO "Data: %s\r\n", rmg_drvstruct[0].RxSerial);
serialfp->f_pos = pos;
serial_thread_condition = 0;
mutex_unlock(&serial_mutex);
break;
}
mutex_unlock(&serial_mutex);
}
}
int file_read(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size)
{
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_read(file, data, size, &offset);
set_fs(oldfs);
return ret;
}
But I got nothing in serial port in my thread, I wanted to use interrupts for new received bytes, but irq_request() function makes kernel panic and computer freezes out, so what should I do to properly receive data with interrupt or a thread?
So after a while searching and testing I used this code to communicate with serial port, But I have changed a bit of it.
Please read my edit before using this code
I used a thread to read from serial port, cause using interrupts (request_irq) makes my kernel to shoot himself in the head!
So this is my code for anyone who might want to use serial port in kernel module.
#include <linux/tty.h>
#include <linux/delay.h>
#include <linux/kthread.h>
#include<linux/mutex.h>
MODULE_LICENSE("GPL");
struct file * serialfp;
struct task_struct *task1;
int pid1 = 1;
....
static struct file *file_open(const char *path, int flags, int rights)
{
struct file *filp = NULL;
mm_segment_t oldfs;
int err = 0;
oldfs = get_fs();
set_fs(get_ds());
filp = filp_open(path, flags, rights);
set_fs(oldfs);
if (IS_ERR(filp)) {
err = PTR_ERR(filp);
return NULL;
}
return filp;
}
static void file_close(struct file *file)
{
filp_close(file, NULL);
}
static int file_read(struct file *file, unsigned long long *offset, unsigned char *data, unsigned int size)
{
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_read(file, data, size, offset);
set_fs(oldfs);
return ret;
}
static int file_write(struct file *file, unsigned long long *offset, unsigned char *data, unsigned int size)
{
mm_segment_t oldfs;
int ret;
oldfs = get_fs();
set_fs(get_ds());
ret = vfs_write(file, data, size, offset);
set_fs(oldfs);
return ret;
}
int file_sync(struct file *file)
{
vfs_fsync(file, 0);
return 0;
}
static int thread_function(void *data){
loff_t pos;
int len;
while(serial_thread_condition){
mutex_lock(&serial_mutex);
if (IS_ERR(serialfp)|| serial_thread_condition==0) {
printk(KERN_INFO "serial reading thread has been terminated.\r\n");
mutex_unlock(&serial_mutex);
serial_thread_condition=0;
return 0;
}
pos = serialfp->f_pos;
if((len=file_read(serialfp,&pos, rmg_drvstruct[0].RxSerial, 100))>0){
printk(KERN_INFO "Received data : %s\r\n", rmg_drvstruct[0].RxSerial);
serialfp->f_pos = pos;
file_write(serialfp,&pos,"I have received:",16);
file_write(serialfp,&pos, rmg_drvstruct[0].RxSerial,len);
serialfp->f_pos = pos;
}
file_sync(serialfp);
mutex_unlock(&serial_mutex);
mdelay(5);
}
}
so my device opening function is like:
static int device_open(struct inode *inode, struct file *file){
loff_t pos =0;
struct tty_struct *tty;
...
serialfp = file_open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NDELAY ,0);
if(serialfp == NULL)
printk(KERN_ALERT "ARIO RMG Serial openning error!!.\n");
else{
tty = (struct tty_struct *)serialfp->private_data;
tty_termios_encode_baud_rate(&tty->termios,B115200,B115200 );
printk(KERN_ALERT "Serial configured....\n");
pos = serialfp->f_pos;
file_write(serialfp,&pos,"\n\n\n\n\nThis is first test of sending serial data from kernel module\n\n\n\n\n",70);
serialfp->f_pos=pos;
file_sync(serialfp);
serial_thread_condition = 1;
mutex_init(&serial_mutex);
task1 = kthread_create(&thread_function, (void *)&pid1, "pradeep");
wake_up_process(task1);
}
...
}
and closing module function:
static int device_release(struct inode *inode, struct file *file){
...
if (!IS_ERR(serialfp)) {
mutex_lock(&serial_mutex);
printk(KERN_INFO " Trying to realease serail thread");
if(serial_thread_condition==1){
int i=0;
while(i++<256)serial_thread_condition =0;
}
printk(KERN_INFO " serial thread released.");
file_close(serialfp);
mutex_unlock(&serial_mutex);
}
....
}
Thanks to 0andriy for helping and dmeister for his answer in that link.
EDIT:
So I did what I wanted in kernel, opening a file in kernel space regardless of any suggestions not to do so.
But I needed to open a file in kernel module...
So why every body said don't open user space files in kernel?
In these two articles there are some reasons to not using files in kernel , this link and this link.
there are some reasons like:
1- kernel module may lose CPU at any time and the file which is opened by kernel may close.
2- I am not sure really about this, But they said files needs to have a process to stay opened but kernel module itself is not a process(maybe I am wrong!).
3- If any error happens while working with files(open/close/read/write), kernel module can not handle it and causes kernel panic...
I have experienced a lot of kernel panics only while opening and closing the file I wanted to work with. These are some of reasons why you should not use files in kernel modules, So as every body said before "If you need to use files in kernel module you probably did something wrong in your code!"

can we perform the operation using functions like fgets(), fputs(), feof(),etc. for the fifo file like we use for the normal file?

I have an assignment where I have to transfer the file from a client process to server process using fifo.I have tried to deal with fifo file as the other files we create in the system. It compiled without any error but it didn't execute properly.Can someone please give me an idea about the fifo file structure inside the computer system? What processes and functions are present for it ?Till now, I know how to use create(),read(),write(), open() function for fifo file.Also, I would be grateful if someone could help me to correct my program?
My client and server program are as follows:-
Client Program:-
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int fd;
char *myfifo ="/tmp/myfifo";
char str[80];
FILE *fp;
char filename[20];
printf("\nEnter filename: ");
gets(filename);
mkfifo(myfifo,0666);
fp = fopen(filename,"r");
if(fp == NULL)
{
printf("\nError opening the file");
exit(1);
}
fd = open(myfifo, O_WRONLY);
while(fgets(str,80,fp)!=NULL)
{
write(fd,str,strlen(str)+1);
}
close(fd);
fclose(fp);
return 0;
}
Client Program:-
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int fd1;
char *myfifo ="/tmp/myfifo";
char str1[80], filename[20];
FILE *fp1, *fp2;
fd1= open(myfifo, O_RDONLY);
fp1 = fopen(filename,"r");
fp2 = fopen(filename,"w");
while(!feof(fp1))
{
read(fd1,str1,strlen(str1)+1);
fputs(str1,fp2);
}
return 0;
}
Yes, but you have a few small problems in your programs. in the first:
write(fd, str, strlen(str)+1);
is a bit unconventional. This sends the string plus its end-of-string delimiter (\0) into the fd. One doesn't normally do this with strings, strlen(str) is probably what you want.
in the second:
fp1 = fopen(filename,"r");
fp2 = fopen(filename,"w");
filename has not been assigned a value, so both of these opens will almost certainly fail. When they do, they return a NULL pointer, so the first attempt to use them:
while(!feof(fp1))
will likely cause a segment violation. Also, you don't use fp1 anyways, so if feof(fp1) returned 1, it would always return 1. You want to base this loop on when the fifo is exhausted, which means there is no data in it, and nobody has it open for write. So changing this program around a bit yields:
#include<stdio.h>
#include<string.h>
#include<fcntl.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<unistd.h>
int main()
{
int fd1;
char *myfifo ="/tmp/myfifo";
char str1[80];
ssize_t n;
fd1= open(myfifo, O_RDONLY);
while ((n=read(fd1,str1,sizeof str1)) > 0)
{
fwrite(str1, 1, n, stdout);
}
return 0;
}
While this set of changes works, it doesn't address your other question, about using stdio functions with pipes. The answer is yes, and here is another functional rewrite of your second program:
#include<stdio.h>
int main()
{
char *myfifo ="/tmp/myfifo";
FILE *fp;
int c;
if ((fp = fopen(myfifo, "r")) != NULL) {
while ((c = getc(fp)) != EOF) {
putchar(c);
}
fclose(fp);
}
return 0;
}
Also, in the first, the critical bit with stdio:
...
FILE *fi = fopen(myfifo, "a");
while(fgets(str,80,fp)!=NULL)
{
fputs(str, fi);
}
fclose(fi);
...
as in the second, the loop could have been implemented with getc, putc.
A general refinement might be functions like these:
ssize_t FCopy(FILE *in, FILE *out) {
int c;
ssize_t len = 0;
while ((c = getc(in)) != EOF) {
len++;
if (putc(c, out) != c) {
return -len;
}
}
return len;
}
ssize_t FileAppend(char *from, char *to) {
FILE *in, *out;
ssize_t n = 0;
if ((in = fopen(from, "rb")) != NULL) {
if ((out = fopen(to, "ab")) != NULL) {
n = FCopy(in, out);
fclose(out);
} else {
n = -1;
}
fclose(in);
} else {
n = -1;
}
return n;
}
so your main would look more like:
...
char filename[80];
printf("Enter a file to store the data in: ");
if (fgets(filename, sizeof filename, stdin)) {
filename[strlen(filename)-1] = '\0';
if (FileAppend(myfifo, filename) < 0) {
printf("Error: could not save data to %s\n", filename);
}
}
....

Passing struct pointer als parameter for pthread function

Hello I'm fallowing a tutorial for pthreading, but something went wrong down the road since can't make the reference to my struct passable by
pthread_create(...,(void* stuctName)) , I'm looking for some advices or fixes since I have no idea where or what I messed up...
code:
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <unistd.h>
#include <math.h>
struct dataBlock{
struct node *root;
int listSize;
int forIndex;
};
struct node { // std linked list node
int value;
int worker;
struct node *next;
};
int slots = 3; // only 3 threads are allowed to access the list
int availableCheck(){ // check if thread can acces the list
if(slots < 3) return 0;
else return -1;
}
pthread_mutex_t mutp = PTHREAD_MUTEX_INITIALIZER; //condvar mutex
pthread_cond_t condvar = PTHREAD_COND_INITIALIZER; //condvar
void * worker( void *data ){ //WORKER FUNCTION
printf( "* Thread start: ^\n");
struct dataBlock *inData = (struct dataBlock *) data;
struct node *root = data->root;
int listSize = data->listSize;
int forIndex = data ->forIndex;
// printf( " * I am %li _ worker # %li : \n", forIndex, pthread_self() );
pthread_mutex_lock( &mutp );
if(availableCheck() < 0){
printf( " ^^^ List not available yet... \n" );
pthread_cond_wait( &condvar, &mutp );
}
// printf( "* Got data: %lu \n", index );
pthread_cond_signal( &condvar ); //
pthread_mutex_unlock( &mutp );
return NULL;
}
int main( int argc, char *argv[] ){
if ( argc != 3 ){
printf( "Programm must be called with \n NR of elements and NR of workers! \n " );
exit( 1 );
int i;
struct node *root;
struct node *iterator;
//prepare list for task
int listSize = atoi(argv[1]);
int nrWorkers = atoi(argv[2]);
root = malloc(sizeof( struct node) );
root->value = rand() % 100;
root->worker = 0;
iterator = root;
for( i=1; i<listSize; i++ ){
iterator->next = malloc(sizeof(struct node));
iterator = iterator->next;
iterator->value = rand() % 100;
iterator->worker = i % nrWorkers;
printf("node #%d worker: %d value: %d\n", i, iterator->worker,iterator->value);
}
// Create all threads to parse the link list
int ret, *id;
printf("workersInput: %d\n",nrWorkers);
pthread_t w_thread;
pthread_t* w_threads = malloc(nrWorkers * sizeof(w_thread));
struct dataBlock *data = malloc(sizeof(struct dataBlock));
data->root = root;
data->listSize = listSize;
for( i=0; i < nrWorkers; i++ ){ // CREATING THREADS
data->forIndex = i;
ret = pthread_create ( &w_threads[i], NULL, worker, (void *) data );
if( ret ) {
perror("Thread creation fail");
exit(2);
}
}
for ( i = 0; i < nrWorkers; i++){
pthread_join(w_threads[i],NULL);
}
free(root);
free(iterator);
return 0;
}
Compiling(Wall flag)
s.c: In function ‘worker’:
s.c:32:26: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:32:26: error: request for member ‘root’ in something not a structure or union
s.c:33:22: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:33:22: error: request for member ‘listSize’ in something not a structure or union
s.c:34:22: warning: dereferencing ‘void *’ pointer [enabled by default]
s.c:34:22: error: request for member ‘forIndex’ in something not a structure or union
s.c:34:6: warning: unused variable ‘forIndex’ [-Wunused-variable]
s.c:33:6: warning: unused variable ‘listSize’ [-Wunused-variable]
s.c:32:15: warning: unused variable ‘root’ [-Wunused-variable]
s.c:31:20: warning: unused variable ‘inData’ [-Wunused-variable]
s.c: In function ‘main’:
s.c:81:12: warning: variable ‘id’ set but not used [-Wunused-but-set-variable]
Use inData->root instead of data->root etc... (or cast explicitly always your data) since data is a void* pointer, that is a pointer to some unspecified data type.

Device Driver IOCTL pass int

I have written a device driver and need to pass an int value to it. Am using copy_from_user() for this. Here is what I have done so far,
#define MY_MAGIC 'G'
#define TEST_IOCTL _IO(MY_MAGIC, 0)
#define PASS_STRUCT_ARRAY_SIZE _IOW(MY_MAGIC, 1, int )
#define TEST_IOCTL_ONE _IO(MY_MAGIC, 2)
int major;
int device_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg){
int ret, SIZE;
switch(cmd){
case TEST_IOCTL:
printk("NO argument IOCTL called\n");
break;
case PASS_STRUCT_ARRAY_SIZE:
printk("Inside PASS_STRUCT_ARRAY_SIZE\n");
ret = copy_from_user(SIZE, arg, sizeof(int));
if(ret < 0){
printk("Error in PASS_STRUCT_ARRAY_SIZE\n");
return -1;
}
printk("Struct Array Size : %d\n",SIZE);
break;
case TEST_IOCTL_ONE:
printk("NO argument IOCTL_ONE called\n");
break;
default :
return -ENOTTY;
}
return 0;
}
When I call the TEST_IOCTL & TEST_IOCTL_ONE the module works properly. However when I call PASS_STRUCT_ARRAY_SIZE the system freezes.
Userspace code is this,
if(ioctl(fd, PASS_STRUCT_ARRAY_SIZE, 10) < 0){
perror("PASS_STRUCT_ARRAY_SIZE : ");
return -1;
}
What can I be doing wrong?
Sorry, my previous answer was wrong. Since the argument is an integer you should just use it without copy_from_user:
SIZE = arg;
You only need copy_from_user when the argument is a pointer.

Resources