File I/O help(newby)

MICHAEL HARRISON (harr9446@cs.uidaho.edu)
Tue, 13 Jul 1999 14:49:28 -0700 (PDT)


I haven't joined the list yet (chaning email accounts) and am
somewhat of a newby when it comes to kernel programming. I have a fair
amount of unix programming experiance, though.

I am working with a research group that is studying security issues. I
have searched the archives and can't seem to find an answer to my
question. Its pretty simple and straight forward I think. How do I open
a file, write to it, and then close it. The following is linked into the
kernel and called several times. The file gets opened erratically and
with many errors. Again I am not on the list yet so CC me if you want to
hepl.

Here is my best guess so far:

void write_clic_dump()
{
int clic_fd = -1;
int len, i;
static int j = 0;
char tempBuffer[1024];

/* lets open a file for appending the data */
clic_fd = open("/var/log/kwatcher",O_APPEND|O_SYNC,644);

/* validate file descriptor */
if(clic_fd == -1)
{
clic_fd = open("/var/log/kwatcher",O_CREAT|O_SYNC,644);
if(clic_fd == -1)
{
printk("\nkwatcher: cannot open the log file:
/var/log/kwatcher(%d:%d)",errno,clic_fd);
return;
}
}
else
{
printk("\nkwatcher: opened log file successfully.");
}

/* lets write the info out there */

len = sprintf(tempBuffer, "\nBOOYAH(%d)!",j++);

write(clic_fd, tempBuffer, len);

/* close up the file */
if(close(clic_fd) == -1)
{
printk("\nkwatcher: couldn't close log file
/var/log/kwatcher(%d)", errno);
}
else
{
printk("\nkwatcher: close log file successfully.");
}
}

Michael Harrison - harr9446@cs.uidaho.edu
"Your superior intellect is no match for our puny weapons."
"It takes a big man to cry, and an even bigger man to laugh at that man."

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/