Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Reporting #12

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
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
186 changes: 84 additions & 102 deletions src/win_oper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,59 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include "x11fs.h"

//Specific read and write functions for each file

void border_color_write(int wid, const char *buf)
{
set_border_color(wid, strtol(buf, NULL, 16));
}
errno = 0;
long color = strtol(buf, NULL, 16);
int errsv = errno;
if ( errsv ) {
syslog(LOG_ERR, "failed to parse color in %s: %s\n", __func__, strerror(errsv));
}

char *border_width_read(int wid)
{
int border_width=get_border_width(wid);
if(border_width==-1){
errno = -EIO;
return NULL;
set_border_color(wid, color);
}

#define DECLARE_NORM_READER(cat, prop, getter) \
char * cat##_##prop##_read (int wid) {\
int i = getter(wid);\
if ( i == -1 ) {\
errno = -EIO;\
return NULL;\
}\
\
char * str = malloc(snprintf(NULL, 0, "%d\n", i) + 1);\
if ( !str ) {\
syslog(LOG_ERR, "failed to allocate in %s: %s\n", __func__, strerror(ENOMEM));\
}\
\
if ( sprintf(str, "%d\n", i) < 0 ) {\
syslog(LOG_ERR, "failed to store value in %s\n", __func__);\
}\
\
return str;\
}
//Work out the size needed to malloc by calling snprintf with a size of 0
char *border_width_string=malloc(snprintf(NULL, 0, "%d\n", border_width)+1);
sprintf(border_width_string, "%d\n", border_width);
return border_width_string;
}

void border_width_write(int wid, const char *buf)
{
set_border_width(wid, atoi(buf));
}
DECLARE_NORM_READER(border, width, get_border_width);
DECLARE_NORM_READER(geometry, width, get_width);
DECLARE_NORM_READER(geometry, height, get_height);
DECLARE_NORM_READER(geometry, x, get_x);
DECLARE_NORM_READER(geometry, y, get_y);

#define DECLARE_NORM_WRITER(cat, prop, setter) \
void cat##_##prop##_write (int wid, const char * buf) {\
setter(wid, atoi(buf));\
}

DECLARE_NORM_WRITER(border, width, set_border_width);
DECLARE_NORM_WRITER(geometry, width, set_width);
DECLARE_NORM_WRITER(geometry, height, set_height);
DECLARE_NORM_WRITER(geometry, x, set_x);
DECLARE_NORM_WRITER(geometry, y, set_y);

char *root_width_read(int wid)
{
Expand All @@ -43,78 +70,6 @@ char *root_height_read(int wid)
return geometry_height_read(-1);
}

char *geometry_width_read(int wid)
{
int width=get_width(wid);
if(width==-1){
errno = -EIO;
return NULL;
}

char *width_string=malloc(snprintf(NULL, 0, "%d\n", width)+1);
sprintf(width_string, "%d\n", width);
return width_string;
}

void geometry_width_write(int wid, const char *buf)
{
set_width(wid, atoi(buf));
}

char *geometry_height_read(int wid)
{
int height=get_height(wid);
if(height==-1){
errno = -EIO;
return NULL;
}

char *height_string=malloc(snprintf(NULL, 0, "%d\n", height)+1);
sprintf(height_string, "%d\n", height);
return height_string;
}

void geometry_height_write(int wid, const char *buf)
{
set_height(wid, atoi(buf));
}

char *geometry_x_read(int wid)
{
int x=get_x(wid);
if(x==-1){
errno = -EIO;
return NULL;
}

char *x_string=malloc(snprintf(NULL, 0, "%d\n", x)+1);
sprintf(x_string, "%d\n", x);
return x_string;
}

void geometry_x_write(int wid, const char *buf)
{
set_x(wid, atoi(buf));
}

char *geometry_y_read(int wid)
{
int y=get_y(wid);
if(y==-1){
errno = -EIO;
return NULL;
}

char *y_string=malloc(snprintf(NULL, 0, "%d\n", y)+1);
sprintf(y_string, "%d\n", y);
return y_string;
}

void geometry_y_write(int wid, const char *buf)
{
set_y(wid, atoi(buf));
}

char *mapped_read(int wid)
{
return strdup(get_mapped(wid) ? "true\n" : "false\n");
Expand Down Expand Up @@ -154,8 +109,15 @@ char *title_read(int wid)
char *title=get_title(wid);
size_t title_len = strlen(title);
char *title_string=malloc(title_len+2);
if ( !title_string ) {
syslog(LOG_ERR, "failed to allocate in %s: %s\n", __func__, strerror(ENOMEM));
}

memset(title_string, 0, title_len+2);
if ( title_len ) { sprintf(title_string, "%s\n", title); }
if ( title_len && sprintf(title_string, "%s\n", title) < 0 ) {
syslog(LOG_ERR, "failed to store title string in %s\n", __func__);
}

free(title);
return title_string;
}
Expand All @@ -165,12 +127,17 @@ char *class_read(int wid)
char **classes=get_class(wid);
size_t class0_len = strlen(classes[0]), class1_len = strlen(classes[1]);
char *class_string=malloc(class0_len + class1_len + 3);
if ( class0_len ) {
sprintf(class_string, "%s\n", classes[0]);
if ( !class_string ) {
syslog(LOG_ERR, "failed to allocate in %s: %s\n", __func__, strerror(ENOMEM));
}

if ( class0_len && sprintf(class_string, "%s\n", classes[0]) < 0) {
syslog(LOG_ERR, "failed to store first class in %s\n", __func__);
}
if ( class1_len ) {
sprintf(class_string + class0_len + 1, "%s\n", classes[1]);
if ( class1_len && sprintf(class_string + class0_len + 1, "%s\n", classes[1]) < 0) {
syslog(LOG_ERR, "failed to store second class in %s\n", __func__);
}

free(classes[0]);
free(classes[1]);
free(classes);
Expand All @@ -185,20 +152,35 @@ char *event_read(int wid)
char *focused_read(int wid)
{
(void) wid;
char *focusedwin;
int focusedid=focused();
if(focusedid){
focusedwin = malloc(WID_STRING_LENGTH+1);
sprintf(focusedwin, "0x%08x\n", focusedid);
}else{
focusedwin = malloc(6);
sprintf(focusedwin, "root\n");
char * focusedwin = malloc(focusedid ? WID_STRING_LENGTH + 1 : 6);
if ( !focusedwin ) {
syslog(LOG_ERR, "failed to allocate in %s: %s\n", __func__, strerror(ENOMEM));
}

int stat = 0;
if ( !focusedid ) {
stat = sprintf(focusedwin, "root\n");
} else {
stat = sprintf(focusedwin, "0x%08x\n", focusedid);
}

if ( stat < 0 ) {
syslog(LOG_ERR, "failed to store focused window in %s\n", __func__);
}

return focusedwin;
}

void focused_write(int wid, const char *buf)
{
(void) wid;
focus(strtol(buf, NULL, 16));
errno = 0;
long id = strtol(buf, NULL, 16);
int errsv = errno;
if ( errsv ) {
syslog(LOG_ERR, "failed to parse id to focus in %s: %s\n", __func__, strerror(errsv));
}

focus(id);
}
31 changes: 28 additions & 3 deletions src/x11fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <errno.h>
#include <string.h>
#include <fnmatch.h>
#include <syslog.h>
#include "x11fs.h"
#include "win_xcb.h"
#include "win_oper.h"
Expand Down Expand Up @@ -325,9 +326,33 @@ static struct fuse_operations x11fs_operations = {
//Just setup our connection to X then let fuse handle the rest
int main(int argc, char **argv)
{

openlog("x11fs", LOG_CONS, LOG_USER);
syslog(LOG_INFO, "Started\n");

pid_t pid = fork();
if ( pid < 0 ) {
syslog(LOG_ERR, "Failed to fork off\n");
closelog();
return EXIT_FAILURE;
} else if ( pid > 0 ) {
exit(EXIT_SUCCESS);
}

pid_t sid = 0;
if ( (sid = setsid()) < 0 ) {
syslog(LOG_ERR, "Failed to create new process group session\n");
exit(EXIT_FAILURE);
}

if(xcb_init()!=X11FS_SUCCESS){
fputs("Failed to setup xcb. Quiting...\n", stderr);
return 1;
syslog(LOG_ERR, "Failed to setup xcb. Quiting...\n", stderr);
exit(EXIT_FAILURE);
}
return fuse_main(argc, argv, &x11fs_operations, NULL);

fuse_main(argc, argv, &x11fs_operations, NULL);

syslog(LOG_INFO, "Shutting down\n");
closelog();
return EXIT_SUCCESS;
}