Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions doc/vitunes.1
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@
.Sh SYNOPSIS
.Nm vitunes
.Bk -words
.Op Fl c Ar command
.Op Fl h
.Op Fl c Ar command Op ...
.Op Fl d Ar database-file
.Op Fl e Ar command Op argument ...
.Op Fl e Ar e-command Op flags
.Op Fl f Ar config-file
.Op Fl m Ar media-backend
.Op Fl p Ar playlist-dir
.Op Fl s Ar socket-file
.Ek
.Sh DESCRIPTION
.Nm
Expand All @@ -37,14 +39,14 @@ creation/management.
It is not intended to be a feature-rich media player, but rather a quick,
vi-like media indexer and playlist manager that also happens to be able to
play the media it indexes (via
.Xr mplayer 1 ).
.Xr mplayer 1 Ns ).
.Pp
.Nm
accepts the following command line options:
.Bl -tag -width Fl
.It Fl c Ar command
.It Fl c Ar command Op ...
Execute the specified
.Ar commands
.Ar command
in the currently running
.Nm
instance, and exit.
Expand All @@ -63,9 +65,8 @@ section below.
.Pp
Note that for this to work, when
.Nm
starts up it attempts to create a socket at
.Pa /tmp/.vitunes
that are used by this option to communicate with the original instance.
starts up it attempts to create a socket that is used by this option to
communicate with the original instance.
If this socket cannot be created for any reason, this option will not work.
.It Fl d Ar database-file
Specifies the
Expand All @@ -79,7 +80,7 @@ be specified before the e-command.
.Pp
The default location is
.Pa ~/.vitunes/vitunes.db .
.It Fl e Ar command Ar options
.It Fl e Ar e-command Op flags
Execute one of the available e-commands to manipulate the database that
.Nm
uses.
Expand Down Expand Up @@ -138,6 +139,12 @@ will be created here.
.Pp
The default location is
.Pa ~/.vitunes/playlists/ .
.It Fl s Ar socket-file
Specifies an alternative
.Ar socket-file .
.Pp
The default location is
.Pa ~/.vitunes/vitunes.sock .
.El
.Sh GETTING STARTED
.Nm
Expand Down Expand Up @@ -241,7 +248,7 @@ Below is a listing of all run-time commands supported by
.Pp
All commands are entered by typing ':' followed by the command name and any
parameters (just like in
.Xr vi 1 ).
.Xr vi 1 Ns ).
.Pp
Note that abbreviations are also supported.
That is, entering any non-ambiguous abbreviation of a command name will also
Expand Down Expand Up @@ -1024,7 +1031,7 @@ Default playlist directory
This can be overridden with the
.Op Fl p Ar playlist-dir
flag.
.It Pa /tmp/.vitunes
.It Pa ~/.vitunes/vitunes.sock
Default location for the socket created on start-up that can be used to control
.Nm .
.It Pa /usr/local/bin/mplayer
Expand Down
6 changes: 3 additions & 3 deletions medialib.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ medialib_setup_files(const char *vitunes_dir, const char *db_file,
else
err(1, "unable to create vitunes directory '%s'", vitunes_dir);
} else
warnx("vitunes directory '%s' created", vitunes_dir);
printf("vitunes directory '%s' created\n", vitunes_dir);

/* create playlists directory */
if (mkdir(playlist_dir, S_IRWXU) == -1) {
Expand All @@ -168,7 +168,7 @@ medialib_setup_files(const char *vitunes_dir, const char *db_file,
else
err(1, "unable to create playlists directory '%s'", playlist_dir);
} else
warnx("playlists directory '%s' created", playlist_dir);
printf("playlists directory '%s' created\n", playlist_dir);

/* create database file */
if (stat(db_file, &sb) < 0) {
Expand All @@ -185,7 +185,7 @@ medialib_setup_files(const char *vitunes_dir, const char *db_file,
fwrite("vitunes", strlen("vitunes"), 1, f);
fwrite(version, sizeof(version), 1, f);

warnx("empty database at '%s' created", db_file);
printf("empty database at '%s' created\n", db_file);
fclose(f);
} else
err(1, "database file '%s' exists, but cannot access it", db_file);
Expand Down
32 changes: 17 additions & 15 deletions socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
Expand All @@ -23,51 +25,51 @@
#include "socket.h"
#include "commands.h"

#define VITUNES_SOCK "/tmp/.vitunes"


int
sock_send_msg(const char *msg)
sock_send_msg(const char *path, const char *msg)
{
int ret;
struct sockaddr_un addr;
socklen_t addr_len;


if((ret = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
return -1;

addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, VITUNES_SOCK);
addr_len = sizeof(addr.sun_family) + strlen(VITUNES_SOCK) + 1;
if (strlcpy(addr.sun_path, path, sizeof addr.sun_path) >= sizeof addr.sun_path) {
errno = ENAMETOOLONG;
return -1;
}

if(sendto(ret, msg, strlen(msg), 0, (struct sockaddr *) &addr, addr_len) == -1) {
if (sendto(ret, msg, strlen(msg), 0, (struct sockaddr *) &addr, sizeof addr) == -1) {
close(ret);
return -1;
}

close(ret);
return 0;
}


int
sock_listen(void)
sock_listen(const char *path)
{
int ret;
struct sockaddr_un addr;
socklen_t addr_len;
int coe = 1;

unlink(VITUNES_SOCK);
unlink(path);

if((ret = socket(AF_UNIX, SOCK_DGRAM, 0)) == -1)
return -1;

addr.sun_family = AF_UNIX;
strcpy(addr.sun_path, VITUNES_SOCK);
addr_len = sizeof(addr.sun_family) + strlen(VITUNES_SOCK) + 1;
if (strlcpy(addr.sun_path, path, sizeof addr.sun_path) >= sizeof addr.sun_path) {
errno = ENAMETOOLONG;
return -1;
}

if(bind(ret, (struct sockaddr *) &addr, addr_len) == -1)
if (bind(ret, (struct sockaddr *) &addr, sizeof addr) == -1)
return -1;

fcntl(ret, F_SETFD, FD_CLOEXEC, &coe);
Expand All @@ -80,7 +82,7 @@ ssize_t
sock_recv_msg(int sock, char *msg, size_t msg_len)
{
struct sockaddr_un addr;
socklen_t addr_len = sizeof(struct sockaddr_un);
socklen_t addr_len = sizeof addr;
ssize_t ret;

ret = recvfrom(sock, msg, msg_len, 0, (struct sockaddr *) &addr, &addr_len);
Expand Down
10 changes: 5 additions & 5 deletions socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
#define VITUNES_RUNNING "WHOWASPHONE?"

/*
* send (null terminated) msg to vitunes. Returns 0 on success,
* send (null terminated) msg to vitunes through path. Returns 0 on success,
* -1 on errors.
*/
int sock_send_msg(const char *msg);
int sock_send_msg(const char *path, const char *msg);

/*
* open vitunes socket for listening. Returns a socket on success,
* -1 on errors.
* open vitunes socket (provided by path) for listening. Returns a socket
* on success, -1 on errors.
*/
int sock_listen(void);
int sock_listen(const char *path);

/*
* Receive message from sock into msg. A maximum number of msg_len
Expand Down
34 changes: 22 additions & 12 deletions vitunes.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,15 @@ const char *VITUNES_DIR_FMT = "%s/.vitunes";
const char *CONF_FILE_FMT = "%s/.vitunes/vitunes.conf";
const char *DB_FILE_FMT = "%s/.vitunes/vitunes.db";
const char *PLAYLIST_DIR_FMT = "%s/.vitunes/playlists";
const char *SOCKET_FILE_FMT = "%s/.vitunes/vitunes.sock";

/* absolute paths of key stuff */
char *vitunes_dir;
char *conf_file;
char *db_file;
char *playlist_dir;
char *player_backend;
char *socket_file;

/* program name with directories removed */
char *progname;
Expand Down Expand Up @@ -122,15 +124,17 @@ main(int argc, char *argv[])
err(1, "main: asprintf failed");
if (asprintf(&player_backend, "%s", DEFAULT_PLAYER_BACKEND) == -1)
err(1, "main: asprintf failed");
if (asprintf(&socket_file, SOCKET_FILE_FMT, home) == -1)
err(1, "main: asprintf failed");

/* handle command-line switches & e-commands */
handle_switches(argc, argv);

if(sock_send_msg(VITUNES_RUNNING) != -1) {
printf("Vitunes appears to be running already. Won't open socket.");
} else {
if((sock = sock_listen()) == -1)
errx(1, "failed to open socket.");
if (sock_send_msg(socket_file, VITUNES_RUNNING) != -1)
warnx("Vitunes appears to be running already. Won't open socket.");
else {
if ((sock = sock_listen(socket_file)) == -1)
err(1, "failed to open socket");
}


Expand Down Expand Up @@ -264,10 +268,10 @@ main(int argc, char *argv[])
void
usage(void)
{
fprintf(stderr,"\
usage: %s [-f config-file] [-d database-file] [-p playlist-dir] [-m player-path] [-e COMMAND ...]\n\
See \"%s -e help\" for information about what e-commands are available.\n\
",
fprintf(stderr, "\
usage: %s [-h] [-c command [...]] [-d database-file] [-e e-command [flags]]\n\
\t[-f config-file] [-m media-backend] [-p playlist-dir] [-s socket-file]\n\
See \"%s -e help\" for information about what e-commands are available.\n",
progname, progname);
exit(1);
}
Expand Down Expand Up @@ -472,11 +476,11 @@ handle_switches(int argc, char *argv[])
{
int ch;

while ((ch = getopt(argc, argv, "he:f:d:p:m:c:")) != -1) {
while ((ch = getopt(argc, argv, "hc:d:e:f:m:p:s:")) != -1) {
switch (ch) {
case 'c':
if(sock_send_msg(optarg) == -1)
errx(1, "Failed to send message. Vitunes not running?");
if (sock_send_msg(socket_file, optarg) == -1)
err(1, "Failed to send message. Vitunes not running?");
exit(0);

case 'd':
Expand Down Expand Up @@ -510,6 +514,12 @@ handle_switches(int argc, char *argv[])
err(1, "handle_switches: strdup playlist_dir failed");
break;

case 's':
free(socket_file);
if ((socket_file = strdup(optarg)) == NULL)
err(1, "handle_switches: strdup socket_file failed");
break;

case 'h':
case '?':
default:
Expand Down