Commit 4cf5d04c authored by Eric Wong's avatar Eric Wong

interface/connection malloc reductions from mpd-ke

This patch massively reduces the amount of heap allocations at the interface/command layer. Most commands with minimal output should not allocate memory from the heap at all. Things like repeatedly polling status, currentsong, and volume changes should be faster as a result, and more importantly, not a source of memory fragmentation. These changes should be safe in that there's no way for a remote-client to corrupt memory or otherwise do bad stuff to MPD, but an extra set of eyes to review would be good. Of course there's never any warranty :) No longer do we use FILE * structures in the interface, which means we don't have to allocate any new memory for most connections. Now, before you go on about losing the buffering that FILE * +implies+, remember that myfprintf() never took advantage of any of the stdio buffering features. To reduce the diff and make bugs easier to spot in the diff, I've kept myfprintf in places where we write to files (and not network interfaces). Expect myfprintf to go away entirely soon (we'll use fprintf for writing regular files). git-svn-id: https://svn.musicpd.org/mpd/trunk@4483 09075e82-0dd4-0310-85a5-a0d7c8717e4f
parent 4d5b8509
......@@ -35,6 +35,7 @@ mpd_headers = \
dbUtils.h \
decode.h \
directory.h \
gcc.h \
inputPlugin.h \
inputPlugins/_flac_common.h \
inputPlugins/_ogg_common.h \
......@@ -61,6 +62,7 @@ mpd_headers = \
replayGain.h \
signal_check.h \
sig_handlers.h \
sllist.h \
song.h \
stats.h \
tag.h \
......@@ -107,6 +109,7 @@ mpd_SOURCES = \
replayGain.c \
sig_handlers.c \
signal_check.c \
sllist.c \
song.c \
stats.c \
tag.c \
......
......@@ -423,10 +423,10 @@ void sendMetadataToAudioDevice(MpdTag * tag)
}
}
int enableAudioDevice(FILE * fp, int device)
int enableAudioDevice(int fd, int device)
{
if (device < 0 || device >= audioOutputArraySize) {
commandError(fp, ACK_ERROR_ARG, "audio output device id %i "
commandError(fd, ACK_ERROR_ARG, "audio output device id %i "
"doesn't exist\n", device);
return -1;
}
......@@ -436,10 +436,10 @@ int enableAudioDevice(FILE * fp, int device)
return 0;
}
int disableAudioDevice(FILE * fp, int device)
int disableAudioDevice(int fd, int device)
{
if (device < 0 || device >= audioOutputArraySize) {
commandError(fp, ACK_ERROR_ARG, "audio output device id %i "
commandError(fd, ACK_ERROR_ARG, "audio output device id %i "
"doesn't exist\n", device);
return -1;
}
......@@ -449,15 +449,16 @@ int disableAudioDevice(FILE * fp, int device)
return 0;
}
void printAudioDevices(FILE * fp)
void printAudioDevices(int fd)
{
int i;
for (i = 0; i < audioOutputArraySize; i++) {
myfprintf(fp, "outputid: %i\n", i);
myfprintf(fp, "outputname: %s\n", audioOutputArray[i]->name);
myfprintf(fp, "outputenabled: %i\n",
(int)pdAudioDevicesEnabled[i]);
fdprintf(fd,
"outputid: %i\noutputname: %s\noutputenabled: %i\n",
i,
audioOutputArray[i]->name,
(int)pdAudioDevicesEnabled[i]);
}
}
......
......@@ -69,11 +69,11 @@ void sendMetadataToAudioDevice(MpdTag * tag);
/* these functions are called in the main parent process while the child
process is busy playing to the audio */
int enableAudioDevice(FILE * fp, int device);
int enableAudioDevice(int fd, int device);
int disableAudioDevice(FILE * fp, int device);
int disableAudioDevice(int fd, int device);
void printAudioDevices(FILE * fp);
void printAudioDevices(int fd);
void readAudioDevicesState();
......
......@@ -25,7 +25,9 @@
#include "myfprintf.h"
#include "log.h"
#include "ack.h"
#include "sllist.h"
#include <unistd.h>
#include <stdio.h>
#define COMMAND_RETURN_KILL 10
......@@ -35,30 +37,29 @@
extern char *current_command;
extern int command_listNum;
int processListOfCommands(FILE * fp, int *permission, int *expired,
int listOK, List * list);
int processListOfCommands(int fd, int *permission, int *expired,
int listOK, struct strnode *list);
int processCommand(FILE * fp, int *permission, char *commandString);
int processCommand(int fd, int *permission, char *commandString);
void initCommands();
void finishCommands();
#define commandSuccess(fp) myfprintf(fp, "OK\n")
#define commandSuccess(fd) fdprintf(fd, "OK\n")
#define commandError(fp, error, format, ... ) \
#define commandError(fd, error, format, ... ) do \
{\
if(current_command) { \
myfprintf(fp, "ACK [%i@%i] {%s} " format "\n", \
if (current_command) { \
fdprintf(fd, "ACK [%i@%i] {%s} " format "\n", \
(int)error, command_listNum, \
current_command, __VA_ARGS__); \
current_command = NULL; \
} \
else { \
myfprintf(stderr, "ACK [%i@%i] " format "\n", \
fdprintf(STDERR_FILENO, "ACK [%i@%i] " format "\n", \
(int)error, command_listNum, \
__VA_ARGS__); \
} \
}
} while (0)
#endif
......@@ -134,7 +134,7 @@ void freeLocateTagItem(LocateTagItem * item)
free(item);
}
static int countSongsInDirectory(FILE * fp, Directory * directory, void *data)
static int countSongsInDirectory(int fd, Directory * directory, void *data)
{
int *count = (int *)data;
......@@ -143,18 +143,18 @@ static int countSongsInDirectory(FILE * fp, Directory * directory, void *data)
return 0;
}
static int printDirectoryInDirectory(FILE * fp, Directory * directory,
static int printDirectoryInDirectory(int fd, Directory * directory,
void *data)
{
if (directory->path) {
myfprintf(fp, "directory: %s\n", getDirectoryPath(directory));
fdprintf(fd, "directory: %s\n", getDirectoryPath(directory));
}
return 0;
}
static int printSongInDirectory(FILE * fp, Song * song, void *data)
static int printSongInDirectory(int fd, Song * song, void *data)
{
printSongUrl(fp, song);
printSongUrl(fd, song);
return 0;
}
......@@ -192,7 +192,7 @@ static int strstrSearchTag(Song * song, int type, char *str)
return ret;
}
static int searchInDirectory(FILE * fp, Song * song, void *data)
static int searchInDirectory(int fd, Song * song, void *data)
{
LocateTagItemArray *array = data;
int i;
......@@ -204,12 +204,12 @@ static int searchInDirectory(FILE * fp, Song * song, void *data)
}
}
printSongInfo(fp, song);
printSongInfo(fd, song);
return 0;
}
int searchForSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items)
int searchForSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
{
int ret = -1;
int i;
......@@ -225,7 +225,7 @@ int searchForSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items)
array.numItems = numItems;
array.items = items;
ret = traverseAllIn(fp, name, searchInDirectory, NULL, &array);
ret = traverseAllIn(fd, name, searchInDirectory, NULL, &array);
for (i = 0; i < numItems; i++) {
free(items[i].needle);
......@@ -260,7 +260,7 @@ static int tagItemFoundAndMatches(Song * song, int type, char *str)
return 0;
}
static int findInDirectory(FILE * fp, Song * song, void *data)
static int findInDirectory(int fd, Song * song, void *data)
{
LocateTagItemArray *array = data;
int i;
......@@ -272,43 +272,43 @@ static int findInDirectory(FILE * fp, Song * song, void *data)
}
}
printSongInfo(fp, song);
printSongInfo(fd, song);
return 0;
}
int findSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items)
int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items)
{
LocateTagItemArray array;
array.numItems = numItems;
array.items = items;
return traverseAllIn(fp, name, findInDirectory, NULL, (void *)&array);
return traverseAllIn(fd, name, findInDirectory, NULL, (void *)&array);
}
int printAllIn(FILE * fp, char *name)
int printAllIn(int fd, char *name)
{
return traverseAllIn(fp, name, printSongInDirectory,
return traverseAllIn(fd, name, printSongInDirectory,
printDirectoryInDirectory, NULL);
}
static int directoryAddSongToPlaylist(FILE * fp, Song * song, void *data)
static int directoryAddSongToPlaylist(int fd, Song * song, void *data)
{
return addSongToPlaylist(fp, song, 0);
return addSongToPlaylist(fd, song, 0);
}
int addAllIn(FILE * fp, char *name)
int addAllIn(int fd, char *name)
{
return traverseAllIn(fp, name, directoryAddSongToPlaylist, NULL, NULL);
return traverseAllIn(fd, name, directoryAddSongToPlaylist, NULL, NULL);
}
static int directoryPrintSongInfo(FILE * fp, Song * song, void *data)
static int directoryPrintSongInfo(int fd, Song * song, void *data)
{
return printSongInfo(fp, song);
return printSongInfo(fd, song);
}
static int sumSongTime(FILE * fp, Song * song, void *data)
static int sumSongTime(int fd, Song * song, void *data)
{
unsigned long *time = (unsigned long *)data;
......@@ -318,28 +318,28 @@ static int sumSongTime(FILE * fp, Song * song, void *data)
return 0;
}
int printInfoForAllIn(FILE * fp, char *name)
int printInfoForAllIn(int fd, char *name)
{
return traverseAllIn(fp, name, directoryPrintSongInfo,
return traverseAllIn(fd, name, directoryPrintSongInfo,
printDirectoryInDirectory, NULL);
}
int countSongsIn(FILE * fp, char *name)
int countSongsIn(int fd, char *name)
{
int count = 0;
void *ptr = (void *)&count;
traverseAllIn(fp, name, NULL, countSongsInDirectory, ptr);
traverseAllIn(fd, name, NULL, countSongsInDirectory, ptr);
return count;
}
unsigned long sumSongTimesIn(FILE * fp, char *name)
unsigned long sumSongTimesIn(int fd, char *name)
{
unsigned long dbPlayTime = 0;
void *ptr = (void *)&dbPlayTime;
traverseAllIn(fp, name, sumSongTime, NULL, ptr);
traverseAllIn(fd, name, sumSongTime, NULL, ptr);
return dbPlayTime;
}
......@@ -361,13 +361,13 @@ static void freeListCommandItem(ListCommandItem * item)
free(item);
}
static void visitTag(FILE * fp, Song * song, int tagType)
static void visitTag(int fd, Song * song, int tagType)
{
int i;
MpdTag *tag = song->tag;
if (tagType == LOCATE_TAG_FILE_TYPE) {
printSongUrl(fp, song);
printSongUrl(fd, song);
return;
}
......@@ -381,7 +381,7 @@ static void visitTag(FILE * fp, Song * song, int tagType)
}
}
static int listUniqueTagsInDirectory(FILE * fp, Song * song, void *data)
static int listUniqueTagsInDirectory(int fd, Song * song, void *data)
{
ListCommandItem *item = data;
int i;
......@@ -393,12 +393,12 @@ static int listUniqueTagsInDirectory(FILE * fp, Song * song, void *data)
}
}
visitTag(fp, song, item->tagType);
visitTag(fd, song, item->tagType);
return 0;
}
int listAllUniqueTags(FILE * fp, int type, int numConditionals,
int listAllUniqueTags(int fd, int type, int numConditionals,
LocateTagItem * conditionals)
{
int ret;
......@@ -409,11 +409,11 @@ int listAllUniqueTags(FILE * fp, int type, int numConditionals,
resetVisitedFlagsInTagTracker(type);
}
ret = traverseAllIn(fp, NULL, listUniqueTagsInDirectory, NULL,
ret = traverseAllIn(fd, NULL, listUniqueTagsInDirectory, NULL,
(void *)item);
if (type >= 0 && type <= TAG_NUM_OF_ITEM_TYPES) {
printVisitedInTagTracker(fp, type);
printVisitedInTagTracker(fd, type);
}
freeListCommandItem(item);
......@@ -421,7 +421,7 @@ int listAllUniqueTags(FILE * fp, int type, int numConditionals,
return ret;
}
static int sumSavedFilenameMemoryInDirectory(FILE * fp, Directory * dir,
static int sumSavedFilenameMemoryInDirectory(int fd, Directory * dir,
void *data)
{
int *sum = data;
......@@ -435,7 +435,7 @@ static int sumSavedFilenameMemoryInDirectory(FILE * fp, Directory * dir,
return 0;
}
static int sumSavedFilenameMemoryInSong(FILE * fp, Song * song, void *data)
static int sumSavedFilenameMemoryInSong(int fd, Song * song, void *data)
{
int *sum = data;
......@@ -448,7 +448,7 @@ void printSavedMemoryFromFilenames(void)
{
int sum = 0;
traverseAllIn(stderr, NULL, sumSavedFilenameMemoryInSong,
traverseAllIn(STDERR_FILENO, NULL, sumSavedFilenameMemoryInSong,
sumSavedFilenameMemoryInDirectory, (void *)&sum);
DEBUG("saved memory from filenames: %i\n", sum);
......
......@@ -43,22 +43,22 @@ void freeLocateTagItemArray(int count, LocateTagItem * array);
void freeLocateTagItem(LocateTagItem * item);
int printAllIn(FILE * fp, char *name);
int printAllIn(int fd, char *name);
int addAllIn(FILE * fp, char *name);
int addAllIn(int fd, char *name);
int printInfoForAllIn(FILE * fp, char *name);
int printInfoForAllIn(int fd, char *name);
int searchForSongsIn(FILE * fp, char *name, int numItems,
int searchForSongsIn(int fd, char *name, int numItems,
LocateTagItem * items);
int findSongsIn(FILE * fp, char *name, int numItems, LocateTagItem * items);
int findSongsIn(int fd, char *name, int numItems, LocateTagItem * items);
int countSongsIn(FILE * fp, char *name);
int countSongsIn(int fd, char *name);
unsigned long sumSongTimesIn(FILE * fp, char *name);
unsigned long sumSongTimesIn(int fd, char *name);
int listAllUniqueTags(FILE * fp, int type, int numConditiionals,
int listAllUniqueTags(int fd, int type, int numConditiionals,
LocateTagItem * conditionals);
void printSavedMemoryFromFilenames();
......
......@@ -159,10 +159,10 @@ void readDirectoryDBIfUpdateIsFinished()
}
}
int updateInit(FILE * fp, List * pathList)
int updateInit(int fd, List * pathList)
{
if (directory_updatePid > 0) {
commandError(fp, ACK_ERROR_UPDATE_ALREADY, "already updating",
commandError(fd, ACK_ERROR_UPDATE_ALREADY, "already updating",
NULL);
return -1;
}
......@@ -216,7 +216,7 @@ int updateInit(FILE * fp, List * pathList)
} else if (directory_updatePid < 0) {
unblockSignals();
ERROR("updateInit: Problems forking()'ing\n");
commandError(fp, ACK_ERROR_SYSTEM,
commandError(fd, ACK_ERROR_SYSTEM,
"problems trying to update", NULL);
directory_updatePid = 0;
return -1;
......@@ -228,7 +228,7 @@ int updateInit(FILE * fp, List * pathList)
directory_updateJobId = 1;
DEBUG("updateInit: fork()'d update child for update job id %i\n",
(int)directory_updateJobId);
myfprintf(fp, "updating_db: %i\n", (int)directory_updateJobId);
fdprintf(fd, "updating_db: %i\n", (int)directory_updateJobId);
return 0;
}
......@@ -871,33 +871,33 @@ static Directory *getDirectory(char *name)
return getSubDirectory(mp3rootDirectory, name, &shortname);
}
static int printDirectoryList(FILE * fp, DirectoryList * directoryList)
static int printDirectoryList(int fd, DirectoryList * directoryList)
{
ListNode *node = directoryList->firstNode;
Directory *directory;
while (node != NULL) {
directory = (Directory *) node->data;
myfprintf(fp, "%s%s\n", DIRECTORY_DIR,
getDirectoryPath(directory));
fdprintf(fd, "%s%s\n", DIRECTORY_DIR,
getDirectoryPath(directory));
node = node->nextNode;
}
return 0;
}
int printDirectoryInfo(FILE * fp, char *name)
int printDirectoryInfo(int fd, char *name)
{
Directory *directory;
if ((directory = getDirectory(name)) == NULL) {
commandError(fp, ACK_ERROR_NO_EXIST, "directory not found",
commandError(fd, ACK_ERROR_NO_EXIST, "directory not found",
NULL);
return -1;
}
printDirectoryList(fp, directory->subDirectories);
printSongInfoFromList(fp, directory->songs);
printDirectoryList(fd, directory->subDirectories);
printSongInfoFromList(fd, directory->songs);
return 0;
}
......@@ -1208,8 +1208,8 @@ int readDirectoryDB()
readDirectoryInfo(fp, mp3rootDirectory);
while (fclose(fp) && errno == EINTR) ;
stats.numberOfSongs = countSongsIn(stderr, NULL);
stats.dbPlayTime = sumSongTimesIn(stderr, NULL);
stats.numberOfSongs = countSongsIn(STDERR_FILENO, NULL);
stats.dbPlayTime = sumSongTimesIn(STDERR_FILENO, NULL);
if (stat(dbFile, &st) == 0)
directory_dbModTime = st.st_mtime;
......@@ -1237,10 +1237,10 @@ void updateMp3Directory()
return;
}
static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
int (*forEachSong) (FILE *, Song *,
static int traverseAllInSubDirectory(int fd, Directory * directory,
int (*forEachSong) (int, Song *,
void *),
int (*forEachDir) (FILE *, Directory *,
int (*forEachDir) (int, Directory *,
void *), void *data)
{
ListNode *node = directory->songs->firstNode;
......@@ -1249,7 +1249,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
int errFlag = 0;
if (forEachDir) {
errFlag = forEachDir(fp, directory, data);
errFlag = forEachDir(fd, directory, data);
if (errFlag)
return errFlag;
}
......@@ -1257,7 +1257,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
if (forEachSong) {
while (node != NULL && !errFlag) {
song = (Song *) node->data;
errFlag = forEachSong(fp, song, data);
errFlag = forEachSong(fd, song, data);
node = node->nextNode;
}
if (errFlag)
......@@ -1268,7 +1268,7 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
while (node != NULL && !errFlag) {
dir = (Directory *) node->data;
errFlag = traverseAllInSubDirectory(fp, dir, forEachSong,
errFlag = traverseAllInSubDirectory(fd, dir, forEachSong,
forEachDir, data);
node = node->nextNode;
}
......@@ -1276,23 +1276,23 @@ static int traverseAllInSubDirectory(FILE * fp, Directory * directory,
return errFlag;
}
int traverseAllIn(FILE * fp, char *name,
int (*forEachSong) (FILE *, Song *, void *),
int (*forEachDir) (FILE *, Directory *, void *), void *data)
int traverseAllIn(int fd, char *name,
int (*forEachSong) (int, Song *, void *),
int (*forEachDir) (int, Directory *, void *), void *data)
{
Directory *directory;
if ((directory = getDirectory(name)) == NULL) {
Song *song;
if ((song = getSongFromDB(name)) && forEachSong) {
return forEachSong(fp, song, data);
return forEachSong(fd, song, data);
}
commandError(fp, ACK_ERROR_NO_EXIST,
commandError(fd, ACK_ERROR_NO_EXIST,
"directory or file not found", NULL);
return -1;
}
return traverseAllInSubDirectory(fp, directory, forEachSong, forEachDir,
return traverseAllInSubDirectory(fd, directory, forEachSong, forEachDir,
data);
}
......@@ -1315,8 +1315,8 @@ void initMp3Directory()
mp3rootDirectory = newDirectory(NULL, NULL);
exploreDirectory(mp3rootDirectory);
freeAllDirectoryStats(mp3rootDirectory);
stats.numberOfSongs = countSongsIn(stderr, NULL);
stats.dbPlayTime = sumSongTimesIn(stderr, NULL);
stats.numberOfSongs = countSongsIn(STDERR_FILENO, NULL);
stats.dbPlayTime = sumSongTimesIn(STDERR_FILENO, NULL);
if (stat(getDbFile(), &st) == 0)
directory_dbModTime = st.st_mtime;
......
......@@ -45,13 +45,13 @@ int isUpdatingDB();
void directory_sigChldHandler(int pid, int status);
int updateInit(FILE * fp, List * pathList);
int updateInit(int fd, List * pathList);
void initMp3Directory();
void closeMp3Directory();
int printDirectoryInfo(FILE * fp, char *dirname);
int printDirectoryInfo(int fd, char *dirname);
int checkDirectoryDB();
......@@ -65,9 +65,9 @@ Song *getSongFromDB(char *file);
time_t getDbModTime();
int traverseAllIn(FILE * fp, char *name,
int (*forEachSong) (FILE *, Song *, void *),
int (*forEachDir) (FILE *, Directory *, void *), void *data);
int traverseAllIn(int fd, char *name,
int (*forEachSong) (int, Song *, void *),
int (*forEachDir) (int, Directory *, void *), void *data);
#define getDirectoryPath(dir) ((dir && dir->path) ? dir->path : "")
......
#ifndef MPD_GCC_H
#define MPD_GCC_H
/* this allows us to take advantage of special gcc features while still
* allowing other compilers to compile:
*
* example taken from: http://rlove.org/log/2005102601
*/
/* disabled (0) until I fix all the warnings :) */
#if (0 && __GNUC__ >= 3)
# define mpd_const __attribute__ ((const))
# define mpd_deprecated __attribute__ ((deprecated))
# define mpd_malloc __attribute__ ((malloc))
# define mpd_must_check __attribute__ ((warn_unused_result))
# define mpd_noreturn __attribute__ ((noreturn))
# define mpd_packed __attribute__ ((packed))
/* these are very useful for type checking */
# define mpd_printf __attribute__ ((format(printf,1,2)))
# define mpd_fprintf __attribute__ ((format(printf,2,3)))
# define mpd_fprintf_ __attribute__ ((format(printf,3,4)))
# define mpd_pure __attribute__ ((pure))
# define mpd_scanf __attribute__ ((format(scanf,1,2)))
# define mpd_unused __attribute__ ((unused))
# define mpd_used __attribute__ ((used))
/* # define inline inline __attribute__ ((always_inline)) */
# define mpd_noinline __attribute__ ((noinline))
# define likely(x) __builtin_expect (!!(x), 1)
# define unlikely(x) __builtin_expect (!!(x), 0)
#else
# define mpd_const
# define mpd_deprecated
# define mpd_malloc
# define mpd_must_check
# define mpd_noreturn
# define mpd_packed
# define mpd_printf
# define mpd_fprintf
# define mpd_fprintf_
# define mpd_pure
# define mpd_scanf
# define mpd_unused
# define mpd_used
/* # define inline */
# define mpd_noinline
# define likely(x) (x)
# define unlikely(x) (x)
#endif
#endif /* MPD_GCC_H */
......@@ -32,12 +32,12 @@ static char *remoteUrlPrefixes[] = {
NULL
};
int printRemoteUrlHandlers(FILE * fp)
int printRemoteUrlHandlers(int fd)
{
char **prefixes = remoteUrlPrefixes;
while (*prefixes) {
myfprintf(fp, "handler: %s\n", *prefixes);
fdprintf(fd, "handler: %s\n", *prefixes);
prefixes++;
}
......@@ -100,7 +100,7 @@ int isRemoteUrl(char *url)
return 0;
}
int lsPlaylists(FILE * fp, char *utf8path)
int lsPlaylists(int fd, char *utf8path)
{
DIR *dir;
struct stat st;
......@@ -166,7 +166,7 @@ int lsPlaylists(FILE * fp, char *utf8path)
node = list->firstNode;
while (node != NULL) {
if (!strchr(node->key, '\n')) {
myfprintf(fp, "playlist: %s%s\n", dup,
fdprintf(fd, "playlist: %s%s\n", dup,
node->key);
}
node = node->nextNode;
......
......@@ -29,7 +29,7 @@
#include <unistd.h>
#include <time.h>
int lsPlaylists(FILE * fp, char *utf8path);
int lsPlaylists(int fd, char *utf8path);
char *getSuffix(char *utf8file);
......@@ -47,6 +47,6 @@ InputPlugin *hasMusicSuffix(char *utf8file, unsigned int next);
InputPlugin *isMusic(char *utf8file, time_t * mtime, unsigned int next);
int printRemoteUrlHandlers(FILE * fp);
int printRemoteUrlHandlers(int fd);
#endif
......@@ -21,6 +21,7 @@
#include "path.h"
#include "log.h"
#include "conf.h"
#include "utils.h"
#include <stdarg.h>
#include <sys/param.h>
......@@ -38,28 +39,41 @@ static FILE *myfprintf_err;
static char *myfprintf_outFilename;
static char *myfprintf_errFilename;
static void blockingWrite(int fd, char *string, int len)
static void blockingWrite(const int fd, const char *string, size_t len)
{
int ret;
while (len) {
ret = write(fd, string, len);
if (ret == 0)
size_t ret = xwrite(fd, string, len);
if (ret == len)
return;
if (ret < 0) {
switch (errno) {
case EAGAIN:
case EINTR:
continue;
default:
return;
}
if (ret >= 0) {
len -= ret;
string += ret;
continue;
}
len -= ret;
string += ret;
return; /* error */
}
}
void vfdprintf(const int fd, const char *fmt, va_list args)
{
static char buffer[BUFFER_LENGTH + 1];
char *buf = buffer;
size_t len;
vsnprintf(buf, BUFFER_LENGTH, fmt, args);
len = strlen(buf);
if (interfacePrintWithFD(fd, buf, len) < 0)
blockingWrite(fd, buf, len);
}
mpd_fprintf void fdprintf(const int fd, const char *fmt, ...)
{
va_list args;
va_start(args, fmt);
vfdprintf(fd, fmt, args);
va_end(args);
}
void myfprintfStdLogMode(FILE * out, FILE * err)
{
myfprintf_stdLogMode = 1;
......@@ -69,38 +83,6 @@ void myfprintfStdLogMode(FILE * out, FILE * err)
myfprintf_errFilename = getConfigParamValue(CONF_ERROR_FILE);
}
void myfprintf(FILE * fp, char *format, ...)
{
static char buffer[BUFFER_LENGTH + 1];
va_list arglist;
int fd = fileno(fp);
va_start(arglist, format);
if (fd == 1 || fd == 2) {
if (myfprintf_stdLogMode) {
time_t t = time(NULL);
if (fd == 1)
fp = myfprintf_out;
else
fp = myfprintf_err;
strftime(buffer, 14, "%b %e %R", localtime(&t));
blockingWrite(fd, buffer, strlen(buffer));
blockingWrite(fd, " : ", 3);
}
vsnprintf(buffer, BUFFER_LENGTH, format, arglist);
blockingWrite(fd, buffer, strlen(buffer));
} else {
int len;
vsnprintf(buffer, BUFFER_LENGTH, format, arglist);
len = strlen(buffer);
if (interfacePrintWithFD(fd, buffer, len) < 0) {
blockingWrite(fd, buffer, len);
}
}
va_end(arglist);
}
int myfprintfCloseAndOpenLogFile(void)
{
if (myfprintf_stdLogMode) {
......
......@@ -20,12 +20,20 @@
#define MYFPRINTF_H
#include "../config.h"
#include "gcc.h"
#include <stdarg.h>
#include <stdio.h>
void myfprintfStdLogMode(FILE * out, FILE * err);
void myfprintf(FILE * fp, char *format, ...);
mpd_fprintf void fdprintf(const int fd, const char *fmt, ...);
void vfdprintf(const int fd, const char *fmt, va_list arglist);
#define myfprintf(fp, ...) do { \
fprintf(fp, __VA_ARGS__); \
fflush(fp); \
} while (0)
int myfprintfCloseAndOpenLogFile();
......
......@@ -156,14 +156,11 @@ int playerInitReal()
return 0;
}
int playerPlay(FILE * fp, Song * song)
int playerPlay(int fd, Song * song)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
if (fp == NULL)
fp = stderr;
if (playerStop(fp) < 0)
if (playerStop(fd) < 0)
return -1;
if (song->tag)
......@@ -189,7 +186,7 @@ int playerPlay(FILE * fp, Song * song)
return 0;
}
int playerStop(FILE * fp)
int playerStop(int fd)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
......@@ -219,7 +216,7 @@ void playerKill()
kill(pid, SIGTERM);
}
int playerPause(FILE * fp)
int playerPause(int fd)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
......@@ -232,7 +229,7 @@ int playerPause(FILE * fp)
return 0;
}
int playerSetPause(FILE * fp, int pause)
int playerSetPause(int fd, int pause)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
......@@ -242,11 +239,11 @@ int playerSetPause(FILE * fp, int pause)
switch (pc->state) {
case PLAYER_STATE_PLAY:
if (pause)
playerPause(fp);
playerPause(fd);
break;
case PLAYER_STATE_PAUSE:
if (!pause)
playerPause(fp);
playerPause(fd);
break;
}
......@@ -329,7 +326,7 @@ void playerCloseAudio()
PlayerControl *pc = &(getPlayerData()->playerControl);
if (getPlayerPid() > 0) {
if (playerStop(stderr) < 0)
if (playerStop(STDERR_FILENO) < 0)
return;
pc->closeAudio = 1;
}
......@@ -393,12 +390,12 @@ void playerQueueUnlock()
}
}
int playerSeek(FILE * fp, Song * song, float time)
int playerSeek(int fd, Song * song, float time)
{
PlayerControl *pc = &(getPlayerData()->playerControl);
if (pc->state == PLAYER_STATE_STOP) {
commandError(fp, ACK_ERROR_PLAYER_SYNC,
commandError(fd, ACK_ERROR_PLAYER_SYNC,
"player not currently playing", NULL);
return -1;
}
......
......@@ -93,13 +93,13 @@ int playerInitReal();
void player_sigChldHandler(int pid, int status);
int playerPlay(FILE * fp, Song * song);
int playerPlay(int fd, Song * song);
int playerSetPause(FILE * fp, int pause);
int playerSetPause(int fd, int pause);
int playerPause(FILE * fp);
int playerPause(int fd);
int playerStop(FILE * fp);
int playerStop(int fd);
void playerCloseAudio();
......@@ -131,7 +131,7 @@ void playerQueueLock();
void playerQueueUnlock();
int playerSeek(FILE * fp, Song * song, float time);
int playerSeek(int fd, Song * song, float time);
void setPlayerCrossFade(float crossFadeInSeconds);
......
......@@ -38,61 +38,61 @@ void readPlaylistState();
void savePlaylistState();
int clearPlaylist(FILE * fp);
int clearPlaylist(int fd);
int addToPlaylist(FILE * fp, char *file, int printId);
int addToPlaylist(int fd, char *file, int printId);
int addSongToPlaylist(FILE * fp, Song * song, int printId);
int addSongToPlaylist(int fd, Song * song, int printId);
int showPlaylist(FILE * fp);
int showPlaylist(int fd);
int deleteFromPlaylist(FILE * fp, int song);
int deleteFromPlaylist(int fd, int song);
int deleteFromPlaylistById(FILE * fp, int song);
int deleteFromPlaylistById(int fd, int song);
int playlistInfo(FILE * fp, int song);
int playlistInfo(int fd, int song);
int playlistId(FILE * fp, int song);
int playlistId(int fd, int song);
int stopPlaylist(FILE * fp);
int stopPlaylist(int fd);
int playPlaylist(FILE * fp, int song, int stopOnError);
int playPlaylist(int fd, int song, int stopOnError);
int playPlaylistById(FILE * fp, int song, int stopOnError);
int playPlaylistById(int fd, int song, int stopOnError);
int nextSongInPlaylist(FILE * fp);
int nextSongInPlaylist(int fd);
void syncPlayerAndPlaylist();
int previousSongInPlaylist(FILE * fp);
int previousSongInPlaylist(int fd);
int shufflePlaylist(FILE * fp);
int shufflePlaylist(int fd);
int savePlaylist(FILE * fp, char *utf8file);
int savePlaylist(int fd, char *utf8file);
int deletePlaylist(FILE * fp, char *utf8file);
int deletePlaylist(int fd, char *utf8file);
int deletePlaylistById(FILE * fp, char *utf8file);
int deletePlaylistById(int fd, char *utf8file);
void deleteASongFromPlaylist(Song * song);
int moveSongInPlaylist(FILE * fp, int from, int to);
int moveSongInPlaylist(int fd, int from, int to);
int moveSongInPlaylistById(FILE * fp, int id, int to);
int moveSongInPlaylistById(int fd, int id, int to);
int swapSongsInPlaylist(FILE * fp, int song1, int song2);
int swapSongsInPlaylist(int fd, int song1, int song2);
int swapSongsInPlaylistById(FILE * fp, int id1, int id2);
int swapSongsInPlaylistById(int fd, int id1, int id2);
int loadPlaylist(FILE * fp, char *utf8file);
int loadPlaylist(int fd, char *utf8file);
int getPlaylistRepeatStatus();
int setPlaylistRepeatStatus(FILE * fp, int status);
int setPlaylistRepeatStatus(int fd, int status);
int getPlaylistRandomStatus();
int setPlaylistRandomStatus(FILE * fp, int status);
int setPlaylistRandomStatus(int fd, int status);
int getPlaylistCurrentSong();
......@@ -104,17 +104,17 @@ unsigned long getPlaylistVersion();
void playPlaylistIfPlayerStopped();
int seekSongInPlaylist(FILE * fp, int song, float time);
int seekSongInPlaylist(int fd, int song, float time);
int seekSongInPlaylistById(FILE * fp, int id, float time);
int seekSongInPlaylistById(int fd, int id, float time);
void playlistVersionChange();
int playlistChanges(FILE * fp, mpd_uint32 version);
int playlistChanges(int fd, mpd_uint32 version);
int playlistChangesPosId(FILE * fp, mpd_uint32 version);
int playlistChangesPosId(int fd, mpd_uint32 version);
int PlaylistInfo(FILE * fp, char *utf8file, int detail);
int PlaylistInfo(int fd, char *utf8file, int detail);
char *getStateFile();
......
/* the Music Player Daemon (MPD)
* (c)2003-2006 by Warren Dukes (warren.dukes@gmail.com)
* This project's homepage is: http://www.musicpd.org
*
* 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
*/
/* a very simple singly-linked-list structure for queues/buffers */
#include "sllist.h"
#include "utils.h"
static void init_strnode(struct strnode *x, char *s)
{
x->data = s;
x->next = NULL;
}
struct strnode *new_strnode(char *s)
{
struct strnode *x = malloc(sizeof(struct strnode));
init_strnode(x, s);
return x;
}
struct strnode *new_strnode_dup(char *s, const size_t size)
{
struct strnode *x = malloc(sizeof(struct strnode) + size);
x->next = NULL;
x->data = ((void *)x + sizeof(struct strnode));
memcpy((void *)x->data, (void*)s, size);
return x;
}
struct sllnode *new_sllnode(void *s, const size_t size)
{
struct sllnode *x = malloc(sizeof(struct sllnode) + size);
x->next = NULL;
x->size = size;
x->data = ((void *)x + sizeof(struct sllnode));
memcpy(x->data, (void *)s, size);
return x;
}
struct strnode *dup_strlist(struct strnode *old)
{
struct strnode *tmp, *new, *cur;
tmp = old;
cur = new = new_strnode_dup(tmp->data, strlen(tmp->data) + 1);
tmp = tmp->next;
while (tmp) {
cur->next = new_strnode_dup(tmp->data, strlen(tmp->data) + 1);
cur = cur->next;
tmp = tmp->next;
}
return new;
}
/* a very simple singly-linked-list structure for queues/buffers */
#ifndef SLLIST_H
#define SLLIST_H
#include "utils.h"
/* just free the entire structure if it's free-able, the 'data' member
* should _NEVER_ be explicitly freed
*
* there's no free command, iterate through them yourself and just
* call free() on it iff you malloc'd them */
struct strnode {
struct strnode *next;
char *data;
};
struct sllnode {
struct sllnode *next;
void *data;
size_t size;
};
struct strnode *new_strnode(char *s);
struct strnode *new_strnode_dup(char *s, const size_t size);
struct strnode *dup_strlist(struct strnode *old);
struct sllnode *new_sllnode(void *s, const size_t size);
#endif /* SLLIST_H */
......@@ -134,32 +134,32 @@ void freeSongList(SongList * list)
freeList(list);
}
void printSongUrl(FILE * fp, Song * song)
void printSongUrl(int fd, Song * song)
{
if (song->parentDir && song->parentDir->path) {
myfprintf(fp, "%s%s/%s\n", SONG_FILE,
fdprintf(fd, "%s%s/%s\n", SONG_FILE,
getDirectoryPath(song->parentDir), song->url);
} else {
myfprintf(fp, "%s%s\n", SONG_FILE, song->url);
fdprintf(fd, "%s%s\n", SONG_FILE, song->url);
}
}
int printSongInfo(FILE * fp, Song * song)
int printSongInfo(int fd, Song * song)
{
printSongUrl(fp, song);
printSongUrl(fd, song);
if (song->tag)
printMpdTag(fp, song->tag);
printMpdTag(fd, song->tag);
return 0;
}
int printSongInfoFromList(FILE * fp, SongList * list)
int printSongInfoFromList(int fd, SongList * list)
{
ListNode *tempNode = list->firstNode;
while (tempNode != NULL) {
printSongInfo(fp, (Song *) tempNode->data);
printSongInfo(fd, (Song *) tempNode->data);
tempNode = tempNode->nextNode;
}
......@@ -174,7 +174,7 @@ void writeSongInfoFromList(FILE * fp, SongList * list)
while (tempNode != NULL) {
myfprintf(fp, "%s%s\n", SONG_KEY, tempNode->key);
printSongInfo(fp, (Song *) tempNode->data);
printSongInfo(fileno(fp), (Song *) tempNode->data);
myfprintf(fp, "%s%li\n", SONG_MTIME,
(long)((Song *) tempNode->data)->mtime);
tempNode = tempNode->nextNode;
......
......@@ -58,9 +58,9 @@ void freeSongList(SongList * list);
Song *addSongToList(SongList * list, char *url, char *utf8path,
int songType, struct _Directory *parentDir);
int printSongInfo(FILE * fp, Song * song);
int printSongInfo(int fd, Song * song);
int printSongInfoFromList(FILE * fp, SongList * list);
int printSongInfoFromList(int fd, SongList * list);
void writeSongInfoFromList(FILE * fp, SongList * list);
......@@ -69,7 +69,7 @@ void readSongInfoIntoList(FILE * fp, SongList * list,
int updateSongInfo(Song * song);
void printSongUrl(FILE * fp, Song * song);
void printSongUrl(int fd, Song * song);
char *getSongUrl(Song * song);
......
......@@ -34,15 +34,15 @@ void initStats(void)
stats.numberOfSongs = 0;
}
int printStats(FILE * fp)
int printStats(int fd)
{
myfprintf(fp, "artists: %li\n", getNumberOfTagItems(TAG_ITEM_ARTIST));
myfprintf(fp, "albums: %li\n", getNumberOfTagItems(TAG_ITEM_ALBUM));
myfprintf(fp, "songs: %i\n", stats.numberOfSongs);
myfprintf(fp, "uptime: %li\n", time(NULL) - stats.daemonStart);
myfprintf(fp, "playtime: %li\n",
fdprintf(fd, "artists: %li\n", getNumberOfTagItems(TAG_ITEM_ARTIST));
fdprintf(fd, "albums: %li\n", getNumberOfTagItems(TAG_ITEM_ALBUM));
fdprintf(fd, "songs: %i\n", stats.numberOfSongs);
fdprintf(fd, "uptime: %li\n", time(NULL) - stats.daemonStart);
fdprintf(fd, "playtime: %li\n",
(long)(getPlayerTotalPlayTime() + 0.5));
myfprintf(fp, "db_playtime: %li\n", stats.dbPlayTime);
myfprintf(fp, "db_update: %li\n", getDbModTime());
fdprintf(fd, "db_playtime: %li\n", stats.dbPlayTime);
fdprintf(fd, "db_update: %li\n", getDbModTime());
return 0;
}
......@@ -35,6 +35,6 @@ extern Stats stats;
void initStats();
int printStats(FILE * fp);
int printStats(int fd);
#endif
......@@ -121,15 +121,15 @@ void initTagConfig(void)
free(temp);
}
void printMpdTag(FILE * fp, MpdTag * tag)
void printMpdTag(int fd, MpdTag * tag)
{
int i;
if (tag->time >= 0)
myfprintf(fp, "Time: %i\n", tag->time);
fdprintf(fd, "Time: %i\n", tag->time);
for (i = 0; i < tag->numOfItems; i++) {
myfprintf(fp, "%s: %s\n", mpdTagItemKeys[tag->items[i].type],
fdprintf(fd, "%s: %s\n", mpdTagItemKeys[tag->items[i].type],
tag->items[i].value);
}
}
......
......@@ -78,7 +78,7 @@ void addItemToMpdTagWithLen(MpdTag * tag, int itemType, char *value, int len);
#define addItemToMpdTag(tag, itemType, value) \
addItemToMpdTagWithLen(tag, itemType, value, strlen(value))
void printMpdTag(FILE * fp, MpdTag * tag);
void printMpdTag(int fd, MpdTag * tag);
MpdTag *mpdTagDup(MpdTag * tag);
......
......@@ -147,7 +147,7 @@ void visitInTagTracker(int type, char *str)
((TagTrackerItem *) item)->visited = 1;
}
void printVisitedInTagTracker(FILE * fp, int type)
void printVisitedInTagTracker(int fd, int type)
{
ListNode *node;
TagTrackerItem *item;
......@@ -160,8 +160,8 @@ void printVisitedInTagTracker(FILE * fp, int type)
while (node) {
item = node->data;
if (item->visited) {
myfprintf(fp, "%s: %s\n", mpdTagItemKeys[type],
node->key);
fdprintf(fd, "%s: %s\n", mpdTagItemKeys[type],
node->key);
}
node = node->nextNode;
}
......
......@@ -33,6 +33,6 @@ void resetVisitedFlagsInTagTracker(int type);
void visitInTagTracker(int type, char *str);
void printVisitedInTagTracker(FILE * fp, int type);
void printVisitedInTagTracker(int fd, int type);
#endif
......@@ -21,7 +21,13 @@
#include "../config.h"
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
char *myFgets(char *buffer, int bufferSize, FILE * fp);
......@@ -37,4 +43,39 @@ char *appendToString(char *dest, const char *src);
unsigned long readLEuint32(const unsigned char *p);
/* trivial functions, keep them inlined */
static inline int xopen(const char *path, int flags, mode_t mode)
{
int fd;
while(0>(fd = open(path,flags,mode)) && errno == EINTR);
return fd;
}
static inline void xclose(int fd)
{
while (close(fd) && errno == EINTR);
}
static inline ssize_t xread(int fd, void *buf, size_t len)
{
ssize_t nr;
while (1) {
nr = read(fd, buf, len);
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
continue;
return nr;
}
}
static inline ssize_t xwrite(int fd, const void *buf, size_t len)
{
ssize_t nr;
while (1) {
nr = write(fd, buf, len);
if ((nr < 0) && (errno == EAGAIN || errno == EINTR))
continue;
return nr;
}
}
#endif
......@@ -169,7 +169,7 @@ static int getOssVolumeLevel(void)
return left;
}
static int changeOssVolumeLevel(FILE * fp, int change, int rel)
static int changeOssVolumeLevel(int fd, int change, int rel)
{
int current;
int new;
......@@ -177,7 +177,7 @@ static int changeOssVolumeLevel(FILE * fp, int change, int rel)
if (rel) {
if ((current = getOssVolumeLevel()) < 0) {
commandError(fp, ACK_ERROR_SYSTEM,
commandError(fd, ACK_ERROR_SYSTEM,
"problem getting current volume", NULL);
return -1;
}
......@@ -198,7 +198,7 @@ static int changeOssVolumeLevel(FILE * fp, int change, int rel)
if (ioctl(volume_ossFd, MIXER_WRITE(volume_ossControl), &level) < 0) {
closeOssMixer();
commandError(fp, ACK_ERROR_SYSTEM, "problems setting volume",
commandError(fd, ACK_ERROR_SYSTEM, "problems setting volume",
NULL);
return -1;
}
......@@ -328,7 +328,7 @@ static int getAlsaVolumeLevel(void)
return ret;
}
static int changeAlsaVolumeLevel(FILE * fp, int change, int rel)
static int changeAlsaVolumeLevel(int fd, int change, int rel)
{
float vol;
long level;
......@@ -361,7 +361,7 @@ static int changeAlsaVolumeLevel(FILE * fp, int change, int rel)
if ((err =
snd_mixer_selem_set_playback_volume_all(volume_alsaElem,
level)) < 0) {
commandError(fp, ACK_ERROR_SYSTEM, "problems setting volume",
commandError(fd, ACK_ERROR_SYSTEM, "problems setting volume",
NULL);
WARNING("problems setting alsa volume: %s\n",
snd_strerror(err));
......@@ -471,7 +471,7 @@ int getVolumeLevel(void)
}
}
static int changeSoftwareVolume(FILE * fp, int change, int rel)
static int changeSoftwareVolume(int fd, int change, int rel)
{
int new = change;
......@@ -499,19 +499,19 @@ static int changeSoftwareVolume(FILE * fp, int change, int rel)
return 0;
}
int changeVolumeLevel(FILE * fp, int change, int rel)
int changeVolumeLevel(int fd, int change, int rel)
{
switch (volume_mixerType) {
#ifdef HAVE_ALSA
case VOLUME_MIXER_TYPE_ALSA:
return changeAlsaVolumeLevel(fp, change, rel);
return changeAlsaVolumeLevel(fd, change, rel);
#endif
#ifdef HAVE_OSS
case VOLUME_MIXER_TYPE_OSS:
return changeOssVolumeLevel(fp, change, rel);
return changeOssVolumeLevel(fd, change, rel);
#endif
case VOLUME_MIXER_TYPE_SOFTWARE:
return changeSoftwareVolume(fp, change, rel);
return changeSoftwareVolume(fd, change, rel);
default:
return 0;
break;
......
......@@ -35,6 +35,6 @@ void finishVolume();
int getVolumeLevel();
int changeVolumeLevel(FILE * fp, int change, int rel);
int changeVolumeLevel(int fd, int change, int rel);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment