moved socket query to ipc-client.c since it's a common resource, required for both sway and swaymsg

This commit is contained in:
mazunki 2022-03-18 04:56:57 +01:00
parent 08cd8ee5cb
commit fc783ba069
2 changed files with 41 additions and 35 deletions

View file

@ -13,6 +13,25 @@ static const char ipc_magic[] = {'i', '3', '-', 'i', 'p', 'c'};
#define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8) #define IPC_HEADER_SIZE (sizeof(ipc_magic) + 8)
int query_for_swaypid(char *xdg_runtime_dir) {
if (xdg_runtime_dir != NULL) {
char pidline[1024];
char *pid;
FILE *fp = popen("pidof sway", "r");
fgets(pidline, 1024, fp);
pid = strtok(pidline, " ");
int sway_pid = atoi(pid);
if (strtok(NULL, " ") != NULL) {
return -1;
} else {
return sway_pid;
}
} else {
exit(EXIT_FAILURE);
};
}
char *get_socketpath(void) { char *get_socketpath(void) {
const char *swaysock = getenv("SWAYSOCK"); const char *swaysock = getenv("SWAYSOCK");
if (swaysock) { if (swaysock) {
@ -46,10 +65,28 @@ char *get_socketpath(void) {
if (line[nret - 1] == '\n') { if (line[nret - 1] == '\n') {
line[nret - 1] = '\0'; line[nret - 1] = '\0';
} }
free(line);
return line; return line;
} }
} }
free(line); free(line);
if (getenv("XDG_RUNTIME_DIR") && getuid()) {
int swaypid = query_for_swaypid(getenv("XDG_RUNTIME_DIR"));
if (swaypid > 0) {
size_t path_sz = snprintf(NULL, 0, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid);
char *swaysock_runtime = malloc(path_sz+1);
snprintf(swaysock_runtime, path_sz+1, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid);
if (access(swaysock_runtime, F_OK) != -1) {
return strdup(swaysock_runtime);
} else {
fprintf(stderr, "sway socket not detected.\n"); // found a pid, but no socket file
}
} else {
fprintf(stderr, "Found more than one sway instance running.\n");
exit(EXIT_FAILURE);
}
}
return NULL; return NULL;
} }

View file

@ -87,24 +87,6 @@ void detect_proprietary(int allow_unsupported_gpu) {
fclose(f); fclose(f);
} }
int query_for_swaypid(char *xdg_runtime_dir) {
if (xdg_runtime_dir != NULL) {
char pidline[1024];
char *pid;
FILE *fp = popen("pidof sway", "r");
fgets(pidline, 1024, fp);
pid = strtok(pidline, " ");
int sway_pid = atoi(pid);
if (strtok(NULL, " ") != NULL) {
return -1;
} else {
return sway_pid;
}
} else {
exit(EXIT_FAILURE);
};
}
void run_as_ipc_client(char *command, char *socket_path) { void run_as_ipc_client(char *command, char *socket_path) {
int socketfd = ipc_open_socket(socket_path); int socketfd = ipc_open_socket(socket_path);
uint32_t len = strlen(command); uint32_t len = strlen(command);
@ -324,27 +306,14 @@ int main(int argc, char **argv) {
verbose = true; verbose = true;
break; break;
case 'p': ; // --get-socketpath case 'p': ; // --get-socketpath
if (getenv("SWAYSOCK")) { char *swaysock = get_socketpath();
printf("%s\n", getenv("SWAYSOCK")); if (swaysock != NULL) {
} else if (getenv("XDG_RUNTIME_DIR") && getuid()) { printf("%s\n", swaysock);
int swaypid = query_for_swaypid(getenv("XDG_RUNTIME_DIR")); exit(EXIT_SUCCESS);
char *swaysock = {0};
if (swaypid > 0) {
sprintf(swaysock, "%s/sway-ipc.%u.%i.sock", getenv("XDG_RUNTIME_DIR"), getuid(), swaypid);
if (access(swaysock, F_OK) != -1) {
printf("%s\n", swaysock);
} else {
fprintf(stderr, "sway socket not detected.\n"); // found a pid, but no socket file
}
} else {
fprintf(stderr, "Found more than one sway instance running.\n");
exit(EXIT_FAILURE);
}
} else { } else {
fprintf(stderr, "sway socket not detected.\n"); fprintf(stderr, "sway socket not detected.\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
break;
default: default:
fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usage);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);