LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 06-21-2012, 04:35 AM   #1
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Rep: Reputation: Disabled
lseek function applied on open disk crashes my program.


I open a disk using
int f= open(devicename,O_RDWR);
then I want to seek to a position. Here it is 0( could be anything).
lseek(f,0,SEEK_SET);

After this I want to write something. The device name is /dev/sdc ( a disk, it could be sda, sdb anything).

But just after lseek is executed, I get a coredumped message on the command prompt.

If I omit lseek, I can go on writing and after closing, can see the data. But I need lseek. Note that lseek64 also creashes the same way.

Am I doing something silly? Please help.
Basudeb
 
Old 06-22-2012, 02:00 AM   #2
bsat
Member
 
Registered: Feb 2009
Posts: 347

Rep: Reputation: 72
Did you confirm if the file is opening properly, what is the value of "f" after open ?
 
Old 06-22-2012, 02:06 AM   #3
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
I actually continued writing without using lseek, took out the disk, connected in another machine and verified that write was done correctly.
But if I add lseek even once, there is no error return, it just crashes out saying core dumped. I have tried it in two versions of Fedora 16 32 bit and 64 bit with the same result.
 
Old 06-22-2012, 02:30 AM   #4
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Additional information: If I write the same code in a test.c file, compile and run it, it works fine. But as my program runs with wxWidgets, I need to compile it in wxWidgets environment. Asking this question in wxWidgets forum did not fetch any response. Is it an issue of g++ or wxWidgets? Why should lseek create core dump?
 
Old 06-22-2012, 03:13 AM   #5
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by basudebg View Post
Additional information: If I write the same code in a test.c file, compile and run it, it works fine. But as my program runs with wxWidgets, I need to compile it in wxWidgets environment. Asking this question in wxWidgets forum did not fetch any response. Is it an issue of g++ or wxWidgets? Why should lseek create core dump?
Can you run program that crashed under strace and post results?
 
Old 06-22-2012, 03:37 AM   #6
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
I am attaching the strace done on the following code:

int sector_size;
char *buf;
int n = 1;

int fd = open("/dev/sdc", O_RDONLY|O_NONBLOCK); //O_NONBLOCK is an afterthought
//the problem happens without it
if(fd<0)
wxMessageBox(L"error in opening /dev/sdc");
else
wxMessageBox(L"success in opening /dev/sdc");
ioctl(fd, BLKSSZGET, &sector_size);
wxMessageBox(L"sector_size=" + INTtoCSTR(sector_size)); // INTtoCSTR is a function written in another cpp
lseek(fd, n*sector_size, SEEK_SET);

buf = malloc(sector_size);
int32_t readbytes= read(fd, buf, sector_size);
wxMessageBox(L"read count=" + INTtoCSTR(readbytes));
close(fd);
wxMessageBox(L"test over");
return false;
Attached Files
File Type: txt tracewithlseek.txt (239.7 KB, 14 views)
 
Old 06-22-2012, 05:01 AM   #7
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
The only lseek that I found in your strace output was on the usr/share/icons/Adwaita/cursors/xterm file.

I don't think that lseek on disk was ever executed.
 
Old 06-22-2012, 05:28 AM   #8
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
I find the same code working fine with both gcc and g++ if I do not link to wxWidgets. After getting your response, I also checked and found ioctl (8,...) after that it is all unknown functions and finally a Segmentation fault.

It appears to me the lseek function is somehow modified by wxWidgets library and then there is some bug; so it is never calling the actual lseek function.

Assuming it is so, how do I get around this and call actual system lseek function at this point? Is it possible?
 
Old 06-22-2012, 11:30 PM   #9
Valery Reznic
ELF Statifier author
 
Registered: Oct 2007
Posts: 676

Rep: Reputation: 137Reputation: 137
Quote:
Originally Posted by basudebg View Post
I find the same code working fine with both gcc and g++ if I do not link to wxWidgets. After getting your response, I also checked and found ioctl (8,...) after that it is all unknown functions and finally a Segmentation fault.

It appears to me the lseek function is somehow modified by wxWidgets library and then there is some bug; so it is never calling the actual lseek function.

Assuming it is so, how do I get around this and call actual system lseek function at this point? Is it possible?
It's strange. I don't think that wxWidget library do such a thing as modify lseek.
Could you run your crashed program under strace -i and under ltrace?
 
Old 06-24-2012, 11:02 AM   #10
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
I am attaching the ltrace -i file, starting from the open function.
Attached Files
File Type: txt ltrace0.txt (79.5 KB, 17 views)
 
Old 06-24-2012, 11:37 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
I would simplify it, it cannot be handled easily. I tried now ltrace -C that will give a bit more info about system calls:
PHP Code:
open("/dev/sdc1"2048036145107030 <unfinished ...>
SYS_open("/dev/sdc1"2048036145107030)                                       = 3
<... open resumed> )                                                            = 3
puts
("success opening /dev/sdc" <unfinished ...>
SYS_fstat(10x7ffff1948bc0)                                                    = 0
SYS_mmap
(040963340xffffffff)                                            = 0x7faac15d9000
SYS_write
(1"success opening /dev/sdc\n"25success opening /dev/sdc
)                                  = 25
<... puts resumed> )                                                            = 25
ioctl
(347120x7ffff1948d0c <unfinished ...>
SYS_ioctl(347120x7ffff1948d0c00xffffffff)                               = 0
<... ioctl resumed> )                                                           = 0
lseek
(3512<unfinished ...>
SYS_lseek(35120)                                                            = 512
<... lseek resumed> )                                                           = 512
close
(<unfinished ...>
SYS_close(3)                                                                    = 0
<... close resumed> )                                                           = 0
SYS_exit_group
(<no return ...> 
try a really simple test, like this:
Code:
int main()
{
int sector_size;
char *buf;
int n = 1;

int fd = open("/dev/sdc", O_RDONLY|O_NONBLOCK); //O_NONBLOCK is an afterthought
//the problem happens without it
if(fd<0) {
    printf("error in opening /dev/sdc");
    exit (1);
    }
else
    printf("success in opening /dev/sdc");
ioctl(fd, BLKSSZGET, &sector_size);
printf("sector size: %i\n", sector_size);
lseek(fd, n*sector_size, SEEK_SET);

close(fd);
return 0;
}
replace printf with wxMessageBox one by one, also try to debug, probably it does not reach lseek at all....
 
Old 06-24-2012, 12:12 PM   #12
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
Well, I did not use main() I did it in OnInit which is the starting point of wxApp Class.
Attached is the ltrace. It clearly shows that at lseek, the code simply vanishes.

Please note that if I put the same code in a separate program and compile it either using gcc or g++, there is no problem at all.

So, I still think it is wxWidgets which is doing something funny.
Attached Files
File Type: txt ltrace.txt (576 Bytes, 12 views)
 
Old 06-24-2012, 12:31 PM   #13
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
it looks like wxWidgets has its own seek, wxSeek (filefn.h) - and also wxOpen and other file operations. Probably you can try to stop after preprocessing and see what was generated (that is -E for gcc).
 
Old 06-24-2012, 12:54 PM   #14
basudebg
LQ Newbie
 
Registered: May 2012
Posts: 25

Original Poster
Rep: Reputation: Disabled
But that is the question: Why should my program crash, if I call lseek function? It is a system function.
I think wxSeek is defined in wxWidgets as

#define wxSeek wxPOSIX_IDENT(lseek)

I have not got any answer in wxwidgets forum on this. Anyhow, is there any way to get around this? How do I bypass wxWidgets for lseek function?
 
Old 06-24-2012, 01:07 PM   #15
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,981

Rep: Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337Reputation: 7337
Yes, there is, but you need to go thru the other possibilities also: have you tried to debug it and step into lseek? Have you tried wxOpen/wxSeek, probably works. Have you tried to catch the result of preprocessing and see what was generated? Have you tried ltrace with -C to see if the real low level seek (SYS_lseek) was called?
Do you have a stack trace of the coredump?
 
  


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
External hard disk and flash disk and Sound do not function on Sun Ray 270 thin cl ssemajose Linux - Newbie 1 05-31-2011 04:38 AM
basic program installation as applied to Wireless using Ubuntu Kitzelmcnickt Linux - Newbie 3 02-13-2009 11:02 AM
problem with lseek crash88 Programming 2 07-08-2006 07:54 PM
C function to execute a program and return the output of the program ryan.n Programming 4 08-14-2004 10:11 PM
wine crashes on 16-bit program open/close StuP Linux - Software 0 06-06-2004 01:21 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 05:27 AM.

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