Can I create a file that won't be saved if not closed properly?

Can I create a file that won't be saved if not closed properly?

Post by Norm Dresne » Sat, 19 Oct 2002 02:55:44



I've tried manpages and Stevens' book and found nothing and I suspect that
it's impossible but I'd like expert confirmation that I'm not missing
something.

    Under normal conditions a file created with creat() (or open() &
O_CREAT) will be closed automatically if the program exits without calling
close().      I'd like to creat() a file in a program but have the file not
saved -- i.e. deleted -- if the program doesn't properly close it.  The
main purpose is that if the program fails during execution the file isn't
valid and I don't want it* around to haunt users trying to reduce
data.

    TIA
        Norm

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Barry Margoli » Sat, 19 Oct 2002 03:06:33




>I've tried manpages and Stevens' book and found nothing and I suspect that
>it's impossible but I'd like expert confirmation that I'm not missing
>something.

>    Under normal conditions a file created with creat() (or open() &
>O_CREAT) will be closed automatically if the program exits without calling
>close().      I'd like to creat() a file in a program but have the file not
>saved -- i.e. deleted -- if the program doesn't properly close it.  The
>main purpose is that if the program fails during execution the file isn't
>valid and I don't want it* around to haunt users trying to reduce
>data.

Create the file with a different name, and rename it when you're about to
close it.

--

Genuity, Woburn, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Grant Taylo » Sat, 19 Oct 2002 03:17:01




>> I'd like to creat() a file in a program but have the file not saved
>> -- i.e. deleted -- if the program doesn't properly close it.  The
>> main purpose is that if the program fails during execution the file
>> isn't valid and I don't want it* around to haunt users
> Create the file with a different name, and rename it when you're about to
> close it.

He'd still leak files if he fails.

Once you have the fd open, unlink the file name.  The nameless file
will continue to exist until you (and any children) close it or exit.

If you want the file to survive when you finish, explicitly re-read
the whole thing and write it into a new persistent file.  That much
can be written in a robust enough way that you won't leak half-baked
files.

--
Grant Taylor - gtaylor<at>picante.com - http://www.veryComputer.com/~gtaylor/
 Linux Printing Website and HOWTO:  http://www.veryComputer.com/

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Marc Rochkin » Sat, 19 Oct 2002 03:31:36


I think Barry's suggestion is about the best you can do. If the bad file is
in /tmp, it will go away at some point. (But typically users' files and /tmp
are in different file systems, so "renaming" the file if it's good is not a
cheap operation. It's better if there's a tmp directory in each file
system.)

In addition, use an atexit()-registered function or a
static-class-destructor to try to clean up.

Other than that, there's no way that I can think of. You can unlink the file
with the file-descriptor open, which keeps the i-node around, but there's no
way to stick that i-number back into a directory so as to make the link
count go from zero to one.

But, heck, the Linux folks like to invent stuff, so why not:

    int fmknod(const char *path, mode_t mode, int fd);

(Not necessary to credit me with this invention--in fact, leave me out of
it!)

--Marc

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by those who know me have no need of my nam » Sat, 19 Oct 2002 07:22:26


in comp.unix.programmer i read:

Quote:>In addition, use an atexit()-registered function or a
>static-class-destructor to try to clean up.

i'd go this route, if i'm understanding the desire correctly ...

  #include <stdio.h>
  #include <stdlib.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/stat.h>
  #include <fcntl.h>

  static int  filekeep = 0;
  static char*filepath = "/path/to/some/file";
  static void filecleanup(void)
  {
    if (0 != filekeep) (void)unlink(filepath);
  }

  int main(void)
  {
    int fh = -1;

    if (0 != atexit(filecleanup))
    {
      fprintf(stderr, "atexit(filecleanup) registration failed\n");
      return EXIT_FAILURE;
    }

    if (-1 == (fh = creat(filepath, S_IRWXU)))
    {
      perror(filepath);
      return EXIT_FAILURE;
    }

    /* ... */

    /* if this isn't non-zero before exiting the file will be deleted */
    filekeep = 1;

    return 0;
  }

--
bringing you boring signatures for 17 years

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Paul Pluzhniko » Sat, 19 Oct 2002 12:05:04



> But, heck, the Linux folks like to invent stuff, so why not:

>     int fmknod(const char *path, mode_t mode, int fd);

Or even:

  sprintf(buf, "/proc/%d/fd/%d", getpid(), fd);
  rename(buf, keepname);

No need to add a new system call ;-)

Cheers,
--
In order to understand recursion you must first understand recursion.

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Satish K. Pagar » Sat, 19 Oct 2002 13:17:10


Still if the program crashes , the atexit function will not be called.
and the file will remain there without getting deleted ! May be
we will have to incorporate signal handlers for that as well.

Satish/


Quote:>in comp.unix.programmer i read:

>>In addition, use an atexit()-registered function or a
>>static-class-destructor to try to clean up.

>i'd go this route, if i'm understanding the desire correctly ...

>  #include <stdio.h>
>  #include <stdlib.h>
>  #include <unistd.h>
>  #include <sys/types.h>
>  #include <sys/stat.h>
>  #include <fcntl.h>

>  static int  filekeep = 0;
>  static char*filepath = "/path/to/some/file";
>  static void filecleanup(void)
>  {
>    if (0 != filekeep) (void)unlink(filepath);
>  }

>  int main(void)
>  {
>    int fh = -1;

>    if (0 != atexit(filecleanup))
>    {
>      fprintf(stderr, "atexit(filecleanup) registration failed\n");
>      return EXIT_FAILURE;
>    }

>    if (-1 == (fh = creat(filepath, S_IRWXU)))
>    {
>      perror(filepath);
>      return EXIT_FAILURE;
>    }

>    /* ... */

>    /* if this isn't non-zero before exiting the file will be deleted */
>    filekeep = 1;

>    return 0;
>  }

--
----------------------------------------------------------------
Thou shalt not add thy speed to the speed of light.
----------------------------------------------------------------
http://www.geocities.com/asatsi
Support SETI: http://setiathome.ssl.berkeley.edu
 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Marc Rochkin » Sat, 19 Oct 2002 13:32:08


Quote:>   sprintf(buf, "/proc/%d/fd/%d", getpid(), fd);
>   rename(buf, keepname);

Very interesting... makes me want to read up on the /proc filesystem...

--Marc

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by those who know me have no need of my nam » Sat, 19 Oct 2002 19:32:40


in comp.unix.programmer i read:

Quote:>Still if the program crashes , the atexit function will not be called.

naturally, that is the purpose of abort()ing, so that you can inspect the
corpse, including any side-effects.  how will you debug it's death if you
cannot look at what it produced?

--
bringing you boring signatures for 17 years

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Satish K. Pagar » Sat, 19 Oct 2002 20:17:52


I understand that. But the original post mentioned that if the
program fails to execute properly he doesnt want those files
lurking around.

Satish/


Quote:>in comp.unix.programmer i read:

>>Still if the program crashes , the atexit function will not be called.

>naturally, that is the purpose of abort()ing, so that you can inspect the
>corpse, including any side-effects.  how will you debug it's death if you
>cannot look at what it produced?

--
----------------------------------------------------------------
Thou shalt not add thy speed to the speed of light.
----------------------------------------------------------------
http://www.geocities.com/asatsi
Support SETI: http://setiathome.ssl.berkeley.edu
 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Forrest J. Cavalier II » Sun, 20 Oct 2002 00:06:25



> I've tried manpages and Stevens' book and found nothing and I suspect that
> it's impossible but I'd like expert confirmation that I'm not missing
> something.

>     Under normal conditions a file created with creat() (or open() &
> O_CREAT) will be closed automatically if the program exits without calling
> close().      I'd like to creat() a file in a program but have the file not
> saved -- i.e. deleted -- if the program doesn't properly close it.  The
> main purpose is that if the program fails during execution the file isn't
> valid and I don't want it* around to haunt users trying to reduce
> data.

Is there a reason tmpfile(3) will not work for you?
   http://www.veryComputer.com/

Notes on that page say it is part of [ANSI C X3.159-1989]

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Norm Dresne » Sun, 20 Oct 2002 04:13:30


Thanks to everyone who answered that writing to a temporary file and
renaming it would accomplish almost all of what I need.  I simply didn't
think of that as a solution, I guess because I forgot that renaming a file
can also change the directory in which it resides.

    Norm


Quote:> I've tried manpages and Stevens' book and found nothing and I suspect
that
> it's impossible but I'd like expert confirmation that I'm not missing
> something.

>     Under normal conditions a file created with creat() (or open() &
> O_CREAT) will be closed automatically if the program exits without
calling
> close().      I'd like to creat() a file in a program but have the file
not
> saved -- i.e. deleted -- if the program doesn't properly close it.  The
> main purpose is that if the program fails during execution the file isn't
> valid and I don't want it* around to haunt users trying to reduce
> data.

>     TIA
>         Norm

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Marc Rochkin » Sun, 20 Oct 2002 04:20:47


Well, by delaying your response, you caused us to generate over 17,000
interesting and useful, even if gratuitous, messages! Fortunately, I have
copied them to a temporary file and ...

--Marc

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Andrew Giert » Mon, 21 Oct 2002 11:17:09



 >> But, heck, the Linux folks like to invent stuff, so why not:
 >>
 >> int fmknod(const char *path, mode_t mode, int fd);

no need for the mode argument

 Paul> Or even:

 Paul>   sprintf(buf, "/proc/%d/fd/%d", getpid(), fd);
 Paul>   rename(buf, keepname);

 Paul> No need to add a new system call ;-)

probably harder to do, given that the usual implementation of rename()
can't operate across filesystems

Last time someone suggested this (as "flink()", or something similar),
someone else came up with a security argument against it, but I forget
the details now (or whether the argument was actually valid).

--
Andrew.

comp.unix.programmer FAQ: see <URL: http://www.erlenstar.demon.co.uk/unix/>

 
 
 

Can I create a file that won't be saved if not closed properly?

Post by Casper H.S. Di » Mon, 21 Oct 2002 22:39:47



> Paul>   sprintf(buf, "/proc/%d/fd/%d", getpid(), fd);
> Paul>   rename(buf, keepname);
> Paul> No need to add a new system call ;-)

On Solaris this actually works (well, not the rename but link() works)
except when the link count of the fd is 0 in which case the underlying
fs refuses the link.

Quote:>probably harder to do, given that the usual implementation of rename()
>can't operate across filesystems
>Last time someone suggested this (as "flink()", or something similar),
>someone else came up with a security argument against it, but I forget
>the details now (or whether the argument was actually valid).

The security argument goes as follows: you have a filedescriptor to
a file open as, e.g., O_RDONLY; but the file permissions are 666 and the
only protection is the fact that it is in a directory not accessible to
you.

In Solaris /proc/<pid>/fd/<fd> actually references the underlying file
and *not* the open fd; i.e., it's not "dup" it's open() so the security
argument no longer applies (the hole is already there)

Solaris also has the openat() function which allows you to open files
relative to directory fds you happen to have open.

Casper
--
Expressed in this posting are my opinions.  They are in no way related
to opinions held by my employer, Sun Microsystems.
Statements on Sun products included here are not gospel and may
be fiction rather than truth.

 
 
 

1. Can I create a file that won't be saved if not closed properly?

I've tried manpages and Stevens' book and found nothing and I suspect that
it's impossible but I'd like expert confirmation that I'm not missing
something.

    Under normal conditions a file created with creat() (or open() &
O_CREAT) will be closed automatically if the program exits without calling
close().      I'd like to creat() a file in a program but have the file not
saved -- i.e. deleted -- if the program doesn't properly close it.  The
main purpose is that if the program fails during execution the file isn't
valid and I don't want it hanging around to haunt users trying to reduce
data.

    TIA
        Norm

2. Can't connect via ssh

3. "Save Desktop to new.xinitrc" won't save...

4. video playback tools in Solaris

5. It's not bad canned meat...

6. rm question

7. Crack5 - Race condition?

8. PROBLEM: 'sed' script 's/^ /\n/' not working properly

9. NFS Problem: Files aren't closed on Server (when closed on client)

10. XF86_SVGA 'Removing mode (800x400) that won't display properly...'

11. JServ 1.1 sockets do not close properly

12. > ksh children won't die ; exec doesn't work properly (solution)