Skip to content

Commit

Permalink
Fixes some typos and errors, adds basic Xorg log/output parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thulinma committed Jan 11, 2012
1 parent eeab941 commit 57fcad2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/bbrun.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ static void bb_run_exec_detached(char **argv) {
int devnull = open("/dev/null", O_RDWR);
if (devnull < 0){
bb_log(LOG_ERR, "Could not open /dev/null: %s\n", strerror(errno));
return EXIT_FAILURE;
}
old_stderr = dup(STDERR_FILENO);
dup2(devnull, STDIN_FILENO);
Expand Down
2 changes: 1 addition & 1 deletion src/bbrun.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
int bb_run_fork(char** argv, int detached);

/// Forks and runs the given application, using an LD_LIBRARY_PATH.
pid_t bb_run_fork_ld_redirect(char** argv, char * ldpath);
pid_t bb_run_fork_ld_redirect(char** argv, char * ldpath, int redirect);

/// Forks and runs the given application, waits for a maximum of timeout seconds for process to finish.
void bb_run_fork_wait(char** argv, int timeout);
Expand Down
4 changes: 3 additions & 1 deletion src/bbsecondary.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* along with Bumblebee. If not, see <http://www.gnu.org/licenses/>.
*/

#define _GNU_SOURCE
#include <unistd.h>
#include <X11/Xlib.h>
#include <stdio.h>
#include <string.h>
Expand Down Expand Up @@ -152,7 +154,7 @@ void start_secondary(void) {
if (bb_status.x_pipe[0] != -1){close(bb_status.x_pipe[0]); bb_status.x_pipe[0] = -1;}
if (bb_status.x_pipe[1] != -1){close(bb_status.x_pipe[1]); bb_status.x_pipe[1] = -1;}
//create a new pipe
if (pipe(bb_status.x_pipe)){
if (pipe2(bb_status.x_pipe, O_NONBLOCK)){
set_bb_error("Could not create output pipe for X");
return;
}
Expand Down
51 changes: 50 additions & 1 deletion src/bumblebeed.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
#include "bbrun.h"
#include "pci.h"

#define X_BUFFER_SIZE 512

/**
* Change GID and umask of the daemon
* @return EXIT_SUCCESS if the gid could be changed, EXIT_FAILURE otherwise
Expand Down Expand Up @@ -201,11 +203,29 @@ static void handle_socket(struct clientsocket * C) {
}
}

/* Parses a single null-terminated string of Xorg output.
* Will call bb_log appropiately.
*/
static void parse_xorg_output(char * string){
int prio = LOG_DEBUG;//most lines are debug messages

//Error lines are errors.
if (strstr(string, "(EE)")){prio = LOG_ERR;}
//Warning lines are warnings.
if (strstr(string, "(WW)")){prio = LOG_WARNING;}
/// \todo Convert useless/meaningless warnings to LOG_INFO

//do the actual logging
bb_log(prio, "[XORG] %s\n", string);
}

/* The main loop handles all connections and cleanup.
* It returns if there are any problems with the listening socket.
*/
static void main_loop(void) {
int optirun_socket_fd;
char x_output_buffer[X_BUFFER_SIZE];
int x_buffer_pos = 0;
struct clientsocket *client;
struct clientsocket *last = 0; // the last client

Expand Down Expand Up @@ -233,7 +253,36 @@ static void main_loop(void) {

//check the X output pipe, if open
if (bb_status.x_pipe[0] != -1){
/// \todo Parse output from X
//attempt to read at most the entire buffer full.
int r = read(bb_status.x_pipe[0], x_output_buffer+x_buffer_pos, X_BUFFER_SIZE-x_buffer_pos);
if (r > 0){
x_buffer_pos += r;
}else{
if (r == 0 || errno == EAGAIN){
//the pipe is closed/invalid. Clean up.
if (bb_status.x_pipe[0] != -1){close(bb_status.x_pipe[0]); bb_status.x_pipe[0] = -1;}
if (bb_status.x_pipe[1] != -1){close(bb_status.x_pipe[1]); bb_status.x_pipe[1] = -1;}
}
}
//while x_buffer_pos>0 and a \n is in the buffer, parse.
//if buffer is full, parse also.
while (x_buffer_pos > 0){
x_output_buffer[X_BUFFER_SIZE-1] = 0;//make sure there's a terminating null byte
if (x_buffer_pos == X_BUFFER_SIZE){
//full buffer, parse
parse_xorg_output(x_output_buffer);
x_buffer_pos = 0;
}else{
char * foundnewline = strchr(x_output_buffer, '\n');
if (!foundnewline){break;}//cancel search if no newline, try again later
foundnewline[0] = 0;//convert newline to null byte
parse_xorg_output(x_output_buffer);//parse the line
r -= foundnewline - x_output_buffer;//cut the parsed part from the buffer size
if (r > 0){//move the unparsed part left, if any
memmove(x_output_buffer, foundnewline+1, X_BUFFER_SIZE - ((foundnewline+1) - x_output_buffer));
}
}
}
}

/* loop through all connections, removing dead ones, receiving/sending data to the rest */
Expand Down

0 comments on commit 57fcad2

Please sign in to comment.