Unix Programming - Print info about file locks

This is Interesting: Free IT Magazines  
Home > Archive > Unix Programming > October 2005 > Print info about file locks





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author Print info about file locks
Eric Lilja

2005-10-24, 3:48 pm

Hello, I was tasked to write, in C, a simple program that can display
info about file locks using fcntl(). However, the following simple
program dumps core when run:
include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

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

static void print_lock_info(int, const char *);

int
main(void)
{
int fd1 = 0, fd2 = 0, fd3 = 0;

fd1 = open("foo", O_RDONLY);
if(fd1 == -1)
printf("fd1 == -1\n");
fd2 = open("bar", O_RDWR);
if(fd3 == -1)
printf("fd2 == -1\n");
fd3 = open("foobar", O_WRONLY);
if(fd2 == -1)
printf("fd3 == -1\n");

assert(fd1 != -1 && fd2 != -1 && fd3 != -1);

print_lock_info(fd1, "foo");
print_lock_info(fd3, "bar");
print_lock_info(fd3, "foobar");

close(fd1);
close(fd2);
close(fd3);

print_lock_info(fd1, "foo");
print_lock_info(fd3, "bar");
print_lock_info(fd3, "foobar");

return EXIT_SUCCESS;
}

static void
print_lock_info(int fd, const char *filename)
{
struct flock lock;
int return_value = 0;
const int fcntl_failed = -1;

memset(&lock, 0, sizeof(lock));

errno = 0;

return_value = fcntl(fd, 0, &lock);

if(return_value == fcntl_failed)
{
fprintf(stderr, "fcntl() failed. Error was: %s\n",
strerror(errno));

return;
}

if(lock.l_type == F_RDLCK)
printf("%s has lock type F_RDLCK.\n", filename);
else if(lock.l_type == F_WRLCK)
printf("%s has lock type F_WRLCK.\n", filename);
else if(lock.l_type == F_UNLCK)
printf("%s has lock type F_UNLCK.\n", filename);
else
printf("%s has unknown lock type %i.\n", filename, lock.l_type);
}

the output is:
$ ./filelock
assertion "fd1 != -1 && fd2 != -1 && fd3 != -1" failed: file
"filelock.c", line 29
Aborted (core dumped)

Compiled with gcc 3.4.4:
gcc -Wall -W -ansi -pedantic -g -O0 -c -o filelock.o filelock.c
gcc filelock.o -o filelock.exe

The contents of the cwd (before building) is:
$ ls -al
total 5
drwxr-xr-x+ 2 mikael None 0 Oct 19 17:46 ./
drwxrwxrwx+ 21 mikael None 0 Oct 19 17:11 ../
-rwxr-xr-x 1 mikael None 252 Oct 19 17:25 Makefile*
-rw-r--r-- 1 mikael None 0 Oct 19 17:16 bar
-rwxr-xr-x 1 mikael None 1631 Oct 19 17:44 filelock.c*
-rw-r--r-- 1 mikael None 0 Oct 19 17:14 foo

What am I doing wrong?

Thanks for any replies!

/ E

Dragan Cvetkovic

2005-10-24, 3:48 pm

"Eric Lilja" <mindcooler@gmail.com> writes:

> Hello, I was tasked to write, in C, a simple program that can display
> info about file locks using fcntl(). However, the following simple
> program dumps core when run:


[snip]

> fd3 = open("foobar", O_WRONLY);
> if(fd2 == -1)
> printf("fd3 == -1\n");
>
> assert(fd1 != -1 && fd2 != -1 && fd3 != -1);


[snip]
>
> the output is:
> $ ./filelock
> assertion "fd1 != -1 && fd2 != -1 && fd3 != -1" failed: file
> "filelock.c", line 29
> Aborted (core dumped)


[snip]

> The contents of the cwd (before building) is:
> $ ls -al
> total 5
> drwxr-xr-x+ 2 mikael None 0 Oct 19 17:46 ./
> drwxrwxrwx+ 21 mikael None 0 Oct 19 17:11 ../
> -rwxr-xr-x 1 mikael None 252 Oct 19 17:25 Makefile*
> -rw-r--r-- 1 mikael None 0 Oct 19 17:16 bar
> -rwxr-xr-x 1 mikael None 1631 Oct 19 17:44 filelock.c*
> -rw-r--r-- 1 mikael None 0 Oct 19 17:14 foo
>
> What am I doing wrong?


Your program dumps core because fd3 == -1.

Bye, Dragan

--
Dragan Cvetkovic,

To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

!!! Sender/From address is bogus. Use reply-to one !!!
Wayne C. Morris

2005-10-24, 3:48 pm

In article <1129736976.074017.130600@f14g2000cwb.googlegroups.com>,
"Eric Lilja" <mindcooler@gmail.com> wrote:

> Hello, I was tasked to write, in C, a simple program that can display
> info about file locks using fcntl(). However, the following simple
> program dumps core when run:


[snip]

> fd2 = open("bar", O_RDWR);
> if(fd3 == -1)


[snip]

> What am I doing wrong?


Take a close look at all references to fd2 and fd3. There are at least 4
places in your program where you used the wrong one.

Those errors aren't causing the core dump, but they're preventing your
program from printing a message that would tell you why it's crashing.
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com