fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
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 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
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 * CDDL HEADER END
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte pid_t pid; /* pid of read locker or local writer */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte daemonaddr_t remote; /* remote machine requesting write lock */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint8_t seq; /* seq number of last lock request */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define lockdaemon_dead(ldp) ((ticker - (ldp)->timeout) > MAX_TIMEOUTS)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define dp_addr(p) inet_ntoa(((struct sockaddr_in *)p)->sin_addr)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default: return "*unknown*";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte default: return "*unknown*";
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic char *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The following is stolen from autod_nfs.c
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((sock = socket(family, SOCK_DGRAM, 0)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (ioctl(sock, SIOCGIFNUM, (char *)&numifs) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte buf = (char *)malloc(numifs * sizeof (struct ifreq));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "getmyaddrs(): malloc failed\n");
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 ((the_lock.nholders > 0) && the_lock.remote_daemon != NULL &&
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (; n > 0; n--, ifr++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* LINTED pointer alignment */
570de38f63910201fdd77246630b7aa8f9dc5661Surya Prakki (char *)&s1->sin_addr, sizeof (s1->sin_addr)) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* it's me */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortesend_lockmsg(int cmd, pid_t pid, daemonaddr_t *dp, uint8_t seq)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile, "%19.19s send %-9.9s to %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = sendto(lock_soc, &message_buf, sizeof (message_buf), 0,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (struct sockaddr *)dp, sizeof (struct sockaddr));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "sendto rc -1 errno %d", errno);
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 for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* find the lock daemon structure for a give daemon address */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortestatic struct lockdaemon *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
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 for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "daemon restarted on %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* new daemon has announced itself */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "new daemon on %s\n", dp_addr(daemon)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* problem, more daemons than expected */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (the_lock.type == LOCK_WRITE && the_lock.remote_daemon == NULL)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = (req - lock_queue); i++ < next_req; req++)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ackmessage, (pid_t)0, &lock_wanted.remote, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (wait == 0 && lock_wanted.type == LOCK_WRITE) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if we already own the lock, throw the msg away */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If the current lock isn't a write lock and we're not
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * asking for one
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The current lock is a write lock and it's not owned by us
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * send back an unlocked message.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* write lock has to wait */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* lock has to wait until write lock is cleared */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "local locker, take write lock\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* tell everyone I'm locking */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "remote locker, take write lock\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* okay to remote */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "weird lock type held - %d\n", the_lock.type));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return; /* no locks queued */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortequeue_lock(cfglockd_t type, struct lock_msg *msg, daemonaddr_t *addr)
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 /* next search queue to check for duplicate */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, lrp = lock_queue; i++ < next_req; lrp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * It's a new lock request. Are we in the middle of
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * obtaining one for ourselves?
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 /* requeue our request */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* let the other lockds know */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte qsort(lock_queue, next_req, sizeof (lock_queue[0]), lock_sort);
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 spcs_log("cfglockd", NULL, "holding_pid[%d] = %6d", i,
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 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 spcs_log("cfglockd", NULL, " client %s port %hu, pid %d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock_queue[i].remote.sin_port, lock_queue[i].pid);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "daemon %d, %s port %hu", i,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte " up %d timeout %ld missed %d state %d\n", ldp->up,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ldp->timeout, ticker - ldp->timeout, ldp->state);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteis_duplicate(cfglockd_t type, pid_t pid, uint8_t seq)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, bufp = unlock_buf; bufp->pid && i < MAX_UNLOCK;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* throw message away */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "duplicate '%d' request received from %d",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* add it to the list */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortelocal_lock(cfglockd_t type, struct lock_msg *msg, daemonaddr_t *client)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_LOCKED, (pid_t)0, client, msg->seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteremote_lock(struct sockaddr_in *remote, struct lock_msg *msg)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* make sure remote knows we are alive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* clear out pid as it is meaningless on this node */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte queue_lock(LOCK_WRITE, msg, (daemonaddr_t *)remote);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* search queue to delete ungranted locks */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, xrp = lrp = lock_queue; i++ < next_req; lrp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (d != NULL && cmp_addr(d, &(lrp->remote)) != 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (method == NORMAL_UNLOCK && is_duplicate(LOCK_NOTLOCKED, pid, seq)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* delete reference to pid of reading process */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte the_lock.holding_pid[i] = the_lock.holding_pid[i+1];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* LOCK_WRITE */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
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 /* search queue to check for ungranted lock */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, lrp = lock_queue; i++ < next_req; lrp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "lockby enquiry from %s ", dp_addr(d)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_READ, the_lock.holding_pid[0], d, seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(LOCK_WRITE, the_lock.holding_pid[0], d, seq);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* ARGSUSED */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * tell any other daemon that has a lock request in our queue that
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * this daemon is still alive.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, locker = lock_queue; i < next_req; i++, locker++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(ALIVE, (pid_t)0, &(locker->remote), 0);
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 send_lockmsg(GRANTED, (pid_t)0, &the_lock.holder, 0);
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 (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 (void) printf(" %ld", the_lock.holding_pid[ i ]);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0, ldp = daemon_list; i < MAX_DAEMONS; i++, ldp++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%-4.4s ", ldp->up? "up" : "down");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) printf("%-10.10s ", lockd_state(ldp->state));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte send_lockmsg(WRITE_LOCK, (pid_t)0, &(ldp->host), 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%19.19s recv %-9.9s from %s (%ld)\n", ctime(&t),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%19.19s recv %-9.9s from %s order %d (%ld)\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "from %s port %hu\n", dp_addr(host), host->sin_port));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "received ALIVE %s\n", dp_addr(host)));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* do nothing, general "not localhost" code above does this */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* throw message away -- this is an error to receive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * unqueue any locks asked for by pid and unlock any locks held by pid.
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 for (i = 0; (x = cfg_filelock(i, 0)) != CFG_LF_EOF; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue; /* can't take lock, must be still alive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "%s: Can't find hostent for %s\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't find localhost");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(&(localhost.sin_addr.s_addr), *(hp->h_addr_list),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't read config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(stderr, "%s: Can't open config file\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't read config");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while ((i = fscanf(fp, "%s %d\n", host, &port)) != EOF) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (host[0] == '#') /* line starting with # are comments */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (i == 1) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "%s: Can't find hostent for %s\n", program, host);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memcpy(&(ldp->host.sin_addr.s_addr), *(hp->h_addr_list),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "is an alias for this host, skipping\n"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte gettext("usage: %s [-d] [-f file]|[-e program]\n"), program);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "pid %d unexpected signal %d, ignoring",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "pid %d terminate on signal %d", getpid(),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if defined(_SunOS_5_6) || defined(_SunOS_5_7) || defined(_SunOS_5_8)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int c, i, x;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Fork off a child that becomes the daemon.
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 * In child - become daemon.
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 debugfile = fopen("/var/tmp/dscfglockd.out", "a");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile, "%19.19s dscfglockd start\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "new lock daemon, pid %d", getpid());
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Catch as unexpected all signals apart from SIGTERM.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; (c = getopt(argc, argv, "df:e:")) != EOF; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte switch (c) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte ifc = (struct ifconf *)malloc(sizeof (struct ifconf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) memset((char *)ifc, 0, sizeof (struct ifconf));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * if (lockdaemonalive()) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * (void) fprintf(stderr, "%s: %s\n", program,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * gettext("There is already a live lockdaemon"));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* order should be set to node number within cluster */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void) fprintf(debugfile, "WARNING: order number "
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (i = 0; (x = cfg_filelock(i, 0)) != CFG_LF_EOF; i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (c++ == 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "init .dscfg.lck slot %d pid %d locked",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DPF((stderr, "client process %ld still alive\n", pid));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue; /* can't take lock, must be still alive */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if ((lock_soc = socket(pf_inet, SOCK_DGRAM, 0)) < 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte spcs_log("cfglockd", NULL, "couldn't create socket");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = bind(lock_soc, (struct sockaddr *)&thishost, sizeof (thishost));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (getsockname(lock_soc, (struct sockaddr *)&thishost, &len) < 0)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * wait 2 time outs before allowing a lock to find if someone else
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * currently has the lock.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int x = 1; /* kludge to stop warnings from compiler */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte rc = recvfrom(lock_soc, &message_buf, sizeof (message_buf),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* if we own the lock, check to see if the process died */