Start work on everywhere prewarm

This commit is contained in:
Kovid Goyal 2022-06-07 20:37:55 +05:30
parent 132f87d152
commit 7a31c7ff50
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
3 changed files with 62 additions and 9 deletions

View File

@ -5,13 +5,9 @@
* Distributed under terms of the GPL3 license.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include "prewarm-launcher.h"
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#ifdef __APPLE__
#include <mach-o/dyld.h>
#include <sys/syslimits.h>
@ -19,9 +15,8 @@
#else
#include <limits.h>
#endif
#include <Python.h>
#include <wchar.h>
#include <stdbool.h>
#include <Python.h>
#ifndef KITTY_LIB_PATH
#define KITTY_LIB_PATH "../.."
@ -275,6 +270,7 @@ read_exe_path(char *exe, size_t buf_sz) {
int main(int argc, char *argv[]) {
if (argc < 1 || !argv) { fprintf(stderr, "Invalid argc/argv\n"); return 1; }
use_prewarmed_process(argc, argv);
char exe[PATH_MAX+1] = {0};
char exe_dir_buf[PATH_MAX+1] = {0};
FREE_AFTER_FUNCTION const char *lc_ctype = NULL;

57
prewarm-launcher.h Normal file
View File

@ -0,0 +1,57 @@
/*
* Copyright (C) 2022 Kovid Goyal <kovid at kovidgoyal.net>
*
* Distributed under terms of the GPL3 license.
*/
#pragma once
// needed for strnlen
#define _XOPEN_SOURCE 700
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <poll.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
static int
connect_to_socket_synchronously(const char *addr) {
struct sockaddr_un sock_addr = {.sun_family=AF_UNIX};
strncpy(sock_addr.sun_path, addr, sizeof(sock_addr.sun_path) - 1);
const size_t addrlen = strnlen(sock_addr.sun_path, sizeof(sock_addr.sun_path)) + sizeof(sock_addr.sun_family);
if (sock_addr.sun_path[0] == '@') sock_addr.sun_path[0] = 0;
int fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (connect(fd, (struct sockaddr*)&sock_addr, addrlen) != 0) {
if (errno != EINTR) return -1;
struct pollfd poll_data = {.fd=fd, .events=POLLOUT};
while (poll (&poll_data, 1, -1) == -1) { if (errno != EINTR) return -1; }
int socket_error_code = 0;
socklen_t sizeof_socket_error_code = sizeof(socket_error_code);
if (getsockopt (fd, SOL_SOCKET, SO_ERROR, &socket_error_code, &sizeof_socket_error_code) == -1) return -1;
if (socket_error_code != 0) return -1;
}
return fd;
}
static bool
is_prewarmable(int argc, char *argv[]) {
if (argc < 2) return false;
if (argv[1][0] != '+') return false;
if (argv[1][1] != 0) return strcmp(argv[1], "+open") != 0;
if (argc < 3) return false;
return strcmp(argv[2], "open") != 0;
}
static void
use_prewarmed_process(int argc, char *argv[]) {
const char *env_addr = getenv("KITTY_PREWARM_SOCKET_ADDRESS");
if (!env_addr || !*env_addr || !is_prewarmable(argc, argv)) return;
int fd = connect_to_socket_synchronously(env_addr);
if (fd < 0) return;
}

View File

@ -870,7 +870,7 @@ def build_launcher(args: Options, launcher_dir: str = '.', bundle_type: str = 's
src, '-o', dest] + ldflags + libs + pylib
key = CompileKey('launcher.c', 'kitty')
desc = f'Building {emphasis("launcher")} ...'
args.compilation_database.add_command(desc, cmd, partial(newer, dest, src), key=key, keyfile=src)
args.compilation_database.add_command(desc, cmd, partial(newer, dest, src, "prewarm-launcher.h"), key=key, keyfile=src)
args.compilation_database.build_all()