Including linux command in C++ program [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I intend to include a linux command in C++ program. I have been using the system() function to achieve the effect, like to print the current directory I've been using system("pwd"), for system calls and signals realted to 'ls' I used system("strace ls"), but now I want to use the strace command such that the user should type in the command(like ls,mv,who etc.) or process for which he wants to strace without hard-coding.

For shell builtins like cd or ulimit (etc...) it is useless to use them in the argument of system(3) since it will only affect the /bin/sh process started by system.
You need to use the relevant syscall in your C program, e.g. call chdir(2) or setrlimit(2)
It may have some sense to do system("cd /tmp; gzip a*"); for example.
The list of system calls is in syscalls(2). Read also Advanced Linux Programming
BTW, most commands are giving some output (at least if asked by a suitable program argument). You may want to use popen(3) with pclose ...

You can use system() system call for for executing the linux commands. You can even use execl variants for executing the linux commands by replacing the a process address space.

Related

Killing a process with system C++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Is killing a process like system("taskkill /IM notepad.exe /F") good? Any other way?
There is no standard way to kill, or otherwise interact with processes in C++.
Note that using std::system with variable command is dangerous due to potential for shell injection, so be sure to use a constant command.
It is generally better to use a system library call instead of executing another process that does so. The API to use depend on the target system.
POSIX standard for example specifies the function kill that can be used to raise a signal to the target process. Raising the SIGETERM signal is a request for the process to terminate gracefully. SIGKILL will cause the operating system to terminate the process unconditionally.

Pros and cons of using a system call vs library based approach to compress a directory [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
Scenario:
I am running on an embedded linux distro with a C++ application on it. I need to simply compress a directory and place the zipped output at a certain location. Thats it.
I know that we can tar compress a directory using the following command on a command line interface.
tar cvzf directory.tar.gz /path/to/directory
I see that I have an option to run system commands in C++. Following is a example ot it.
void CompressDirectory() {
std::system("tar cvzf directory.tar.gz /path/to/directory");
}
Environment:
Embedded Linux
Question:
I wish to know the pros and cons of making a system call to tar a directory vs using a library like zlib, zipios etc.
system() is generally frowned upon because of the potential security risks associated with it. In your specific circumstances where everything is explicitly stated, you'd be OK.
You CAN code it in the program to be handled and processed programmatically and not shelling it out and that's the proper way to do it.
So long as your tarball name and directory are both hard coded, I can't think of any security issues, but I'd be careful that you're sure you're not overwriting anything or something similar.
Do not use system() if anybody other than yourself is even able to touch your server.
At least you don't check the result in the current code. You don't know if compressing fails.
You have to check exit code at least.
In general case tar can be not available in command line (on windows, for example).
It's not your case, so you can ignore this point.
Custom library should give more flexible compressing. Like pause, select files, unlimited number of files and so on.

How to recover all current processes on linux [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I need to recover all running processes using c ++ and under linux. But I don't find lib or corresponding functions.
I can't run a linux command such as "ps".
(I'm guessing that you need to inspect, scan, list, or discover processes; recovering processes is a difficult thing, read about application checkpointing)
You should consider using proc(5), that is the /proc/ filesystem (Linux specific).
The process of pid 1234 is described by the /proc/1234/ directory. Try ls /proc/$$/ in a terminal.
So you can explore processes by using appropriate calls (e.g. opendir(3), readdir(3), closedir(3), stat(2) etc....) on the /proc/ file tree (and this is what ps, top etc.. are using; check with strace(1) ...).
Beware that many files under /proc/ have a 0 size as given by stat(2) but are sequentially readable (a bit like a pipe). Try for example stat /proc/$$/status then cat /proc/$$/status. See also this.

Launching another application through c++ code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I would like to know if there is a way to launch an application through a c++ code? As if I was launching it through the command line (with giving parameters for example).
If it exists, please can you provide me with both the windows code as well as linux code (in case they differ).
You can use system calls, like:
exec()
fork()
You can find plenty of examples. I had also answered a question about fork() here.
For exec(), you could read this: Please explain exec() function and its family.
For Windows, you can use one of the spawn family of functions, like _wspawnl. For Linux, you can use one of the exec family of functions, in combination with fork, like execl.

questions about writing an operating system [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have some very specific questions about writing operating systems that I was hoping could get answered:
How much assembly code would I need to write to load a minimal C Kernel if I use GRUB as a boot loader?
My kernel will be written in C, It will load a command line shell that I wrote in C++, it does not make any API calls only standard library calls, will I need to rewrite the entire C++ Standard library to do so?
Can I write video, keyboard and floppy drivers in C++?
Do GCC and G++ output 16 bit real mode code?
Can I write this all using Mingw on Windows or will I have to write it on Linux?
Do I need to be in real mode in order to write directly to the video memory?
If anyone can answer my questions I will be very thankful
1: You should only need a small amount of assembly to handle the boot process and load the C code. Shouldn't be more than like 20-30 lines I think.
2-4: I haven't really used C++ with OS dev, but I think I remember reading that it takes more work to get it running somewhere. Sorry I can't be of more help.
5: You "can" do it using MinGW, but from my experience it mostly complicates things. I could never really get a Windows environment working, but I also gave up without too much effort.
EDIT: Here is a link to some example assembly. This is all I ever had to use:
http://www.jamesmolloy.co.uk/tutorial_html/2.-Genesis.html
The rest of that site is a pretty good tutorial too if you are at all interested in that kind of thing.