diff options
Diffstat (limited to '')
-rw-r--r-- | util/ChangeLog | 4 | ||||
-rw-r--r-- | util/Makefile.am | 3 | ||||
-rw-r--r-- | util/dotlock.c | 178 | ||||
-rw-r--r-- | util/memory.c | 44 |
4 files changed, 227 insertions, 2 deletions
diff --git a/util/ChangeLog b/util/ChangeLog index 05c624430..0f444d990 100644 --- a/util/ChangeLog +++ b/util/ChangeLog @@ -1,3 +1,7 @@ +Wed Oct 7 19:27:50 1998 Werner Koch ([email protected]) + + * memory.c (m_print_stats): New. + Tue Oct 6 09:53:56 1998 Werner Koch ([email protected]) * strgutil.c (memicmp): Add HAVE_MEMICMP. diff --git a/util/Makefile.am b/util/Makefile.am index baef6045a..eee154ed7 100644 --- a/util/Makefile.am +++ b/util/Makefile.am @@ -6,7 +6,8 @@ noinst_LIBRARIES = libutil.a libutil_a_SOURCES = g10u.c logger.c fileutil.c miscutil.c strgutil.c \ - ttyio.c argparse.c memory.c secmem.c errors.c iobuf.c + ttyio.c argparse.c memory.c secmem.c errors.c iobuf.c \ + dotlock.c diff --git a/util/dotlock.c b/util/dotlock.c new file mode 100644 index 000000000..42bb7270a --- /dev/null +++ b/util/dotlock.c @@ -0,0 +1,178 @@ +/* dotlock.c - dotfile locking + * Copyright (C) 1998 Free Software Foundation, Inc. + * + * This file is part of GNUPG. + * + * GNUPG is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * GNUPG is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + */ + +#include <config.h> +#include <stdlib.h> +#include <string.h> +#include <errno.h> +#include <ctype.h> +#include "types.h" +#include "util.h" +#include "memory.h" + + + + +#if 0 +/**************** + * Create a lockfile with the given name. A TIMEOUT of 0 + * returns immediately, -1 waits forever (hopefully not), other + * values are timeouts in milliseconds. + * Returns: a char pointer used as handle for reelase lock + * or NULL in case of an error. + * + * Notes: This function creates a lock file in the same directory + * as file_to_lock with the name "file_to_lock.lock" + * A temporary file ".#lk.<pid>.<hostname> is used. + * This function does nothing for Windoze. + */ +int +make_dotlock( const char *file_to_lock, long timeout ) +{ + int rc=-1, fd=-1, pid; + char pidstr[16]; + char *tname = NULL; + char *p; + + log_debug("dotlock_make: lock='%s'\n", lockfile ); + sprintf( pidstr, "%10d\n", getpid() ); + /* add the hostname to the second line (FQDN or IP addr?) */ + + /* create a temporary file */ + tname = CreateTmpFile2( p, ".#lk" ); + free(p); + if( !tname ) + log_fatal( "could not create temporary lock file '%s'\n"); + log_debug( "dotlock_make: tmpname='%s'\n", tname ); + chmod( tname, 0644 ); /* just in case an "umask" is set */ + if( !(fd = open( tname, O_WRONLY )) ) + log_fatal( "could not open temporary lock file '%s'\n", tname); + if( write(fd, pidstr, 11 ) != 11 ) + log_fatal( "error writing to temporary lock file '%s'\n", tname); + if( close(fd) ) { + log_fatal( "error closing '%s'\n", tname); + + retry: + if( !link(tname, lockfile) ) + rc = 0; /* okay */ + else if( errno != EEXIST ) + log_error( "lock not made: link() failed: %s\n", strerror(errno) ); + else { /* lock file already there */ + if( (pid = read_lockfile(lockfile)) == -1 ) { + if( errno == ENOENT ) { + log_debug( "lockfile disappeared\n"); + goto retry; + } + log_debug("cannot read lockfile\n"); + } + else if( pid == getpid() ) { + log_info( "Oops: lock already hold by us\n"); + rc = 0; /* okay */ + } + else if( kill(pid, 0) && errno == ESRCH ) { + log_info( "removing stale lockfile (created by %d)", (int)pid ); + remove( lockfile ); + goto retry; + } + log_debug( "lock not made: lock file exists\n" ); + } + + if( tname ) { + remove(tname); + free(tname); + } + if( !rc ) + log_debug( "lock made\n"); + return rc; +} + +/**************** + * Create a lockfile for a existing file + * Returns: a char pointer used as handle for release lock + * or NULL in case of an error. + * + * Notes: This function creates a lock file in the same directory + * as file_to_lock with the name "lock.<inode-no>" + * + * int + * make_inodelock( const char *file_to_lock ) + * + */ + + + + +/**************** + * release a lock + * Returns: 0 := success + */ +int +release_dotlock( const char *lockfile ) +{ + int pid = ReadLockfile( lockfile ); + if( pid == -1 ) { + Log_printf( LERROR, "ReleaseLock: lockfile error"); + return -1; + } + if( pid != getpid() ) { + Log_printf( LERROR, "ReleaseLock: not our lock (pid=%d)", pid); + return -1; + } + if( remove(lockfile) ) { + Log_printf( LERROR, "ReleaseLock: error removing lockfile '%s'", + lockfile); + return -1; + } + Log_printf( LMESG, "ReleaseLock: released lockfile '%s'", lockfile); + return 0; +} + + +/**************** + * Read the lock file and return the pid, returns -1 on error. + */ +static int +read_lockfile( const char *name ) +{ + int fd, pid; + char pidstr[16]; + + if( (fd = open(name, O_RDONLY)) == -1 ) { + int e = errno; + Log_printf(LJUNK, "error opening lockfile '%s'", name ); + errno = e; /* restore errno */ + return -1; + } + if( read(fd, pidstr, 10 ) != 10 ) { + Log_printf(LNOISE, "error reading lockfile '%s'", name ); + close(fd); + errno = 0; + return -1; + } + close(fd); + pid = atoi(pidstr); + if( !pid || pid == -1 ) { + Log_printf(LERROR, "invalid pid %d in lockfile '%s'", pid, name ); + errno = 0; + return -1; + } + return pid; +} +#endif diff --git a/util/memory.c b/util/memory.c index 013bd55cc..586a744ec 100644 --- a/util/memory.c +++ b/util/memory.c @@ -80,6 +80,10 @@ const void membug( const char *fmt, ... ); #endif +#ifdef M_GUARD +static long used_memory; +#endif + #ifdef M_DEBUG /* stuff used for memory debuging */ struct info_entry { @@ -302,6 +306,7 @@ dump_table( void) sum, chunks ); } + static void check_allmem( const char *info ) { @@ -333,6 +338,40 @@ membug( const char *fmt, ... ) } +void +m_print_stats( const char *prefix ) +{ + #ifdef M_DEBUG + unsigned n; + struct memtbl_entry *e; + ulong sum = 0, chunks =0; + + for( e = memtbl, n = 0; n < memtbl_len; n++, e++ ) { + if(e->inuse) { + sum += e->user_n; + chunks++; + } + } + + log_debug( "%s%smemstat: %8lu bytes in %ld chunks used\n", + prefix? prefix:"", prefix? ": ":"", sum, chunks ); + #elif defined(M_GUARD) + log_debug( "%s%smemstat: %8ld bytes\n", + prefix? prefix:"", prefix? ": ":"", used_memory ); + #endif +} + +void +m_dump_table( const char *prefix ) +{ + #if M_DEBUG + fprintf(stderr,"Memory-Table-Dump: %s\n", prefix); + dump_table(); + #endif + m_print_stats( prefix ); +} + + static void out_of_core(size_t n, int secure) { @@ -354,6 +393,7 @@ FNAME(alloc)( size_t n FNAMEPRT ) if( !(p = malloc( n + EXTRA_ALIGN+5 )) ) out_of_core(n,0); store_len(p,n,0); + used_memory += n; p[4+EXTRA_ALIGN+n] = MAGIC_END_BYTE; return p+EXTRA_ALIGN+4; #else @@ -457,8 +497,10 @@ FNAME(free)( void *a FNAMEPRT ) m_check(p); if( m_is_secure(a) ) secmem_free(p-EXTRA_ALIGN-4); - else + else { + used_memory -= m_size(a); free(p-EXTRA_ALIGN-4); + } #else if( m_is_secure(a) ) secmem_free(p); |