On Sun, Aug 16, 2009 at 7:38 PM, Siddu <siddu.sjce@xxxxxxxxx> wrote:
On Sun, Aug 16, 2009 at 3:09 PM, loody <miloody@xxxxxxxxx> wrote:Dear all:
I try to open /dev/sda1 with system call, open("/dev/sda1", O_RDONLY).
But each time it will return negative value, that means fail.
my program is like below:
(gdb) list
1 #include<stdio.h>
2 #include<fcntl.h>
3 int main(void)
4 {
5 int fd=0;
6 fd = open("/dev/sda1", O_RDONLY);
7
8 printf("%d\n",fd);
9 }
10
(gdb) b 8
Breakpoint 1 at 0x8048423: file test.c, line 8.
(gdb) r
Starting program: /home/cc/a.out
Breakpoint 1, main () at test.c:8
8 printf("%d\n",fd);
(gdb) p fd
$1 = -1
(gdb)
But I am quite sure I really get /dev/sda1 on my linux system.
below are the dmesg:
$ dmesg |grep sda
[ 4.304624] sd 0:0:0:0: [sda] 117210240 512-byte hardware sectors (60012 MB)
[ 4.304642] sd 0:0:0:0: [sda] Write Protect is off
[ 4.304646] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
[ 4.304672] sd 0:0:0:0: [sda] Write cache: enabled, read cache:
enabled, doesn't support DPO or FUA
[ 4.304742] sd 0:0:0:0: [sda] 117210240 512-byte hardware sectors (60012 MB)
[ 4.304757] sd 0:0:0:0: [sda] Write Protect is off
[ 4.304760] sd 0:0:0:0: [sda] Mode Sense: 00 3a 00 00
[ 4.304786] sd 0:0:0:0: [sda] Write cache: enabled, read cache:
enabled, doesn't support DPO or FUA
[ 4.304790] sda:<4>Driver 'sr' needs updating - please use bus_type methods
[ 4.319499] sda1 sda2 sda3 < sda5<6>usb 4-2: configuration #1
chosen from 1 choice
[ 4.359729] sda6 > sda4
[ 4.359951] sd 0:0:0:0: [sda] Attached SCSI disk
[ 22.637589] EXT3 FS on sda4, internal journal
[ 23.586600] EXT3 FS on sda6, internal journal
$
Does that mean I use the wrong systemcall or we cannot open /dev/sda1 like this?
appreciate your help,
miloody
Try to run the following code as root and also as normal user . You ll see the difference#include <stdio.h>
#include <fcntl.h>
int main ( int argc , char *argv[] ) {
int fd = 0;if ( fd == -1 ) {
fd = open( "/dev/sda1" , O_RDONLY );
perror( "The System Error Message was " );
}
else {
printf("The File Descriptor is : %d \n", fd );
}
return 0;
}--
To unsubscribe from this list: send an email with
"unsubscribe kernelnewbies" to ecartis@xxxxxxxxxxxx
Please read the FAQ at http://kernelnewbies.org/FAQ
--
Regards,
~Sid~
--
Regards,
~Sid~