fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <signal.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/time.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/socket.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <netinet/in.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <netinet/tcp.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <arpa/inet.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <netdb.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <fcntl.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <memory.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/param.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/pathconf.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <netdir.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <netconfig.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/sockio.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <net/if.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/resource.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdio.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <errno.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <assert.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <locale.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stdlib.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <string.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <strings.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/unistat/spcs_s.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/unistat/spcs_s_u.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/unistat/spcs_errors.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/nsctl/cfg.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/nsctl/cfg_lockd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define DPF(m) if (debug) (void) fprintf m
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define DPF(m)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef TTY_MESSAGES
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define CLOSE_FD 3
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define CLOSE_FD 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_LOCKQ 1024
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_DAEMONS 1024
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_LOCAL 1024
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_UNLOCK 32
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_TIMEOUTS 3
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define TIMEOUT_SECS 5
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char program[] = "dscfglockd";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int debug;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int lstate;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int msgtrace;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic FILE *debugfile = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct lock_req {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfglockd_t type; /* read or write */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid; /* pid of read locker or local writer */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemonaddr_t remote; /* remote machine requesting write lock */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int state; /* for write locks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int32_t order; /* who gets priority? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} lock_queue[MAX_LOCKQ];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct unlock_s {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid; /* pid of locker */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint8_t seq; /* seq number of last lock request */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} unlock_buf[MAX_UNLOCK];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint next_req;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint32_t order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define lock_wanted lock_queue[0]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelong ticker = 1l;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define ALIVE 0x10
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define READ_LOCK 0x11
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define WRITE_LOCK 0x12
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define UNLOCK 0x13
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define GRANTED 0x14
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint next_q;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfglockd_t type;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int nholders;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int state;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemonaddr_t holder;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *remote_daemon;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t holding_pid[MAX_LOCAL];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} the_lock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortedaemonaddr_t thishost;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortedaemonaddr_t localhost;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define STATE_CLEAR 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define STATE_ASKED 1
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define STATE_OKAYED 2
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define STATE_WANTS 3
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define lockdaemon_dead(ldp) ((ticker - (ldp)->timeout) > MAX_TIMEOUTS)
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki#define CRIT_BEGIN() (void) sighold(SIGALRM)
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki#define CRIT_END() (void) sigrelse(SIGALRM)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define NORMAL_UNLOCK 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define FORCE_UNLOCK 1
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct lockdaemon {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemonaddr_t host;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int up;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte long timeout;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int inuse;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int state;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int32_t order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} daemon_list[MAX_DAEMONS];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteunsigned short lock_port = CFG_SERVER_PORT;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint lock_soc = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint pf_inet = PF_INET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define dp_addr(p) inet_ntoa(((struct sockaddr_in *)p)->sin_addr)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAXIFS 32
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelockd_type(cfglockd_t type)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (type) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_NOTLOCKED: return "NotLocked";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_READ: return "Read";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_WRITE: return "Write";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_LOCKED: return "Locked";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_LOCKEDBY: return "LockedBy";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_STAT: return "Stat";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_ACK: return "Ack";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default: return "*unknown*";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelockd_state(int state)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (state) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case STATE_CLEAR: return "Clear";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case STATE_ASKED: return "Asked";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case STATE_OKAYED: return "Okayed";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case STATE_WANTS: return "Wants";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default: return "*unknown*";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelockd_msg(int message)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (message) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case ALIVE: return "Alive";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case READ_LOCK: return "ReadLock";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case WRITE_LOCK: return "WriteLock";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case UNLOCK: return "Unlock";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case GRANTED: return "Granted";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default: return lockd_type((cfglockd_t)message);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The following is stolen from autod_nfs.c
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortegetmyaddrs(struct ifconf *ifc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int sock;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int numifs;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int family;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifc->ifc_buf = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifc->ifc_len = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef AF_INET6
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte family = AF_INET6;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte family = AF_INET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((sock = socket(family, SOCK_DGRAM, 0)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("getmyaddrs(): socket");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ioctl(sock, SIOCGIFNUM, (char *)&numifs) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("getmyaddrs(): SIOCGIFNUM");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte numifs = MAXIFS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte buf = (char *)malloc(numifs * sizeof (struct ifreq));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (buf == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "getmyaddrs(): malloc failed\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(sock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifc->ifc_buf = buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifc->ifc_len = numifs * sizeof (struct ifreq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ioctl(sock, SIOCGIFCONF, (char *)ifc) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("getmyaddrs(): SIOCGIFCONF");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(sock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestruct ifconf *ifc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortecmp_addr(daemonaddr_t *a, daemonaddr_t *b)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = memcmp(&(a->sin_addr), &(b->sin_addr), sizeof (a->sin_addr));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "compare %s %hu with", dp_addr(a), a->sin_port));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, " %s %hu = %d\n", dp_addr(b), b->sin_port, rc));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (rc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteaddr_is_holder(int32_t order)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return ((the_lock.nholders > 0) && the_lock.remote_daemon != NULL &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (order == the_lock.remote_daemon->order));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteislocalhost(daemonaddr_t *host)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int n;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct sockaddr_in *s1, *s2;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct ifreq *ifr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int retval = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifr = ifc->ifc_req;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte n = ifc->ifc_len / sizeof (struct ifreq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s1 = host;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s2 = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; n > 0; n--, ifr++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ifr->ifr_addr.sa_family != AF_INET)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* LINTED pointer alignment */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte s2 = (struct sockaddr_in *)&ifr->ifr_addr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (memcmp((char *)&s2->sin_addr,
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki (char *)&s1->sin_addr, sizeof (s1->sin_addr)) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte retval = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* it's me */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (retval);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesend_lockmsg(int cmd, pid_t pid, daemonaddr_t *dp, uint8_t seq)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_msg message_buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgtrace && debugfile) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte time_t t = time(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile, "%19.19s send %-9.9s to %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ctime(&t), lockd_msg(cmd), dp_addr(dp));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "send %d to %s port %hu\n", cmd,
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki dp_addr(dp), dp->sin_port));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message_buf.message = cmd;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message_buf.pid = pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message_buf.order = order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte message_buf.seq = seq;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte do {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = sendto(lock_soc, &message_buf, sizeof (message_buf), 0,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (struct sockaddr *)dp, sizeof (struct sockaddr));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } while (rc == -1 && errno == EINTR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (rc == -1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "sendto rc -1 errno %d", errno);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * send an alive message to all configured daemons so that they can tell
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * us if they are holding a write lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesend_aliveall()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ALIVE, (pid_t)0, &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* find the lock daemon structure for a give daemon address */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic struct lockdaemon *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortefind_lockdaemon(daemonaddr_t *d)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (cmp_addr(&(ldp->host), d) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (ldp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a messge has been received from daemon, note this and if the daemon
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * was previously dead and we have the write lock tell it that we do.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortedaemon_alive(daemonaddr_t *daemon, int32_t order)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (cmp_addr(&(ldp->host), daemon) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->order = order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->timeout = ticker;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->up == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "daemon restarted on %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dp_addr(daemon));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "daemon restarted on %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dp_addr(daemon)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->up = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte goto come_up;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* new daemon has announced itself */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (i < MAX_DAEMONS) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "new daemon on %s\n", dp_addr(daemon)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "new daemon on %s\n", dp_addr(daemon));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->host = *daemon;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->inuse = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->timeout = ticker;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->order = order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* problem, more daemons than expected */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortecome_up:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type == LOCK_WRITE && the_lock.remote_daemon == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(WRITE_LOCK, (pid_t)0, daemon, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortedelete_queue_entry(struct lock_req *req)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = (req - lock_queue); i++ < next_req; req++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *req = *(req+1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte next_req--;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetake_lock(int ackmessage)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ackmessage, (pid_t)0, &lock_wanted.remote, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte delete_queue_entry(lock_queue);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortecheck_for_write_lock()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int wait = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "check for lock\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_wanted.state != STATE_ASKED)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->up && ldp->state != STATE_OKAYED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte wait = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (wait == 0 && lock_wanted.type == LOCK_WRITE) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type = LOCK_WRITE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[0] = lock_wanted.pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte take_lock(LOCK_LOCKED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelock_granted(daemonaddr_t *da)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((ldp = find_lockdaemon(da)) != NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if we already own the lock, throw the msg away */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.remote_daemon == NULL &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type == LOCK_WRITE) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If the current lock isn't a write lock and we're not
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * asking for one
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -OR-
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The current lock is a write lock and it's not owned by us
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -THEN-
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * send back an unlocked message.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((the_lock.type != LOCK_WRITE &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state != STATE_ASKED) ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (the_lock.type == LOCK_WRITE &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon != NULL)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(UNLOCK, (pid_t)0, &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->state = STATE_OKAYED;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte check_for_write_lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetry_lock()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (the_lock.type) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_READ:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_wanted.type == LOCK_READ) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i = the_lock.nholders++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[i] = lock_wanted.pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "increment read lockers to %d\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte take_lock(LOCK_LOCKED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* write lock has to wait */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_WRITE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* lock has to wait until write lock is cleared */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_NOTLOCKED:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_wanted.type == LOCK_READ) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "local locker, 1 lock holder\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[0] = lock_wanted.pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type = LOCK_READ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte take_lock(LOCK_LOCKED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (islocalhost(&lock_wanted.remote)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "local locker, take write lock\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* tell everyone I'm locking */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lock_wanted.state != STATE_ASKED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->state = STATE_ASKED;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(WRITE_LOCK, (pid_t)0,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_wanted.state = STATE_ASKED;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte check_for_write_lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_ASKED;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "remote locker, take write lock\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type = LOCK_WRITE;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holder = lock_wanted.remote;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon =
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte find_lockdaemon(&the_lock.holder);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* okay to remote */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte take_lock(GRANTED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "weird lock type held - %d\n", the_lock.type));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type = LOCK_NOTLOCKED;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteprocess_queue()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (next_req < 1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return; /* no locks queued */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (try_lock())
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelock_sort(const void *a, const void *b)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_req *left = (struct lock_req *)a;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_req *right = (struct lock_req *)b;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (left->order - right->order);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortequeue_lock(cfglockd_t type, struct lock_msg *msg, daemonaddr_t *addr)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_req *lrp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* first check if new lock matches current lock */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type == type && addr_is_holder(msg->order)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* remote daemon missed locked message */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(GRANTED, (pid_t)0, addr, msg->seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* next search queue to check for duplicate */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, lrp = lock_queue; i++ < next_req; lrp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lrp->type == type && lrp->pid == msg->pid &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cmp_addr(addr, &(lrp->remote)) == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * It's a new lock request. Are we in the middle of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * obtaining one for ourselves?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type == LOCK_NOTLOCKED && the_lock.state == STATE_ASKED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* did a higher priority request just come in? */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msg->order < order) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* requeue our request */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_wanted.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* let the other lockds know */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->up && ldp->state == STATE_OKAYED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(UNLOCK, (pid_t)0,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp = lock_queue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp += (next_req++);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp->type = type;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp->pid = msg->pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp->state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp->order = msg->order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (addr) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lrp->remote = *addr;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (next_req > 1)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte qsort(lock_queue, next_req, sizeof (lock_queue[0]), lock_sort);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type != LOCK_WRITE)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_queue();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelock_stat()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *lt = "Unknown";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (the_lock.type) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_NOTLOCKED:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lt = "not locked";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_READ:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lt = "read locked";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_WRITE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lt = "write locked";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "Lock is %s (%d)", lt, the_lock.type);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "There are %d holders of the lock",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.nholders > 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < the_lock.nholders; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "holding_pid[%d] = %6d", i,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[i]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "holder daemon was %s port %hu, remote %x",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dp_addr(&the_lock.holder), the_lock.holder.sin_port,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "Lock queue, %d requests", next_req);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < next_req; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "request %d type %d order %d", i,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_queue[i].type, lock_queue[i].order);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, " client %s port %hu, pid %d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dp_addr(&lock_queue[i].remote),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_queue[i].remote.sin_port, lock_queue[i].pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "Daemon list");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "daemon %d, %s port %hu", i,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dp_addr(&ldp->host), ldp->host.sin_port);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte " up %d timeout %ld missed %d state %d\n", ldp->up,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->timeout, ticker - ldp->timeout, ldp->state);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic int
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteis_duplicate(cfglockd_t type, pid_t pid, uint8_t seq)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct unlock_s *bufp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!pid) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, bufp = unlock_buf; bufp->pid && i < MAX_UNLOCK;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i++, bufp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (bufp->pid == pid && bufp->seq == seq) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* throw message away */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "duplicate '%d' request received from %d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte type, pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* add it to the list */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bcopy(unlock_buf, &unlock_buf[ 1 ],
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (unlock_buf) - sizeof (struct unlock_s));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*unlock_buf).pid = pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*unlock_buf).seq = seq;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelocal_lock(cfglockd_t type, struct lock_msg *msg, daemonaddr_t *client)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (is_duplicate(type, msg->pid, msg->seq)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.remote_daemon == NULL &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (the_lock.type == LOCK_WRITE ||
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type == LOCK_READ) &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[0] == msg->pid) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_LOCKED, (pid_t)0, client, msg->seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte queue_lock(type, msg, client);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteremote_lock(struct sockaddr_in *remote, struct lock_msg *msg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* make sure remote knows we are alive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ALIVE, (pid_t)0, remote, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* clear out pid as it is meaningless on this node */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte msg->pid = (pid_t)0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte queue_lock(LOCK_WRITE, msg, (daemonaddr_t *)remote);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteunqueue_lock(daemonaddr_t *d, pid_t pid)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_req *lrp, *xrp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int diff;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* search queue to delete ungranted locks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, xrp = lrp = lock_queue; i++ < next_req; lrp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *xrp = *lrp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte diff = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid != (pid_t)0 && lrp->pid != pid)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte diff = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (d != NULL && cmp_addr(d, &(lrp->remote)) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte diff = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!diff)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte xrp++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte next_req = xrp - lock_queue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortexxunlock()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "** UNLOCK **\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.type = LOCK_NOTLOCKED;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte process_queue();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelocal_unlock(pid_t pid, uint8_t seq, int method)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (method == NORMAL_UNLOCK && is_duplicate(LOCK_NOTLOCKED, pid, seq)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type == LOCK_READ) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* delete reference to pid of reading process */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < the_lock.nholders; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.holding_pid[i] == pid) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "decrement lockers from %d\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.nholders));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte --the_lock.nholders;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; i < the_lock.nholders; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[i] = the_lock.holding_pid[i+1];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.nholders > 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* LOCK_WRITE */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid != the_lock.holding_pid[0])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[0] = (pid_t)0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->up)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(UNLOCK, (pid_t)0, &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte xxunlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteremote_unlock(int32_t order, daemonaddr_t *d)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_req *lrp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "remote unlock from %s ", dp_addr(d)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "when %s holds lock\n", dp_addr(&the_lock.holder)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* search queue to check for ungranted lock */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, lrp = lock_queue; i++ < next_req; lrp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lrp->type == LOCK_WRITE &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cmp_addr(d, &(lrp->remote)) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte delete_queue_entry(lrp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (addr_is_holder(order)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte xxunlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelockedby(daemonaddr_t *d, uint8_t seq)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "lockby enquiry from %s ", dp_addr(d)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (the_lock.type) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_NOTLOCKED:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_NOTLOCKED, (pid_t)0, d, seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_READ:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_READ, the_lock.holding_pid[0], d, seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_WRITE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_WRITE, the_lock.holding_pid[0], d, seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortekeepalive(int signo)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_req *locker;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "keepalive...\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ticker++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * tell any other daemon that has a lock request in our queue that
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * this daemon is still alive.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, locker = lock_queue; i < next_req; i++, locker++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (locker->pid == 0) /* remote lock request */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ALIVE, (pid_t)0, &(locker->remote), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if a remote daemon holds the lock, check it is still alive and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if the remote daemon is sent it a grant message in case the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * remote daemon missed our original grant.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.remote_daemon) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lockdaemon_dead(the_lock.remote_daemon)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "lock owner died\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon->up = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte xxunlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(GRANTED, (pid_t)0, &the_lock.holder, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * check for response from daemons preventing this daemon
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * from taking a write lock by not sending a grant message.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if the remote daemon is alive send another lock request,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * otherwise mark it as dead.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * send alive message to any live remote daemons if this
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * daemon has the write lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lstate) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("\nlock: %s\n", lockd_type(the_lock.type));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf(" no. holders: %d\n", the_lock.nholders);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf(" hold addr : %s\n", the_lock.remote_daemon?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dp_addr(the_lock.remote_daemon): "0.0.0.0");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf(" holding pid:");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; i < the_lock.nholders; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf(" %ld", the_lock.holding_pid[ i ]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("\n");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->inuse == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lstate) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%-15.15s ", dp_addr(&ldp->host));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%-4.4s ", ldp->up? "up" : "down");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%5ld ", ldp->timeout);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%-10.10s ", lockd_state(ldp->state));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%6d\n", ldp->order);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ldp->state == STATE_ASKED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (lockdaemon_dead(ldp)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->up = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->state = STATE_CLEAR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(WRITE_LOCK, (pid_t)0, &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type == LOCK_WRITE &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ALIVE, (pid_t)0, &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortedispatch(struct lock_msg *mp, daemonaddr_t *host)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int message = mp->message;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int localhost;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte localhost = islocalhost(host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgtrace && debugfile) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte time_t t = time(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (localhost) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%19.19s recv %-9.9s from %s (%ld)\n", ctime(&t),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lockd_msg(message), dp_addr(host), mp->pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%19.19s recv %-9.9s from %s order %d (%ld)\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ctime(&t), lockd_msg(message), dp_addr(host),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mp->order, mp->pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received message %d\n", message));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "from %s port %hu\n", dp_addr(host), host->sin_port));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!localhost)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemon_alive(host, mp->order);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mp->order = order;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (message) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case ALIVE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received ALIVE %s\n", dp_addr(host)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* do nothing, general "not localhost" code above does this */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case UNLOCK:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received UNLOCK\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte remote_unlock(mp->order, host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case GRANTED:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received GRANTED\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_granted(host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case WRITE_LOCK:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received WRITE_LOCK\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte assert(!localhost);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte remote_lock(host, mp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case READ_LOCK:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_READ:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received READ_LOCK\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte assert(localhost);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte local_lock(LOCK_READ, mp, host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_WRITE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received LOCK_WRITE\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte assert(localhost);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte local_lock(LOCK_WRITE, mp, host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_NOTLOCKED:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received LOCK_NOTLOCKED\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_ACK, (pid_t)0, host, mp->seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type != LOCK_NOTLOCKED) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte local_unlock(mp->pid, mp->seq, NORMAL_UNLOCK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_LOCKEDBY:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lockedby(host, mp->seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_STAT:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_stat();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case LOCK_ACK:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* throw message away -- this is an error to receive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * unqueue any locks asked for by pid and unlock any locks held by pid.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepurge_pid(pid_t pid)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "purge locks for %ld\n", pid));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unqueue_lock(NULL, pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type != LOCK_NOTLOCKED)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte local_unlock(pid, 0, FORCE_UNLOCK);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Check for exit or exec of client processes.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The lock protecting the processes pid in the lockfile will
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * be removed by the kernel when a client exits or execs.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortecheck_for_dead()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i, x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; (x = cfg_filelock(i, 0)) != CFG_LF_EOF; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (x == CFG_LF_AGAIN)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue; /* can't take lock, must be still alive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfg_readpid(i, &pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfg_writepid(i, (pid_t)0);
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki (void) cfg_fileunlock(i);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (pid != (pid_t)0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte purge_pid(pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortebuild_daemon_list(char *cf_file, int exe)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *fp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char host[1024];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int port;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int i;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct hostent *hp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lockdaemon *ldp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((hp = gethostbyname("localhost")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "%s: Can't find hostent for %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte program, "localhost");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't find localhost");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(&(localhost.sin_addr.s_addr), *(hp->h_addr_list),
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki sizeof (localhost.sin_addr));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (cf_file == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) endhostent();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (exe) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fp = popen(cf_file, "r")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(cf_file);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s: Can't open config program\n", program);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't read config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fp = fopen(cf_file, "r")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(cf_file);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "%s: Can't open config file\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte program);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't read config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp = daemon_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((i = fscanf(fp, "%s %d\n", host, &port)) != EOF) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (host[0] == '#') /* line starting with # are comments */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (i == 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte port = lock_port;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(host, "localhost") == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_port = port;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((hp = gethostbyname(host)) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s: Can't find hostent for %s\n", program, host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(&(ldp->host.sin_addr.s_addr), *(hp->h_addr_list),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sizeof (ldp->host.sin_addr));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "daemon: %s\t%s\n",
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki inet_ntoa(ldp->host.sin_addr), hp->h_name));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (islocalhost(&(ldp->host))) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "is an alias for this host, skipping\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->host.sin_port = htons((short)port);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->host.sin_family = hp->h_addrtype;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->inuse = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->up = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (exe)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) pclose(fp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fclose(fp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) endhostent();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteusage()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gettext("usage: %s [-d] [-f file]|[-e program]\n"), program);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteunexpected(int sig)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "pid %d unexpected signal %d, ignoring",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte getpid(), sig);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteterm(int sig)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) unlink(CFG_PIDFILE);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "pid %d terminate on signal %d", getpid(),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte sig);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic void
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteinit(int argc, char *argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if defined(_SunOS_5_6) || defined(_SunOS_5_7) || defined(_SunOS_5_8)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct rlimit rl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int c, i, x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *cp = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct itimerval tv;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct timeval tp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte socklen_t len = sizeof (thishost);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int exe = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte FILE *fp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lstate = (getenv("LOCKD_STATE") != NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte msgtrace = (getenv("LOCKD_MSG") != NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Fork off a child that becomes the daemon.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef TTY_MESSAGES
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((rc = fork()) > 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else if (rc < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "can't fork %d", errno);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, gettext("dscfglockd: cannot fork: %s\n"),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte strerror(errno));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * In child - become daemon.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if !defined(_SunOS_5_6) && !defined(_SunOS_5_7) && !defined(_SunOS_5_8)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* use closefrom(3C) from PSARC/2000/193 when possible */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte closefrom(CLOSE_FD);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) getrlimit(RLIMIT_NOFILE, &rl);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = CLOSE_FD; i < rl.rlim_max; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(i);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef DEBUG
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef TTY_MESSAGES
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) open("/dev/console", O_WRONLY|O_APPEND);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) dup(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) dup(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) close(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (msgtrace || lstate) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte debugfile = fopen("/var/tmp/dscfglockd.out", "a");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debugfile) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte time_t t = time(0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte setbuf(debugfile, (char *)0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile, "%19.19s dscfglockd start\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ctime(&t));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) setpgrp();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "new lock daemon, pid %d", getpid());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Catch as unexpected all signals apart from SIGTERM.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 1; i < _sys_nsig; i++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sigset(i, unexpected);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) sigset(SIGTERM, term);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; (c = getopt(argc, argv, "df:e:")) != EOF; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (c) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case 'd':
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte debug = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case 'e':
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exe = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (cp) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte usage();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = optarg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte case 'f':
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (cp) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte usage();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cp = optarg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte usage();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifc = (struct ifconf *)malloc(sizeof (struct ifconf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ifc == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(CFG_PIDFILE);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "Can't open pid file\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memset((char *)ifc, 0, sizeof (struct ifconf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte getmyaddrs(ifc);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if (lockdaemonalive()) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * (void) fprintf(stderr, "%s: %s\n", program,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * gettext("There is already a live lockdaemon"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((fp = fopen(CFG_PIDFILE, "w")) == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror(CFG_PIDFILE);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "Can't open pid file\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(fp, "%ld\n", getpid());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fclose(fp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* order should be set to node number within cluster */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte order = cfg_iscluster();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfg_lfinit();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (!order) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) gettimeofday(&tp, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte srand48(tp.tv_usec);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte order = lrand48();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (debugfile) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile, "WARNING: order number "
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "is 0 -- changing randomly to %d\n", order);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte c = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; (x = cfg_filelock(i, 0)) != CFG_LF_EOF; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (x == CFG_LF_AGAIN) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfg_readpid(i, &pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c++ == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "init .dscfg.lck slot %d pid %d locked",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte i, pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "client process %ld still alive\n", pid));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue; /* can't take lock, must be still alive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte cfg_writepid(i, 0);
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki (void) cfg_fileunlock(i);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tv.it_interval.tv_sec = TIMEOUT_SECS;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tv.it_interval.tv_usec = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tv.it_value = tv.it_interval;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte bzero(unlock_buf, sizeof (unlock_buf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte next_q = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte build_daemon_list(cp, exe);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((lock_soc = socket(pf_inet, SOCK_DGRAM, 0)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "%s: %s\n", program,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gettext("failed to create socket"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("socket");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't create socket");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte thishost.sin_family = AF_INET;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte thishost.sin_addr.s_addr = INADDR_ANY;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte thishost.sin_port = htons(lock_port);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = bind(lock_soc, (struct sockaddr *)&thishost, sizeof (thishost));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (rc < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("bind");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't bind");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte exit(1);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (getsockname(lock_soc, (struct sockaddr *)&thishost, &len) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte perror("getsockname");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_aliveall();
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki (void) sigset(SIGALRM, keepalive);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) setitimer(ITIMER_REAL, &tv, NULL);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * wait 2 time outs before allowing a lock to find if someone else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * currently has the lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef lint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelintmain(int argc, char *argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortemain(int argc, char *argv[])
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte{
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte struct lock_msg message_buf;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemonaddr_t from;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int addrlen;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int rc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int x = 1; /* kludge to stop warnings from compiler */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte init(argc, argv);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CRIT_BEGIN();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (x) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CRIT_END();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte addrlen = sizeof (from);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "begin recvfrom\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = recvfrom(lock_soc, &message_buf, sizeof (message_buf),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte 0, (struct sockaddr *)&from, &addrlen);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "end recvfrom rc = %d\n", rc));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CRIT_BEGIN();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (rc == sizeof (message_buf))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dispatch(&message_buf, &from);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte check_for_write_lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if we own the lock, check to see if the process died */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type != LOCK_NOTLOCKED &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.remote_daemon == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte check_for_dead();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte CRIT_END();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}