Skip to content

Commit

Permalink
Mitigate segfault during getting file locations
Browse files Browse the repository at this point in the history
When environment was misconfigured (or properly configured but in a very
strange way) segfault might occur due to fixed memory constraints.
  • Loading branch information
arkq committed Apr 12, 2015
1 parent b0455a1 commit 6e459c6
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 67 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ Exemplary configuration might be as follows:
* `format-coverfile = "^(cover|folder)\.jpg$"`


Instalation
-----------
Installation
------------

$ autoreconf --install
$ mkdir build && cd build
Expand Down
16 changes: 6 additions & 10 deletions src/cache.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cmusfm - cache.c
* Copyright (c) 2014 Arkadiusz Bokowy
* Copyright (c) 2014-2015 Arkadiusz Bokowy
*
* This file is a part of a cmusfm.
*
Expand Down Expand Up @@ -104,7 +104,7 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) {
sb_tinf->artist, sb_tinf->album, sb_tinf->album_artist,
sb_tinf->track_number, sb_tinf->track, sb_tinf->duration);

if ((f = fopen(get_cmusfm_cache_file(), "a")) == NULL)
if ((f = fopen(cmusfm_cache_file, "a")) == NULL)
return;

record = get_cache_record(sb_tinf);
Expand All @@ -118,7 +118,6 @@ void cmusfm_cache_update(const scrobbler_trackinfo_t *sb_tinf) {
void cmusfm_cache_submit(scrobbler_session_t *sbs) {

char rd_buff[4096];
char *fname;
FILE *f;
scrobbler_trackinfo_t sb_tinf;
struct cmusfm_cache_record *record;
Expand All @@ -127,8 +126,7 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) {

debug("cache submit");

fname = get_cmusfm_cache_file();
if ((f = fopen(fname, "r")) == NULL)
if ((f = fopen(cmusfm_cache_file, "r")) == NULL)
return;

// read file until EOF
Expand Down Expand Up @@ -200,12 +198,10 @@ void cmusfm_cache_submit(scrobbler_session_t *sbs) {
fclose(f);

// remove cache file
unlink(fname);
unlink(cmusfm_cache_file);
}

// Helper function for retrieving cmusfm cache file.
/* Helper function for retrieving cmusfm cache file. */
char *get_cmusfm_cache_file(void) {
static char fname[128];
sprintf(fname, "%s/" CACHE_FNAME, get_cmus_home_dir());
return fname;
return get_cmus_home_file(CACHE_FNAME);
}
12 changes: 8 additions & 4 deletions src/cmusfm.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cmusfm - cmusfm.h
* Copyright (c) 2010-2014 Arkadiusz Bokowy
* Copyright (c) 2010-2015 Arkadiusz Bokowy
*
* This file is a part of a cmusfm.
*
Expand Down Expand Up @@ -30,14 +30,17 @@
#define CACHE_FNAME "cmusfm.cache"


// time delay (in seconds) between login attempts to the Last.fm
// scrobbling service after a submit failure
/* time delay (in seconds) between login attempts to the Last.fm
* scrobbling service after a submit failure */
#define SERVICE_RETRY_DELAY 60 * 30


// global variable definitions
/* global variable definitions */
extern unsigned char SC_api_key[16];
extern unsigned char SC_secret[16];
extern const char *cmusfm_cache_file;
extern const char *cmusfm_config_file;
extern const char *cmusfm_socket_file;
extern struct cmusfm_config config;


Expand Down Expand Up @@ -71,6 +74,7 @@ struct format_match {


char *get_cmus_home_dir(void);
char *get_cmus_home_file(const char *file);
#ifdef ENABLE_LIBNOTIFY
char *get_album_cover_file(const char *location, const char *format);
#endif
Expand Down
20 changes: 9 additions & 11 deletions src/config.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cmusfm - config.c
* Copyright (c) 2014 Arkadiusz Bokowy
* Copyright (c) 2014-2015 Arkadiusz Bokowy
*
* This file is a part of a cmusfm.
*
Expand All @@ -24,10 +24,10 @@

#include "config.h"

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#ifdef HAVE_SYS_INOTIFY_H
#include <sys/inotify.h>
Expand Down Expand Up @@ -156,17 +156,15 @@ int cmusfm_config_write(const char *fname, struct cmusfm_config *conf) {
}

#ifdef HAVE_SYS_INOTIFY_H
// Add the cmusfm configuration file into the inotify watch stack. File
// is watched for modification, and after event is triggered it will be
// automatically removed from the stack.
/* Add the cmusfm configuration file into the inotify watch stack. File
* is watched for modification, and after event is triggered it will be
* automatically removed from the stack. */
int cmusfm_config_add_watch(int fd) {
return inotify_add_watch(fd, get_cmusfm_config_file(), IN_MODIFY | IN_ONESHOT);
return inotify_add_watch(fd, cmusfm_config_file, IN_MODIFY | IN_ONESHOT);
}
#endif

// Helper function for retrieving cmusfm configuration file.
/* Helper function for retrieving cmusfm configuration file. */
char *get_cmusfm_config_file(void) {
static char fname[128];
sprintf(fname, "%s/" CONFIG_FNAME, get_cmus_home_dir());
return fname;
return get_cmus_home_file(CONFIG_FNAME);
}
31 changes: 20 additions & 11 deletions src/main.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cmusfm - main.c
* Copyright (c) 2010-2014 Arkadiusz Bokowy
* Copyright (c) 2010-2015 Arkadiusz Bokowy
*
* This file is a part of a cmusfm.
*
Expand All @@ -22,23 +22,29 @@
#include "../config.h"
#endif

#include "cmusfm.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cmusfm.h"
#include "cache.h"
#include "config.h"
#include "debug.h"
#include "server.h"


// Last.fm API key for cmusfm
/* Last.fm API key for cmusfm */
unsigned char SC_api_key[16] = {0x67, 0x08, 0x2e, 0x45, 0xda, 0xb1,
0xf6, 0x43, 0x3d, 0xa7, 0x2a, 0x00, 0xe3, 0xbc, 0x03, 0x7a};
unsigned char SC_secret[16] = {0x02, 0xfc, 0xbc, 0x90, 0x34, 0x1a,
0x01, 0xf2, 0x1c, 0x3b, 0xfc, 0x05, 0xb6, 0x36, 0xe3, 0xae};

// Global configuration structure
/* Global cmusfm file location variables */
const char *cmusfm_cache_file = NULL;
const char *cmusfm_config_file = NULL;
const char *cmusfm_socket_file = NULL;

/* Global configuration structure */
struct cmusfm_config config;


Expand Down Expand Up @@ -102,15 +108,13 @@ static int cmusfm_initialization() {
scrobbler_session_t *sbs;
struct cmusfm_config conf;
int fetch_session_key;
char *conf_fname;
char yesno[8], *ptr;

fetch_session_key = 1;
conf_fname = get_cmusfm_config_file();
sbs = scrobbler_initialize(SC_api_key, SC_secret);

// try to read previous configuration
if (cmusfm_config_read(conf_fname, &conf) == 0) {
if (cmusfm_config_read(cmusfm_config_file, &conf) == 0) {
printf("Checking previous session (user: %s) ...", conf.user_name);
fflush(stdout);
scrobbler_set_session_key_str(sbs, conf.session_key);
Expand All @@ -135,8 +139,8 @@ static int cmusfm_initialization() {
}
scrobbler_free(sbs);

if (cmusfm_config_write(conf_fname, &conf) != 0)
printf("Error: unable to write file: %s\n", conf_fname);
if (cmusfm_config_write(cmusfm_config_file, &conf) != 0)
printf("Error: unable to write file: %s\n", cmusfm_config_file);

return 0;
}
Expand All @@ -153,11 +157,16 @@ int main(int argc, char *argv[]) {
return EXIT_SUCCESS;
}

/* setup global variables - file locations */
cmusfm_cache_file = get_cmusfm_cache_file();
cmusfm_config_file = get_cmusfm_config_file();
cmusfm_socket_file = get_cmusfm_socket_file();

if (argc == 2 && strcmp(argv[1], "init") == 0)
return cmusfm_initialization();

if (cmusfm_config_read(get_cmusfm_config_file(), &config) == -1) {
perror("error: unable to read config file");
if (cmusfm_config_read(cmusfm_config_file, &config) == -1) {
perror("error: config read");
return EXIT_FAILURE;
}

Expand Down
30 changes: 14 additions & 16 deletions src/server.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* cmusfm - server.c
* Copyright (c) 2010-2014 Arkadiusz Bokowy
* Copyright (c) 2010-2015 Arkadiusz Bokowy
*
* This file is a part of a cmusfm.
*
Expand All @@ -24,14 +24,14 @@

#include "server.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <libgen.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#ifdef HAVE_SYS_INOTIFY_H
Expand All @@ -47,22 +47,22 @@
#endif


// Helper function for artist name retrieval.
/* Helper function for artist name retrieval. */
static char *get_sock_data_artist(struct sock_data_tag *dt) {
return (char *)(dt + 1);
}

// Helper function for album name retrieval.
/* Helper function for album name retrieval. */
static char *get_sock_data_album(struct sock_data_tag *dt) {
return &((char *)(dt + 1))[dt->alboff];
}

// Helper function for track name retrieval.
/* Helper function for track name retrieval. */
static char *get_sock_data_track(struct sock_data_tag *dt) {
return &((char *)(dt + 1))[dt->titoff];
}

// Helper function for location retrieval.
/* Helper function for location retrieval. */
static char *get_sock_data_location(struct sock_data_tag *dt) {
return &((char *)(dt + 1))[dt->locoff];
}
Expand Down Expand Up @@ -270,7 +270,7 @@ void cmusfm_server_start(void) {

memset(&sock_a, 0, sizeof(sock_a));
sock_a.sun_family = AF_UNIX;
strcpy(sock_a.sun_path, get_cmusfm_socket_file());
strcpy(sock_a.sun_path, cmusfm_socket_file);
pfds[0].fd = socket(PF_UNIX, SOCK_STREAM, 0);

// check if behind the socket there is already an active server instance
Expand Down Expand Up @@ -329,7 +329,7 @@ void cmusfm_server_start(void) {
// to us, simply read out the inotify file descriptor
read(pfds[2].fd, &inot_even, sizeof(inot_even));
debug("inotify event occurred: %x", inot_even.mask);
cmusfm_config_read(get_cmusfm_config_file(), &config);
cmusfm_config_read(cmusfm_config_file, &config);
cmusfm_config_add_watch(pfds[2].fd);
}
#endif
Expand Down Expand Up @@ -435,7 +435,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) {

// connect to the communication socket
memset(&sock_a, 0, sizeof(sock_a));
strcpy(sock_a.sun_path, get_cmusfm_socket_file());
strcpy(sock_a.sun_path, cmusfm_socket_file);
sock_a.sun_family = AF_UNIX;
sock = socket(PF_UNIX, SOCK_STREAM, 0);
if (connect(sock, (struct sockaddr *)(&sock_a), sizeof(sock_a)) == -1) {
Expand All @@ -450,9 +450,7 @@ int cmusfm_server_send_track(struct cmtrack_info *tinfo) {
return close(sock);
}

// Helper function for retrieving cmusfm server socket file.
/* Helper function for retrieving cmusfm server socket file. */
char *get_cmusfm_socket_file(void) {
static char fname[128];
sprintf(fname, "%s/" SOCKET_FNAME, get_cmus_home_dir());
return fname;
return get_cmus_home_file(SOCKET_FNAME);
}
Loading

0 comments on commit 6e459c6

Please sign in to comment.