Is this a legal close-on-exec-move?


 
Thread Tools Search this Thread
Top Forums Programming Is this a legal close-on-exec-move?
# 1  
Old 07-07-2007
Is this a legal close-on-exec-move?

In another part of the program, a file is opened using fopen(). Anyhow, I was wondering if using dup2() in the following snippet was a legal close-on-exec move.

Code:
 static int
 tel(char *user, char *tty, const char *what)
{
  pid_t   cpid, wpid;
  int     stats;

  cpid = fork();
  if (cpid < 0) {
    perror("fork failed");
    exit(EXIT_FAILURE);
  }

  if (cpid == 0) {
    dup2(0,STDIN_FILENO);
    dup2(1,STDOUT_FILENO);
    if((execlp("tel", "tel", user, tty, what, (char *)0)) < 0)
      perror("execlp tel failed");
    _exit(EXIT_FAILURE);
  }
 

  while ((wpid= wait(&stats)) != cpid && wpid != -1)
    ;

  return (WIFEXITED(stats) ? WEXITSTATUS(stats) : -2);

# 2  
Old 07-07-2007
Quote:
Originally Posted by frequency8
Anyhow, I was wondering if using dup2() in the following snippet was a legal close-on-exec move.
Eh?

0 == STDIN_FILENO

and

1 == STDOUT_FILENO

so what are you trying to achieve?

I would only call dup2 if the file descriptors were different.

Yes, if a file descriptor has the close on exec bit set, they will be closed when exec() is called, otherwise I'm not sure what you are getting at.
# 3  
Old 07-07-2007
On the code snippet I posted, fork and exec get called like 50 times in a row. The guy that is helping me develop this program says that every once in a while he gets the error message

"Out of File Descriptors"

on the Unix machines at his Universities computers. I haven't gotten this error message yet. Anyhow, I figured this might because the program keeps opening new file descriptors everytime fork() and exec() are executed, but doesn't actually close any of them until the program exits.

I figure that if I do a close-on-exec, it might solve the problem.
# 4  
Old 07-07-2007
1. What OS are you running on?
2. Roughly how many open file descriptors does a running process have?
3. What is the open files ulimit ( ulimit -n ) on the system where the errors happen?
# 5  
Old 07-07-2007
Quote:
Originally Posted by frequency8
I figured this might because the program keeps opening new file descriptors everytime fork() and exec() are executed
1. no fork does open any new descriptors

2. exec is in another process so would not matter, kernel opens program and shared libraries on your behalf
# 6  
Old 07-07-2007
1) The OS I'm running on is Fedora Core 6. However, the target systems are FreeBSD 5.x and OpenBSD 3.x. I even had some guy network engineer from ebay email me saying that he was able to get a clean compile of this engineering wonder on the Solaris machines at his work.

2)I have no idea. I don't have the permissions to run lsof on the target machines.

3)I was told the error happened on the computers that were running OpenBSD. I have no idea what the ulimit is on those machines. I guess I'll have to check later on this week.
# 7  
Old 07-07-2007
Quote:
Originally Posted by porter
1. no fork does open any new descriptors

2. exec is in another process so would not matter, kernel opens program and shared libraries on your behalf
I could have sworn to god that I read someone that fork and exec created new fd's in the book "Advanced Programming in the Unix Environment" by Stevens and Rago.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Array[count+1] legal?

I get weird decimal digits when I run the program below. int coe_amount; cout << "How many coefficients exist in your term? "; cin >> coe_amount; float coefficient; for (int count = 0; count < coe_amount; count ++) { ... (4 Replies)
Discussion started by: DyslexicChciken
4 Replies

2. Shell Programming and Scripting

Find and move command with exec

Hi all, I am trying to find files newer than a given file and them mv them to a new location. So I far I have: find . ! -newer <file_name> -exec ls -l {} \; and find . ! -newer <file_name> -exec mv /TEMP_LOCATION {} \; find is not liking this. Anyone know how to modify the last... (2 Replies)
Discussion started by: jonnyd
2 Replies

3. Shell Programming and Scripting

Script Variables Inquiry, Values Okay in Standalone Exec, No-Show in Cron Exec

I have the following bash script lines in a file named test.sh. #!/bin/bash # # Write Date to cron.log # echo "Begin SSI Load $(date +%d%b%y_%T)" # # Get the latest rates file for processing. # d=$(ls -tr /rms/data/ssi | grep -v "processed" | tail -n 1) filename=$d export filename... (3 Replies)
Discussion started by: ginowms
3 Replies

4. Programming

when parent process close, how to close the child?

can someone provide an example, where if the parent process quits for any reason, then the child process will also close? (3 Replies)
Discussion started by: omega666
3 Replies

5. AIX

Legal Disclaimer setup in CDE

Hi pals I manage nearly 200+ aix workstations. I need to setup a legal disclaimer in all the workstations. When the user do a interactive login in CDE the legal disclaimer should be displayed and once he accepts the same he should be able to login to system. Can anybody suggest me as to... (0 Replies)
Discussion started by: sriram.s
0 Replies

6. Programming

legal code?

hi friends, the following code works fine,but the question is "is this a valid c". i really have no idea....... void func() { int x = 50; { int y; y = x + 400; printf("x = %d\n",x); printf("y = %d\n",y); } } (2 Replies)
Discussion started by: mxms755
2 Replies

7. Shell Programming and Scripting

Not Legal Characters

I have a file that I want to grep and identify all of the illegal characters. I have a list of legal ascii characters \11\12\40-\176,\0-\255 so i try a grep -v to exclude these but my syntax is not correct?? $ cat TRANS_20050613_00.DAT.ERROR | grep -v '\11\12\40-\176\0-\255' grep:... (2 Replies)
Discussion started by: lesstjm
2 Replies
Login or Register to Ask a Question