Passing parameters to already running instance of a program
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.