I am trying to write a program on UNIX that splits the screen or terminal in half, spawns a new process on the new screen and close it with commands sent from the original. I am programming using the C language and the GCC compiler.
According to my knowledge, this would be possible by running the fork+exec functions to create the separate process, and communication would be done through the pipe function.
Is this possible, and/or has it been done before?
I would very much like to do my work using an existing project.
When I've done something similar to what you're describe, I've used screen to handle the display output (in particular, the -X option to have it do things like split windows and start commands). I imagine tmux has similar control abilities. I've usually used another mechanism (pipes, sockets, or files) to coordinate input/output between processes.
Related
I'm creating a C application daemon that I want to be able to interact with through named pipes. For example, I write in the shell echo hello > /tmp/appname/interface, and the application reads it and does stuff with what I just echoed into the named pipe.
I've successfully managed to implement this with a singular named pipe with read(). However, I want to be able to listen to many different named pipes at the same time. I don't think spawning a new thread for each named pipe is a very good solution. I am also concerned about whether constantly listening for something to be written to the named pipes with read() will use an unnecessarily high amount of CPU. Is there a better way to approach this?
I have a TCP server application which occasionally needs to reconfigure the bound ports by closing them and then opening them at a later time.
The application also needs to execute an external binary with communication to it. This is currently done using a popen() call. The external binary run time can span the period when the network ports need to be reconfigured.
The problem is, when the main application closes a port it is taken on by the 'forked' process which popen has created to run the binary.
That makes sense (it's discussed at What happens when a tcp server binds and forks before doing an accept ? Which process would handle the client requests? ), but is undesirable, as the main application is then unable to reopen the port.
Is this where FD_CLOEXEC O_CLOEXEC as available in popen(3) can be used? The application needs the pipe that popen(3) offers as stdin to the binary which is executed, is that filehandle left open when CLOEXEC closes the others.
Is there a better way to run the binary, which won't result in a forked process which will hold on to a closed port?
There is another, possibly related question at How to fork process without inheriting handles?
No, you cannot start another program and get back from it without fork(2) followed by some execve(2) code (which is what popen, posix_spawn, and system are doing). You'll better avoid popen or system and code the pipe+fork+execve explicitly yourself (since you know which file descriptors should be kept and which to close(2), generally after the fork and before the execve in the child process...), see this.
(every process & program, except /sbin/init and some hotplug things, is started with fork + execve; and your shell is constantly using fork + execve for most commands, except the builtin ones like cd)
the fork(2) call could be implemented by clone(2).
Read some good book like the Advanced Linux Programming book freely available online here. See also syscalls(2). Use (at least for debugging and to understand things) strace(1) and your debugger (gdb). Study the source code of popen and of system in your free software libc (GNU libc or musl-libc), and the source code of your shell.
You could nearly mimic execve(2) by a tricky sequence of mmap(2) (and related munmap) calls, but not quite (in particular w.r.t. close-on-exec etc...). You'll probably need also to call the obsolete setcontext(3) (or write the equivalent assembler code).
You might consider communicating with a specialized shell-like server-like program doing the fork & execve (see my execicar.c for example and inspiration). Maybe you'll find daemon(3) useful.
A better alternative might be to embed some interpreter (like lua or guile) in your application and/or to dlopen(3) some plugin. The disadvantage is that a bug (in the interpreted script or the plugin) affects your entire server.
You can definitely use the close-on-exec flag to avoid newly started processes inheriting open files or sockets (and there's no other way I'm aware of). If you open a file with open(2) you can set the flag at that moment by adding O_CLOEXEC to the file creation flags. If the file is already open you can set it using fcntl() (and if you opened the file using fopen(3) you can get the file descriptor required for this via fileno(3)). For a socket you can also set the flag when opening it with socket(2) by setting SOCK_CLOEXEC. No file with this flag set will be inherited by a process spawned by your program, be it directly via fork + exec or any other "disguise" of that combination, like system(3) or popen(3).
I would like to inject a shared library into a process (I'm using ptrace() to do that part) and then be able to get output from the shared library back into the debugger I'm writing using some form of IPC. My instinct is to use a pipe, but the only real requirements are:
I don't want to store anything on the filesystem to facilitate the communication as it will only last as long as the debugger is running.
I want a portable Unix solution (so Unix-standard syscalls would be ideal).
The problem I'm running into is that as far as I can see, if I call pipe() in the debugger, there is no way to pass the "sending" end of the pipe to the target process, and vice versa with the receiving end. I could set up shared memory, but I think that would require creating a file somewhere so I could reference the memory segment from both processes. How do other debuggers capture output when they attach to a process after it has already begun running?
I assume that you are in need of a debugging system for your business logic code (I mean application). From my experience, this kind of problem is tackled with below explained system design. (My experience is in C++, I think the same must hold good for the C based system also.)
Have a logger system (a separate process). This will contain - logger manager and the logging code - which will take the responsibility of dumping the log into hard disk.
Each application instance (process running in Unix) will communicate to this process with sockets. So you can have your own messaging protocol and communicate with the logger system with socket based communication.
Later, for each of this application - have a switch which can switch off/on the log.So that you can have a tool - to send signal to this process to switch on/off the message logging.
At a high level, this is the most generic way to develop a logging system. In case you need any information - Do comment it. I will try to answer.
Using better search terms showed me this question is a dup of these guys:
Can I share a file descriptor to another process on linux or are they local to the process?
Can I open a socket and pass it to another process in Linux
How to use sendmsg() to send a file-descriptor via sockets between 2 processes?
The top answers were what I was looking for. You can use a Unix-domain socket to hand a file descriptor off to a different process. This could work either from debugger to library or vice versa, but is probably easier to do from debugger to library because the debugger can write the socket's address into the target process while it injects the library.
However, once I pass the socket's address into the target process, I might as well just use the socket itself instead of using a pipe in addition.
I'm not sure how to go about handling asynchronous tasks in a program I am writing and I'm hoping someone more experienced can at least point me in the right direction.
I'm running Angstrom Linux on an embedded ARM processor. My program controls several servos through exposed hardware PWM and a camera over PTP. Additionally it is socket daemon which takes commands from an arbitrary client (Android in this instance). The camera PTP is slow, and I don't want to wait around for it to finish its task because the rest of the program needs to be responsive.
I've tried threads, but any problems in the camera thread seems to kill the whole process. Ideally I want to send the camera off on its own to do its thing and when it is finished let the main function know. Is this an appropriate forking technique or have I implemented threading improperly?
Additionally, I would like to stay away from large secondary libraries to avoid any more cross compiling issues then I already have. Thanks in advance for any suggestions.
Your problem sounds like a classic case for multiple processes, communicating with inter-process communications (IPC) of some sort.
The camera should have its own process, and if that process dies, the main process should not have a problem. You could even have the init(8) process manage the camera process; that can automatically restart the process if it dies for any reason.
You could set up a named pipe permanently, and then the camera process could re-open it any time it restarts after failure.
Here is some documentation about named pipes:
http://www.tldp.org/LDP/lpg/node15.html
I found this from the Wikipedia page:
http://en.wikipedia.org/wiki/Named_pipe
I searched StackOverflow and found a discussion of named pipes vs. sockets:
IPC performance: Named Pipe vs Socket
Take the basic method of steveha's answer but skip the init(8) and named pipes.
fork() a child containing your camera code and communicate through regular pipes or domain sockets. Code a signal handler for SIGCHLD in the parent.If the child dies interrogate the reasons why with the return code from wait(). If it died on its own then cleanup and restart it; if it ended normally do what is appropriate in that case. Communicate with the child through whichever IPC you end up choosing. This give you more control over the child than init and domain sockets or pipes, in particular, will make it easier to set up and communicate between parent and child than messing with the funky semantics of FIFOs.
Of course, if there is really problems with the camera code all you have really done is make the failures somewhat more manageable by not taking down the whole program. Ideally you should get the camera code to work flawlessly if that is within your power.
I've tried threads, but any problems in the camera thread seems to kill the whole process.
When you say kill the whole process, what actually happens?
I put it to you that you are better off debugging the above problem, than trying to wrap the bug away in a forked process. You would rather have a reliable system including a reliable camera, than a reliable core system with an unreliable camera.
I Would like to capture the process entry, exit and maintain a log for the entire system (probably a daemon process).
One approach was to read /proc file system periodically and maintain the list, as I do not see the possibility to register inotify for /proc. Also, for desktop applications, I could get the help of dbus, and whenever client registers to desktop, I can capture.
But for non-desktop applications, I don't know how to go ahead apart from reading /proc periodically.
Kindly provide suggestions.
You mentioned /proc, so I'm going to assume you've got a linux system there.
Install the acct package. The lastcomm command shows all processes executed and their run duration, which is what you're asking for. Have your program "tail" /var/log/account/pacct (you'll find its structure described in acct(5)) and voila. It's just notification on termination, though. To detect start-ups, you'll need to dig through the system process table periodically, if that's what you really need.
Maybe the safer way to move is to create a SuperProcess that acts as a parent and forks children. Everytime a child process stops the father can find it. That is just a thought in case that architecture fits your needs.
Of course, if the parent process is not doable then you must go to the kernel.
If you want to log really all process entry and exits, you'll need to hook into kernel. Which means modifying the kernel or at least writing a kernel module. The "linux security modules" will certainly allow hooking into entry, but I am not sure whether it's possible to hook into exit.
If you can live with occasional exit slipping past (if the binary is linked statically or somehow avoids your environment setting), there is a simple option by preloading a library.
Linux dynamic linker has a feature, that if environment variable LD_PRELOAD (see this question) names a shared library, it will force-load that library into the starting process. So you can create a library, that will in it's static initialization tell the daemon that a process has started and do it so that the process will find out when the process exits.
Static initialization is easiest done by creating a global object with constructor in C++. The dynamic linker will ensure the static constructor will run when the library is loaded.
It will also try to make the corresponding destructor to run when the process exits, so you could simply log the process in the constructor and destructor. But it won't work if the process dies of signal 9 (KILL) and I am not sure what other signals will do.
So instead you should have a daemon and in the constructor tell the daemon about process start and make sure it will notice when the process exits on it's own. One option that comes to mind is opening a unix-domain socket to the daemon and leave it open. Kernel will close it when the process dies and the daemon will notice. You should take some precautions to use high descriptor number for the socket, since some processes may assume the low descriptor numbers (3, 4, 5) are free and dup2 to them. And don't forget to allow more filedescriptors for the daemon and for the system in general.
Note that just polling the /proc filesystem you would probably miss the great number of processes that only live for split second. There are really many of them on unix.
Here is an outline of the solution that we came up with.
We created a program that read a configuration file of all possible applications that the system is able to monitor. This program read the configuration file and through a command line interface you was able to start or stop programs. The program itself stored a table in shared memory that marked applications as running or not. A interface that anybody could access could get the status of these programs. This program also had an alarm system that could either email/page or set off an alarm.
This solution does not require any changes to the kernel and is therefore a less painful solution.
Hope this helps.