changeset 4975:8544fd5abe8b

PSARC/2007/277 tsalarm library 6553178 The tsalarm library is needed for Turgo.
author willard
date Fri, 31 Aug 2007 16:18:41 -0700
parents c7454a80ea3d
children b707e78c75ad
files usr/src/Makefile.lint usr/src/lib/Makefile usr/src/lib/libtsalarm/Makefile usr/src/lib/libtsalarm/Makefile.com usr/src/lib/libtsalarm/common/mapfile-vers usr/src/lib/libtsalarm/common/tsalarm.c usr/src/lib/libtsalarm/common/tsalarm.h usr/src/lib/libtsalarm/sparc/Makefile
diffstat 8 files changed, 537 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/usr/src/Makefile.lint	Fri Aug 31 14:35:42 2007 -0700
+++ b/usr/src/Makefile.lint	Fri Aug 31 16:18:41 2007 -0700
@@ -442,6 +442,7 @@
 	lib/libdscp \
 	lib/libpri \
 	lib/libpcp \
+	lib/libtsalarm \
 	stand
 
 $(CLOSED_BUILD)COMMON_SUBDIRS += $(CLOSED)/cmd/mtst
--- a/usr/src/lib/Makefile	Fri Aug 31 14:35:42 2007 -0700
+++ b/usr/src/lib/Makefile	Fri Aug 31 16:18:41 2007 -0700
@@ -248,7 +248,8 @@
 	libfruutils	.WAIT	\
 	libfru		\
 	storage		\
-	libpcp
+	libpcp		\
+	libtsalarm
 
 FM_sparc_DEPLIBS= libpri
 
@@ -543,6 +544,7 @@
 brand:		libc libsocket
 libshare:	libscf libzfs libuuid libfsmgt libsecdb
 libexacct/demo:	libexacct libproject libsocket libnsl
+libtsalarm:	libpcp
 
 #
 # The reason this rule checks for the existence of the
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libtsalarm/Makefile	Fri Aug 31 16:18:41 2007 -0700
@@ -0,0 +1,51 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#
+# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+# Use is subject to license terms.
+#
+#ident	"%Z%%M%	%I%	%E% SMI"
+#
+# lib/libtsalarm/Makefile
+
+include $(SRC)/lib/Makefile.lib
+
+SUBDIRS	=	$(MACH)
+
+all :=		TARGET= all
+clean :=	TARGET= clean
+clobber :=	TARGET= clobber
+install :=	TARGET= install
+install_h :=	TARGET= install_h
+lint :=		TARGET= lint
+
+.KEEP_STATE:
+
+all clean clobber install_h lint : $(SUBDIRS)
+
+install	:	$(SUBDIRS)
+
+$(SUBDIRS): FRC
+	@cd $@; pwd; $(MAKE) $(TARGET)
+
+FRC:
+
+include $(SRC)/lib/Makefile.targ
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libtsalarm/Makefile.com	Fri Aug 31 16:18:41 2007 -0700
@@ -0,0 +1,55 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#
+# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+# Use is subject to license terms.
+#
+#ident	"%Z%%M%	%I%	%E% SMI"
+#
+
+LIBRARY= libtsalarm.a
+VERS= .1
+
+# PLATFORM_OBJECTS is defined in platform Makefile
+OBJECTS= $(PLATFORM_OBJECTS)
+
+include $(SRC)/lib/Makefile.lib
+include $(SRC)/Makefile.psm
+
+SRCDIR =	../common
+
+CPPFLAGS +=	-I../../libpcp/common
+
+LIBS = $(DYNLIB)
+CFLAGS +=	$(CCVERBOSE)
+LDLIBS +=	-R$(USR_PLAT_DIR)/$(PLATFORM)/lib -L$(USR_PLAT_DIR)/$(PLATFORM)/lib -lc -lpcp
+PLATLIBS =	$(USR_PLAT_DIR)/$(PLATFORM)/lib
+
+.KEEP_STATE:
+
+#
+# build/lint rules
+#
+all:	$(LIBS)
+
+lint:	lintcheck
+
+include $(SRC)/lib/Makefile.targ
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libtsalarm/common/mapfile-vers	Fri Aug 31 16:18:41 2007 -0700
@@ -0,0 +1,35 @@
+#
+# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+# Use is subject to license terms.
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+# ident	"%Z%%M%	%I%	%E% SMI"
+#
+# Interface definition for usr/src/lib/libtsalarm
+#
+
+SUNWprivate {
+	global:
+		tsalarm_get;
+		tsalarm_set;
+	local:
+		*;
+};
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libtsalarm/common/tsalarm.c	Fri Aug 31 16:18:41 2007 -0700
@@ -0,0 +1,239 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ */
+
+/*
+ * Telco-alarm library, which communicates through libpcp to set/get
+ * alarms.
+ */
+
+#pragma ident	"%Z%%M%	%I%	%E% SMI"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <libpcp.h>
+
+#include "tsalarm.h"
+
+/* Message Types */
+#define	TSALARM_CONTROL		15
+#define	TSALARM_CONTROL_R	16
+
+#define	TSALARM_CHANNEL_TIMEOUT	20
+#define	TSALARM_MAX_RETRIES	3
+#define	TSALARM_SERVICE_NAME	"SUNW,sun4v-telco-alarm"
+
+int
+tsalarm_get(uint32_t alarm_type, uint32_t *alarm_state)
+{
+	int		chnl_fd;
+	tsalarm_req_t	*req_ptr = NULL;
+	tsalarm_resp_t	*resp_ptr = NULL;
+	pcp_msg_t	send_msg;
+	pcp_msg_t	recv_msg;
+	int		rc = TSALARM_SUCCESS;
+	int		retries;
+
+	/* initialize virtual channel */
+	for (retries = 1; retries <= TSALARM_MAX_RETRIES; retries++) {
+		if ((chnl_fd = pcp_init(TSALARM_SERVICE_NAME)) < 0) {
+			if (retries == TSALARM_MAX_RETRIES) {
+				rc = TSALARM_CHANNEL_INIT_FAILURE;
+				goto cleanup;
+			}
+			(void) sleep(TSALARM_CHANNEL_TIMEOUT);
+		} else
+			break;
+	}
+
+	/* create request message data */
+	req_ptr = malloc(sizeof (tsalarm_req_t));
+	if (req_ptr == NULL) {
+		rc = TSALARM_NULL_REQ_DATA;
+		goto cleanup;
+	}
+	req_ptr->alarm_action = TSALARM_STATUS;
+	req_ptr->alarm_id = alarm_type;
+
+	send_msg.msg_type = TSALARM_CONTROL;
+	send_msg.sub_type = NULL;
+	send_msg.msg_len = sizeof (tsalarm_req_t);
+	send_msg.msg_data = (uint8_t *)req_ptr;
+
+	/*
+	 * Send the request and receive the response.
+	 */
+	if (pcp_send_recv(chnl_fd, &send_msg, &recv_msg,
+	    TSALARM_CHANNEL_TIMEOUT) < 0) {
+		/* we either timed out or erred; either way try again */
+		(void) sleep(TSALARM_CHANNEL_TIMEOUT);
+
+		if (pcp_send_recv(chnl_fd, &send_msg, &recv_msg,
+		    TSALARM_CHANNEL_TIMEOUT) < 0) {
+			rc = TSALARM_COMM_FAILURE;
+			goto cleanup;
+		}
+	}
+
+	/*
+	 * validate that this data was meant for us
+	 */
+	if (recv_msg.msg_type != TSALARM_CONTROL_R) {
+		rc = TSALARM_UNBOUND_PACKET_RECVD;
+		goto cleanup;
+	}
+
+	/*
+	 * verify that the Alarm action has taken place
+	 */
+	if ((resp_ptr = (tsalarm_resp_t *)recv_msg.msg_data) == NULL)
+		goto cleanup;
+
+	if (resp_ptr->status == TSALARM_ERROR) {
+		rc = TSALARM_GET_ERROR;
+		goto cleanup;
+	}
+
+	if (resp_ptr->alarm_state == TSALARM_STATE_UNKNOWN) {
+		rc = TSALARM_GET_ERROR;
+		goto cleanup;
+	}
+
+	*alarm_state = resp_ptr->alarm_state;
+
+cleanup:
+	if (req_ptr != NULL)
+		free(req_ptr);
+	if (recv_msg.msg_data != NULL)
+		free(recv_msg.msg_data);
+
+	/* close virtual channel fd */
+	(void) pcp_close(chnl_fd);
+
+	return (rc);
+}
+
+int
+tsalarm_set(uint32_t alarm_type, uint32_t alarm_state)
+{
+	int		chnl_fd;
+	tsalarm_req_t   *req_ptr = NULL;
+	tsalarm_resp_t  *resp_ptr = NULL;
+	pcp_msg_t	send_msg;
+	pcp_msg_t	recv_msg;
+	int		rc = TSALARM_SUCCESS;
+	int		retries;
+
+	/* initialize virtual channel */
+	for (retries = 1; retries <= TSALARM_MAX_RETRIES; retries++) {
+		if ((chnl_fd = pcp_init(TSALARM_SERVICE_NAME)) < 0) {
+			if (retries == TSALARM_MAX_RETRIES) {
+				rc = TSALARM_CHANNEL_INIT_FAILURE;
+				goto cleanup;
+			}
+			(void) sleep(TSALARM_CHANNEL_TIMEOUT);
+		} else
+			break;
+	}
+
+	/* create request message data */
+	req_ptr = malloc(sizeof (tsalarm_req_t));
+	if (req_ptr == NULL) {
+		rc = TSALARM_NULL_REQ_DATA;
+		goto cleanup;
+	}
+	req_ptr->alarm_id = alarm_type;
+	if (alarm_state == TSALARM_STATE_ON)
+		req_ptr->alarm_action = TSALARM_ENABLE;
+	else if (alarm_state == TSALARM_STATE_OFF)
+		req_ptr->alarm_action = TSALARM_DISABLE;
+
+	send_msg.msg_type = TSALARM_CONTROL;
+	send_msg.sub_type = NULL;
+	send_msg.msg_len = sizeof (tsalarm_req_t);
+	send_msg.msg_data = (uint8_t *)req_ptr;
+
+	/*
+	 * Send the request and receive the response.
+	 */
+	if (pcp_send_recv(chnl_fd, &send_msg, &recv_msg,
+	    TSALARM_CHANNEL_TIMEOUT) < 0) {
+		/* we either timed out or erred; either way try again */
+		(void) sleep(TSALARM_CHANNEL_TIMEOUT);
+
+		if (pcp_send_recv(chnl_fd, &send_msg, &recv_msg,
+		    TSALARM_CHANNEL_TIMEOUT) < 0) {
+			rc = TSALARM_COMM_FAILURE;
+			goto cleanup;
+		}
+	}
+
+	/*
+	 * validate that this data was meant for us
+	 */
+	if (recv_msg.msg_type != TSALARM_CONTROL_R) {
+		rc = TSALARM_UNBOUND_PACKET_RECVD;
+		goto cleanup;
+	}
+
+	/*
+	 * verify that the Alarm action has taken place
+	 */
+	if ((resp_ptr = (tsalarm_resp_t *)recv_msg.msg_data) == NULL)
+		goto cleanup;
+
+	if (resp_ptr->status == TSALARM_ERROR) {
+		rc = TSALARM_SET_ERROR;
+		goto cleanup;
+	}
+
+	/*
+	 * ensure the Alarm action taken is the one requested
+	 */
+	if ((req_ptr->alarm_action == TSALARM_DISABLE) &&
+	    (resp_ptr->alarm_state != TSALARM_STATE_OFF)) {
+		rc = TSALARM_SET_ERROR;
+		goto cleanup;
+	} else if ((req_ptr->alarm_action == TSALARM_ENABLE) &&
+	    (resp_ptr->alarm_state != TSALARM_STATE_ON)) {
+		rc = TSALARM_SET_ERROR;
+		goto cleanup;
+	} else if (resp_ptr->alarm_state == TSALARM_STATE_UNKNOWN) {
+		rc = TSALARM_SET_ERROR;
+		goto cleanup;
+	}
+
+cleanup:
+	if (req_ptr != NULL)
+		free(req_ptr);
+	if (recv_msg.msg_data != NULL)
+		free(recv_msg.msg_data);
+
+	/* close virtual channel fd */
+	(void) pcp_close(chnl_fd);
+
+	return (rc);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libtsalarm/common/tsalarm.h	Fri Aug 31 16:18:41 2007 -0700
@@ -0,0 +1,88 @@
+/*
+ * CDDL HEADER START
+ *
+ * The contents of this file are subject to the terms of the
+ * Common Development and Distribution License (the "License").
+ * You may not use this file except in compliance with the License.
+ *
+ * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+ * or http://www.opensolaris.org/os/licensing.
+ * See the License for the specific language governing permissions
+ * and limitations under the License.
+ *
+ * When distributing Covered Code, include this CDDL HEADER in each
+ * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+ * If applicable, add the following below this CDDL HEADER, with the
+ * fields enclosed by brackets "[]" replaced with your own identifying
+ * information: Portions Copyright [yyyy] [name of copyright owner]
+ *
+ * CDDL HEADER END
+ */
+/*
+ * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ */
+
+#ifndef	_TSALARM_H
+#define	_TSALARM_H
+
+#pragma ident	"%Z%%M%	%I%	%E% SMI"
+
+#ifdef	__cplusplus
+extern "C" {
+#endif
+
+/* alarm_action */
+#define	TSALARM_ENABLE		1
+#define	TSALARM_DISABLE		2
+#define	TSALARM_STATUS		3
+
+/* alarm_id */
+#define	TSALARM_CRITICAL	0
+#define	TSALARM_MAJOR		1
+#define	TSALARM_MINOR		2
+#define	TSALARM_USER		3
+
+/* alarm_state */
+#define	TSALARM_STATE_ON	1
+#define	TSALARM_STATE_OFF	2
+#define	TSALARM_STATE_UNKNOWN	3
+
+/* Status Types */
+#define	TSALARM_OK		1
+#define	TSALARM_ERROR		2
+
+/* Error codes */
+#define	TSALARM_SUCCESS			0
+#define	TSALARM_CHANNEL_INIT_FAILURE	-1
+#define	TSALARM_NULL_REQ_DATA		-2
+#define	TSALARM_COMM_FAILURE		-3
+#define	TSALARM_UNBOUND_PACKET_RECVD	-4
+#define	TSALARM_GET_ERROR		-5
+#define	TSALARM_SET_ERROR		-6
+
+/*
+ * alarm set/get request message
+ */
+typedef struct tsalarm_req {
+	uint32_t	alarm_id;
+	uint32_t	alarm_action;
+} tsalarm_req_t;
+
+/*
+ * alarm set/get response message
+ */
+typedef struct tsalarm_resp {
+	uint32_t	status;
+	uint32_t	alarm_id;
+	uint32_t	alarm_state;
+} tsalarm_resp_t;
+
+int tsalarm_get(uint32_t alarm_type, uint32_t *alarm_state);
+int tsalarm_set(uint32_t alarm_type, uint32_t alarm_state);
+
+#ifdef	__cplusplus
+}
+#endif
+
+#endif /* _TSALARM_H */
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/usr/src/lib/libtsalarm/sparc/Makefile	Fri Aug 31 16:18:41 2007 -0700
@@ -0,0 +1,65 @@
+#
+# CDDL HEADER START
+#
+# The contents of this file are subject to the terms of the
+# Common Development and Distribution License (the "License").
+# You may not use this file except in compliance with the License.
+#
+# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
+# or http://www.opensolaris.org/os/licensing.
+# See the License for the specific language governing permissions
+# and limitations under the License.
+#
+# When distributing Covered Code, include this CDDL HEADER in each
+# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
+# If applicable, add the following below this CDDL HEADER, with the
+# fields enclosed by brackets "[]" replaced with your own identifying
+# information: Portions Copyright [yyyy] [name of copyright owner]
+#
+# CDDL HEADER END
+#
+#
+# Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
+# Use is subject to license terms.
+#
+#ident	"%Z%%M%	%I%	%E% SMI"
+#
+# lib/libtsalarm/sparc/Makefile
+
+UTSBASE= ../../../uts
+
+PLATINCS += -I$(USR_PLAT_DIR)/sun4v/include -I$(UTSBASE)/sun4v
+
+PLATFORM_OBJECTS= tsalarm.o
+
+#
+# platform library directory (/usr/platform/SUNW,Sun-Fire-T200/lib)
+#
+PLATFORM=SUNW,Sun-Fire-T200
+
+include ../Makefile.com
+
+#
+# install rules
+#
+
+$(PLATLIBS)/libtsalarm.so:
+	$(RM) -r $@; $(SYMLINK) libtsalarm.so.1 $@ $(CHOWNLINK) $(CHGRPLINK)
+
+$(USR_PSM_LIB_DIR)/% := FILEMODE= 644
+
+$(USR_PSM_LIB_DIR)/%: % $(USR_PSM_LIB_DIR)
+	$(INS.file)
+
+$(USR_PSM_LIB_DIR):
+	$(INS.dir.root.sys)
+
+$(USR_PSM_INCL_DIR)/%:	../common/% $(USR_PSM_INCL_DIR)
+	$(INS.file)
+
+$(USR_PSM_INCL_DIR):
+	$(INS.dir.root.sys)
+
+install: all install_h $(USR_PSM_LIBS) $(PLATLIBS)/libtsalarm.so
+
+install_h: $(USR_PSM_INCL_DIR)/tsalarm.h