diff --git a/mediachanger/castorrmc/h/rmc.h b/mediachanger/castorrmc/h/rmc.h
deleted file mode 100644
index a6a7be9fd29db065310b874ff503156cfb48b3ad..0000000000000000000000000000000000000000
--- a/mediachanger/castorrmc/h/rmc.h
+++ /dev/null
@@ -1,77 +0,0 @@
-/*
- * $Id: rmc.h,v 1.2 2007/04/19 15:18:19 sponcec3 Exp $
- */
-
-/*
- * Copyright (C) 2001 by CERN/IT/PDP/DM
- * All rights reserved
- */
-
-/*
- */
-
-#ifndef _RMC_H
-#define _RMC_H
-
-			/* SCSI media changer server constants */
-
-#include "osdep.h"
-#include "rmc_constants.h"
-#include "smc.h"
-#define	CHECKI  5	/* max interval to check for work to be done */
-#define	MAXRETRY 5
-#define	RETRYI	60
-#define	LOGBUFSZ 1024
-#define	PRTBUFSZ 180
-#define	REPBUFSZ 524288	/* must be >= max media changer server reply size */
-#define	REQBUFSZ 256	/* must be >= max media changer server request size */
-#define RMC_MAGIC	0x120D0301
-#define RMC_TIMEOUT	5	/* netread timeout while receiving a request */
-
-int rmclogit(char*, char*, ...);
-
-#define RETURN(x) \
-	{ \
-	rmclogit (func, "returns %d\n", (x)); \
-	return ((x)); \
-	}
-
-			/* Request types */
-
-#define	RMC_GETGEOM	1	/* Get robot geometry */
-#define	RMC_FINDCART	2	/* Find cartridge(s) */
-#define RMC_READELEM	3	/* Read element status */
-#define	RMC_MOUNT	4	/* Mount request */
-#define	RMC_UNMOUNT	5	/* Unmount request */
-#define	RMC_EXPORT	6	/* Export tape request */
-#define	RMC_IMPORT	7	/* Import tape request */
-
-			/* SCSI media changer server reply types */
-
-#define	MSG_ERR		1
-#define	MSG_DATA	2
-#define	RMC_RC		3
-
-                        /* SCSI media changer server messages */
-
-#define	RMC00	"RMC00 - SCSI media changer server not available on %s\n"
-#define	RMC01	"RMC01 - robot parameter is mandatory\n"
-#define	RMC02	"RMC02 - %s error : %s\n"
-#define	RMC03	"RMC03 - illegal function %d\n"
-#define	RMC04	"RMC04 - error getting request, netread = %d\n"
-#define	RMC05	"RMC05 - cannot allocate enough memory\n"
-#define	RMC06	"RMC06 - invalid value for %s\n"
-#define	RMC09	"RMC09 - fatal configuration error: %s %s\n"
-#define	RMC46	"RMC46 - request too large (max. %d)\n"
-#define	RMC92	"RMC92 - %s request by %d,%d from %s\n"
-#define	RMC98	"RMC98 - %s\n"
-
-                        /* SCSI media changer server structures */
-
-struct extended_robot_info {
-	int	smc_fd;
-	char	smc_ldr[CA_MAXRBTNAMELEN+1];
-	int	smc_support_voltag;
-	struct robot_info robot_info;
-};
-#endif
diff --git a/mediachanger/castorrmc/h/rmc_api.h b/mediachanger/castorrmc/h/rmc_api.h
index 489bf1e93bc1aad9e70973fc73fc7cdd4cdef477..462bd1e1f2a9077919d43277dfbfb6ffe894165a 100644
--- a/mediachanger/castorrmc/h/rmc_api.h
+++ b/mediachanger/castorrmc/h/rmc_api.h
@@ -12,7 +12,8 @@
 
 #ifndef _RMC_API_H
 #define _RMC_API_H
-#include "smc.h"
+
+#include "h/smc_struct.h"
 
                         /*  function prototypes */
 
diff --git a/mediachanger/castorrmc/h/rmc_constants.h b/mediachanger/castorrmc/h/rmc_constants.h
index 159d807b12308ed60bb6797490640f9e3b9473af..02887bb496fc94e67293d4d1153635543180b8c6 100644
--- a/mediachanger/castorrmc/h/rmc_constants.h
+++ b/mediachanger/castorrmc/h/rmc_constants.h
@@ -14,6 +14,15 @@
 #define _RMC_CONSTANTS_H
 #include "Castor_limits.h"
 
+#define RMC_CHECKI  5   /* max interval to check for work to be done */
+#define RMC_PRTBUFSZ 180
+#define RMC_REPBUFSZ 524288 /* must be >= max media changer server reply size */
+#define RMC_REQBUFSZ 256    /* must be >= max media changer server request size */
+#define RMC_MAGIC	0x120D0301
+#define RMC_TIMEOUT	5	/* netread timeout while receiving a request */
+#define	RMC_RETRYI	60
+#define	RMC_LOGBUFSZ 1024
+
 #define RMC_PORT 5014
 
 			/* SCSI media changer utilities exit codes */
@@ -21,4 +30,37 @@
 #define	USERR	  1	/* user error */
 #define	SYERR 	  2	/* system error */
 #define	CONFERR	  4	/* configuration error */
+
+			/* Request types */
+
+#define	RMC_GETGEOM        1 /* Get robot geometry */
+#define	RMC_FINDCART       2 /* Find cartridge(s) */
+#define RMC_READELEM       3 /* Read element status */
+#define	RMC_MOUNT          4 /* Mount request */
+#define	RMC_UNMOUNT        5 /* Unmount request */
+#define	RMC_EXPORT         6 /* Export tape request */
+#define	RMC_IMPORT         7 /* Import tape request */
+#define	RMC_GENERICMOUNT   8 /* Generic (SCSI or ACS) mount request */
+#define	RMC_GENERICUNMOUNT 9 /* Generic (SCSI or ACS) mount request */
+
+			/* SCSI media changer server reply types */
+
+#define	MSG_ERR		1
+#define	MSG_DATA	2
+#define	RMC_RC		3
+
+                        /* SCSI media changer server messages */
+
+#define	RMC00	"RMC00 - SCSI media changer server not available on %s\n"
+#define	RMC01	"RMC01 - robot parameter is mandatory\n"
+#define	RMC02	"RMC02 - %s error : %s\n"
+#define	RMC03	"RMC03 - illegal function %d\n"
+#define	RMC04	"RMC04 - error getting request, netread = %d\n"
+#define	RMC05	"RMC05 - cannot allocate enough memory\n"
+#define	RMC06	"RMC06 - invalid value for %s\n"
+#define	RMC09	"RMC09 - fatal configuration error: %s %s\n"
+#define	RMC46	"RMC46 - request too large (max. %d)\n"
+#define	RMC92	"RMC92 - %s request by %d,%d from %s\n"
+#define	RMC98	"RMC98 - %s\n"
+
 #endif
diff --git a/mediachanger/castorrmc/h/rmc_logit.h b/mediachanger/castorrmc/h/rmc_logit.h
new file mode 100644
index 0000000000000000000000000000000000000000..607abb6ce569c9fc610a0e4fe12f6b37d9a99a73
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_logit.h
@@ -0,0 +1,11 @@
+/*
+ * Copyright (C) 2001 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _RMC_LOGIT_H
+#define _RMC_LOGIT_H 1
+
+int rmc_logit(const char *const func, const char *const msg, ...);
+
+#endif
diff --git a/mediachanger/castorrmc/h/rmc_logreq.h b/mediachanger/castorrmc/h/rmc_logreq.h
new file mode 100644
index 0000000000000000000000000000000000000000..aa2437b153406a0fb1aec41fc3a7a0de33c0412d
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_logreq.h
@@ -0,0 +1,11 @@
+/*
+ * Copyright (C) 2001 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _RMC_LOGREQ_H
+#define _RMC_LOGREQ_H 1
+
+void rmc_logreq(const char *const func, char *const logbuf);
+
+#endif
diff --git a/mediachanger/castorrmc/h/rmc_marshall_element.h b/mediachanger/castorrmc/h/rmc_marshall_element.h
new file mode 100644
index 0000000000000000000000000000000000000000..29c014adde578cdd76def5ae3354ec1a64fdbab6
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_marshall_element.h
@@ -0,0 +1,13 @@
+/*
+ * Copyright (C) 2001 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _RMC_MARSHALL_ELEMENT_H
+#define _RMC_MARSHALL_ELEMENT_H 1
+
+#include "h/smc_struct.h"
+
+int rmc_marshall_element (char **const sbpp, const struct smc_element_info *const element_info);
+
+#endif
diff --git a/mediachanger/castorrmc/h/rmc_procreq.h b/mediachanger/castorrmc/h/rmc_procreq.h
new file mode 100644
index 0000000000000000000000000000000000000000..00c5e899726e3540e80413eeb8ed07a165e447a1
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_procreq.h
@@ -0,0 +1,46 @@
+/******************************************************************************
+ *                      rmc_procreq.h
+ *
+ * This file is part of the Castor project.
+ * See http://castor.web.cern.ch/castor
+ *
+ * Copyright (C) 2003  CERN
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ * 
+ *
+ * @author Castor Dev team, castor-dev@cern.ch
+ *****************************************************************************/
+
+#ifndef _RMC_PROCREQ_H
+#define _RMC_PROCREQ_H 1
+
+struct rmc_srv_rqst_context {
+  const char *localhost;
+  int rpfd;
+  char *req_data;
+  const char *clienthost;
+};
+
+int rmc_srv_export(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_findcart(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_getgeom(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_import(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_mount(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_readelem(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_unmount(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_genericmount(struct rmc_srv_rqst_context *const rqst_context);
+int rmc_srv_genericunmount(struct rmc_srv_rqst_context *const rqst_context);
+
+#endif
diff --git a/mediachanger/castorrmc/h/rmc_send_scsi_cmd.h b/mediachanger/castorrmc/h/rmc_send_scsi_cmd.h
new file mode 100644
index 0000000000000000000000000000000000000000..0112e93985aaef4a77abe8981a37a92334db7b0c
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_send_scsi_cmd.h
@@ -0,0 +1,44 @@
+/******************************************************************************
+ *                      rmc_send_scsi_cmd.h
+ *
+ * This file is part of the Castor project.
+ * See http://castor.web.cern.ch/castor
+ *
+ * Copyright (C) 2003  CERN
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ * 
+ *
+ * @author Sebastien Ponce
+ *****************************************************************************/
+
+#ifndef H_RMC_SEND_SCSI_CMD_H 
+#define H_RMC_SEND_SCSI_CMD_H 1
+
+int rmc_send_scsi_cmd (
+  const int tapefd,
+  const char *const path,
+  const int do_not_open,
+  const unsigned char *const cdb,
+  const int cdblen,
+  unsigned char *const buffer,
+  const int buflen,
+  char *const sense,
+  const int senselen,
+  const int timeout,   /* in milliseconds */
+  const int flags,
+  int *const nb_sense_ret,
+  char **const msgaddr);
+
+#endif /* H_RMC_SEND_SCSI_CMD_H */
diff --git a/mediachanger/castorrmc/h/rmc_sendrep.h b/mediachanger/castorrmc/h/rmc_sendrep.h
new file mode 100644
index 0000000000000000000000000000000000000000..f8c5b8453dd6539701463a5d6540f4adacfcaa4e
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_sendrep.h
@@ -0,0 +1,11 @@
+/*
+ * Copyright (C) 1998-2002 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _RMC_SENDREP_H
+#define _RMC_SENDREP_H 1
+
+int rmc_sendrep(const int rpfd, const int rep_type, ...);
+
+#endif
diff --git a/mediachanger/castorrmc/h/rmc_server_api.h b/mediachanger/castorrmc/h/rmc_server_api.h
deleted file mode 100644
index 1366fceb2a7357eb89368e22ae3d890790e7395d..0000000000000000000000000000000000000000
--- a/mediachanger/castorrmc/h/rmc_server_api.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- */
-
-#ifndef _RMC_SERVER_API_H
-#define _RMC_SERVER_API_H
-
-                        /*  function prototypes */
-
-EXTERN_C int rmc_srv_export (char*, char*);
-EXTERN_C int rmc_srv_findcart (char*, char*);
-EXTERN_C int rmc_srv_getgeom (char*, char*);
-EXTERN_C int rmc_srv_import (char*, char*);
-EXTERN_C int rmc_srv_mount (char*, char*);
-EXTERN_C int rmc_srv_readelem (char*, char*);
-EXTERN_C int rmc_srv_unmount (char*, char*);
-
-#endif
diff --git a/mediachanger/castorrmc/h/rmc_smcsubr.h b/mediachanger/castorrmc/h/rmc_smcsubr.h
new file mode 100644
index 0000000000000000000000000000000000000000..a84ef69aaf62665be03659d5cb91258e99edc9fd
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_smcsubr.h
@@ -0,0 +1,72 @@
+/******************************************************************************
+ *                 h/rmc_smcsubr.h
+ *
+ * This file is part of the Castor project.
+ * See http://castor.web.cern.ch/castor
+ *
+ * Copyright (C) 2003  CERN
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ *
+ *
+ *
+ * @author Steven.Murray@cern.ch
+ *****************************************************************************/
+
+#ifndef H_RMC_SMCSUBR_H
+#define H_RMC_SMCSUBR_H 1
+
+#include "h/smc_struct.h"
+
+int smc_get_geometry(
+  const int fd,
+  const char *const rbtdev,
+  struct robot_info *const robot_info);
+
+int smc_read_elem_status(
+  const int fd,
+  const char *const rbtdev,
+  const int type,
+  const int start,
+  const int nbelem,
+  struct smc_element_info element_info[]);
+
+int smc_find_cartridge2 (
+  const int fd,
+  const char *const rbtdev,
+  const char *const template,
+  const int type,
+  const int start,
+  const int nbelem,
+  struct smc_element_info element_info[]);
+
+int smc_find_cartridge(
+  const int fd,
+  const char *const rbtdev,
+  const char *const template,
+  const int type,
+  const int start,
+  const int nbelem,
+  struct smc_element_info element_info[]);
+
+int smc_lasterror(
+  struct smc_status *const smc_stat,
+  char **const msgaddr);
+
+int smc_move_medium(
+  const int fd,
+  const char *const rbtdev,
+  const int from,
+  const int to,
+  const int invert);
+
+#endif /* H_RMC_SMCSUBR_H */
diff --git a/mediachanger/castorrmc/h/rmc_smcsubr2.h b/mediachanger/castorrmc/h/rmc_smcsubr2.h
new file mode 100644
index 0000000000000000000000000000000000000000..448e488ac9258975ed23653e553dcc7812cae7dc
--- /dev/null
+++ b/mediachanger/castorrmc/h/rmc_smcsubr2.h
@@ -0,0 +1,42 @@
+/*
+ * Copyright (C) 1998-2002 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _RMC_SMCSUBR2_H
+#define _RMC_SMCSUBR2_H 1
+
+#include "h/smc_struct.h"
+
+int smc_dismount (
+  const int rpfd,
+  const int fd,
+  const char *const loader,
+  struct robot_info *const robot_info,
+  const int drvord,
+  const char *const vid);
+
+int smc_export (
+  const int rpfd,
+  const int fd,
+  const char *const loader,
+  struct robot_info *const robot_info,
+  const char *const vid);
+
+int smc_import (
+  const int rpfd,
+  const int fd,
+  const char *const loader,
+  struct robot_info *const robot_info,
+  const char *const vid);
+
+int smc_mount (
+  const int rpfd,
+  const int fd,
+  const char *const loader,
+  struct robot_info *const robot_info,
+  const int drvord,
+  const char *const vid,
+  const int invert);
+
+#endif
diff --git a/mediachanger/castorrmc/h/smc_constants.h b/mediachanger/castorrmc/h/smc_constants.h
new file mode 100644
index 0000000000000000000000000000000000000000..e5eb67bef9161b69091dc568c878a8031056589c
--- /dev/null
+++ b/mediachanger/castorrmc/h/smc_constants.h
@@ -0,0 +1,33 @@
+/*
+ * Copyright (C) 1998-2002 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _SMC_CONSTANT_H
+#define _SMC_CONSTANT_H 1
+
+			/* error messages */
+
+#define	SR001	"SR001 - drive ordinal must be a non negative integer\n"
+#define	SR002	"SR002 - option -%c and -%c are mutually exclusive\n"
+#define	SR003	"SR003 - invalid query type %c\n"
+#define	SR004	"SR004 - vid %s must be at most 6 characters long\n"
+#define	SR005	"SR005 - loader must be specified\n"
+#define	SR006	"SR006 - drive ordinal is mandatory for demount operations\n"
+#define	SR007	"SR007 - drive ordinal and vid are mandatory for mount operations\n"
+#define	SR008	"SR008 - invalid device ordinal (must be < %d)\n"
+#define	SR009	"SR009 - vid mismatch: %s on request, %s on drive\n"
+#define	SR010	"SR010 - number of elements must be a positive integer\n"
+#define	SR011	"SR011 - vid is mandatory for export operations\n"
+#define	SR012	"SR012 - cannot allocate enough memory\n"
+#define	SR013	"SR013 - export slots are full\n"
+#define	SR014	"SR014 - slot ordinal must be a non negative integer\n"
+#define	SR015	"SR015 - storage cells are full\n"
+#define	SR016	"SR016 - invalid slot address (must be < %d)\n"
+#define	SR017	"SR017 - %s %s failed : %s\n"
+#define	SR018	"SR018 - %s of %s on drive %d failed : %s\n"
+#define	SR019	"SR019 - %s : %s error : %s\n"
+#define	SR020	"SR020 - %s failed : %s\n"
+#define	SR021	"SR021 - specify source slot and target slot\n"
+
+#endif
diff --git a/mediachanger/castorrmc/h/smc_struct.h b/mediachanger/castorrmc/h/smc_struct.h
new file mode 100644
index 0000000000000000000000000000000000000000..067e7e280a41b30091dd88468e796129f360c29f
--- /dev/null
+++ b/mediachanger/castorrmc/h/smc_struct.h
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 1998-2002 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+#ifndef _SMC_STRUCT_H
+#define _SMC_STRUCT_H 1
+
+#include "h/Castor_limits.h"
+
+struct robot_info {
+  char inquiry[32];
+  int transport_start;
+  int transport_count;
+  int slot_start;
+  int slot_count;
+  int port_start;
+  int port_count;
+  int device_start;
+  int device_count;
+};
+
+struct extended_robot_info {
+  int     smc_fd;
+  char    smc_ldr[CA_MAXRBTNAMELEN+1];
+  int     smc_support_voltag;
+  struct robot_info robot_info;
+};
+
+struct smc_element_info {
+  int element_address;
+  int element_type;
+  int state;
+  unsigned char asc;
+  unsigned char ascq;
+  int flags;
+  int source_address;
+  char name[9];
+};
+
+struct smc_status {
+  unsigned char asc;
+  unsigned char ascq;
+  int save_errno;
+  int rc;		/* return code from send_scsi_cmd */
+  unsigned char sensekey;
+  int skvalid;	/* sense key is valid */
+};
+
+#endif
diff --git a/mediachanger/castorrmc/rmc/Imakefile b/mediachanger/castorrmc/rmc/Imakefile
index 26150ad14e9ec9faf33a5a838dc585c4efc26bb4..531e86271b0b8adbaa02a97c73840f1e23d01a88 100644
--- a/mediachanger/castorrmc/rmc/Imakefile
+++ b/mediachanger/castorrmc/rmc/Imakefile
@@ -11,7 +11,15 @@ include $(CASTOR_ROOT)/tape/Makefile
 
 RMCD_DEPLIBS = DepSharedLibraryTargetName(tape,castortape)
 RMCD_LIBS = $(RMCD_DEPLIBS) BuildRPathcastortape
-RMCD_OBJS = rmc_serv.o rmc_procreq.o rmclogit.o sendrep.o usrmsg.o smcsubr.o smcsubr2.o
+RMCD_OBJS = \
+  rmc_serv.o \
+  rmc_procreq.o \
+  rmc_logit.o \
+  rmc_logreq.o \
+  rmc_marshall_element.o \
+  rmc_sendrep.o \
+  rmc_smcsubr.o \
+  rmc_send_scsi_cmd.o
 TapeProgramTarget(rmcd,$(RMCD_OBJS),$(RMCD_DEPLIBS),$(RMCD_LIBS),755)
 ADMMANPAGE(rmcd)
 TapeMakeDir($(LOGPATH),0755)
diff --git a/mediachanger/castorrmc/rmc/rmc_dismount.c b/mediachanger/castorrmc/rmc/rmc_dismount.c
index 13ce8015766651aed0d591bf281383fa4656961e..6b4690177177747d55dc168dd8ca17e30ca156db 100644
--- a/mediachanger/castorrmc/rmc/rmc_dismount.c
+++ b/mediachanger/castorrmc/rmc/rmc_dismount.c
@@ -9,10 +9,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_dismount(
 	const char *const server,
 	const char *const vid,
@@ -25,7 +25,7 @@ int rmc_dismount(
 	char *q;
 	char repbuf[1];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -54,6 +54,6 @@ int rmc_dismount(
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	return (c);
 }
diff --git a/mediachanger/castorrmc/rmc/rmc_errmsg.c b/mediachanger/castorrmc/rmc/rmc_errmsg.c
index d4f60c56599f8d47c174a846e2fcbc8822b9d06a..9c2342810bf738376b3961fe0a0f6851f59b7e12 100644
--- a/mediachanger/castorrmc/rmc/rmc_errmsg.c
+++ b/mediachanger/castorrmc/rmc/rmc_errmsg.c
@@ -8,7 +8,8 @@
 #include <string.h>
 #include <stdarg.h>
 #include <sys/types.h>
-#include "rmc.h"
+
+#include "h/rmc_constants.h"
 
 static char *errbufp = NULL;
 static int errbuflen;
@@ -28,7 +29,7 @@ rmc_seterrbuf(char *buffer,
 int rmc_errmsg(char *func, char *msg, ...)
 {
 	va_list args;
-	char prtbuf[PRTBUFSZ];
+	char prtbuf[RMC_PRTBUFSZ];
 	int save_errno;
 
 	save_errno = errno;
diff --git a/mediachanger/castorrmc/rmc/rmc_export.c b/mediachanger/castorrmc/rmc/rmc_export.c
index 38914793e10c1d2ead757b6c95f24b7d16314e0d..08bc1ccd03eae32eab5c8e4cdfc48ef761b80961 100644
--- a/mediachanger/castorrmc/rmc/rmc_export.c
+++ b/mediachanger/castorrmc/rmc/rmc_export.c
@@ -8,10 +8,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_export(const char *const server, const char *const vid)
 {
 	int c;
@@ -20,7 +20,7 @@ int rmc_export(const char *const server, const char *const vid)
 	char *q;
 	char repbuf[1];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -47,6 +47,6 @@ int rmc_export(const char *const server, const char *const vid)
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	return (c);
 }
diff --git a/mediachanger/castorrmc/rmc/rmc_find_cartridge.c b/mediachanger/castorrmc/rmc/rmc_find_cartridge.c
index 9497a1ac36379f3a4ba45a7995b58242200f58b5..8728ba2f4e1dd0285d06f85beaceb08896ddf12c 100644
--- a/mediachanger/castorrmc/rmc/rmc_find_cartridge.c
+++ b/mediachanger/castorrmc/rmc/rmc_find_cartridge.c
@@ -9,10 +9,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_find_cartridge(
 	const char *const server,
 	const char *const pattern,
@@ -28,9 +28,9 @@ int rmc_find_cartridge(
 	int msglen;
 	char *q;
 	char *rbp;
-	char repbuf[REPBUFSZ];
+	char repbuf[RMC_REPBUFSZ];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -60,7 +60,7 @@ int rmc_find_cartridge(
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	if (c == 0) {
 		rbp = repbuf;
 		unmarshall_LONG (rbp, c);
diff --git a/mediachanger/castorrmc/rmc/rmc_get_geometry.c b/mediachanger/castorrmc/rmc/rmc_get_geometry.c
index cbf23df57920dbcd927127c5b61814b3448045c3..16f7e42f5c57ed7f94ae8636bcd92aac904593e4 100644
--- a/mediachanger/castorrmc/rmc/rmc_get_geometry.c
+++ b/mediachanger/castorrmc/rmc/rmc_get_geometry.c
@@ -9,10 +9,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_get_geometry(
 	const char *const server,
 	struct robot_info *const robot_info)
@@ -24,7 +24,7 @@ int rmc_get_geometry(
 	char *rbp;
 	char repbuf[64];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -50,7 +50,7 @@ int rmc_get_geometry(
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	if (c == 0) {
 		rbp = repbuf;
         	unmarshall_STRING (rbp, robot_info->inquiry);
diff --git a/mediachanger/castorrmc/rmc/rmc_import.c b/mediachanger/castorrmc/rmc/rmc_import.c
index c198b2868ae50516273c8f292bb0c55dde07e8f8..bc0d20e3ab2ba984a4ad2c6f65f004105d86d379 100644
--- a/mediachanger/castorrmc/rmc/rmc_import.c
+++ b/mediachanger/castorrmc/rmc/rmc_import.c
@@ -9,10 +9,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_import(const char *const server, const char *const vid)
 {
 	int c;
@@ -21,7 +21,7 @@ int rmc_import(const char *const server, const char *const vid)
 	char *q;
 	char repbuf[1];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -48,6 +48,6 @@ int rmc_import(const char *const server, const char *const vid)
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	return (c);
 }
diff --git a/mediachanger/castorrmc/rmc/rmclogit.c b/mediachanger/castorrmc/rmc/rmc_logit.c
similarity index 85%
rename from mediachanger/castorrmc/rmc/rmclogit.c
rename to mediachanger/castorrmc/rmc/rmc_logit.c
index dd93368c7fdb1a9beff550822eb90ab384c10481..76054ebbaa593bccc74ec6c1c7a20277fa2c92bd 100644
--- a/mediachanger/castorrmc/rmc/rmclogit.c
+++ b/mediachanger/castorrmc/rmc/rmc_logit.c
@@ -11,13 +11,14 @@
 #include <time.h>
 #include <stdarg.h>
 #include <unistd.h>
-#include "rmc.h"
+#include "h/rmc_constants.h"
+#include "h/rmc_logit.h"
 extern int jid;
 
-int rmclogit(char *func, char *msg, ...)
+int rmc_logit(const char *const func, const char *const msg, ...)
 {
 	va_list args;
-	char prtbuf[PRTBUFSZ];
+	char prtbuf[RMC_PRTBUFSZ];
 	int save_errno;
 	struct tm *tm;
 	time_t current_time;
diff --git a/mediachanger/castorrmc/rmc/rmc_logreq.c b/mediachanger/castorrmc/rmc/rmc_logreq.c
new file mode 100644
index 0000000000000000000000000000000000000000..6ee62b40d8f1afff89a104c86586be7bbfb17894
--- /dev/null
+++ b/mediachanger/castorrmc/rmc/rmc_logreq.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (C) 2001-2002 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+ 
+#include "h/rmc_constants.h"
+#include "h/rmc_logit.h"
+#include "h/rmc_logreq.h"
+#include "h/tplogger_api.h"
+
+#include <string.h>
+ 
+/*	rmc_logreq - log a request */
+
+/*	Split the message into lines so they don't exceed LOGBUFSZ-1 characters
+ *	A backslash is appended to a line to be continued
+ *	A continuation line is prefixed by '+ '
+ */
+void rmc_logreq(const char *const func, char *const logbuf) {
+	int n1, n2;
+	char *p;
+	char savechrs1[2];
+	char savechrs2[2];
+
+	n1 = RMC_LOGBUFSZ - strlen (func) - 36;
+	n2 = strlen (logbuf);
+	p = logbuf;
+	while (n2 > n1) {
+		savechrs1[0] = *(p + n1);
+		savechrs1[1] = *(p + n1 + 1);
+		*(p + n1) = '\\';
+		*(p + n1 + 1) = '\0';
+		rmc_logit (func, RMC98, p);
+                tl_rmcdaemon.tl_log( &tl_rmcdaemon, 98, 2,
+                                     "func"   , TL_MSG_PARAM_STR, "rmc_logreq",
+                                     "Request", TL_MSG_PARAM_STR, p );
+		if (p != logbuf) {
+			*p = savechrs2[0];
+			*(p + 1) = savechrs2[1];
+		}
+		p += n1 - 2;
+		savechrs2[0] = *p;
+		savechrs2[1] = *(p + 1);
+		*p = '+';
+		*(p + 1) = ' ';
+		*(p + 2) = savechrs1[0];
+		*(p + 3) = savechrs1[1];
+		n2 -= n1;
+	}
+	rmc_logit (func, RMC98, p);
+        tl_rmcdaemon.tl_log( &tl_rmcdaemon, 98, 2,
+                             "func"   , TL_MSG_PARAM_STR, "rmc_logreq",
+                             "Request", TL_MSG_PARAM_STR, p );
+	if (p != logbuf) {
+		*p = savechrs2[0];
+		*(p + 1) = savechrs2[1];
+	}
+}
diff --git a/mediachanger/castorrmc/rmc/rmc_marshall_element.c b/mediachanger/castorrmc/rmc/rmc_marshall_element.c
new file mode 100644
index 0000000000000000000000000000000000000000..f1607fcce2debc6e1546e534c2b5e011ffe04757
--- /dev/null
+++ b/mediachanger/castorrmc/rmc/rmc_marshall_element.c
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2001-2002 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+ 
+#include "h/marshall.h"
+#include "h/rmc_marshall_element.h"
+ 
+int rmc_marshall_element (
+	char **const sbpp,
+	const struct smc_element_info *const element_info)
+{
+	char *sbp = *sbpp;
+
+	marshall_WORD (sbp, element_info->element_address);
+	marshall_BYTE (sbp, element_info->element_type);
+	marshall_BYTE (sbp, element_info->state);
+	marshall_BYTE (sbp, element_info->asc);
+	marshall_BYTE (sbp, element_info->ascq);
+	marshall_BYTE (sbp, element_info->flags);
+	marshall_WORD (sbp, element_info->source_address);
+	marshall_STRING (sbp, element_info->name);
+	*sbpp = sbp;
+	return (0);
+}
diff --git a/mediachanger/castorrmc/rmc/rmc_mount.c b/mediachanger/castorrmc/rmc/rmc_mount.c
index f1dd077aac53725769ad4f5dde93cef4a31fadaa..aa72260c325419e5dac143eca669ec7383e84b87 100644
--- a/mediachanger/castorrmc/rmc/rmc_mount.c
+++ b/mediachanger/castorrmc/rmc/rmc_mount.c
@@ -9,10 +9,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_mount(
 	const char *const server,
 	const char *const vid,
@@ -25,7 +25,7 @@ int rmc_mount(
 	char *q;
 	char repbuf[1];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -54,6 +54,6 @@ int rmc_mount(
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	return (c);
 }
diff --git a/mediachanger/castorrmc/rmc/rmc_procreq.c b/mediachanger/castorrmc/rmc/rmc_procreq.c
index 4cda2a7acb644fb10865988725868ec70e13f00e..5390ea0483832690247d6197cd0d830847818fa2 100644
--- a/mediachanger/castorrmc/rmc/rmc_procreq.c
+++ b/mediachanger/castorrmc/rmc/rmc_procreq.c
@@ -12,91 +12,25 @@
 #include <time.h>
 #include <sys/types.h>
 #include <netinet/in.h>
-#include "Cupv_api.h"
-#include "marshall.h"
-#include "rmc.h"
-#include "serrno.h"
-#include "tplogger_api.h"
+#include "h/Cupv_api.h"
+#include "h/marshall.h"
+#include "h/serrno.h"
+#include "h/rmc_constants.h"
+#include "h/rmc_logit.h"
+#include "h/rmc_logreq.h"
+#include "h/rmc_marshall_element.h"
+#include "h/rmc_procreq.h"
+#include "h/rmc_smcsubr.h"
+#include "h/rmc_smcsubr2.h"
+#include "h/rmc_sendrep.h"
+#include "h/tplogger_api.h"
 #include <string.h>
 #include <Ctape_api.h>
-extern int being_shutdown;
 extern struct extended_robot_info extended_robot_info;
-extern char localhost[CA_MAXHOSTNAMELEN+1];
-extern int rpfd;
-void procreq(int, char*, char*);
  
-/*	rmc_logreq - log a request */
-
-/*	Split the message into lines so they don't exceed LOGBUFSZ-1 characters
- *	A backslash is appended to a line to be continued
- *	A continuation line is prefixed by '+ '
- */
-void
-rmc_logreq(char *func,
-	   char *logbuf)
-{
-	int n1, n2;
-	char *p;
-	char savechrs1[2];
-	char savechrs2[2];
-
-	n1 = LOGBUFSZ - strlen (func) - 36;
-	n2 = strlen (logbuf);
-	p = logbuf;
-	while (n2 > n1) {
-		savechrs1[0] = *(p + n1);
-		savechrs1[1] = *(p + n1 + 1);
-		*(p + n1) = '\\';
-		*(p + n1 + 1) = '\0';
-		rmclogit (func, RMC98, p);
-                tl_rmcdaemon.tl_log( &tl_rmcdaemon, 98, 2,
-                                     "func"   , TL_MSG_PARAM_STR, "rmc_logreq",
-                                     "Request", TL_MSG_PARAM_STR, p );
-		if (p != logbuf) {
-			*p = savechrs2[0];
-			*(p + 1) = savechrs2[1];
-		}
-		p += n1 - 2;
-		savechrs2[0] = *p;
-		savechrs2[1] = *(p + 1);
-		*p = '+';
-		*(p + 1) = ' ';
-		*(p + 2) = savechrs1[0];
-		*(p + 3) = savechrs1[1];
-		n2 -= n1;
-	}
-	rmclogit (func, RMC98, p);
-        tl_rmcdaemon.tl_log( &tl_rmcdaemon, 98, 2,
-                             "func"   , TL_MSG_PARAM_STR, "rmc_logreq",
-                             "Request", TL_MSG_PARAM_STR, p );
-	if (p != logbuf) {
-		*p = savechrs2[0];
-		*(p + 1) = savechrs2[1];
-	}
-}
-
-int marshall_ELEMENT (char **sbpp,
-                      struct smc_element_info *element_info)
-{
-	char *sbp = *sbpp;
-
-	marshall_WORD (sbp, element_info->element_address);
-	marshall_BYTE (sbp, element_info->element_type);
-	marshall_BYTE (sbp, element_info->state);
-	marshall_BYTE (sbp, element_info->asc);
-	marshall_BYTE (sbp, element_info->ascq);
-	marshall_BYTE (sbp, element_info->flags);
-	marshall_WORD (sbp, element_info->source_address);
-	marshall_STRING (sbp, element_info->name);
-	*sbpp = sbp;
-	return (0);
-}
-
 /*	rmc_srv_export - export/eject a cartridge from the robot */
 
-int rmc_srv_export(char *req_data,
-                   char *clienthost)
-{
+int rmc_srv_export(struct rmc_srv_rqst_context *const rqst_context) {
 	int c;
 	char func[16];
 	gid_t gid;
@@ -106,46 +40,51 @@ int rmc_srv_export(char *req_data,
 	char vid[CA_MAXVIDLEN+1];
 
 	strncpy (func, "rmc_srv_export", 16);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "export", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "export", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_export",
-                             "Type"      , TL_MSG_PARAM_STR, "export",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_export",
+		"Type"      , TL_MSG_PARAM_STR, "export",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR, rqst_context->clienthost );
 	/* Unmarshall and ignore the loader field as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	if (unmarshall_STRINGN (rbp, vid, CA_MAXVIDLEN+1)) {
-		sendrep (rpfd, MSG_ERR, RMC06, "vid");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "vid");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	snprintf (logbuf, CA_MAXVIDLEN+8, "export %s", vid);
 	rmc_logreq (func, logbuf);
 
-	if (Cupv_check (uid, gid, clienthost, localhost, P_TAPE_OPERATOR)) {
-		sendrep (rpfd, MSG_ERR, "%s\n", sstrerror(serrno));
-		RETURN (ERMCUNREC);
+	if (Cupv_check (uid, gid, rqst_context->clienthost,
+		rqst_context->localhost, P_TAPE_OPERATOR)) {
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, "%s\n",
+			sstrerror(serrno));
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
-	c = smc_export (extended_robot_info.smc_fd, extended_robot_info.smc_ldr,
-	    &extended_robot_info.robot_info, vid);
+	c = smc_export (rqst_context->rpfd, extended_robot_info.smc_fd,
+          extended_robot_info.smc_ldr, &extended_robot_info.robot_info, vid);
 	if (c) c += ERMCRBTERR;
-	RETURN (c);
+	rmc_logit (func, "returns %d\n", c);
+	return c;
 }
 
 /*	rmc_srv_findcart - find cartridge(s) */
 
-int rmc_srv_findcart(char *req_data,
-                     char *clienthost)
-{
+int rmc_srv_findcart(struct rmc_srv_rqst_context *const rqst_context) {
 	int c;
 	struct smc_element_info *element_info;
 	struct smc_element_info *elemp;
@@ -165,27 +104,31 @@ int rmc_srv_findcart(char *req_data,
 	uid_t uid;
 
 	strncpy (func, "rmc_srv_findcart", 17);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "findcart", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "findcart", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_findcart",
-                             "Type"      , TL_MSG_PARAM_STR, "findcart",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_findcart",
+		"Type"      , TL_MSG_PARAM_STR, "findcart",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR,
+		rqst_context->clienthost);
 	/* Unmarshall and ignore the loader fiel as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	if (unmarshall_STRINGN (rbp, template, 40)) {
-		sendrep (rpfd, MSG_ERR, RMC06, "template");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "template");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	unmarshall_LONG (rbp, type);
 	unmarshall_LONG (rbp, startaddr);
@@ -194,12 +137,14 @@ int rmc_srv_findcart(char *req_data,
 	rmc_logreq (func, logbuf);
 
 	if (nbelem < 1) {
-		sendrep (rpfd, MSG_ERR, RMC06, "nbelem");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "nbelem");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	if ((element_info = malloc (nbelem * sizeof(struct smc_element_info))) == NULL) {
-		sendrep (rpfd, MSG_ERR, RMC05);
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC05);
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	if (extended_robot_info.smc_support_voltag)
 		c = smc_find_cartridge (extended_robot_info.smc_fd,
@@ -212,30 +157,32 @@ int rmc_srv_findcart(char *req_data,
 	if (c < 0) {
 		c = smc_lasterror (&smc_status, &msgaddr);
 		free (element_info);
-		sendrep (rpfd, MSG_ERR, RMC02, "smc_find_cartridge", msgaddr);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC02,
+			"smc_find_cartridge", msgaddr);
 		c += ERMCRBTERR;
-		RETURN (c);
+		rmc_logit (func, "returns %d\n", c);
+		return c;
 	}
 	if ((repbuf = malloc (c * 18 + 4)) == NULL) {
-		sendrep (rpfd, MSG_ERR, RMC05);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC05);
 		free (element_info);
-		RETURN (ERMCUNREC);
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	sbp = repbuf;
 	marshall_LONG (sbp, c);
 	for (i = 0, elemp = element_info; i < c; i++, elemp++)
-		marshall_ELEMENT (&sbp, elemp);
+		rmc_marshall_element (&sbp, elemp);
 	free (element_info);
-	sendrep (rpfd, MSG_DATA, sbp - repbuf, repbuf);
+	rmc_sendrep (rqst_context->rpfd, MSG_DATA, sbp - repbuf, repbuf);
 	free (repbuf);
-	RETURN (0);
+	rmc_logit (func, "returns %d\n", 0);
+	return 0;
 }
 
 /*	rmc_srv_getgeom - get the robot geometry */
 
-int rmc_srv_getgeom(char *req_data,
-                    char *clienthost)
-{
+int rmc_srv_getgeom(struct rmc_srv_rqst_context *const rqst_context) {
 	char func[16];
 	gid_t gid;
 	char logbuf[8];
@@ -245,22 +192,24 @@ int rmc_srv_getgeom(char *req_data,
 	uid_t uid;
 
 	strncpy (func, "rmc_srv_getgeom", 16);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "getgeom", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "getgeom", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_getgeom",
-                             "Type"      , TL_MSG_PARAM_STR, "getgeom",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_getgeom",
+		"Type"      , TL_MSG_PARAM_STR, "getgeom",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR, rqst_context->clienthost );
 	/* Unmarshall and ignore the loader field as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	snprintf (logbuf, 8, "getgeom");
@@ -276,15 +225,14 @@ int rmc_srv_getgeom(char *req_data,
 	marshall_LONG (sbp, extended_robot_info.robot_info.port_count);
 	marshall_LONG (sbp, extended_robot_info.robot_info.device_start);
 	marshall_LONG (sbp, extended_robot_info.robot_info.device_count);
-	sendrep (rpfd, MSG_DATA, sbp - repbuf, repbuf);
-	RETURN (0);
+	rmc_sendrep (rqst_context->rpfd, MSG_DATA, sbp - repbuf, repbuf);
+	rmc_logit (func, "returns %d\n", 0);
+	return 0;
 }
 
 /*	rmc_srv_import - import/inject a cartridge into the robot */
 
-int rmc_srv_import(char *req_data,
-                   char *clienthost)
-{
+int rmc_srv_import(struct rmc_srv_rqst_context *const rqst_context) {
 	int c;
 	char func[16];
 	gid_t gid;
@@ -294,46 +242,51 @@ int rmc_srv_import(char *req_data,
 	char vid[CA_MAXVIDLEN+1];
 
 	strncpy (func, "rmc_srv_import", 16);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "import", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "import", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_import",
-                             "Type"      , TL_MSG_PARAM_STR, "import",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_import",
+		"Type"      , TL_MSG_PARAM_STR, "import",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR, rqst_context->clienthost );
 	/* Unmarshall and ignore the loader field as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	if (unmarshall_STRINGN (rbp, vid, CA_MAXVIDLEN+1)) {
-		sendrep (rpfd, MSG_ERR, RMC06, "vid");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "vid");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	snprintf (logbuf, CA_MAXVIDLEN+8, "import %s", vid);
 	rmc_logreq (func, logbuf);
 
-	if (Cupv_check (uid, gid, clienthost, localhost, P_TAPE_OPERATOR)) {
-		sendrep (rpfd, MSG_ERR, "%s\n", sstrerror(serrno));
-		RETURN (ERMCUNREC);
+	if (Cupv_check (uid, gid, rqst_context->clienthost,
+		rqst_context->localhost, P_TAPE_OPERATOR)) {
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, "%s\n",
+			sstrerror(serrno));
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
-	c = smc_import (extended_robot_info.smc_fd, extended_robot_info.smc_ldr,
-	    &extended_robot_info.robot_info, vid);
+	c = smc_import (rqst_context->rpfd, extended_robot_info.smc_fd,
+	  extended_robot_info.smc_ldr, &extended_robot_info.robot_info, vid);
 	if (c) c += ERMCRBTERR;
-	RETURN (c);
+	rmc_logit (func, "returns %d\n", c);
+	return c;
 }
 
 /*	rmc_srv_mount - mount a cartridge on a drive */
 
-int rmc_srv_mount(char *req_data,
-                  char *clienthost)
-{
+int rmc_srv_mount(struct rmc_srv_rqst_context *const rqst_context) {
 	int c;
 	int drvord;
 	char func[16];
@@ -345,48 +298,54 @@ int rmc_srv_mount(char *req_data,
 	char vid[CA_MAXVIDLEN+1];
 
 	strncpy (func, "rmc_srv_mount", 16);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "mount", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "mount", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_mount",
-                             "Type"      , TL_MSG_PARAM_STR, "mount",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_mount",
+		"Type"      , TL_MSG_PARAM_STR, "mount",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR, rqst_context->clienthost );
 	/* Unmarshall and ignore the loader field as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	if (unmarshall_STRINGN (rbp, vid, CA_MAXVIDLEN+1)) {
-		sendrep (rpfd, MSG_ERR, RMC06, "vid");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "vid");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	unmarshall_WORD (rbp, invert);
 	unmarshall_WORD (rbp, drvord);
 	snprintf (logbuf, CA_MAXVIDLEN+64, "mount %s/%d on drive %d", vid, invert, drvord);
 	rmc_logreq (func, logbuf);
 
-	if (Cupv_check (uid, gid, clienthost, localhost, P_TAPE_SYSTEM)) {
-		sendrep (rpfd, MSG_ERR, "%s\n", sstrerror(serrno));
-		RETURN (ERMCUNREC);
+	if (Cupv_check (uid, gid, rqst_context->clienthost,
+		rqst_context->localhost, P_TAPE_SYSTEM)) {
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, "%s\n",
+			sstrerror(serrno));
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
-	c = smc_mount (extended_robot_info.smc_fd, extended_robot_info.smc_ldr,
-	    &extended_robot_info.robot_info, drvord, vid, invert);
+	c = smc_mount (rqst_context->rpfd, extended_robot_info.smc_fd,
+	  extended_robot_info.smc_ldr, &extended_robot_info.robot_info, drvord,
+	  vid, invert);
 	if (c) c += ERMCRBTERR;
-	RETURN (c);
+	rmc_logit (func, "returns %d\n", c);
+	return c;
 }
 
 /*	rmc_srv_readelem - read element status */
 
-int rmc_srv_readelem(char *req_data,
-                     char *clienthost)
-{
+int rmc_srv_readelem(struct rmc_srv_rqst_context *const rqst_context) {
 	int c;
 	struct smc_element_info *element_info;
 	struct smc_element_info *elemp;
@@ -405,22 +364,24 @@ int rmc_srv_readelem(char *req_data,
 	uid_t uid;
 
 	strncpy (func, "rmc_srv_readelem", 17);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "readelem", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "readelem", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_readelem",
-                             "Type"      , TL_MSG_PARAM_STR, "readelem",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_readelem",
+		"Type"      , TL_MSG_PARAM_STR, "readelem",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR, rqst_context->clienthost );
 	/* Unmarshall and ignore the loader field as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	unmarshall_LONG (rbp, type);
@@ -430,46 +391,51 @@ int rmc_srv_readelem(char *req_data,
 	rmc_logreq (func, logbuf);
 
 	if (type < 0 || type > 4) {
-		sendrep (rpfd, MSG_ERR, RMC06, "type");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "type");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	if (nbelem < 1) {
-		sendrep (rpfd, MSG_ERR, RMC06, "nbelem");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "nbelem");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	if ((element_info = malloc (nbelem * sizeof(struct smc_element_info))) == NULL) {
-		sendrep (rpfd, MSG_ERR, RMC05);
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC05);
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	if ((c = smc_read_elem_status (extended_robot_info.smc_fd,
 	    extended_robot_info.smc_ldr, type, startaddr, nbelem,
 	    element_info)) < 0) {
 		c = smc_lasterror (&smc_status, &msgaddr);
 		free (element_info);
-		sendrep (rpfd, MSG_ERR, RMC02, "smc_read_elem_status", msgaddr);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC02,
+			"smc_read_elem_status", msgaddr);
 		c += ERMCRBTERR;
-		RETURN (c);
+		rmc_logit (func, "returns %d\n", c);
+		return c;
 	}
 	if ((repbuf = malloc (c * 18 + 4)) == NULL) {
-		sendrep (rpfd, MSG_ERR, RMC05);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC05);
 		free (element_info);
-		RETURN (ERMCUNREC);
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	sbp = repbuf;
 	marshall_LONG (sbp, c);
 	for (i = 0, elemp = element_info; i < c; i++, elemp++)
-		marshall_ELEMENT (&sbp, elemp);
+		rmc_marshall_element (&sbp, elemp);
 	free (element_info);
-	sendrep (rpfd, MSG_DATA, sbp - repbuf, repbuf);
+	rmc_sendrep (rqst_context->rpfd, MSG_DATA, sbp - repbuf, repbuf);
 	free (repbuf);
-	RETURN (0);
+	rmc_logit (func, "returns %d\n", 0);
+	return 0;
 }
 
 /*	rmc_srv_unmount - dismount a cartridge from a drive */
 
-int rmc_srv_unmount(char *req_data,
-                    char *clienthost)
-{
+int rmc_srv_unmount(struct rmc_srv_rqst_context *const rqst_context) {
 	int c;
 	int drvord;
 	int force;
@@ -481,39 +447,55 @@ int rmc_srv_unmount(char *req_data,
 	char vid[CA_MAXVIDLEN+1];
 
 	strncpy (func, "rmc_srv_unmount", 16);
-	rbp = req_data;
+	rbp = rqst_context->req_data;
 	unmarshall_LONG (rbp, uid);
 	unmarshall_LONG (rbp, gid);
-	rmclogit (func, RMC92, "unmount", uid, gid, clienthost);
+	rmc_logit (func, RMC92, "unmount", uid, gid, rqst_context->clienthost);
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 92, 5,
-                             "func"      , TL_MSG_PARAM_STR, "rmc_srv_unmount",
-                             "Type"      , TL_MSG_PARAM_STR, "unmount",
-                             "UID"       , TL_MSG_PARAM_UID, uid,
-                             "GID"       , TL_MSG_PARAM_GID, gid,
-                             "ClientHost", TL_MSG_PARAM_STR, clienthost );
+		"func"      , TL_MSG_PARAM_STR, "rmc_srv_unmount",
+		"Type"      , TL_MSG_PARAM_STR, "unmount",
+		"UID"       , TL_MSG_PARAM_UID, uid,
+		"GID"       , TL_MSG_PARAM_GID, gid,
+		"ClientHost", TL_MSG_PARAM_STR, rqst_context->clienthost );
 	/* Unmarshall and ignore the loader field as it is no longer used */
 	{
 		char smc_ldr[CA_MAXRBTNAMELEN+1];
 		if (unmarshall_STRINGN (rbp, smc_ldr, CA_MAXRBTNAMELEN+1)) {
-			sendrep (rpfd, MSG_ERR, RMC06, "loader");
-			RETURN (ERMCUNREC);
+			rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06,
+				"loader");
+			rmc_logit (func, "returns %d\n", ERMCUNREC);
+			return ERMCUNREC;
 		}
 	}
 	if (unmarshall_STRINGN (rbp, vid, CA_MAXVIDLEN+1)) {
-		sendrep (rpfd, MSG_ERR, RMC06, "vid");
-		RETURN (ERMCUNREC);
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, RMC06, "vid");
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
 	unmarshall_WORD (rbp, drvord);
 	unmarshall_WORD (rbp, force);
 	snprintf (logbuf, CA_MAXVIDLEN+64, "unmount %s %d %d", vid, drvord, force);
 	rmc_logreq (func, logbuf);
 
-	if (Cupv_check (uid, gid, clienthost, localhost, P_TAPE_SYSTEM)) {
-		sendrep (rpfd, MSG_ERR, "%s\n", sstrerror(serrno));
-		RETURN (ERMCUNREC);
+	if (Cupv_check (uid, gid, rqst_context->clienthost,
+		rqst_context->localhost, P_TAPE_SYSTEM)) {
+		rmc_sendrep (rqst_context->rpfd, MSG_ERR, "%s\n",
+			sstrerror(serrno));
+		rmc_logit (func, "returns %d\n", ERMCUNREC);
+		return ERMCUNREC;
 	}
-	c = smc_dismount (extended_robot_info.smc_fd, extended_robot_info.smc_ldr,
-	    &extended_robot_info.robot_info, drvord, force == 0 ? vid : "");
+	c = smc_dismount (rqst_context->rpfd, extended_robot_info.smc_fd,
+	  extended_robot_info.smc_ldr, &extended_robot_info.robot_info, drvord,
+	  force == 0 ? vid : "");
 	if (c) c += ERMCRBTERR;
-	RETURN (c);
+	rmc_logit (func, "returns %d\n", c);
+	return c;
+}
+
+int rmc_srv_genericmount(struct rmc_srv_rqst_context *const rqst_context) {
+	return 0;
+}
+
+int rmc_srv_genericunmount(struct rmc_srv_rqst_context *const rqst_context) {
+	return 0;
 }
diff --git a/mediachanger/castorrmc/rmc/rmc_read_elem_status.c b/mediachanger/castorrmc/rmc/rmc_read_elem_status.c
index b8c92ef609e05ac985ecd312417b91d60b0f9966..551c256b827ba9b1edf7457ce1da495ae05bf0eb 100644
--- a/mediachanger/castorrmc/rmc/rmc_read_elem_status.c
+++ b/mediachanger/castorrmc/rmc/rmc_read_elem_status.c
@@ -9,10 +9,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <netinet/in.h>
-#include "marshall.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/marshall.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 int rmc_read_elem_status(
 	const char *const server,
 	const int type,
@@ -27,9 +27,9 @@ int rmc_read_elem_status(
 	int msglen;
 	char *q;
 	char *rbp;
-	char repbuf[REPBUFSZ];
+	char repbuf[RMC_REPBUFSZ];
 	char *sbp;
-	char sendbuf[REQBUFSZ];
+	char sendbuf[RMC_REQBUFSZ];
 	uid_t uid;
 
 	uid = getuid();
@@ -58,7 +58,7 @@ int rmc_read_elem_status(
 
         while ((c = send2rmc (server, sendbuf, msglen, repbuf, sizeof(repbuf))) &&
             serrno == ERMCNACT)
-                sleep (RETRYI);
+                sleep (RMC_RETRYI);
 	if (c == 0) {
 		rbp = repbuf;
 		unmarshall_LONG (rbp, c);
diff --git a/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c b/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c
new file mode 100644
index 0000000000000000000000000000000000000000..3fd4163043da0b127fd99d227318b4f225eccbb3
--- /dev/null
+++ b/mediachanger/castorrmc/rmc/rmc_send_scsi_cmd.c
@@ -0,0 +1,299 @@
+/*
+ * Copyright (C) 1996-2000 by CERN/IT/PDP/DM
+ * All rights reserved
+ */
+
+/*	rmc_send_scsi_cmd - Send a SCSI command to a device */
+/*	return	-5	if not supported on this platform (serrno = SEOPNOTSUP)
+ *		-4	if SCSI error (serrno = EIO)
+ *		-3	if CAM error (serrno = EIO)
+ *		-2	if ioctl fails with errno (serrno = errno)
+ *		-1	if open/stat fails with errno (message fully formatted)
+ *		 0	if successful with no data transfer
+ *		>0	number of bytes transferred
+ */
+
+#include <unistd.h>
+#include <sys/ioctl.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <fcntl.h>
+#include <stdlib.h>
+#include <dirent.h>
+#include <sys/stat.h>
+#include <linux/version.h>
+#include <sys/param.h>
+/* Impossible unless very very old kernels: */
+#ifndef KERNEL_VERSION
+#define KERNEL_VERSION(a,b,c) (((a) << 16) + ((b) << 8) + (c))
+#endif
+#include "/usr/include/scsi/sg.h"
+#include <sys/stat.h>
+#include "h/scsictl.h"
+#include "h/serrno.h"
+#include "h/rmc_send_scsi_cmd.h"
+static char rmc_err_msgbuf[132];
+static char *sk_msg[] = {
+        "No sense",
+        "Recovered error",
+        "Not ready",
+        "Medium error",
+        "Hardware error",
+        "Illegal request",
+        "Unit attention",
+        "Data protect",
+        "Blank check",
+        "Vendor unique",
+        "Copy aborted",
+        "Aborted command",
+        "Equal",
+        "Volume overflow",
+        "Miscompare",
+        "Reserved",
+};
+
+static void find_sgpath(char *const sgpath, const int maj, const int min) {
+        
+        /*
+          Find the sg device for a pair of major and minor device IDs
+          of a tape device. The match is done by
+          
+          . identifying the tape's st device node
+          . getting the device's unique ID from sysfs
+          . searching the sg device with the same ID (in sysfs)
+          
+          If no match is found, the returned sg path will be an empty
+          string.
+        */
+
+        char systape[] = "/sys/class/scsi_tape";
+        char sysgen[]  = "/sys/class/scsi_generic";
+        char syspath[256];
+
+        char tlink[256];
+        char glink[256];
+
+        int match = 0;        
+        DIR *dir_tape, *dir_gen;
+        struct dirent *dirent;
+        char st_dev[64];
+
+        struct stat sbuf;
+
+        sgpath[0] = '\0';
+
+        /* find the st sysfs entry */
+        if (!(dir_tape = opendir(systape))) return;  
+        while ((dirent = readdir(dir_tape))) {
+                
+                if (0 == strcmp(".", dirent->d_name)) continue;
+                if (0 == strcmp("..", dirent->d_name)) continue;
+
+                sprintf(st_dev, "/dev/%s", dirent->d_name);                
+                stat(st_dev, &sbuf);
+                if (maj == (int)major(sbuf.st_rdev) && min == (int)minor(sbuf.st_rdev)) {
+                        sprintf(syspath, "%s/%s/device", systape, dirent->d_name);
+                        match = 1;
+                        break;
+                }
+        }
+        closedir(dir_tape);
+
+        if (0 == match) return;
+
+        memset(tlink, 0, 256);
+        readlink(syspath, tlink, 256);
+
+        /* find the corresponding sg sysfs entry */
+        if (!(dir_gen = opendir(sysgen))) return;
+        while ((dirent = readdir(dir_gen))) {
+                
+                if (0 == strcmp(".", dirent->d_name)) continue;
+                if (0 == strcmp("..", dirent->d_name)) continue;
+
+                sprintf(syspath, "%s/%s/device", sysgen, dirent->d_name);
+                
+                memset(glink, 0, 256);
+                readlink(syspath, glink, 256);
+                
+                if (0 == strcmp(glink, tlink)) {
+                        sprintf(sgpath, "/dev/%s", dirent->d_name);
+                        goto out;
+                }
+        }
+ out:
+        closedir(dir_gen);
+        return;
+}
+
+
+int rmc_send_scsi_cmd (
+	const int tapefd,
+	const char *const path,
+	const int do_not_open,
+	const unsigned char *const cdb,
+	const int cdblen,
+	unsigned char *const buffer,
+	const int buflen,
+	char *const sense,
+	const int senselen,
+	const int timeout,   /* in milliseconds */
+	const int flags,
+	int *const nb_sense_ret,
+	char **const msgaddr)
+{
+	int fd;
+	FILE *fopen();
+	int n;
+	int resid = 0;
+	struct stat sbuf;
+	struct stat sbufa;
+	static char *sg_buffer;
+	static int sg_bufsiz = 0;
+	struct sg_header *sg_hd;
+	char sgpath[80];
+	int timeout_in_jiffies = 0;
+	int sg_big_buff_val =  SG_BIG_BUFF;
+	int procfd, nbread;
+	char procbuf[80];
+
+  (void)senselen;
+	/* First the value in /proc of the max buffer size for the sg driver */
+	procfd = open("/proc/scsi/sg/def_reserved_size", O_RDONLY);
+	if (procfd >= 0) {
+	  memset(procbuf, 0, sizeof(procbuf));
+	  nbread = read(procfd, procbuf, sizeof(procbuf) - 1);
+	  if (nbread > 0) {
+	    long int tmp;
+	    char *endptr = NULL;
+	    tmp = strtol(procbuf, &endptr, 10);
+	    if (endptr == NULL || *endptr == '\n') {
+	      sg_big_buff_val = (int) tmp;
+	    }
+	  }
+	  close(procfd);
+	}
+
+	if ((int)sizeof(struct sg_header) + cdblen + buflen > sg_big_buff_val) {
+		sprintf (rmc_err_msgbuf, "blocksize too large (max %zd)\n",
+		    sg_big_buff_val - sizeof(struct sg_header) - cdblen);
+		*msgaddr = rmc_err_msgbuf;
+		serrno = EINVAL;
+		return (-1);
+	}
+	if ((int)sizeof(struct sg_header)+cdblen+buflen > sg_bufsiz) {
+		if (sg_bufsiz > 0) free (sg_buffer);
+		if ((sg_buffer = malloc (sizeof(struct sg_header)+cdblen+buflen)) == NULL) {
+			serrno = errno;
+			sprintf (rmc_err_msgbuf, "cannot get memory");
+			*msgaddr = rmc_err_msgbuf;
+			return (-1);
+		}
+		sg_bufsiz = sizeof(struct sg_header) + cdblen + buflen;
+	}
+	if (do_not_open) {
+		fd = tapefd;
+		strcpy (sgpath, path);
+	} else {
+		if (stat (path, &sbuf) < 0) {
+			serrno = errno;
+        		snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "%s : stat error : %s\n", path, strerror(errno));
+			rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+        		*msgaddr = rmc_err_msgbuf;
+			return (-1);
+		}
+
+                /* get the major device ID of the sg devices ... */
+		if (stat ("/dev/sg0", &sbufa) < 0) {
+			serrno = errno;
+			snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "/dev/sg0 : stat error : %s\n", strerror(errno));
+			rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+			*msgaddr = rmc_err_msgbuf;
+			return (-1);
+		}
+                /* ... to detect links and use the path directly! */
+		if (major(sbuf.st_rdev) == major(sbufa.st_rdev)) {
+			strcpy (sgpath, path);
+		} else {
+                        find_sgpath(sgpath, major(sbuf.st_rdev), minor(sbuf.st_rdev));  
+		}
+
+		if ((fd = open (sgpath, O_RDWR)) < 0) {
+			serrno = errno;
+			snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "%s : open error : %s\n", sgpath, strerror(errno));
+			rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+			*msgaddr = rmc_err_msgbuf;
+			return (-1);
+		}
+	}
+
+        /* set the sg timeout (in jiffies) */
+        timeout_in_jiffies = timeout * HZ / 1000;
+        ioctl (fd, SG_SET_TIMEOUT, &timeout_in_jiffies);
+
+	memset (sg_buffer, 0, sizeof(struct sg_header));
+	sg_hd = (struct sg_header *) sg_buffer;
+	sg_hd->reply_len = sizeof(struct sg_header) + ((flags & SCSI_IN) ? buflen : 0);
+	sg_hd->twelve_byte = cdblen == 12;
+	memcpy (sg_buffer+sizeof(struct sg_header), cdb, cdblen);
+	n = sizeof(struct sg_header) + cdblen;
+	if (buflen && (flags & SCSI_OUT)) {
+		memcpy (sg_buffer+n, buffer, buflen);
+		n+= buflen;
+	}
+	if (write (fd, sg_buffer, n) < 0) {
+		*msgaddr = (char *) strerror(errno);
+		serrno = errno;
+		snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "%s : write error : %s\n", sgpath, *msgaddr);
+		rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+		*msgaddr = rmc_err_msgbuf;
+		if (! do_not_open) close (fd);
+		return (-2);
+	}
+	if ((n = read (fd, sg_buffer, sizeof(struct sg_header) +
+	    ((flags & SCSI_IN) ? buflen : 0))) < 0) {
+		*msgaddr = (char *) strerror(errno);
+		serrno = errno;
+		snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "%s : read error : %s\n", sgpath, *msgaddr);
+		rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+		*msgaddr = rmc_err_msgbuf;
+		if (! do_not_open) close (fd);
+		return (-2);
+	}
+	if (! do_not_open) close (fd);
+	if (sg_hd->sense_buffer[0]) {
+		memcpy (sense, sg_hd->sense_buffer, sizeof(sg_hd->sense_buffer));
+		*nb_sense_ret = sizeof(sg_hd->sense_buffer);
+	}
+	if (sg_hd->sense_buffer[0] & 0x80) {	/* valid */
+		resid = sg_hd->sense_buffer[3] << 24 | sg_hd->sense_buffer[4] << 16 |
+		    sg_hd->sense_buffer[5] << 8 | sg_hd->sense_buffer[6];
+	}
+	if ((sg_hd->sense_buffer[0] & 0x70) &&
+	    ((sg_hd->sense_buffer[2] & 0xE0) == 0 ||
+	    (sg_hd->sense_buffer[2] & 0xF) != 0)) {
+		char tmp_msgbuf[132];
+		snprintf (tmp_msgbuf, sizeof(tmp_msgbuf), "%s ASC=%X ASCQ=%X",
+		    sk_msg[*(sense+2) & 0xF], *(sense+12), *(sense+13));
+		tmp_msgbuf[sizeof(tmp_msgbuf) - 1] = '\0';
+		serrno = EIO;
+		snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "%s : scsi error : %s\n", sgpath, tmp_msgbuf);
+		rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+		*msgaddr = rmc_err_msgbuf;
+		return (-4);
+	} else if (sg_hd->result) {
+		*msgaddr = (char *) strerror(sg_hd->result);
+		serrno = sg_hd->result;
+		snprintf (rmc_err_msgbuf, sizeof(rmc_err_msgbuf), "%s : read error : %s\n", sgpath, *msgaddr);
+		rmc_err_msgbuf[sizeof(rmc_err_msgbuf) - 1] = '\0';
+		*msgaddr = rmc_err_msgbuf;
+		return (-2);
+	}
+	if (n)
+		n -= sizeof(struct sg_header) + resid;
+	if (n && (flags & SCSI_IN))
+		memcpy (buffer, sg_buffer+sizeof(struct sg_header), n);
+	return ((flags & SCSI_IN) ? n : buflen - resid);
+}
diff --git a/mediachanger/castorrmc/rmc/sendrep.c b/mediachanger/castorrmc/rmc/rmc_sendrep.c
similarity index 78%
rename from mediachanger/castorrmc/rmc/sendrep.c
rename to mediachanger/castorrmc/rmc/rmc_sendrep.c
index 1cf84fa5b96cfc13747c0dbb64fe4c650a707b0d..eefee63be6fbeaa655565ed985c1dcd17fa6eee3 100644
--- a/mediachanger/castorrmc/rmc/sendrep.c
+++ b/mediachanger/castorrmc/rmc/rmc_sendrep.c
@@ -9,25 +9,28 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdio.h>
-#include "marshall.h"
-#include "net.h"
-#include "rmc.h"
-#include "tplogger_api.h"
+#include "h/marshall.h"
+#include "h/net.h"
+#include "h/rmc_constants.h"
+#include "h/rmc_logit.h"
+#include "h/rmc_sendrep.h"
+#include "h/tplogger_api.h"
 #include <unistd.h>
 
-int sendrep(int rpfd, int rep_type, ...)
+int rmc_sendrep(const int rpfd, const int rep_type, ...)
 {
 	va_list args;
 	char func[16];
 	char *msg;
 	int n;
-	char prtbuf[PRTBUFSZ];
+	char prtbuf[RMC_PRTBUFSZ];
 	char *rbp;
 	int rc;
-	char repbuf[REPBUFSZ];
+	char repbuf[RMC_REPBUFSZ];
 	int repsize;
 
-	strncpy (func, "sendrep", 16);
+	strncpy (func, "rmc_sendrep", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
 	rbp = repbuf;
 	marshall_LONG (rbp, RMC_MAGIC);
 	va_start (args, rep_type);
@@ -38,7 +41,7 @@ int sendrep(int rpfd, int rep_type, ...)
 		vsprintf (prtbuf, msg, args);
 		marshall_LONG (rbp, strlen (prtbuf) + 1);
 		marshall_STRING (rbp, prtbuf);
-		rmclogit (func, "%s", prtbuf);
+		rmc_logit (func, "%s", prtbuf);
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 103, 2,
                                      "func"   , TL_MSG_PARAM_STR, func,
                                      "Message", TL_MSG_PARAM_STR, prtbuf );
@@ -58,7 +61,7 @@ int sendrep(int rpfd, int rep_type, ...)
 	va_end (args);
 	repsize = rbp - repbuf;
 	if (netwrite (rpfd, repbuf, repsize) != repsize) {
-		rmclogit (func, RMC02, "send", neterror());
+		rmc_logit (func, RMC02, "send", neterror());
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 3,
                                      "func" , TL_MSG_PARAM_STR, func,
                                      "On"   , TL_MSG_PARAM_STR, "send",
diff --git a/mediachanger/castorrmc/rmc/rmc_serv.c b/mediachanger/castorrmc/rmc/rmc_serv.c
index fef906b2bf86d9e4e1715eae56a7f41da6700506..d2cff293f36ea496490cf038509bc44af59be0b1 100644
--- a/mediachanger/castorrmc/rmc/rmc_serv.c
+++ b/mediachanger/castorrmc/rmc/rmc_serv.c
@@ -17,37 +17,39 @@
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include "Cinit.h"
-#include "marshall.h"
-#include "net.h"
-#include "rmc.h"
-#include "scsictl.h"
-#include "serrno.h"
-#include "rmc_server_api.h"
-#include "Cdomainname.h"
-#include "tplogger_api.h"
+#include "h/Cinit.h"
+#include "h/marshall.h"
+#include "h/net.h"
+#include "h/rmc_constants.h"
+#include "h/rmc_logit.h"
+#include "h/rmc_procreq.h"
+#include "h/rmc_sendrep.h"
+#include "h/rmc_smcsubr.h"
+#include "h/scsictl.h"
+#include "h/serrno.h"
+#include "h/Cdomainname.h"
+#include "h/tplogger_api.h"
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <Ctape_api.h>
-#include "sendscsicmd.h"
+#include "h/sendscsicmd.h"
 
 /* Forward declaration */
-int getreq(int, int*, char*, char**);
-void procreq(int, char*, char*);
+static int getreq(const int s, int *const req_type, char *const req_data,
+  char **const clienthost);
+static void procreq(const int rpfd, const int req_type, char *const req_data,
+  char *const clienthost);
+static void rmc_doit(const int rpfd);
 
-int being_shutdown;
-char func[16];
 int jid;
 char localhost[CA_MAXHOSTNAMELEN+1];
 int maxfds;
 struct extended_robot_info extended_robot_info;
-int rpfd;
 
 int rmc_main(struct main_args *main_args)
 {
 	int c;
 	unsigned char cdb[12];
-	void doit(int);
 	char domainname[CA_MAXHOSTNAMELEN+1];
 	struct sockaddr_in from;
 	socklen_t fromlen = sizeof(from);
@@ -55,11 +57,9 @@ int rmc_main(struct main_args *main_args)
 	char *msgaddr;
 	int nb_sense_ret;
 	int on = 1;	/* for REUSEADDR */
-	char *p;
 	char plist[40];
 	fd_set readfd, readmask;
 	char *robot;
-	int rqfd;
 	int s;
     int n=0;
 	char sense[MAXSENSE];
@@ -67,22 +67,14 @@ int rmc_main(struct main_args *main_args)
 	struct smc_status smc_status;
 	struct servent *sp;
 	struct timeval timeval;
+	char func[16];
+
+	strncpy (func, "rmc_serv", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
 
         /* init the tplogger interface */
         {
-                mode_t save_mask;
-                /* char *p; */
-                
-                save_mask = umask(0);
-
-                /*
-                p = getconfent ("TAPE", "TPLOGGER", 0);
-                if (p && (0 == strcasecmp(p, "SYSLOG"))) {
-                        tl_init_handle( &tl_rtcpd, "syslog" ); 
-                } else {
-                        tl_init_handle( &tl_rmc, "dlf" );  
-                }
-                */
+                const mode_t save_mask = umask(0);
 
                 /* only syslog support */
                 tl_init_handle( &tl_rmcdaemon, "syslog" );
@@ -95,8 +87,7 @@ int rmc_main(struct main_args *main_args)
         }
 
 	jid = getpid();
-	strncpy (func, "rmc_serv", 16);
-	rmclogit (func, "started\n");
+	rmc_logit (func, "started\n");
         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 109, 2,
                              "func"   , TL_MSG_PARAM_STR, "rmc_main",
                              "Message", TL_MSG_PARAM_STR, "Daemon started" );        
@@ -104,7 +95,7 @@ int rmc_main(struct main_args *main_args)
 	gethostname (localhost, CA_MAXHOSTNAMELEN+1);
 	if (strchr (localhost, '.') == NULL) {
 		if (Cdomainname (domainname, sizeof(domainname)) < 0) {
-			rmclogit (func, "Unable to get domainname\n");
+			rmc_logit (func, "Unable to get domainname\n");
                         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 103, 2,
                                              "func"   , TL_MSG_PARAM_STR, "rmc_main",
                                              "Message", TL_MSG_PARAM_STR, "Unable to get domainname" );
@@ -114,7 +105,7 @@ int rmc_main(struct main_args *main_args)
 	}
 
 	if (main_args->argc != 2) {
-		rmclogit (func, RMC01);
+		rmc_logit (func, RMC01);
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 1, 1,
                                      "func", TL_MSG_PARAM_STR, "rmc_main" );
 		exit (USERR);
@@ -122,7 +113,7 @@ int rmc_main(struct main_args *main_args)
 	robot = main_args->argv[1];
 	if (*robot == '\0' ||
 	    (strlen (robot) + (*robot == '/') ? 0 : 5) > CA_MAXRBTNAMELEN) {
-		rmclogit (func, RMC06, "robot");
+		rmc_logit (func, RMC06, "robot");
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 6, 2,
                                      "func", TL_MSG_PARAM_STR, "rmc_main",
                                      "For" , TL_MSG_PARAM_STR, "robot" );
@@ -142,20 +133,20 @@ int rmc_main(struct main_args *main_args)
                                  extended_robot_info.smc_ldr,
                                  &extended_robot_info.robot_info))) {
             c = smc_lasterror (&smc_status, &msgaddr);
-            rmclogit (func, RMC02, "get_geometry", msgaddr);
+            rmc_logit (func, RMC02, "get_geometry", msgaddr);
             tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                  "func"    , TL_MSG_PARAM_STR, "rmc_main",
                                  "On"      , TL_MSG_PARAM_STR, "get_geometry",
                                  "Message" , TL_MSG_PARAM_STR, msgaddr,
                                  "NextStep", TL_MSG_PARAM_STR, "Retry" );
 
-            rmclogit (func,"trying again get_geometry\n");
+            rmc_logit (func,"trying again get_geometry\n");
             tl_rmcdaemon.tl_log( &tl_rmcdaemon, 110, 2,
                                  "func"   , TL_MSG_PARAM_STR, "rmc_main",
                                  "Message", TL_MSG_PARAM_STR, "trying again get_geometry" );
             n++;
             if (n==2) {
-              rmclogit (func, RMC02, "get_geometry", msgaddr);
+              rmc_logit (func, RMC02, "get_geometry", msgaddr);
               tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                    "func"    , TL_MSG_PARAM_STR, "rmc_main",
                                    "On"      , TL_MSG_PARAM_STR, "get_geometry",
@@ -185,7 +176,7 @@ int rmc_main(struct main_args *main_args)
 		    sense[12] == 0x20) {
 			extended_robot_info.smc_support_voltag = 0;
 		} else {
-			rmclogit (func, RMC02, "find_cartridge", msgaddr);
+			rmc_logit (func, RMC02, "find_cartridge", msgaddr);
                         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                              "func"    , TL_MSG_PARAM_STR, "rmc_main",
                                              "On"      , TL_MSG_PARAM_STR, "find_cartridge",
@@ -202,7 +193,7 @@ int rmc_main(struct main_args *main_args)
 	/* open request socket */
 
 	if ((s = socket (AF_INET, SOCK_STREAM, 0)) < 0) {
-		rmclogit (func, RMC02, "socket", neterror());
+		rmc_logit (func, RMC02, "socket", neterror());
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                      "func"    , TL_MSG_PARAM_STR, "rmc_main",
                                      "On"      , TL_MSG_PARAM_STR, "socket",
@@ -212,23 +203,26 @@ int rmc_main(struct main_args *main_args)
 	}
 	memset ((char *)&sin, 0, sizeof(struct sockaddr_in)) ;
 	sin.sin_family = AF_INET ;
-	if ((p = getenv ("RMC_PORT")) || (p = getconfent ("RMC", "PORT", 0))) {
-		sin.sin_port = htons ((unsigned short)atoi (p));
-	} else if ((sp = getservbyname ("rmc", "tcp"))) {
-		sin.sin_port = sp->s_port;
-	} else {
-		sin.sin_port = htons ((unsigned short)RMC_PORT);
+	{
+		const char *p;
+		if ((p = getenv ("RMC_PORT")) || (p = getconfent ("RMC", "PORT", 0))) {
+			sin.sin_port = htons ((unsigned short)atoi (p));
+		} else if ((sp = getservbyname ("rmc", "tcp"))) {
+			sin.sin_port = sp->s_port;
+		} else {
+			sin.sin_port = htons ((unsigned short)RMC_PORT);
+		}
 	}
 	sin.sin_addr.s_addr = htonl(INADDR_ANY);
 	if (setsockopt (s, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
-		rmclogit (func, RMC02, "setsockopt", neterror());
+		rmc_logit (func, RMC02, "setsockopt", neterror());
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 3,
                                      "func"    , TL_MSG_PARAM_STR, "rmc_main",
                                      "On"      , TL_MSG_PARAM_STR, "setsockopt",
                                      "Message" , TL_MSG_PARAM_STR, msgaddr );
         }
 	if (bind (s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
-		rmclogit (func, RMC02, "bind", neterror());
+		rmc_logit (func, RMC02, "bind", neterror());
                 tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                      "func"    , TL_MSG_PARAM_STR, "rmc_main",
                                      "On"      , TL_MSG_PARAM_STR, "bind",
@@ -245,12 +239,12 @@ int rmc_main(struct main_args *main_args)
 	while (1) {
 		if (FD_ISSET (s, &readfd)) {
 			FD_CLR (s, &readfd);
-			rqfd = accept (s, (struct sockaddr *) &from, &fromlen);
-			rpfd = rqfd;
-			(void) doit (rqfd);
+			const int rpfd =
+				accept (s, (struct sockaddr *) &from, &fromlen);
+			(void) rmc_doit (rpfd);
 		}
 		memcpy (&readfd, &readmask, sizeof(readmask));
-		timeval.tv_sec = CHECKI;
+		timeval.tv_sec = RMC_CHECKI;
 		timeval.tv_usec = 0;
 		if (select (maxfds, &readfd, (fd_set *)0, (fd_set *)0, &timeval) < 0) {
 			FD_ZERO (&readfd);
@@ -272,25 +266,26 @@ int main(int argc,
 	exit (rmc_main (&main_args));
 }
 
-void doit(int rqfd)
+static void rmc_doit(const int rpfd)
 {
 	int c;
 	char *clienthost;
-	char req_data[REQBUFSZ-3*LONGSIZE];
+	char req_data[RMC_REQBUFSZ-3*LONGSIZE];
 	int req_type = 0;
 
-	if ((c = getreq (rqfd, &req_type, req_data, &clienthost)) == 0)
-		procreq (req_type, req_data, clienthost);
+	if ((c = getreq (rpfd, &req_type, req_data, &clienthost)) == 0)
+		procreq (rpfd, req_type, req_data, clienthost);
 	else if (c > 0)
-		sendrep (rqfd, RMC_RC, c);
+		rmc_sendrep (rpfd, RMC_RC, c);
 	else
-		close (rqfd);
+		close (rpfd);
 }
 
-int getreq(int s,
-           int *req_type,
-           char *req_data,
-           char **clienthost)
+static int getreq(
+  const int s,
+  int *const req_type,
+  char *const req_data,
+  char **const clienthost)
 {
 	struct sockaddr_in from;
 	socklen_t fromlen = sizeof(from);
@@ -301,6 +296,10 @@ int getreq(int s,
 	int n;
 	char *rbp;
 	char req_hdr[3*LONGSIZE];
+	char func[16];
+
+	strncpy (func, "rmc_serv", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
 
 	l = netread_timeout (s, req_hdr, sizeof(req_hdr), RMC_TIMEOUT);
 	if (l == sizeof(req_hdr)) {
@@ -309,20 +308,17 @@ int getreq(int s,
 		unmarshall_LONG (rbp, n);
 		*req_type = n;
 		unmarshall_LONG (rbp, msglen);
-		if (msglen > REQBUFSZ) {
-			rmclogit (func, RMC46, REQBUFSZ);
+		if (msglen > RMC_REQBUFSZ) {
+			rmc_logit (func, RMC46, RMC_REQBUFSZ);
                         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 46, 2,
                                              "func"   , TL_MSG_PARAM_STR, "getreq",
-                                             "MaxSize", TL_MSG_PARAM_INT,  REQBUFSZ);                        
+                                             "MaxSize", TL_MSG_PARAM_INT,  RMC_REQBUFSZ);                        
 			return (-1);
 		}
 		l = msglen - sizeof(req_hdr);
 		n = netread_timeout (s, req_data, l, RMC_TIMEOUT);
-		if (being_shutdown) {
-			return (ERMCNACT);
-		}
 		if (getpeername (s, (struct sockaddr *) &from, &fromlen) < 0) {
-			rmclogit (func, RMC02, "getpeername", neterror());
+			rmc_logit (func, RMC02, "getpeername", neterror());
                         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                              "func"   , TL_MSG_PARAM_STR, "getreq",
                                              "On"     , TL_MSG_PARAM_STR, "getpeername",
@@ -339,13 +335,13 @@ int getreq(int s,
 		return (0);
 	} else {
 		if (l > 0) {
-			rmclogit (func, RMC04, l);
+			rmc_logit (func, RMC04, l);
                         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 4, 3,
                                              "func"   , TL_MSG_PARAM_STR, "getreq",
                                              "netread", TL_MSG_PARAM_INT, l,
                                              "Return" , TL_MSG_PARAM_STR, "ERMCUNREC" );
 		} else if (l < 0) {
-			rmclogit (func, RMC02, "netread", strerror(errno));
+			rmc_logit (func, RMC02, "netread", strerror(errno));
                         tl_rmcdaemon.tl_log( &tl_rmcdaemon, 2, 4,
                                              "func"   , TL_MSG_PARAM_STR, "getreq",
                                              "On"     , TL_MSG_PARAM_STR, "netread",
@@ -356,37 +352,53 @@ int getreq(int s,
 	}
 }
 
-void procreq(int req_type,
-             char *req_data,
-             char *clienthost)
+static void procreq(
+  const int rpfd,
+  const int req_type,
+  char *const req_data,
+  char *const clienthost)
 {
-	int c;
+	int c = 0;
+	struct rmc_srv_rqst_context rqst_context;
+
+	rqst_context.localhost = localhost;
+	rqst_context.rpfd = rpfd;
+	rqst_context.req_data = req_data;
+	rqst_context.clienthost = clienthost;
 
 	switch (req_type) {
 	case RMC_MOUNT:
-		c = rmc_srv_mount (req_data, clienthost);
+		c = rmc_srv_mount (&rqst_context);
 		break;
 	case RMC_UNMOUNT:
-		c = rmc_srv_unmount (req_data, clienthost);
+		c = rmc_srv_unmount (&rqst_context);
 		break;
 	case RMC_EXPORT:
-		c = rmc_srv_export (req_data, clienthost);
+		c = rmc_srv_export (&rqst_context);
 		break;
 	case RMC_IMPORT:
-		c = rmc_srv_import (req_data, clienthost);
+		c = rmc_srv_import (&rqst_context);
 		break;
 	case RMC_GETGEOM:
-		c = rmc_srv_getgeom (req_data, clienthost);
+		c = rmc_srv_getgeom (&rqst_context);
 		break;
 	case RMC_READELEM:
-		c = rmc_srv_readelem (req_data, clienthost);
+		c = rmc_srv_readelem (&rqst_context);
 		break;
 	case RMC_FINDCART:
-		c = rmc_srv_findcart (req_data, clienthost);
+		c = rmc_srv_findcart (&rqst_context);
+		break;
+/*
+	case RMC_GENERICMOUNT:
+		c = rmc_srv_genericmount (localhost, rpfd, req_data, clienthost);
+		break;
+	case RMC_GENERICUNMOUNT:
+		c = rmc_srv_genericunmount (localhost, rpfd, req_data, clienthost);
 		break;
+*/
 	default:
-		sendrep (rpfd, MSG_ERR, RMC03, req_type);
+		rmc_sendrep (rpfd, MSG_ERR, RMC03, req_type);
 		c = ERMCUNREC;
 	}
-	sendrep (rpfd, RMC_RC, c);
+	rmc_sendrep (rpfd, RMC_RC, c);
 }
diff --git a/mediachanger/castorrmc/rmc/smcsubr.c b/mediachanger/castorrmc/rmc/rmc_smcsubr.c
similarity index 56%
rename from mediachanger/castorrmc/rmc/smcsubr.c
rename to mediachanger/castorrmc/rmc/rmc_smcsubr.c
index bf4f0e742302d44023897ccd4792e4a67309928c..d1954d2b6eeb1efdd1afc3b7e1000b817354e994 100644
--- a/mediachanger/castorrmc/rmc/smcsubr.c
+++ b/mediachanger/castorrmc/rmc/rmc_smcsubr.c
@@ -5,28 +5,33 @@
  
 
 #include <errno.h>
+#include <stdarg.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
-#include "Ctape.h"
-#include "scsictl.h"
-#include "serrno.h"
-#include "smc.h"
-#include "sendscsicmd.h"
-#include "getconfent.h"
+
+#include "h/Ctape.h"
+#include "h/rmc_constants.h"
+#include "h/rmc_sendrep.h"
+#include "h/rmc_smcsubr.h"
+#include "h/rmc_smcsubr2.h"
+#include "h/scsictl.h"
+#include "h/serrno.h"
+#include "h/sendscsicmd.h"
+#include "h/smc_constants.h"
+#include "h/getconfent.h"
 
 #define	RBT_XTRA_PROC 10
 static struct smc_status smc_status;
 static char *smc_msgaddr;
 
-static void
-save_error(rc, nb_sense, sense, msgaddr)
-int rc;
-int nb_sense;
-char *sense;
-char *msgaddr;
+static void save_error(
+	const int rc,
+	const int nb_sense,
+	const char *const sense,
+	char *const msgaddr)
 {
 	smc_msgaddr = msgaddr;
 	smc_status.rc = rc;
@@ -44,11 +49,10 @@ char *msgaddr;
 	}
 }
 
-static int
-vmatch (char *pattern, char *vid)
+static int vmatch (const char *const pattern, const char *const vid)
 {
-	char *p;
-	char *v;
+	const char *p;
+	const char *v;
 
 	for (p = pattern, v = vid; *p; p++, v++) {
 		if (*v == 0 && *p != '*')
@@ -72,10 +76,10 @@ vmatch (char *pattern, char *vid)
 	return (*v != 0);
 }
 
-static int
-get_element_size(int fd,
-                            char *rbtdev,
-                            int type)
+static int get_element_size(
+	const int fd,
+	const char *const rbtdev,
+	const int type)
 {
 	unsigned char buf[128];
 	unsigned char cdb[12];
@@ -117,14 +121,14 @@ get_element_size(int fd,
 	return (buf[10] * 256 + buf[11]);
 }
 
-static int
-get_element_info(char opcode,
-                            int fd,
-                            char *rbtdev,
-                            int type,
-                            int start,
-                            int nbelem,
-                            struct smc_element_info element_info[])
+static int get_element_info(
+	const char opcode,
+	const int fd,
+	const char *const rbtdev,
+	const int type,
+	const int start,
+	const int nbelem,
+	struct smc_element_info element_info[])
 {
 	int avail_elem;
 	unsigned char cdb[12];
@@ -237,9 +241,10 @@ get_element_info(char opcode,
 	return (avail_elem);
 }
 
-int smc_get_geometry(int fd,
-                     char *rbtdev,
-                     struct robot_info *robot_info)
+int smc_get_geometry(
+	const int fd,
+	const char *const rbtdev,
+        struct robot_info *const robot_info)
 {
 	unsigned char buf[36];
 	unsigned char cdb[6];
@@ -321,12 +326,13 @@ int smc_get_geometry(int fd,
 	return (0);
 }
 
-int smc_read_elem_status(int fd,
-			 char *rbtdev,
-			 int type,
-			 int start,
-			 int nbelem,
-			 struct smc_element_info element_info[])
+int smc_read_elem_status(
+	const int fd,
+	const char *const rbtdev,
+	const int type,
+	const int start,
+	const int nbelem,
+	struct smc_element_info element_info[])
 {
 	char func[16];
 	int rc;
@@ -338,13 +344,14 @@ int smc_read_elem_status(int fd,
 	return (rc);
 }
 
-int smc_find_cartridge2 (int fd,
-                         char *rbtdev,
-                         char *template,
-                         int type,
-                         int start,
-                         int nbelem,
-                         struct smc_element_info element_info[])
+int smc_find_cartridge2 (
+	const int fd,
+	const char *const rbtdev,
+	const char *const template,
+	const int type,
+	const int start,
+	const int nbelem,
+	struct smc_element_info element_info[])
 {
 	int c;
 	static char err_msgbuf[132];
@@ -404,13 +411,14 @@ int smc_find_cartridge2 (int fd,
 }
 
 
-int smc_find_cartridge(int fd,
-                       char *rbtdev,
-                       char *template,
-                       int type,
-                       int start,
-                       int nbelem,
-                       struct smc_element_info element_info[])
+int smc_find_cartridge(
+	const int fd,
+	const char *const rbtdev,
+	const char *const template,
+	const int type,
+	const int start,
+	const int nbelem,
+	struct smc_element_info element_info[])
 {
 	unsigned char cdb[12];
 	char func[16];
@@ -488,7 +496,7 @@ struct scsierr_codact {
 	short action;
 	char *txt;
 };
-struct scsierr_codact scsierr_acttbl[] = {
+static struct scsierr_codact scsierr_acttbl[] = {
     {0x02, 0x04, 0x00, RBT_FAST_RETRY, "Logical Unit Not Ready, Cause Not Reportable"},
     {0x02, 0x04, 0x01, RBT_FAST_RETRY, "Logical Unit Is In Process of Becoming Ready"},
     {0x02, 0x04, 0x02, RBT_NORETRY, "Logical Unit Not Ready, initialization required"},
@@ -528,8 +536,9 @@ struct scsierr_codact scsierr_acttbl[] = {
     {0x02, 0x5A, 0x01, RBT_NORETRY, "Operator Medium Removal Request"}
 };
 
-int smc_lasterror(struct smc_status *smc_stat,
-                  char **msgaddr)
+int smc_lasterror(
+	struct smc_status *const smc_stat,
+	char **const msgaddr)
 {
 	unsigned int i;
 
@@ -555,11 +564,12 @@ int smc_lasterror(struct smc_status *smc_stat,
 	return (RBT_NORETRY);
 }
 
-int smc_move_medium(int fd,
-                    char *rbtdev,
-                    int from,
-                    int to,
-                    int invert)
+int smc_move_medium(
+	const int fd,
+	const char *const rbtdev,
+	const int from,
+	const int to,
+	const int invert)
 {
 	unsigned char cdb[12];
 	char func[16];
@@ -603,3 +613,341 @@ int smc_move_medium(int fd,
 	}
 	return (0);
 }
+
+static int rmc_usrmsg(
+	const int rpfd,
+	const char *func,
+	const char *const msg,
+	...)
+{
+	va_list args;
+	char prtbuf[RMC_PRTBUFSZ];
+	const int save_errno = errno;
+
+	va_start (args, msg);
+	snprintf (prtbuf, sizeof(prtbuf), "%s: ", func);
+	prtbuf[sizeof(prtbuf) - 1] = '\0';
+	{
+		const size_t nbBytesUsed = strlen (prtbuf);
+
+		/* If there is still space in the print buffer */
+		if(nbBytesUsed < (sizeof(prtbuf))) {
+			const size_t nbBytesRemaining = sizeof(prtbuf) -
+				nbBytesUsed;
+			char *const p = prtbuf + nbBytesUsed;
+			vsnprintf (p, nbBytesRemaining, msg, args);
+			prtbuf[sizeof(prtbuf) - 1] = '\0';
+		}
+	}
+	rmc_sendrep (rpfd, MSG_ERR, "%s", prtbuf);
+	va_end (args);
+	errno = save_errno;
+	return (0);
+}
+
+int smc_dismount (
+	const int rpfd,
+	const int fd,
+	const char *const loader,
+	struct robot_info *const robot_info,
+	const int drvord,
+	const char *const vid)
+{
+	const unsigned int max_element_status_reads = 20;
+	const unsigned int dismount_status_read_delay = 1; /* In seconds */
+	unsigned int nb_element_status_reads = 0;
+	int drive_not_unloaded = 1;
+	struct smc_element_info drive_element_info;
+	char func[16];
+	char *msgaddr = 0;
+	struct smc_status smc_status;
+ 
+	strncpy (func, "smc_dismount", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
+
+	memset(&smc_status, '\0', sizeof(smc_status));
+
+	/* IBM libraries sometimes disagree with the eject of their drives. */
+	/* Sometimes the access bit of the result of Read Element Status    */
+	/* (XB8) indicates the gripper cannot access the tape even though   */
+	/* the eject was successful.  Reading the element status at a later */
+	/* point in time eventually indicates the tape is accessible.       */
+	while(drive_not_unloaded && nb_element_status_reads < max_element_status_reads) {
+		if (0 > smc_read_elem_status (fd, loader, 4, robot_info->device_start+drvord,
+		    	1, &drive_element_info)) {
+			const int smc_error = smc_lasterror (&smc_status, &msgaddr);
+			rmc_usrmsg ( rpfd, func, SR020, "read_elem_status", msgaddr);
+			return (smc_error);
+		}
+		if (0 == (drive_element_info.state & 0x1)) {
+			rmc_usrmsg ( rpfd, func, SR018, "demount", vid, drvord, "Medium Not Present");
+			return (RBT_OK);
+		}
+
+		drive_not_unloaded = (0 == (drive_element_info.state & 0x8));
+		if (drive_not_unloaded) {
+			rmc_usrmsg ( rpfd, func, "read_elem_status of %s on drive %d detected Drive Not Unloaded\n", vid, drvord);
+		}
+
+		nb_element_status_reads++;
+
+		if(nb_element_status_reads < max_element_status_reads) {
+			sleep(dismount_status_read_delay);
+		}
+	}
+	if(drive_not_unloaded) {
+		rmc_usrmsg ( rpfd, func, SR018, "demount", vid, drvord, "Drive Not Unloaded");
+		return (RBT_UNLD_DMNT);
+	}
+
+	if (*vid && strcmp (drive_element_info.name, vid)) {
+		rmc_usrmsg ( rpfd, func, SR009, vid, drive_element_info.name);
+		return (RBT_NORETRY);
+	}
+	if (0 > smc_move_medium (fd, loader, robot_info->device_start+drvord,
+	    drive_element_info.source_address, (drive_element_info.flags & 0x40) ? 1 : 0)) {
+		const int smc_error = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR018, "demount", vid, drvord, msgaddr);
+		return (smc_error);
+	}
+    /* check that the vid is in a slot before returning */
+    while (1) {   
+          struct smc_element_info vol_element_info;
+          if (0 > smc_find_cartridge (fd, loader, drive_element_info.name, 0, 0, 1, &vol_element_info)) {
+              const int smc_error = smc_lasterror (&smc_status, &msgaddr);
+              rmc_usrmsg ( rpfd, func, SR017, "find_cartridge", drive_element_info.name, msgaddr);
+              return (smc_error);
+          }
+         
+          /* vid is in a storage slot */  
+          if (vol_element_info.element_type == 2) break; 
+          /* give time for the tape enter the slot */
+          sleep (2);
+    }
+
+	return (0);
+}
+
+int smc_export (
+	const int rpfd,
+	const int fd,
+	const char *const loader,
+	struct robot_info *const robot_info,
+	const char *const vid)
+{
+        int c;
+        struct smc_element_info element_info;
+	char func[16];
+	int i;
+        struct smc_element_info *impexp_info;
+	char *msgaddr;
+	int nbelem;
+	struct smc_status smc_status;
+ 
+	strncpy (func, "smc_export", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
+
+	if ((c = smc_find_cartridge (fd, loader, vid, 0, 0, 1, &element_info)) < 0) {
+		c = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR017, "find_cartridge", vid, msgaddr);
+		return (c);
+	}
+	if (c == 0) {
+		rmc_usrmsg ( rpfd, func, SR017, "export", vid, "volume not in library");
+		return (RBT_NORETRY);
+	}
+	if (element_info.element_type != 2) {
+		rmc_usrmsg ( rpfd, func, SR017, "export", vid, "volume in use");
+		return (RBT_SLOW_RETRY);
+	}
+	/* look for a free export slot */
+
+	nbelem = robot_info->port_count;
+	if ((impexp_info = malloc (nbelem * sizeof(struct smc_element_info))) == NULL) {
+		rmc_usrmsg ( rpfd, func, SR012);
+		return (RBT_NORETRY);
+	}
+
+	if ((c = smc_read_elem_status (fd, loader, 3, robot_info->port_start,
+	    nbelem, impexp_info)) < 0) {
+		c = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR020, "read_elem_status", msgaddr);
+		free (impexp_info);
+		return (c);
+	}
+	for (i = 0; i < nbelem; i++) {
+		if (((impexp_info+i)->state & 0x1) == 0)	/* element free */
+			break;
+	}
+	if (i >= nbelem) {	/* export slots are full */
+		rmc_usrmsg ( rpfd, func, SR013);
+		free (impexp_info);
+		return (RBT_NORETRY);
+	}
+
+	if ((c = smc_move_medium (fd, loader, element_info.element_address,
+	    (impexp_info+i)->element_address, 0)) < 0) {
+		c = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR017, "export", vid, msgaddr);
+		free (impexp_info);
+		return (c);
+	}
+	free (impexp_info);
+	return (0);
+}
+
+int smc_import (
+	const int rpfd,
+	const int fd,
+	const char *const loader,
+	struct robot_info *const robot_info,
+	const char *const vid)
+{
+        int c;
+	int device_start;
+        struct smc_element_info *element_info;
+	char func[16];
+	int i, j;
+	char *msgaddr;
+	int nbelem;
+	int port_start;
+	int slot_start;
+	struct smc_status smc_status;
+ 
+	strncpy (func, "smc_import", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
+
+	nbelem = robot_info->transport_count + robot_info->slot_count +
+		 robot_info->port_count + robot_info->device_count;
+	if ((element_info = malloc (nbelem * sizeof(struct smc_element_info))) == NULL) {
+		rmc_usrmsg ( rpfd, func, SR012);
+		return (RBT_NORETRY);
+	}
+
+	/* get inventory */
+
+	if ((c = smc_read_elem_status (fd, loader, 0, 0, nbelem, element_info)) < 0) {
+		c = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR020, "read_elem_status", msgaddr);
+		free (element_info);
+		return (c);
+	}
+	for (i = 0; i < c; i++)
+		if ((element_info+i)->element_type == 2) break;
+	slot_start = i;
+	for (i = 0; i < c; i++)
+		if ((element_info+i)->element_type == 3) break;
+	port_start = i;
+	for (i = 0; i < c; i++)
+		if ((element_info+i)->element_type == 4) break;
+	device_start = i;
+
+	/* mark home slots of cartridges currently on drives as non free */
+
+	for (i = device_start; i < device_start+robot_info->device_count; i++) {
+		if (((element_info+i)->state & 0x1) == 0) continue;
+		for (j = slot_start; j < slot_start+robot_info->slot_count; j++)
+			if ((element_info+i)->source_address ==
+				(element_info+j)->element_address) break;
+		(element_info+j)->state |= 1;
+	}
+
+	/* loop on all import slots */
+
+	for (i = port_start; i < port_start+robot_info->port_count; i++) {
+		if (*vid && strcmp (vid, (element_info+i)->name)) continue;
+		if (*vid || (*vid == '\0' && ((element_info+i)->state & 2))) {
+
+			/* find a free storage slot */
+
+			for (j = slot_start; j < slot_start+robot_info->slot_count; j++)
+				if (((element_info+j)->state & 0x1) == 0) break;
+			if (j >= slot_start+robot_info->slot_count) {
+				rmc_usrmsg ( rpfd, func, SR015);
+				free (element_info);
+				return (RBT_NORETRY);
+			}
+
+			if ((c = smc_move_medium (fd, loader, (element_info+i)->element_address,
+			    (element_info+j)->element_address, 0)) < 0) {
+				c = smc_lasterror (&smc_status, &msgaddr);
+				rmc_usrmsg ( rpfd, func, SR017, "import",
+				    (element_info+i)->name, msgaddr);
+				free (element_info);
+				return (c);
+			}
+			if (*vid || c) break;
+			(element_info+j)->state |= 1;	/* dest slot is now full */
+		}
+	}
+	free (element_info);
+	return (c);
+}
+
+int smc_mount (
+	const int rpfd,
+	const int fd,
+	const char *const loader,
+	struct robot_info *const robot_info,
+	const int drvord,
+	const char *const vid,
+	const int invert)
+{
+    int c;
+    struct smc_element_info element_info;
+	char func[16];
+	char *msgaddr;
+	struct smc_status smc_status;
+ 
+	strncpy (func, "smc_mount", sizeof(func));
+	func[sizeof(func) - 1] = '\0';
+
+	if ((c = smc_find_cartridge (fd, loader, vid, 0, 0, 1, &element_info)) < 0) {
+		c = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR017, "find_cartridge", vid, msgaddr);
+		return (c);
+	}
+	if (c == 0) {
+		rmc_usrmsg ( rpfd, func, SR018, "mount", vid, drvord, "volume not in library");
+		return (RBT_NORETRY);
+	}
+	if (element_info.element_type != 2) {
+
+                /* compare requested and replied vid   */
+                rmc_usrmsg ( rpfd, func, "Asked for %s, got reply for %s\n", 
+                        vid, element_info.name );
+
+                /* detail on a tape's current location */
+                switch (element_info.element_type) {
+
+                case 1:
+                        rmc_usrmsg ( rpfd, func, "Location: medium transport element (0x%x)\n", 
+                                element_info.element_type );
+                        break;
+                case 2:                        
+                        /* normal case: in its home slot, not possible inside the if */
+                        break;
+                case 3:
+                        rmc_usrmsg ( rpfd, func, "Location: import/export element (0x%x)\n", 
+                                element_info.element_type );
+                        break;
+                case 4:
+                        rmc_usrmsg ( rpfd, func, "Location: data transfer element (0x%x)\n", 
+                                element_info.element_type );
+                        break;
+                default:
+                        rmc_usrmsg ( rpfd, func, "Location: unknown (0x%x)\n", 
+                                element_info.element_type );
+                }
+
+                rmc_usrmsg ( rpfd, func, SR018, "mount", vid, drvord, "volume in use");
+		return (RBT_SLOW_RETRY);
+	}
+	if ((c = smc_move_medium (fd, loader, element_info.element_address,
+	    robot_info->device_start+drvord, invert)) < 0) {
+		c = smc_lasterror (&smc_status, &msgaddr);
+		rmc_usrmsg ( rpfd, func, SR018, "mount", vid, drvord, msgaddr);
+		return (c);
+	}
+	return (0);
+}
diff --git a/mediachanger/castorrmc/rmc/send2rmc.c b/mediachanger/castorrmc/rmc/send2rmc.c
index 69b265204eacaddae841a01a36aaea37c088371c..c2464e25810771ba73ddd79bdbc4dbc3a39a693c 100644
--- a/mediachanger/castorrmc/rmc/send2rmc.c
+++ b/mediachanger/castorrmc/rmc/send2rmc.c
@@ -10,12 +10,12 @@
 #include <unistd.h>
 #include <netinet/in.h>
 #include <sys/socket.h>
-#include "Cnetdb.h"
-#include "marshall.h"
-#include "net.h"
-#include "rmc.h"
-#include "rmc_api.h"
-#include "serrno.h"
+#include "h/Cnetdb.h"
+#include "h/marshall.h"
+#include "h/net.h"
+#include "h/rmc_api.h"
+#include "h/rmc_constants.h"
+#include "h/serrno.h"
 
 /* send2tpd - send a request to the SCSI media changer server and wait for the reply */
 
@@ -35,9 +35,9 @@ int send2rmc(
 	int magic;
 	int n;
 	char *p;
-	char prtbuf[PRTBUFSZ];
+	char prtbuf[RMC_PRTBUFSZ];
 	int rep_type;
-	char repbuf[REPBUFSZ];
+	char repbuf[RMC_REPBUFSZ];
 	char rmchost[CA_MAXHOSTNAMELEN+1];
 	int s;
 	struct sockaddr_in sin; /* internet socket */
diff --git a/mediachanger/castorrmc/rmc/smc.c b/mediachanger/castorrmc/rmc/smc.c
index 1eb6b843957f9e5f2fcee41154e496678f37d123..7d5b1cf4ba9edd1671678d7688165efd102c184d 100644
--- a/mediachanger/castorrmc/rmc/smc.c
+++ b/mediachanger/castorrmc/rmc/smc.c
@@ -9,11 +9,11 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include "Ctape.h"
-#include "rmc_api.h"
-#include "serrno.h"
-#include "smc.h"
-#include "getconfent.h"
+#include "h/Ctape.h"
+#include "h/rmc_api.h"
+#include "h/serrno.h"
+#include "h/smc_constants.h"
+#include "h/getconfent.h"
 			/* exit codes */
 
 #define	USERR	1
diff --git a/mediachanger/castorrmc/rmc/smcsubr2.c b/mediachanger/castorrmc/rmc/smcsubr2.c
deleted file mode 100644
index 95d47c0b4ecefa66c4ed12ead7bce53b3a78e248..0000000000000000000000000000000000000000
--- a/mediachanger/castorrmc/rmc/smcsubr2.c
+++ /dev/null
@@ -1,323 +0,0 @@
-/*
- * Copyright (C) 1998-2003 by CERN/IT/PDP/DM
- * All rights reserved
- */
- 
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/types.h>
-#include "Ctape.h"
-#include "Ctape_api.h"
-#include "getconfent.h"
-#include "serrno.h"
-#include "smc.h"
-
-/* from smcsubr.c */
-extern int smc_read_elem_status(int, char *, int, int, int, struct smc_element_info[]);
-extern int smc_lasterror(struct smc_status *, char **);
-extern int smc_move_medium(int, char *, int, int, int);
-extern int smc_find_cartridge(int, char *, char *, int, int, int, struct smc_element_info[] );
-
-int smc_dismount (int fd,
-                  char *loader,
-                  struct robot_info *robot_info,
-                  int drvord,
-                  char *vid)
-{
-	const unsigned int max_element_status_reads = 20;
-	const unsigned int dismount_status_read_delay = 1; /* In seconds */
-	unsigned int nb_element_status_reads = 0;
-	int drive_not_unloaded = 1;
-	struct smc_element_info drive_element_info;
-	char func[16];
-	char *msgaddr = 0;
-	struct smc_status smc_status;
- 
-	strncpy (func, "smc_dismount", sizeof(func));
-	func[sizeof(func) - 1] = '\0';
-
-	memset(&smc_status, '\0', sizeof(smc_status));
-
-	/* IBM libraries sometimes disagree with the eject of their drives. */
-	/* Sometimes the access bit of the result of Read Element Status    */
-	/* (XB8) indicates the gripper cannot access the tape even though   */
-	/* the eject was successful.  Reading the element status at a later */
-	/* point in time eventually indicates the tape is accessible.       */
-	while(drive_not_unloaded && nb_element_status_reads < max_element_status_reads) {
-		if (0 > smc_read_elem_status (fd, loader, 4, robot_info->device_start+drvord,
-		    	1, &drive_element_info)) {
-			const int smc_error = smc_lasterror (&smc_status, &msgaddr);
-			usrmsg (func, SR020, "read_elem_status", msgaddr);
-			return (smc_error);
-		}
-		if (0 == (drive_element_info.state & 0x1)) {
-			usrmsg (func, SR018, "demount", vid, drvord, "Medium Not Present");
-			return (RBT_OK);
-		}
-
-		drive_not_unloaded = (0 == (drive_element_info.state & 0x8));
-		if (drive_not_unloaded) {
-			usrmsg (func, "read_elem_status of %s on drive %d detected Drive Not Unloaded\n", vid, drvord);
-		}
-
-		nb_element_status_reads++;
-
-		if(nb_element_status_reads < max_element_status_reads) {
-			sleep(dismount_status_read_delay);
-		}
-	}
-	if(drive_not_unloaded) {
-		usrmsg (func, SR018, "demount", vid, drvord, "Drive Not Unloaded");
-		return (RBT_UNLD_DMNT);
-	}
-
-	if (*vid && strcmp (drive_element_info.name, vid)) {
-		usrmsg (func, SR009, vid, drive_element_info.name);
-		return (RBT_NORETRY);
-	}
-	if (0 > smc_move_medium (fd, loader, robot_info->device_start+drvord,
-	    drive_element_info.source_address, (drive_element_info.flags & 0x40) ? 1 : 0)) {
-		const int smc_error = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR018, "demount", vid, drvord, msgaddr);
-		return (smc_error);
-	}
-    /* check that the vid is in a slot before returning */
-    while (1) {   
-          struct smc_element_info vol_element_info;
-          if (0 > smc_find_cartridge (fd, loader, drive_element_info.name, 0, 0, 1, &vol_element_info)) {
-              const int smc_error = smc_lasterror (&smc_status, &msgaddr);
-              usrmsg (func, SR017, "find_cartridge", drive_element_info.name, msgaddr);
-              return (smc_error);
-          }
-         
-          /* vid is in a storage slot */  
-          if (vol_element_info.element_type == 2) break; 
-          /* give time for the tape enter the slot */
-          sleep (2);
-    }
-
-	return (0);
-}
-
-int smc_export (int fd,
-                char *loader,
-                struct robot_info *robot_info,
-                char *vid)
-{
-        int c;
-        struct smc_element_info element_info;
-	char func[16];
-	int i;
-        struct smc_element_info *impexp_info;
-	char *msgaddr;
-	int nbelem;
-	struct smc_status smc_status;
- 
-	strncpy (func, "smc_export", sizeof(func));
-	func[sizeof(func) - 1] = '\0';
-
-	if ((c = smc_find_cartridge (fd, loader, vid, 0, 0, 1, &element_info)) < 0) {
-		c = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR017, "find_cartridge", vid, msgaddr);
-		return (c);
-	}
-	if (c == 0) {
-		usrmsg (func, SR017, "export", vid, "volume not in library");
-		return (RBT_NORETRY);
-	}
-	if (element_info.element_type != 2) {
-		usrmsg (func, SR017, "export", vid, "volume in use");
-		return (RBT_SLOW_RETRY);
-	}
-	/* look for a free export slot */
-
-	nbelem = robot_info->port_count;
-	if ((impexp_info = malloc (nbelem * sizeof(struct smc_element_info))) == NULL) {
-		usrmsg (func, SR012);
-		return (RBT_NORETRY);
-	}
-
-	if ((c = smc_read_elem_status (fd, loader, 3, robot_info->port_start,
-	    nbelem, impexp_info)) < 0) {
-		c = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR020, "read_elem_status", msgaddr);
-		free (impexp_info);
-		return (c);
-	}
-	for (i = 0; i < nbelem; i++) {
-		if (((impexp_info+i)->state & 0x1) == 0)	/* element free */
-			break;
-	}
-	if (i >= nbelem) {	/* export slots are full */
-		usrmsg (func, SR013);
-		free (impexp_info);
-		return (RBT_NORETRY);
-	}
-
-	if ((c = smc_move_medium (fd, loader, element_info.element_address,
-	    (impexp_info+i)->element_address, 0)) < 0) {
-		c = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR017, "export", vid, msgaddr);
-		free (impexp_info);
-		return (c);
-	}
-	free (impexp_info);
-	return (0);
-}
-
-int smc_import (int fd,
-                char *loader,
-                struct robot_info *robot_info,
-                char *vid)
-{
-        int c;
-	int device_start;
-        struct smc_element_info *element_info;
-	char func[16];
-	int i, j;
-	char *msgaddr;
-	int nbelem;
-	int port_start;
-	int slot_start;
-	struct smc_status smc_status;
- 
-	strncpy (func, "smc_import", sizeof(func));
-	func[sizeof(func) - 1] = '\0';
-
-	nbelem = robot_info->transport_count + robot_info->slot_count +
-		 robot_info->port_count + robot_info->device_count;
-	if ((element_info = malloc (nbelem * sizeof(struct smc_element_info))) == NULL) {
-		usrmsg (func, SR012);
-		return (RBT_NORETRY);
-	}
-
-	/* get inventory */
-
-	if ((c = smc_read_elem_status (fd, loader, 0, 0, nbelem, element_info)) < 0) {
-		c = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR020, "read_elem_status", msgaddr);
-		free (element_info);
-		return (c);
-	}
-	for (i = 0; i < c; i++)
-		if ((element_info+i)->element_type == 2) break;
-	slot_start = i;
-	for (i = 0; i < c; i++)
-		if ((element_info+i)->element_type == 3) break;
-	port_start = i;
-	for (i = 0; i < c; i++)
-		if ((element_info+i)->element_type == 4) break;
-	device_start = i;
-
-	/* mark home slots of cartridges currently on drives as non free */
-
-	for (i = device_start; i < device_start+robot_info->device_count; i++) {
-		if (((element_info+i)->state & 0x1) == 0) continue;
-		for (j = slot_start; j < slot_start+robot_info->slot_count; j++)
-			if ((element_info+i)->source_address ==
-				(element_info+j)->element_address) break;
-		(element_info+j)->state |= 1;
-	}
-
-	/* loop on all import slots */
-
-	for (i = port_start; i < port_start+robot_info->port_count; i++) {
-		if (*vid && strcmp (vid, (element_info+i)->name)) continue;
-		if (*vid || (*vid == '\0' && ((element_info+i)->state & 2))) {
-
-			/* find a free storage slot */
-
-			for (j = slot_start; j < slot_start+robot_info->slot_count; j++)
-				if (((element_info+j)->state & 0x1) == 0) break;
-			if (j >= slot_start+robot_info->slot_count) {
-				usrmsg (func, SR015);
-				free (element_info);
-				return (RBT_NORETRY);
-			}
-
-			if ((c = smc_move_medium (fd, loader, (element_info+i)->element_address,
-			    (element_info+j)->element_address, 0)) < 0) {
-				c = smc_lasterror (&smc_status, &msgaddr);
-				usrmsg (func, SR017, "import",
-				    (element_info+i)->name, msgaddr);
-				free (element_info);
-				return (c);
-			}
-			if (*vid || c) break;
-			(element_info+j)->state |= 1;	/* dest slot is now full */
-		}
-	}
-	free (element_info);
-	return (c);
-}
-
-int smc_mount (int fd,
-               char *loader,
-               struct robot_info *robot_info,
-               int drvord,
-               char *vid,
-               int invert)
-{
-    int c;
-    struct smc_element_info element_info;
-	char func[16];
-	char *msgaddr;
-	struct smc_status smc_status;
- 
-	strncpy (func, "smc_mount", sizeof(func));
-	func[sizeof(func) - 1] = '\0';
-
-	if ((c = smc_find_cartridge (fd, loader, vid, 0, 0, 1, &element_info)) < 0) {
-		c = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR017, "find_cartridge", vid, msgaddr);
-		return (c);
-	}
-	if (c == 0) {
-		usrmsg (func, SR018, "mount", vid, drvord, "volume not in library");
-		return (RBT_NORETRY);
-	}
-	if (element_info.element_type != 2) {
-
-                /* compare requested and replied vid   */
-                usrmsg( func, "Asked for %s, got reply for %s\n", 
-                        vid, element_info.name );
-
-                /* detail on a tape's current location */
-                switch (element_info.element_type) {
-
-                case 1:
-                        usrmsg( func, "Location: medium transport element (0x%x)\n", 
-                                element_info.element_type );
-                        break;
-                case 2:                        
-                        /* normal case: in its home slot, not possible inside the if */
-                        break;
-                case 3:
-                        usrmsg( func, "Location: import/export element (0x%x)\n", 
-                                element_info.element_type );
-                        break;
-                case 4:
-                        usrmsg( func, "Location: data transfer element (0x%x)\n", 
-                                element_info.element_type );
-                        break;
-                default:
-                        usrmsg( func, "Location: unknown (0x%x)\n", 
-                                element_info.element_type );
-                }
-
-                usrmsg (func, SR018, "mount", vid, drvord, "volume in use");
-		return (RBT_SLOW_RETRY);
-	}
-	if ((c = smc_move_medium (fd, loader, element_info.element_address,
-	    robot_info->device_start+drvord, invert)) < 0) {
-		c = smc_lasterror (&smc_status, &msgaddr);
-		usrmsg (func, SR018, "mount", vid, drvord, msgaddr);
-		return (c);
-	}
-	return (0);
-}
diff --git a/mediachanger/castorrmc/rmc/usrmsg.c b/mediachanger/castorrmc/rmc/usrmsg.c
deleted file mode 100644
index 6a1dc62074e9e20755ae2d58175af1dbf2ce1c3d..0000000000000000000000000000000000000000
--- a/mediachanger/castorrmc/rmc/usrmsg.c
+++ /dev/null
@@ -1,31 +0,0 @@
-/*
- * Copyright (C) 1990-2002 by CERN/IT/PDP/DM
- * All rights reserved
- */
-
-#include <stdio.h>
-#include <errno.h>
-#include <string.h>
-#include <sys/types.h>
-#include <stdarg.h>
-#include "rmc.h"
-#include <Ctape_api.h>
-
-int usrmsg(const char *func, const char *const msg, ...)
-{
-	va_list args;
-	char *p;
-	char prtbuf[PRTBUFSZ];
-	extern int rpfd;
-	int save_errno;
-
-	save_errno = errno;
-	va_start (args, msg);
-	sprintf (prtbuf, "%s: ", func);
-	p = prtbuf + strlen (prtbuf);
-	vsprintf (p, msg, args);
-	sendrep (rpfd, MSG_ERR, "%s", prtbuf);
-	va_end (args);
-	errno = save_errno;
-	return (0);
-}