2 Copyright (c) 1995-1998 by Cisco systems, Inc.
4 Permission to use, copy, modify, and distribute this software for
5 any purpose and without fee is hereby granted, provided that this
6 copyright and permission notice appear on all copies of the
7 software and supporting documentation, the name of Cisco Systems,
8 Inc. not be used in advertising or publicity pertaining to
9 distribution of the program without specific prior permission, and
10 notice be given in supporting documentation that modification,
11 copying and distribution is by permission of Cisco Systems, Inc.
13 Cisco Systems, Inc. makes no representations about the suitability
14 of this software for any purpose. THIS SOFTWARE IS PROVIDED ``AS
15 IS'' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
16 WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS FOR A PARTICULAR PURPOSE.
26 #else /* !STDLIB_MALLOC */
30 #endif /* STDLIB_MALLOC */
38 /* some mallocs don't like requests for zero length */
43 p = (char *) malloc(size);
46 report(LOG_ERR, "malloc %d failure", size);
53 tac_realloc(ptr, size)
60 /* realloc(0, size) is not portable */
63 p = (char *)realloc(ptr, size);
67 report(LOG_ERR, "realloc %d failure", size);
76 if (debug & DEBUG_FORK_FLAG)
77 report(LOG_DEBUG, "exit status=%d", status);
88 report(LOG_ERR, "strdup allocation failure");
95 tac_make_string(p, len)
103 * Add space for a null terminator if needed. Also, no telling
104 * what various mallocs will do when asked for a length of zero.
106 if (len == 0 || p[len - 1])
109 string = (char *) tac_malloc(new_len);
111 bzero(string, new_len);
112 bcopy(p, string, len);
116 /* return a pointer to the end of substring in string, or NULL. Substring
117 must begin at start of string.
120 tac_find_substring(substring, string)
121 char *substring, *string;
125 if (!(substring && string)) {
129 len = strlen(substring);
131 if (len > (int) strlen(string)) {
135 if (strncmp(substring, string, len)) {
139 return(string + len);
153 for (n=0; n < len; n++) {
160 register char *s1, *s2;
178 if (*s1++ != *s2++) {
184 #endif /* NEED_BZERO */
186 /* Lock a file descriptor using fcntl. Returns 1 on successfully
187 acquiring the lock. The lock disappears when we close the file.
189 Note that if the locked file is on an NFS-mounted partition, you
190 are at the mercy of SUN's lockd, which is probably a bad idea
194 tac_lockfd (filename, lockfd)
202 flock.l_type = F_WRLCK;
203 flock.l_whence = SEEK_SET; /* relative to bof */
204 flock.l_start = 0L; /* from offset zero */
205 flock.l_len = 0L; /* lock to eof */
207 if (debug & DEBUG_LOCK_FLAG) {
208 syslog(LOG_ERR, "Attempting to lock %s fd %d", filename, lockfd);
211 for (tries = 0; tries < 10; tries++) {
213 status = fcntl(lockfd, F_SETLK, &flock);
215 if (errno == EACCES || errno == EAGAIN) {
219 syslog(LOG_ERR, "fcntl lock error status %d on %s %d %s",
220 status, filename, lockfd, sys_errlist[errno]);
224 /* successful lock */
229 syslog(LOG_ERR, "Cannot lock %s fd %d in %d tries %s",
230 filename, lockfd, tries+1, sys_errlist[errno]);
232 /* who is hogging this lock */
233 flock.l_type = F_WRLCK;
234 flock.l_whence = SEEK_SET; /* relative to bof */
235 flock.l_start = 0L; /* from offset zero */
236 flock.l_len = 0L; /* lock to eof */
237 #ifdef HAS_FLOCK_SYSID
242 status = fcntl(lockfd, F_GETLK, &flock);
243 if ((status == -1) || (flock.l_type == F_UNLCK)) {
244 syslog(LOG_ERR, "Cannot determine %s lockholder status=%d type=%d",
245 filename, status, flock.l_type);
249 if (debug & DEBUG_LOCK_FLAG) {
250 syslog(LOG_ERR, "Lock on %s is being held by sys=%u pid=%d",
252 #ifdef HAS_FLOCK_SYSID
262 if (debug & DEBUG_LOCK_FLAG) {
263 syslog(LOG_ERR, "Successfully locked %s fd %d after %d tries",
264 filename, lockfd, tries+1);
269 /* Unlock a file descriptor using fcntl. Returns 1 on successfully
270 releasing a lock. The lock dies when we close the file.
272 Note that if the locked file is on an NFS-mounted partition, you
273 are at the mercy of SUN's lockd, which is probably a bad idea
277 tac_unlockfd (filename,lockfd)
284 flock.l_type = F_WRLCK;
285 flock.l_whence = SEEK_SET; /* relative to bof */
286 flock.l_start = 0L; /* from offset zero */
287 flock.l_len = 0L; /* lock to eof */
289 if (debug & DEBUG_LOCK_FLAG) {
290 syslog(LOG_ERR, "Attempting to unlock %s fd %d", filename, lockfd);
293 status = fcntl(lockfd, F_UNLCK, &flock);
295 syslog(LOG_ERR, "fcntl unlock error status %d on %s %d %s",
296 status, filename, lockfd, sys_errlist[errno]);
300 if (debug & DEBUG_LOCK_FLAG) {
301 syslog(LOG_ERR, "Successfully unlocked %s fd %d",