Making a system call


 
Thread Tools Search this Thread
Top Forums Programming Making a system call
# 1  
Old 07-05-2006
Making a system call

This is a total nooby question, but here goes...

I am trying to do something in C for the very first time, and doing it in Solaris, for the very first time. It would otherwise be easy enough to test out and find out, but I wont be able to do so until the week after next for various reasons, so I was just wondering if this would work...

I am replacing a script file with C code. The script starts out such as:

#!/bin/ksh
export BUILD_DIR=`cat /nssn/cm/current_build`
export HOSTNAME=`hostname`
echo ColdStart>$BUILD_DIR/logs/StartType
etc...

so, can I just replace that with system calls such as:

system("#!/bin/ksh");
system("export BUILD_DIR=`cat /nssn/cm/current_build`");
system("export HOSTNAME=`hostname`");
etc...

At first I thought yes but then I wondered, does each system call sort of call in a different shell? If so, would the environment variable defined in one system call be seen from another system call?

If that is a problem, is there any way to do all of the system calls in a single system call?

Help a noob out...and thanks for any help in advance!!!
# 2  
Old 07-05-2006
If you want to do what the script does in C why do it that way?

Write the functionality in C, not just embed the script in C.
# 3  
Old 07-06-2006
reborg is right. Using system will not help if you are going to do it that way.

Understand that system() actually forks and execs whatever command you have given. So your export command would be executed in a child process of the current running process and the environment variables would be set in the child process. When the system() call completes, the child process would end and its environment will not be visible to any further child processes that the main parent process may have. If you want to execute the script through a C program, you have to run it as:
Code:
#include<stdlib.h>
int main() {
   system("full_path_to_script");
}

# 4  
Old 07-06-2006
Right, I know I can do it just by calling the script, however, this is actually what I am trying to avoid. Why you ask? Because the C code is being made "Privilege Aware" (something unique to Solaris 10 I believe) and will have the privilege to start up something that the user normally would not have privilege to do. If it calls a script, that script can be modified to start up a terminal per se. Thus the reason I am trying to do this in C instead of what is currently in place (a script), so it cannot be easily modified.

Any other ideas? Could I use a popen() instead of sytem() to do this? Right about now I wish I would have taken that Systems Programming course in school!

I greatly appreciate any of your help!
# 5  
Old 07-06-2006
Are you sure that launching shells from a C program obtains the permissions you want? Since that's what system() does -- launches a new shell.

That'd seem pretty pointless to me if it worked -- anyone with a shell could compile a C program to bypass it. I think you're going to actually have to write the program.
# 6  
Old 07-06-2006
Quote:
Originally Posted by hardwickj
Any other ideas? Could I use a popen() instead of sytem() to do this?
popen() is going to do just what system() is doing, only it will allow you to read the output of the command, or to feed input to the command. Apart from that, pretty much the same.

You will actually have to write a C program to do what you are doing. Just from the first few lines of the script that you have in the first post, you are going to have to use getenv, setenv, fopen, fprintf... most probably fork() and exec() too...
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Making HDD backup in sun Solaris system

Hello to all, I have Fujitsu Celzius M470 workstation which has Sun solaris system installed and want to make a complete backup of the hard drive. The existing HDD is SATA II, 500 GB. I don't have much experience working with SUN solaris systems (not at all) but have some experience with... (5 Replies)
Discussion started by: Mick
5 Replies

2. Shell Programming and Scripting

system call

Trying to figure out a load issue with a webserver. I have traced a php script and noticed the following connect(4, {sa_family=AF_INET, sin_port=htons(3306), sin_addr=inet_addr("XX.XX.XX.XX")}, 16) = -1 EINPROGRESS (Operation now in progress) <0.000035> poll(, 1, 2000) = 1 () <0.000120>... (5 Replies)
Discussion started by: rajan007
5 Replies

3. UNIX for Dummies Questions & Answers

Making tar of a remote file system

Hello, I was asked by my boss to make a backup of one of our systems that is slated to be decommissioned. When I suggested if could tar the "/" directory he nodded and said that would do the trick, When I try and execute the command I get EOF error. I think it is because there is not enough... (2 Replies)
Discussion started by: mojoman
2 Replies

4. Programming

C:system call

Hi I'm studing the system call. I've written a small program that return the time spent in doing some operations. Now I'd like to write one that return the time spent in user mode of a process. I'm reading that i should use the tms struct: clock_t times(struct tms *buf); struct tms {... (2 Replies)
Discussion started by: Dedalus
2 Replies

5. Shell Programming and Scripting

system call

Hi, How to write a system calls in a script ? > cd $HOME > ls -ltr thanks in advance.. (10 Replies)
Discussion started by: hegdeshashi
10 Replies

6. Programming

c system call

How the c compiler differentiates the system calls and function calls? (1 Reply)
Discussion started by: rangaswamy
1 Replies

7. UNIX for Advanced & Expert Users

how to differentiate system call from library call

Hi, Ho do I differentiate system call from library call? for example if I am using chmod , how do I find out if it is a system call or library call? Thanks Muru (2 Replies)
Discussion started by: muru
2 Replies

8. Linux

Making Socket System Call From Linux Kernel Module?

Hi Everyone! How can we make a socket() system call from a linux module executing in kernel space? If any one knows, kindly tell me. It will be great. I want to use the socket interface in linux kernel space for sending raw packets over the network. Hamayun (0 Replies)
Discussion started by: mian_m_hamayun
0 Replies

9. Shell Programming and Scripting

Making a SOAP call from within unix shell scripts

Hi guys, Is it possible to make SOAP calls from within Unix shell scripts? I need to access a web service from within UNIX in order to lookup something while I am doing some parsing on a file. Regards, Laud (2 Replies)
Discussion started by: Laud12345
2 Replies
Login or Register to Ask a Question