118c2affartem/***************************************************************************
218c2affartem *
318c2affartem * addon-storage.c : watch removable media state changes
418c2affartem *
5d7c5785Gary Mills * Copyright 2017 Gary Mills
697ddcdcArtem Kachitchkine * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
718c2affartem * Use is subject to license terms.
818c2affartem *
918c2affartem * Licensed under the Academic Free License version 2.1
1018c2affartem *
1118c2affartem **************************************************************************/
1218c2affartem
1318c2affartem#ifdef HAVE_CONFIG_H
1418c2affartem#  include <config.h>
1518c2affartem#endif
1618c2affartem
1718c2affartem#include <errno.h>
1818c2affartem#include <string.h>
1918c2affartem#include <strings.h>
2018c2affartem#include <stdlib.h>
2118c2affartem#include <stdio.h>
2218c2affartem#include <sys/ioctl.h>
2318c2affartem#include <sys/types.h>
2418c2affartem#include <sys/stat.h>
2518c2affartem#include <sys/types.h>
2618c2affartem#include <sys/wait.h>
2718c2affartem#include <fcntl.h>
2818c2affartem#include <unistd.h>
2918c2affartem#include <sys/mnttab.h>
3018c2affartem#include <sys/dkio.h>
3118c2affartem#include <priv.h>
3297ddcdcArtem Kachitchkine#include <libsysevent.h>
3397ddcdcArtem Kachitchkine#include <sys/sysevent/dev.h>
3418c2affartem
3518c2affartem#include <libhal.h>
3618c2affartem
3718c2affartem#include "../../hald/logger.h"
3818c2affartem
3918c2affartem#define	SLEEP_PERIOD	5
4018c2affartem
4197ddcdcArtem Kachitchkinestatic char			*udi;
4297ddcdcArtem Kachitchkinestatic char			*devfs_path;
4397ddcdcArtem KachitchkineLibHalContext			*ctx = NULL;
4497ddcdcArtem Kachitchkinestatic sysevent_handle_t	*shp = NULL;
4597ddcdcArtem Kachitchkine
4697ddcdcArtem Kachitchkinestatic void	sysevent_dev_handler(sysevent_t *);
4797ddcdcArtem Kachitchkine
4818c2affartemstatic void
4918c2affartemmy_dbus_error_free(DBusError *error)
5018c2affartem{
5118c2affartem	if (dbus_error_is_set(error)) {
5218c2affartem		dbus_error_free(error);
5318c2affartem	}
5418c2affartem}
5518c2affartem
5618c2affartemstatic void
5797ddcdcArtem Kachitchkinesysevent_init ()
5897ddcdcArtem Kachitchkine{
5997ddcdcArtem Kachitchkine	const char	*subcl[1];
6097ddcdcArtem Kachitchkine
6197ddcdcArtem Kachitchkine	shp = sysevent_bind_handle (sysevent_dev_handler);
6297ddcdcArtem Kachitchkine	if (shp == NULL) {
6397ddcdcArtem Kachitchkine		HAL_DEBUG (("sysevent_bind_handle failed %d", errno));
6497ddcdcArtem Kachitchkine		return;
6597ddcdcArtem Kachitchkine	}
6697ddcdcArtem Kachitchkine
6797ddcdcArtem Kachitchkine	subcl[0] = ESC_DEV_EJECT_REQUEST;
6897ddcdcArtem Kachitchkine	if (sysevent_subscribe_event (shp, EC_DEV_STATUS, subcl, 1) != 0) {
6997ddcdcArtem Kachitchkine		HAL_INFO (("subscribe(dev_status) failed %d", errno));
7097ddcdcArtem Kachitchkine		sysevent_unbind_handle (shp);
7197ddcdcArtem Kachitchkine		return;
7297ddcdcArtem Kachitchkine	}
7397ddcdcArtem Kachitchkine}
7497ddcdcArtem Kachitchkine
7597ddcdcArtem Kachitchkinestatic void
7697ddcdcArtem Kachitchkinesysevent_fini ()
7797ddcdcArtem Kachitchkine{
7897ddcdcArtem Kachitchkine	if (shp != NULL) {
7997ddcdcArtem Kachitchkine		sysevent_unbind_handle (shp);
8097ddcdcArtem Kachitchkine		shp = NULL;
8197ddcdcArtem Kachitchkine	}
8297ddcdcArtem Kachitchkine}
8397ddcdcArtem Kachitchkine
8497ddcdcArtem Kachitchkinestatic void
8597ddcdcArtem Kachitchkinesysevent_dev_handler (sysevent_t *ev)
8697ddcdcArtem Kachitchkine{
8797ddcdcArtem Kachitchkine	char		*class;
8897ddcdcArtem Kachitchkine	char		*subclass;
8997ddcdcArtem Kachitchkine	nvlist_t	*attr_list;
9097ddcdcArtem Kachitchkine	char		*phys_path, *path;
9197ddcdcArtem Kachitchkine	char		*p;
9297ddcdcArtem Kachitchkine	int		len;
9397ddcdcArtem Kachitchkine	DBusError	error;
9497ddcdcArtem Kachitchkine
9597ddcdcArtem Kachitchkine	if ((class = sysevent_get_class_name (ev)) == NULL)
9697ddcdcArtem Kachitchkine		return;
9797ddcdcArtem Kachitchkine
9897ddcdcArtem Kachitchkine	if ((subclass = sysevent_get_subclass_name (ev)) == NULL)
9997ddcdcArtem Kachitchkine		return;
10097ddcdcArtem Kachitchkine
10197ddcdcArtem Kachitchkine	if ((strcmp (class, EC_DEV_STATUS) != 0) ||
10297ddcdcArtem Kachitchkine	    (strcmp (subclass, ESC_DEV_EJECT_REQUEST) != 0))
10397ddcdcArtem Kachitchkine		return;
10497ddcdcArtem Kachitchkine
10597ddcdcArtem Kachitchkine	if (sysevent_get_attr_list (ev, &attr_list) != 0)
10697ddcdcArtem Kachitchkine		return;
10797ddcdcArtem Kachitchkine
10897ddcdcArtem Kachitchkine	if (nvlist_lookup_string (attr_list, DEV_PHYS_PATH, &phys_path) != 0) {
10997ddcdcArtem Kachitchkine		goto