From d79c420304e9ae7e0dec89ca2644f2e04a7134e2 Mon Sep 17 00:00:00 2001
From: Andre Noll <maan@systemlinux.org>
Date: Sat, 12 Apr 2008 19:18:58 +0200
Subject: [PATCH] Kill mmd_lock() and mmd_unlock().

They were only wrappers for mutex_lock() and mutex_unlock().
So export the lock, use mutex_lock() and mutex_unlock()
everywhere and remove the wrappers.
---
 afs.c     |  9 +++++----
 command.c | 58 ++++++++++++++++++++++++++++---------------------------
 server.c  | 34 ++++++++------------------------
 server.h  |  2 --
 4 files changed, 43 insertions(+), 60 deletions(-)

diff --git a/afs.c b/afs.c
index d9461bb7..58b04a8b 100644
--- a/afs.c
+++ b/afs.c
@@ -75,6 +75,7 @@ struct command_task {
 	struct task task;
 };
 
+extern int mmd_mutex;
 extern struct misc_meta_data *mmd;
 
 static int server_socket;
@@ -571,15 +572,15 @@ static int activate_mood_or_playlist(char *arg, int *num_admissible)
 		free(current_mop);
 		if (arg) {
 			current_mop = para_strdup(arg);
-			mmd_lock();
+			mutex_lock(mmd_mutex);
 			strncpy(mmd->afs_mode_string, arg,
 				sizeof(mmd->afs_mode_string));
 			mmd->afs_mode_string[sizeof(mmd->afs_mode_string) - 1] = '\0';
-			mmd_unlock();
+			mutex_unlock(mmd_mutex);
 		} else {
-			mmd_lock();
+			mutex_lock(mmd_mutex);
 			strcpy(mmd->afs_mode_string, "dummy");
-			mmd_unlock();
+			mutex_unlock(mmd_mutex);
 			current_mop = NULL;
 		}
 	}
diff --git a/command.c b/command.c
index b6c2de27..1f9dd857 100644
--- a/command.c
+++ b/command.c
@@ -26,6 +26,7 @@
 #include "net.h"
 #include "daemon.h"
 #include "fd.h"
+#include "ipc.h"
 #include "user_list.h"
 #include "server_command_list.h"
 #include "afs_command_list.h"
@@ -37,6 +38,7 @@ static RC4_KEY rc4_recv_key;
 static RC4_KEY rc4_send_key;
 static unsigned char rc4_buf[2 * RC4_KEY_LEN];
 
+extern int mmd_mutex;
 extern struct misc_meta_data *mmd;
 extern struct sender senders[];
 
@@ -53,9 +55,9 @@ static void dummy(int s)
 
 static void mmd_dup(struct misc_meta_data *new_mmd)
 {
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	*new_mmd = *mmd;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 }
 
 /*
@@ -175,12 +177,12 @@ static int check_sender_args(int argc, char * const * argv, struct sender_comman
 	if (!subcmds[i])
 		return -E_COMMAND_SYNTAX;
 	scd->cmd_num = i;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	if (!senders[scd->sender_num].client_cmds[scd->cmd_num]) {
-		mmd_unlock();
+		mutex_unlock(mmd_mutex);
 		return -E_SENDER_CMD;
 	}
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	switch (scd->cmd_num) {
 	case SENDER_ON:
 	case SENDER_OFF:
@@ -247,14 +249,14 @@ int com_sender(int fd, int argc, char * const * argv)
 		return ret;
 	}
 	for (i = 0; i < 10; i++) {
-		mmd_lock();
+		mutex_lock(mmd_mutex);
 		if (mmd->sender_cmd_data.cmd_num >= 0) {
-			mmd_unlock();
+			mutex_unlock(mmd_mutex);
 			usleep(100 * 1000);
 			continue;
 		}
 		mmd->sender_cmd_data = scd;
-		mmd_unlock();
+		mutex_unlock(mmd_mutex);
 		break;
 	}
 	return (i < 10)? 1 : -E_LOCK;
@@ -269,7 +271,7 @@ int com_si(int fd, int argc, __a_unused char * const * argv)
 
 	if (argc != 1)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	for (i = 0; senders[i].name; i++) {
 		char *info = senders[i].info();
 		sender_info = para_strcat(sender_info, info);
@@ -295,7 +297,7 @@ int com_si(int fd, int argc, __a_unused char * const * argv)
 		sender_list,
 		sender_info
 	);
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	free(ut);
 	free(sender_list);
 	free(sender_info);
@@ -444,10 +446,10 @@ int com_play(__a_unused int fd, int argc, __a_unused char * const * argv)
 {
 	if (argc != 1)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	mmd->new_vss_status_flags |= VSS_PLAYING;
 	mmd->new_vss_status_flags &= ~VSS_NOMORE;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return 1;
 
 }
@@ -457,11 +459,11 @@ int com_stop(__a_unused int fd, int argc, __a_unused char * const * argv)
 {
 	if (argc != 1)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	mmd->new_vss_status_flags &= ~VSS_PLAYING;
 	mmd->new_vss_status_flags &= ~VSS_REPOS;
 	mmd->new_vss_status_flags |= VSS_NEXT;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return 1;
 }
 
@@ -470,13 +472,13 @@ int com_pause(__a_unused int fd, int argc, __a_unused char * const * argv)
 {
 	if (argc != 1)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	if (!vss_paused() && !vss_stopped()) {
 		mmd->events++;
 		mmd->new_vss_status_flags &= ~VSS_PLAYING;
 		mmd->new_vss_status_flags &= ~VSS_NEXT;
 	}
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return 1;
 }
 
@@ -485,10 +487,10 @@ int com_next(__a_unused int fd, int argc, __a_unused char * const * argv)
 {
 	if (argc != 1)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	mmd->events++;
 	mmd->new_vss_status_flags |= VSS_NEXT;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return 1;
 }
 
@@ -497,10 +499,10 @@ int com_nomore(__a_unused int fd, int argc, __a_unused char * const * argv)
 {
 	if (argc != 1)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	if (vss_playing() || vss_paused())
 		mmd->new_vss_status_flags |= VSS_NOMORE;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return 1;
 }
 
@@ -518,7 +520,7 @@ int com_ff(__a_unused int fd, int argc, char * const * argv)
 		return -E_COMMAND_SYNTAX;
 	if (ret > 1 && c == '-')
 		backwards = 1; /* jmp backwards */
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	ret = -E_NO_AUDIO_FILE;
 	if (!mmd->afd.afhi.chunks_total || !mmd->afd.afhi.seconds_total)
 		goto out;
@@ -539,7 +541,7 @@ int com_ff(__a_unused int fd, int argc, char * const * argv)
 	mmd->events++;
 	ret = 1;
 out:
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return ret;
 }
 
@@ -553,7 +555,7 @@ int com_jmp(__a_unused int fd, int argc, char * const * argv)
 		return -E_COMMAND_SYNTAX;
 	if (sscanf(argv[1], "%lu", &i) <= 0)
 		return -E_COMMAND_SYNTAX;
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	ret = -E_NO_AUDIO_FILE;
 	if (!mmd->afd.afhi.chunks_total)
 		goto out;
@@ -568,7 +570,7 @@ int com_jmp(__a_unused int fd, int argc, char * const * argv)
 	ret = 1;
 	mmd->events++;
 out:
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	return ret;
 }
 
@@ -791,9 +793,9 @@ __noreturn void handle_connect(int fd, const char *peername)
 	/* valid command and sufficient perms */
 	alarm(0);
 	argc = split_args(command, &argv, "\n");
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	mmd->num_commands++;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	PARA_NOTICE_LOG("calling com_%s() for %s@%s\n", cmd->name, u->name,
 			peername);
 	ret = cmd->handler(fd, argc, argv);
@@ -806,10 +808,10 @@ net_err:
 out:
 	free(command);
 	free(argv);
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	if (cmd && (cmd->perms & AFS_WRITE) && ret >= 0)
 		mmd->events++;
 	mmd->active_connections--;
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	exit(ret < 0? EXIT_FAILURE : EXIT_SUCCESS);
 }
diff --git a/server.c b/server.c
index 244c1ecd..5ea8811f 100644
--- a/server.c
+++ b/server.c
@@ -107,15 +107,18 @@ struct server_args_info conf;
 /** A random value used in child context for authentication. */
 uint32_t afs_socket_cookie;
 
+/** The mutex protecting the shared memory area containing the mmd struct. */
+int mmd_mutex;
+
 /* global variables for server-internal use */
 static FILE *logfile;
 /** The file containing user information (public key, permissions). */
 static char *user_list_file = NULL;
-static int mmd_mutex, mmd_shm_id;
+static int mmd_shm_id;
 static pid_t afs_pid;
 
 
-/** The task resposible for server command handling. */
+/** The task responsible for server command handling. */
 struct server_command_task {
 	/** TCP port on which para_server listens for connections. */
 	int listen_fd;
@@ -194,27 +197,6 @@ err_out:
 	exit(EXIT_FAILURE);
 }
 
-/**
- * Lock the shared memory area containing the mmd struct.
- *
- * \sa semop(2), struct misc_meta_data.
- */
-void mmd_lock(void)
-{
-	mutex_lock(mmd_mutex);
-}
-
-/**
- * Unlock the shared memory area containing the mmd struct.
- *
- * \sa semop(2), struct misc_meta_data.
- */
-
-void mmd_unlock(void)
-{
-	mutex_unlock(mmd_mutex);
-}
-
 static void parse_config(int override)
 {
 	char *home = para_homedir();
@@ -552,9 +534,9 @@ static int server_select(int max_fileno, fd_set *readfds, fd_set *writefds,
 	int ret;
 
 	status_refresh();
-	mmd_unlock();
+	mutex_unlock(mmd_mutex);
 	ret = para_select(max_fileno + 1, readfds, writefds, timeout_tv);
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	return ret;
 }
 
@@ -577,7 +559,7 @@ int main(int argc, char *argv[])
 		.select_function = server_select
 	};
 	server_init(argc, argv);
-	mmd_lock();
+	mutex_lock(mmd_mutex);
 	ret = schedule(&s);
 	if (ret < 0) {
 		PARA_EMERG_LOG("%s\n", para_strerror(-ret));
diff --git a/server.h b/server.h
index c1e670ad..1468b458 100644
--- a/server.h
+++ b/server.h
@@ -106,5 +106,3 @@ struct misc_meta_data {
 extern struct server_args_info conf;
 
 __noreturn void handle_connect(int fd, const char *peername);
-void mmd_unlock(void);
-void mmd_lock(void);
-- 
2.39.5