LinuxQuestions.org
Register a domain and help support LQ
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 10-31-2004, 01:11 PM   #1
saravkrish
Member
 
Registered: Mar 2004
Location: KY, USA
Distribution: Fedora Core 1
Posts: 190

Rep: Reputation: 30
Passing parameters to already running instance of a program


Hi,

I have a program written in C and I want to be able to pass parameters to an already running instance of the program if a second instance of the program is called with a particular switch.

Say:

1st instance:
# myprog &
(goes background)

2nd instance:
# myprog -send "Hello"
Message sent.
(2nd instance quits)

1st instance prints:
Reveived "Hello"

I already looked at another thread that discusses how to prevent multiple instances, but none of the posts talk about how to pass messages (the intent of the thread was not sending messages). I was thinking of listening on a TCP port on the localhost, but then any user can connect and send messages. I want the 1st instance of the process to accept messages from the 2nd instance of the program only if the owner of the process is the same. A best example of a program that does this well is Opera. You can run opera and then call "opera -newpage someurl" to open "someurl" in a new tab of the already running opera browser.

Suggestions please. I pretty good at C, so you can get technical without worrying I wont understand anything. Code snippets or references/tutorials for doing this would be great.

Thanks in advance,
Sarav
 
Old 10-31-2004, 01:33 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
One way I can think of would be to have your program check for the lock file when it starts. If one doesn't exist, it creates it. Inside the lock file you could store a randomly-generated string to act as a password. If it does exist then it could connect via a socket and send the password that was in the lock file as security credentials along with the string you want to send.

So the first instance of your program starts. It creates the lock file, generates the password, and stores it in the lock file. A second instance of your program starts and finds the lock file. It reads the password in from the file, connects to whatever port the first instance of your program is listening to and then sends the password from the lock file and then whatever information you want. The first instance of your program checks the supplied password against the password it generated. If they match, it accepts the message from the second instance of your program.

You'd make it so the lock file was only readable by the whoever owned your program. Your program could check this before trying to send the message. That way you (and only you) could also create little helper programs to send messages to your program also, but other users wouldn't be able to read the password.

The password you generate could be of variable length too, so it should be pretty secure. How long would it take your computer to guess a string that's somewhere between 250 and 500 characters long when each character could be 1 of 50-ish different symbols? You wouldn't even have to stick to ASCII. So the password could be made where each symbol could be 1 through 255. I'm no math whiz, but I'm guessing it would be pretty difficult

Last edited by itsme86; 10-31-2004 at 01:57 PM.
 
Old 10-31-2004, 01:56 PM   #3
saravkrish
Member
 
Registered: Mar 2004
Location: KY, USA
Distribution: Fedora Core 1
Posts: 190

Original Poster
Rep: Reputation: 30
Thought abt that.

Hi,

I did think about this method, but there should definitely be a better and elegant method in Linux to do that (no offense meant to you) - considering how many applications need that. XMMS is another good example of a program that does this.

~Sarav
 
Old 10-31-2004, 02:00 PM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Well, another option would be to have your program catch SIGUSR1. When you want to send a message to your program you write your message to a file and send SIGUSR1 to your original process. Then your program could read the message from that file. Since only the owner of the program (and root) can send signals to your process it should work okay.

According to 'man 2 kill':
Code:
       EPERM  The process does not have permission  to  send  the
              signal  to  any  of the receiving processes.  For a
              process to have permission to send a signal to pro-
              cess  pid  it  must either have root privileges, or
              the real or effective user ID of the  sending  pro-
              cess  must  equal  the real or saved set-user-ID of
              the receiving process.  In the case of  SIGCONT  it
              suffices  when  the sending and receiving processes
              belong to the same session.

Last edited by itsme86; 10-31-2004 at 02:03 PM.
 
Old 10-31-2004, 02:13 PM   #5
saravkrish
Member
 
Registered: Mar 2004
Location: KY, USA
Distribution: Fedora Core 1
Posts: 190

Original Poster
Rep: Reputation: 30
Hey thanx.

Hi,

Thats a nice idea. I will use that if I can't find any other method. I'm looking for more in the direction of semaphores/mutex and shared memory (or message passing). I have used semaphores and shared memory long back (for course assignments) but I forgot how to use them. Also, there are so many types of semaphores - pthreads, POSIX, SystemV - and I don't know which of these is more portable. Probably shared memory is an overkill for my application and message passing might be the right thing (dunno how it is done though!). I just don't like creating files on disk for communication. I also think that SIGUSR1 can have a user message attached to the signal - it might be "int" only though. Must look into it. Thanks a lot buddy.

~Sarav
 
Old 10-31-2004, 02:19 PM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
No problem Check out 'man ipc', 'man shmat', and all the little SEE ALSO guys at the bottom of those man pages for shared memory information
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
passing parameters to network modules muru Linux - Networking 4 10-21-2005 10:44 AM
Passing parameters to a shell script neocookie Linux - General 5 10-20-2005 11:44 AM
Passing parameters to bash script Kamikazee Programming 4 10-01-2005 06:41 AM
Passing Parameters to Bash Script mooreted Linux - Software 3 04-05-2004 09:08 PM
PHP - passing multiple parameters jacksmash Programming 1 11-25-2003 10:56 AM


All times are GMT -5. The time now is 01:32 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration