f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * CDDL HEADER START
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * The contents of this file are subject to the terms of the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Common Development and Distribution License, Version 1.0 only
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * (the "License"). You may not use this file except in compliance
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * with the License.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * or http://www.opensolaris.org/os/licensing.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * See the License for the specific language governing permissions
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * and limitations under the License.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * When distributing Covered Code, include this CDDL HEADER in each
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * If applicable, add the following below this CDDL HEADER, with the
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * fields enclosed by brackets "[]" replaced with your own identifying
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * information: Portions Copyright [yyyy] [name of copyright owner]
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby *
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * CDDL HEADER END
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Copyright (c) 2000-2001 by Sun Microsystems, Inc.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * All rights reserved.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#pragma ident "%Z%%M% %I% %E% SMI"
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * syseventd client interfaces
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <stdio.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <sys/types.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <stdarg.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <stddef.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <stdlib.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <unistd.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <door.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <errno.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <strings.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <thread.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <pthread.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <synch.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <syslog.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <fcntl.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <stropts.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <locale.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <libsysevent.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <sys/stat.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <sys/sysevent.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include "syseventd.h"
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include "message.h"
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * sysevent_client.c - contains routines particular to syseventd client
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * management (addition and deletion).
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/* Global client table and lock */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbystruct sysevent_client *sysevent_client_tbl[MAX_SLM];
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbymutex_t client_tbl_lock;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * initialize_client_tbl - Initialize each client entry in the syseventd
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * client table. Each entry in the client table
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * entry represents one shared-object (SLM) client.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyvoid
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyinitialize_client_tbl()
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby{
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby struct sysevent_client *scp;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby int i;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby for (i = 0; i < MAX_SLM; ++i) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if ((scp = (struct sysevent_client *)malloc(
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby sizeof (struct sysevent_client))) == NULL)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby goto init_error;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (mutex_init(&scp->client_lock, USYNC_THREAD, NULL) != 0)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby goto init_error;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_data = NULL;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_num = i;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->eventq = NULL;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby /* Clear all flags when setting UNLOADED */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_flags = SE_CLIENT_UNLOADED;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby sysevent_client_tbl[i] = scp;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby }
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyinit_error:
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby syseventd_err_print(INIT_CLIENT_TBL_ERR);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby syseventd_exit(1);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby}
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * insert_client - called when a new SLM is loaded with syseventd. The
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * client specific data is updated to reflect this addition
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyint
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyinsert_client(void *client_data, int client_type, int retry_limit)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby{
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby int i;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby struct sysevent_client *scp;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) mutex_lock(&client_tbl_lock);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby for (i = 0; i < MAX_SLM; ++i) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp = sysevent_client_tbl[i];
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby if (scp->client_data == NULL) {
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) mutex_lock(&scp->client_lock);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_data = client_data;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_type = client_type;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->retry_limit = retry_limit;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_flags |= SE_CLIENT_LOADED;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) cond_init(&scp->client_cv, USYNC_THREAD,
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby NULL);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) mutex_unlock(&scp->client_lock);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) mutex_unlock(&client_tbl_lock);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (i);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby }
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby }
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) mutex_unlock(&client_tbl_lock);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby syseventd_print(1, "Unable to insert into syseventd client table\n");
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby return (-1);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby}
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * delete_client - called to remove an SLM from the client table. Client
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * removal may occur when syseventd terminates, receives
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * a SIGHUP or the client must be force unloaded due
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * it's unresponsive nature.
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbyvoid
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltbydelete_client(int id)
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby{
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby struct sysevent_client *scp;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp = sysevent_client_tbl[id];
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby free(scp->client_data);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_data = NULL;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby /* Clear all flags when setting UNLOADED */
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby scp->client_flags = SE_CLIENT_UNLOADED;
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby (void) cond_destroy(&scp->client_cv);
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby bzero(&scp->client_cv, sizeof (cond_t));
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby}
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby