LinuxQuestions.org
Help answer threads with 0 replies.
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 02-07-2008, 03:49 PM   #1
JudyL
LQ Newbie
 
Registered: Aug 2007
Location: Florida, USA
Distribution: Ubuntu
Posts: 29

Rep: Reputation: 15
modifable data in a program's disk file


Here's what I'm trying to do. I have a program that needs to have a data area as part of the actual program file (what would be the .exe file in windows). That data area needs to be a) accessible from within the program itself and b) modified by another program. The second program will NOT modify the data area while the first program is running. My question is simple - how to do this? I would hope to find a solution where the data area is "created" with default values as part of the compile / link process.

Think of it this way. There is program A. A runs in a default mode for most installations. There are a few installations that will run differently if the data is not the default. For version control reasons, the same copy of A is copied onto all installations. Program B runs just once when the system is installed and modifies the data area of A's file on disk for those few installations that get to run differently.

The windows version of this program stores the data area in a RCDATA section in the program's resource area, where both programs can get to it using the XxxResources API calls.

Version control is just one of multiple reasons for the "integrated" data area. These reason are not negotiable. Please don't give possibilities that involve a separate data file. This requirement cannot be changed.

Target Platform - Ubuntu Feisty
Language - C / C++

Thanks,
Judy
 
Old 02-07-2008, 08:15 PM   #2
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by JudyL View Post
This requirement cannot be changed.
I strongly doubt this, but I will appease the request .

Under normal circumstances, global variables in C (even those with file scope) and function variables declared with static are put in the .data section the executable unless they are uninitialized (if they are uninitialized, they are put in the .bss section. The problem is that once program execution begins, it is not necessarily true that changes to the file will reflect changes in the program image. So this gives you some place within a file to store the data, but you should not use the symbol(s) there from within your program (since the changes will not be written to the file after exiting).

So in order to account for this, you need to have some way of finding your static variable data in an open file descriptor of the executable from within the program. Obviously, this depends on the executable format (of which linux supports a few). I will assume that you are dealing with ELF files in particular. In this case, you might use libelf or libbfd to find the offset and size of a symbol in the file. Once you find this information, you could mmap it to a struct or something (in both programs, using MAP_SHARED).

You can use file locking to ensure that the two processes don’t open the same file for reading and writing at the same time.
 
Old 02-08-2008, 06:43 AM   #3
JudyL
LQ Newbie
 
Registered: Aug 2007
Location: Florida, USA
Distribution: Ubuntu
Posts: 29

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by osor View Post
I strongly doubt this, but I will appease the request
You don't know the person this came from. Trust me on this - the requirement isn't changing.

I'm thinking I didn't explain this well. You warn about changes to the file not affecting a running program and having to do some locking to prevent both programs from modifying the file at the same time.

This will not happen. The "real" program A will never be running when the "modifying" program B is running. A is placed (i.e. copied) onto the hard drive of a system. In a very few cases, B is now run on the system and should modify a data area in A's disk file. Some time later, when the system is actually in use, A is executed and needs to access the data contained in the data area. A's actions will depend on whether the data is the default data that was originally placed in the data area when A was compiled / linked or if it is the new data. A will never modify this data.

Your idea of finding the .data segment is one I will investigate . If program B can find it and change the locations within the file, this should work. A will execute and "poof" ... the data is there in the global var defined in the .data seg.

Thanks!!
Judy
 
Old 02-08-2008, 08:17 PM   #4
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
Quote:
Originally Posted by JudyL View Post
A will never modify this data.
Well, that greatly simplifies things from how I read it before (the idea now seems a lot less harebrained). You no longer need to indirectly access the symbols in program A through mmaping, you can use them directly as variables.

Also, let me clarify something from my other post: from the ELF headers, you should be able to find the offset of a symbol in the .data section. The size of your data (whatever it happens to be) should be known by program B ahead of time. My advice is to use a struct, and give explicit alignment directives to gcc to control the size yourself. You can always use the result of a sizeof from program A as the actual size as well.
 
  


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
gnuplot, pull X data from file, specify Y data at cmd line? hedpe Programming 5 03-15-2007 11:32 PM
floppy only reads first disk, if new disk is inserted data from first disk is shown. lakosked Debian 3 02-18-2006 08:44 PM
how to get the data of a disk anywhere? nice007 Linux - Newbie 1 03-24-2005 05:42 AM
Device Driver Program's file operations error thirumalai Programming 5 08-16-2004 02:56 AM
how to log data to disk file mikesievers Programming 1 04-25-2002 12:36 PM

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

All times are GMT -5. The time now is 08:12 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