LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2009, 02:42 AM   #1
sunnyboy
LQ Newbie
 
Registered: Jul 2009
Posts: 3

Rep: Reputation: 0
Question raw device I/O


Hello!
I use an USB device an raw device.The USB device file is:/dev/sda1 and I use raw command to bind it to /dev/raw/raw1:
[root@localhost zhang]# raw /dev/raw/raw1 /dev/sda1
/dev/raw/raw1: bound to major 8, minor 1
[root@localhost zhang]# raw -qa
/dev/raw/raw1: bound to major 8, minor 1

Then I use write and read system call to write to and read from the raw devices,the program is :
1#include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <string.h>
6
7 int main(void){
8
9 char *p = "test raw devices";
10 int fd;
11 int len = strlen(p);
12 int n=0;
13 char buf[len+1];
14
15 if((fd = open("/dev/raw/raw1",O_RDWR)) == -1){
16 perror("fail to open /dev/raw/raw1\n");
17 exit(1);
18 }
19 printf("/dev/raw/raw1:%d\n",fd);
20
21 if((n=write(fd,p,len)) == -1){
22 perror("fail to write.\n");
23 exit(1);
}
25
26 printf("write done.%d chars written!\n",n);
27
28 lseek(fd,0,SEEK_SET);
29
30 n = 0;
31 if((n=read(fd,buf,len)) == -1){
32 perror("fail to read\n");
33 exit(1);
34 }
35 buf[len+1] = '\0';
36 printf("read ok,%d chars read:%s\n",n,buf);
37
38 close(fd);
39
40 return 0;
41 }

When I run this program,it gives me the message:
/dev/raw/raw1:3
fail to write.
: Invalid argument
In the 15 line if I replace "/dev/raw/raw1" by "/dev/sda1",It's OK! Why?

In addition,I write another program using fwrite and fread:
1 #include <stdio.h>
2 #include <string.h>
3
4 int main(void){
5 FILE *fp;
6 char *p = "test writing raw devices!";
7 int len = strlen(p);
8 int n=0;
9 char buf[len];
10
11 if((fp=fopen("/dev/raw/raw1","wb"))==NULL){
12 perror("fail to open\n");
13 exit(1);
14 }
15 if((n=fwrite(p,sizeof(char),len,fp)) == -1){
16 perror("fail to write\n");
17 exit(1);
18 }
19 printf("write ok,%d chars written.\n",n);
20
21 fclose(fp);
22
23 if((fp=fopen("/dev/raw/raw1","rb"))==NULL){

24 perror("fail to open\n");
25 exit(0);
26 }
27
28 n = 0;
29
30 if((n=fread(buf,sizeof(char),len,fp)) == -1){
31 perror("fail to fread raw device\n");
32 exit(1);
33 }
34 printf("%d chars read:%s\n",n,buf);
35
36 fclose(fp);
37
38 return 0;
39 }

It gives me the message:
write ok,25 chars written.
25 chars read:test raw devicesces!ices.
But it does not really write to the raw device.If I replace "/dev/raw/raw1" by "/dev/sda1",it's OK! Whatever "/dev/raw/raw1" and "/dev/sda1",fread is all OK! Why?
Thank you very much!
 
Old 07-10-2009, 07:52 AM   #2
nanoo
LQ Newbie
 
Registered: Jul 2009
Posts: 19

Rep: Reputation: 1
Did you read raw(8) man page? I found there this
Quote:
...
WARNING
The rawio is a deprecated interface since Linux kernel 2.6.3. Please, modify your application to open the block device with the O_DIRECT flag.
...
and this
Quote:
...
BUGS
The Linux dd (1) command should be used without bs= option or the blocksize needs to be a multiple of the sector size of the device (512 bytes usually) otherwise it will fail with "Invalid Argument" messages (EINVAL).
...
May be this is somehow linked with your problem. Try to make write buffer 512 byte long (just idea).
Good luck.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
raw device sunnyboy Linux - Software 1 07-10-2009 08:14 PM
How to create raw device winoiknow Linux - General 1 02-02-2009 12:32 AM
what is a raw device? saintkay Linux - Desktop 2 09-17-2007 11:13 AM
what is raw device? DataSheet Linux - Newbie 19 01-08-2007 03:13 PM
SuSE: How to create raw device stjacqd Linux - General 3 07-22-2004 06:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration