2013-11-23 10:20:05 +01:00
|
|
|
/* vim:fileencoding=utf-8:noet
|
|
|
|
*/
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2014-08-04 00:21:44 +02:00
|
|
|
#include <stddef.h>
|
2013-07-31 22:15:35 +02:00
|
|
|
#include <sys/un.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
2014-08-04 00:11:19 +02:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define HANDLE_ERROR(msg) \
|
|
|
|
do { \
|
2014-08-06 20:31:16 +02:00
|
|
|
perror(msg); \
|
|
|
|
exit(EXIT_FAILURE); \
|
2014-08-04 00:11:19 +02:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define TEMP_FAILURE_RETRY(var, expression) \
|
|
|
|
do { \
|
2014-08-06 20:31:16 +02:00
|
|
|
ptrdiff_t __result; \
|
2014-08-04 00:11:19 +02:00
|
|
|
do { \
|
2014-08-06 20:31:16 +02:00
|
|
|
__result = (expression); \
|
2014-08-04 00:11:19 +02:00
|
|
|
} while (__result == -1L && errno == EINTR); \
|
|
|
|
var = __result; \
|
|
|
|
} while (0)
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
extern char **environ;
|
|
|
|
|
2014-08-04 00:21:44 +02:00
|
|
|
void do_write(int sd, const char *raw, size_t len) {
|
|
|
|
size_t written = 0;
|
|
|
|
ptrdiff_t n = -1;
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
while (written < len) {
|
2014-08-04 00:11:19 +02:00
|
|
|
TEMP_FAILURE_RETRY(n, write(sd, raw + written, len - written));
|
2013-07-31 22:15:35 +02:00
|
|
|
if (n == -1) {
|
|
|
|
close(sd);
|
2014-08-04 00:11:19 +02:00
|
|
|
HANDLE_ERROR("write() failed");
|
2013-07-31 22:15:35 +02:00
|
|
|
}
|
2014-08-04 00:21:44 +02:00
|
|
|
written += (size_t) n;
|
2013-07-31 22:15:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-04 00:11:19 +02:00
|
|
|
#ifdef __APPLE__
|
|
|
|
# define ADDRESS_TEMPLATE "/tmp/powerline-ipc-%d"
|
|
|
|
# define A
|
|
|
|
#else
|
|
|
|
# define ADDRESS_TEMPLATE "powerline-ipc-%d"
|
|
|
|
# define A +1
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define ADDRESS_SIZE sizeof(ADDRESS_TEMPLATE) + (sizeof(uid_t) * 4)
|
|
|
|
#define NUM_ARGS_SIZE (sizeof(int) * 2)
|
|
|
|
#define BUF_SIZE 4096
|
|
|
|
#define NEW_ARGV_SIZE 200
|
|
|
|
|
2013-07-31 22:15:35 +02:00
|
|
|
int main(int argc, char *argv[]) {
|
2014-08-04 00:11:19 +02:00
|
|
|
int sd = -1;
|
2014-08-06 20:31:16 +02:00
|
|
|
int i;
|
|
|
|
ptrdiff_t read_size;
|
2013-07-31 22:15:35 +02:00
|
|
|
struct sockaddr_un server;
|
2014-08-04 00:11:19 +02:00
|
|
|
char address[ADDRESS_SIZE];
|
2013-07-31 22:15:35 +02:00
|
|
|
const char eof[2] = "\0\0";
|
2014-08-04 00:59:02 +02:00
|
|
|
char num_args[NUM_ARGS_SIZE];
|
2014-08-04 00:11:19 +02:00
|
|
|
char buf[BUF_SIZE];
|
|
|
|
char *newargv[NEW_ARGV_SIZE];
|
2013-07-31 22:15:35 +02:00
|
|
|
char *wd = NULL;
|
|
|
|
char **envp;
|
|
|
|
|
2014-08-04 00:11:19 +02:00
|
|
|
if (argc < 2) {
|
2014-08-06 20:31:16 +02:00
|
|
|
printf("Must provide at least one argument.\n");
|
|
|
|
return EXIT_FAILURE;
|
2014-08-04 00:11:19 +02:00
|
|
|
}
|
2013-07-31 22:15:35 +02:00
|
|
|
|
2014-08-04 00:11:19 +02:00
|
|
|
snprintf(address, ADDRESS_SIZE, ADDRESS_TEMPLATE, getuid());
|
2013-07-31 22:15:35 +02:00
|
|
|
|
|
|
|
sd = socket(AF_UNIX, SOCK_STREAM, 0);
|
2014-08-04 00:11:19 +02:00
|
|
|
if (sd == -1)
|
|
|
|
HANDLE_ERROR("socket() failed");
|
2013-07-31 22:15:35 +02:00
|
|
|
|
2014-08-04 00:11:19 +02:00
|
|
|
memset(&server, 0, sizeof(struct sockaddr_un));
|
2013-07-31 22:15:35 +02:00
|
|
|
server.sun_family = AF_UNIX;
|
2014-08-04 00:11:19 +02:00
|
|
|
strncpy(server.sun_path A, address, strlen(address));
|
2013-07-31 22:15:35 +02:00
|
|
|
|
2014-08-04 00:21:44 +02:00
|
|
|
if (connect(sd, (struct sockaddr *) &server, (socklen_t) (sizeof(server.sun_family) + strlen(address) A)) < 0) {
|
2013-07-31 22:15:35 +02:00
|
|
|
close(sd);
|
2014-08-04 00:11:19 +02:00
|
|
|
/* We failed to connect to the daemon, execute powerline instead */
|
|
|
|
argc = (argc < NEW_ARGV_SIZE - 1) ? argc : NEW_ARGV_SIZE - 1;
|
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
newargv[i] = argv[i];
|
2013-07-31 22:15:35 +02:00
|
|
|
newargv[0] = "powerline-render";
|
|
|
|
newargv[argc] = NULL;
|
|
|
|
execvp("powerline-render", newargv);
|
|
|
|
}
|
|
|
|
|
2014-08-04 00:59:02 +02:00
|
|
|
snprintf(num_args, NUM_ARGS_SIZE, "%x", argc - 1);
|
|
|
|
do_write(sd, num_args, strlen(num_args));
|
|
|
|
do_write(sd, eof, 1);
|
|
|
|
|
2013-07-31 22:15:35 +02:00
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
do_write(sd, argv[i], strlen(argv[i]));
|
|
|
|
do_write(sd, eof, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
wd = getcwd(NULL, 0);
|
|
|
|
if (wd != NULL) {
|
|
|
|
do_write(sd, wd, strlen(wd));
|
|
|
|
free(wd); wd = NULL;
|
|
|
|
}
|
|
|
|
|
2014-08-04 00:59:02 +02:00
|
|
|
for(envp=environ; *envp; envp++) {
|
|
|
|
do_write(sd, *envp, strlen(*envp));
|
|
|
|
do_write(sd, eof, 1);
|
|
|
|
}
|
|
|
|
|
2013-07-31 22:15:35 +02:00
|
|
|
do_write(sd, eof, 2);
|
|
|
|
|
2014-08-06 20:31:16 +02:00
|
|
|
read_size = -1;
|
|
|
|
while (read_size != 0) {
|
|
|
|
TEMP_FAILURE_RETRY(read_size, read(sd, buf, BUF_SIZE));
|
|
|
|
if (read_size == -1) {
|
2013-07-31 22:15:35 +02:00
|
|
|
close(sd);
|
2014-08-04 00:11:19 +02:00
|
|
|
HANDLE_ERROR("read() failed");
|
2014-08-06 20:31:16 +02:00
|
|
|
} else if (read_size > 0) {
|
2014-08-06 20:32:39 +02:00
|
|
|
do_write(STDOUT_FILENO, buf, (size_t) read_size);
|
2013-07-31 22:15:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(sd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|