184369d76SAnthony Harivel /*
284369d76SAnthony Harivel * Privileged RAPL MSR helper commands for QEMU
384369d76SAnthony Harivel *
484369d76SAnthony Harivel * Copyright (C) 2024 Red Hat, Inc. <aharivel@redhat.com>
584369d76SAnthony Harivel *
684369d76SAnthony Harivel * Author: Anthony Harivel <aharivel@redhat.com>
784369d76SAnthony Harivel *
884369d76SAnthony Harivel * This program is free software; you can redistribute it and/or modify
984369d76SAnthony Harivel * it under the terms of the GNU General Public License as published by
1084369d76SAnthony Harivel * the Free Software Foundation; under version 2 of the License.
1184369d76SAnthony Harivel *
1284369d76SAnthony Harivel * This program is distributed in the hope that it will be useful,
1384369d76SAnthony Harivel * but WITHOUT ANY WARRANTY; without even the implied warranty of
1484369d76SAnthony Harivel * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1584369d76SAnthony Harivel * GNU General Public License for more details.
1684369d76SAnthony Harivel *
1784369d76SAnthony Harivel * You should have received a copy of the GNU General Public License
1884369d76SAnthony Harivel * along with this program; if not, see <http://www.gnu.org/licenses/>.
1984369d76SAnthony Harivel */
2084369d76SAnthony Harivel
2184369d76SAnthony Harivel #include "qemu/osdep.h"
2284369d76SAnthony Harivel #include <getopt.h>
2384369d76SAnthony Harivel #include <stdbool.h>
2484369d76SAnthony Harivel #include <sys/ioctl.h>
2584369d76SAnthony Harivel #ifdef CONFIG_LIBCAP_NG
2684369d76SAnthony Harivel #include <cap-ng.h>
2784369d76SAnthony Harivel #endif
2884369d76SAnthony Harivel #include <pwd.h>
2984369d76SAnthony Harivel #include <grp.h>
3084369d76SAnthony Harivel
3184369d76SAnthony Harivel #include "qemu/help-texts.h"
3284369d76SAnthony Harivel #include "qapi/error.h"
3384369d76SAnthony Harivel #include "qemu/cutils.h"
3484369d76SAnthony Harivel #include "qemu/main-loop.h"
3584369d76SAnthony Harivel #include "qemu/module.h"
3684369d76SAnthony Harivel #include "qemu/error-report.h"
3784369d76SAnthony Harivel #include "qemu/config-file.h"
3884369d76SAnthony Harivel #include "qemu-version.h"
3984369d76SAnthony Harivel #include "qapi/error.h"
4084369d76SAnthony Harivel #include "qemu/error-report.h"
4184369d76SAnthony Harivel #include "qemu/log.h"
4284369d76SAnthony Harivel #include "qemu/systemd.h"
4384369d76SAnthony Harivel #include "io/channel.h"
4484369d76SAnthony Harivel #include "io/channel-socket.h"
4584369d76SAnthony Harivel #include "trace/control.h"
4684369d76SAnthony Harivel #include "qemu-version.h"
4784369d76SAnthony Harivel #include "rapl-msr-index.h"
4884369d76SAnthony Harivel
4984369d76SAnthony Harivel #define MSR_PATH_TEMPLATE "/dev/cpu/%u/msr"
5084369d76SAnthony Harivel
5184369d76SAnthony Harivel static char *socket_path;
5284369d76SAnthony Harivel static char *pidfile;
5384369d76SAnthony Harivel static enum { RUNNING, TERMINATE, TERMINATING } state;
5484369d76SAnthony Harivel static QIOChannelSocket *server_ioc;
5584369d76SAnthony Harivel static int server_watch;
5684369d76SAnthony Harivel static int num_active_sockets = 1;
57*2a99c2baSPaolo Bonzini static bool verbose;
5884369d76SAnthony Harivel
5984369d76SAnthony Harivel #ifdef CONFIG_LIBCAP_NG
6084369d76SAnthony Harivel static int uid = -1;
6184369d76SAnthony Harivel static int gid = -1;
6284369d76SAnthony Harivel #endif
6384369d76SAnthony Harivel
compute_default_paths(void)6484369d76SAnthony Harivel static void compute_default_paths(void)
6584369d76SAnthony Harivel {
6684369d76SAnthony Harivel g_autofree char *state = qemu_get_local_state_dir();
6784369d76SAnthony Harivel
6884369d76SAnthony Harivel socket_path = g_build_filename(state, "run", "qemu-vmsr-helper.sock", NULL);
6984369d76SAnthony Harivel pidfile = g_build_filename(state, "run", "qemu-vmsr-helper.pid", NULL);
7084369d76SAnthony Harivel }
7184369d76SAnthony Harivel
is_intel_processor(void)7284369d76SAnthony Harivel static int is_intel_processor(void)
7384369d76SAnthony Harivel {
7484369d76SAnthony Harivel int result;
7584369d76SAnthony Harivel int ebx, ecx, edx;
7684369d76SAnthony Harivel
7784369d76SAnthony Harivel /* Execute CPUID instruction with eax=0 (basic identification) */
7884369d76SAnthony Harivel asm volatile (
7984369d76SAnthony Harivel "cpuid"
8084369d76SAnthony Harivel : "=b" (ebx), "=c" (ecx), "=d" (edx)
8184369d76SAnthony Harivel : "a" (0)
8284369d76SAnthony Harivel );
8384369d76SAnthony Harivel
8484369d76SAnthony Harivel /*
8584369d76SAnthony Harivel * Check if processor is "GenuineIntel"
8684369d76SAnthony Harivel * 0x756e6547 = "Genu"
8784369d76SAnthony Harivel * 0x49656e69 = "ineI"
8884369d76SAnthony Harivel * 0x6c65746e = "ntel"
8984369d76SAnthony Harivel */
9084369d76SAnthony Harivel result = (ebx == 0x756e6547) && (edx == 0x49656e69) && (ecx == 0x6c65746e);
9184369d76SAnthony Harivel
9284369d76SAnthony Harivel return result;
9384369d76SAnthony Harivel }
9484369d76SAnthony Harivel
is_rapl_enabled(void)9584369d76SAnthony Harivel static int is_rapl_enabled(void)
9684369d76SAnthony Harivel {
9784369d76SAnthony Harivel const char *path = "/sys/class/powercap/intel-rapl/enabled";
9884369d76SAnthony Harivel FILE *file = fopen(path, "r");
9984369d76SAnthony Harivel int value = 0;
10084369d76SAnthony Harivel
10184369d76SAnthony Harivel if (file != NULL) {
10284369d76SAnthony Harivel if (fscanf(file, "%d", &value) != 1) {
10384369d76SAnthony Harivel error_report("INTEL RAPL not enabled");
10484369d76SAnthony Harivel }
10584369d76SAnthony Harivel fclose(file);
10684369d76SAnthony Harivel } else {
10784369d76SAnthony Harivel error_report("Error opening %s", path);
10884369d76SAnthony Harivel }
10984369d76SAnthony Harivel
11084369d76SAnthony Harivel return value;
11184369d76SAnthony Harivel }
11284369d76SAnthony Harivel
11384369d76SAnthony Harivel /*
11484369d76SAnthony Harivel * Check if the TID that request the MSR read
11584369d76SAnthony Harivel * belongs to the peer. It be should a TID of a vCPU.
11684369d76SAnthony Harivel */
is_tid_present(pid_t pid,pid_t tid)11784369d76SAnthony Harivel static bool is_tid_present(pid_t pid, pid_t tid)
11884369d76SAnthony Harivel {
11984369d76SAnthony Harivel g_autofree char *tidPath = g_strdup_printf("/proc/%d/task/%d", pid, tid);
12084369d76SAnthony Harivel
12184369d76SAnthony Harivel /* Check if the TID directory exists within the PID directory */
12284369d76SAnthony Harivel if (access(tidPath, F_OK) == 0) {
12384369d76SAnthony Harivel return true;
12484369d76SAnthony Harivel }
12584369d76SAnthony Harivel
12684369d76SAnthony Harivel error_report("Failed to open /proc at %s", tidPath);
12784369d76SAnthony Harivel return false;
12884369d76SAnthony Harivel }
12984369d76SAnthony Harivel
13084369d76SAnthony Harivel /*
13184369d76SAnthony Harivel * Only the RAPL MSR in target/i386/cpu.h are allowed
13284369d76SAnthony Harivel */
is_msr_allowed(uint32_t reg)13384369d76SAnthony Harivel static bool is_msr_allowed(uint32_t reg)
13484369d76SAnthony Harivel {
13584369d76SAnthony Harivel switch (reg) {
13684369d76SAnthony Harivel case MSR_RAPL_POWER_UNIT:
13784369d76SAnthony Harivel case MSR_PKG_POWER_LIMIT:
13884369d76SAnthony Harivel case MSR_PKG_ENERGY_STATUS:
13984369d76SAnthony Harivel case MSR_PKG_POWER_INFO:
14084369d76SAnthony Harivel return true;
14184369d76SAnthony Harivel default:
14284369d76SAnthony Harivel return false;
14384369d76SAnthony Harivel }
14484369d76SAnthony Harivel }
14584369d76SAnthony Harivel
vmsr_read_msr(uint32_t msr_register,unsigned int cpu_id)14684369d76SAnthony Harivel static uint64_t vmsr_read_msr(uint32_t msr_register, unsigned int cpu_id)
14784369d76SAnthony Harivel {
14884369d76SAnthony Harivel int fd;
14984369d76SAnthony Harivel uint64_t result = 0;
15084369d76SAnthony Harivel
15184369d76SAnthony Harivel g_autofree char *path = g_strdup_printf(MSR_PATH_TEMPLATE, cpu_id);
15284369d76SAnthony Harivel
15384369d76SAnthony Harivel fd = open(path, O_RDONLY);
15484369d76SAnthony Harivel if (fd < 0) {
15584369d76SAnthony Harivel error_report("Failed to open MSR file at %s", path);
15684369d76SAnthony Harivel return result;
15784369d76SAnthony Harivel }
15884369d76SAnthony Harivel
15984369d76SAnthony Harivel if (pread(fd, &result, sizeof(result), msr_register) != sizeof(result)) {
16084369d76SAnthony Harivel error_report("Failed to read MSR");
16184369d76SAnthony Harivel result = 0;
16284369d76SAnthony Harivel }
16384369d76SAnthony Harivel
16484369d76SAnthony Harivel close(fd);
16584369d76SAnthony Harivel return result;
16684369d76SAnthony Harivel }
16784369d76SAnthony Harivel
usage(const char * name)16884369d76SAnthony Harivel static void usage(const char *name)
16984369d76SAnthony Harivel {
17084369d76SAnthony Harivel (printf) (
17184369d76SAnthony Harivel "Usage: %s [OPTIONS] FILE\n"
17284369d76SAnthony Harivel "Virtual RAPL MSR helper program for QEMU\n"
17384369d76SAnthony Harivel "\n"
17484369d76SAnthony Harivel " -h, --help display this help and exit\n"
17584369d76SAnthony Harivel " -V, --version output version information and exit\n"
17684369d76SAnthony Harivel "\n"
17784369d76SAnthony Harivel " -d, --daemon run in the background\n"
17884369d76SAnthony Harivel " -f, --pidfile=PATH PID file when running as a daemon\n"
17984369d76SAnthony Harivel " (default '%s')\n"
18084369d76SAnthony Harivel " -k, --socket=PATH path to the unix socket\n"
18184369d76SAnthony Harivel " (default '%s')\n"
18284369d76SAnthony Harivel " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
18384369d76SAnthony Harivel " specify tracing options\n"
18484369d76SAnthony Harivel #ifdef CONFIG_LIBCAP_NG
18584369d76SAnthony Harivel " -u, --user=USER user to drop privileges to\n"
18684369d76SAnthony Harivel " -g, --group=GROUP group to drop privileges to\n"
18784369d76SAnthony Harivel #endif
18884369d76SAnthony Harivel "\n"
18984369d76SAnthony Harivel QEMU_HELP_BOTTOM "\n"
19084369d76SAnthony Harivel , name, pidfile, socket_path);
19184369d76SAnthony Harivel }
19284369d76SAnthony Harivel
version(const char * name)19384369d76SAnthony Harivel static void version(const char *name)
19484369d76SAnthony Harivel {
19584369d76SAnthony Harivel printf(
19684369d76SAnthony Harivel "%s " QEMU_FULL_VERSION "\n"
19784369d76SAnthony Harivel "Written by Anthony Harivel.\n"
19884369d76SAnthony Harivel "\n"
19984369d76SAnthony Harivel QEMU_COPYRIGHT "\n"
20084369d76SAnthony Harivel "This is free software; see the source for copying conditions. There is NO\n"
20184369d76SAnthony Harivel "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
20284369d76SAnthony Harivel , name);
20384369d76SAnthony Harivel }
20484369d76SAnthony Harivel
20584369d76SAnthony Harivel typedef struct VMSRHelperClient {
20684369d76SAnthony Harivel QIOChannelSocket *ioc;
20784369d76SAnthony Harivel Coroutine *co;
20884369d76SAnthony Harivel } VMSRHelperClient;
20984369d76SAnthony Harivel
vh_co_entry(void * opaque)21084369d76SAnthony Harivel static void coroutine_fn vh_co_entry(void *opaque)
21184369d76SAnthony Harivel {
21284369d76SAnthony Harivel VMSRHelperClient *client = opaque;
21384369d76SAnthony Harivel Error *local_err = NULL;
21484369d76SAnthony Harivel unsigned int peer_pid;
21584369d76SAnthony Harivel uint32_t request[3];
21684369d76SAnthony Harivel uint64_t vmsr;
21784369d76SAnthony Harivel int r;
21884369d76SAnthony Harivel
21984369d76SAnthony Harivel qio_channel_set_blocking(QIO_CHANNEL(client->ioc),
22084369d76SAnthony Harivel false, NULL);
22184369d76SAnthony Harivel
22284369d76SAnthony Harivel qio_channel_set_follow_coroutine_ctx(QIO_CHANNEL(client->ioc), true);
22384369d76SAnthony Harivel
22484369d76SAnthony Harivel /*
22584369d76SAnthony Harivel * Check peer credentials
22684369d76SAnthony Harivel */
22784369d76SAnthony Harivel r = qio_channel_get_peerpid(QIO_CHANNEL(client->ioc),
22884369d76SAnthony Harivel &peer_pid,
22984369d76SAnthony Harivel &local_err);
23084369d76SAnthony Harivel if (r < 0) {
23184369d76SAnthony Harivel goto out;
23284369d76SAnthony Harivel }
23384369d76SAnthony Harivel
234768a2839SPaolo Bonzini for (;;) {
23584369d76SAnthony Harivel /*
23684369d76SAnthony Harivel * Read the requested MSR
23784369d76SAnthony Harivel * Only RAPL MSR in rapl-msr-index.h is allowed
23884369d76SAnthony Harivel */
239768a2839SPaolo Bonzini r = qio_channel_read_all_eof(QIO_CHANNEL(client->ioc),
24084369d76SAnthony Harivel (char *) &request, sizeof(request), &local_err);
241768a2839SPaolo Bonzini if (r <= 0) {
24284369d76SAnthony Harivel break;
24384369d76SAnthony Harivel }
24484369d76SAnthony Harivel
24584369d76SAnthony Harivel if (!is_msr_allowed(request[0])) {
24684369d76SAnthony Harivel error_report("Requested unallowed msr: %d", request[0]);
24784369d76SAnthony Harivel break;
24884369d76SAnthony Harivel }
24984369d76SAnthony Harivel
25084369d76SAnthony Harivel vmsr = vmsr_read_msr(request[0], request[1]);
25184369d76SAnthony Harivel
25284369d76SAnthony Harivel if (!is_tid_present(peer_pid, request[2])) {
25384369d76SAnthony Harivel error_report("Requested TID not in peer PID: %d %d",
25484369d76SAnthony Harivel peer_pid, request[2]);
25584369d76SAnthony Harivel vmsr = 0;
25684369d76SAnthony Harivel }
25784369d76SAnthony Harivel
25884369d76SAnthony Harivel r = qio_channel_write_all(QIO_CHANNEL(client->ioc),
25984369d76SAnthony Harivel (char *) &vmsr,
26084369d76SAnthony Harivel sizeof(vmsr),
26184369d76SAnthony Harivel &local_err);
26284369d76SAnthony Harivel if (r < 0) {
26384369d76SAnthony Harivel break;
26484369d76SAnthony Harivel }
26584369d76SAnthony Harivel }
266768a2839SPaolo Bonzini
26784369d76SAnthony Harivel out:
268768a2839SPaolo Bonzini if (local_err) {
269*2a99c2baSPaolo Bonzini if (!verbose) {
270*2a99c2baSPaolo Bonzini error_free(local_err);
271*2a99c2baSPaolo Bonzini } else {
272768a2839SPaolo Bonzini error_report_err(local_err);
273768a2839SPaolo Bonzini }
274*2a99c2baSPaolo Bonzini }
275768a2839SPaolo Bonzini
27684369d76SAnthony Harivel object_unref(OBJECT(client->ioc));
27784369d76SAnthony Harivel g_free(client);
27884369d76SAnthony Harivel }
27984369d76SAnthony Harivel
accept_client(QIOChannel * ioc,GIOCondition cond,gpointer opaque)28084369d76SAnthony Harivel static gboolean accept_client(QIOChannel *ioc,
28184369d76SAnthony Harivel GIOCondition cond,
28284369d76SAnthony Harivel gpointer opaque)
28384369d76SAnthony Harivel {
28484369d76SAnthony Harivel QIOChannelSocket *cioc;
28584369d76SAnthony Harivel VMSRHelperClient *vmsrh;
28684369d76SAnthony Harivel
28784369d76SAnthony Harivel cioc = qio_channel_socket_accept(QIO_CHANNEL_SOCKET(ioc),
28884369d76SAnthony Harivel NULL);
28984369d76SAnthony Harivel if (!cioc) {
29084369d76SAnthony Harivel return TRUE;
29184369d76SAnthony Harivel }
29284369d76SAnthony Harivel
29384369d76SAnthony Harivel vmsrh = g_new(VMSRHelperClient, 1);
29484369d76SAnthony Harivel vmsrh->ioc = cioc;
29584369d76SAnthony Harivel vmsrh->co = qemu_coroutine_create(vh_co_entry, vmsrh);
29684369d76SAnthony Harivel qemu_coroutine_enter(vmsrh->co);
29784369d76SAnthony Harivel
29884369d76SAnthony Harivel return TRUE;
29984369d76SAnthony Harivel }
30084369d76SAnthony Harivel
termsig_handler(int signum)30184369d76SAnthony Harivel static void termsig_handler(int signum)
30284369d76SAnthony Harivel {
30384369d76SAnthony Harivel qatomic_cmpxchg(&state, RUNNING, TERMINATE);
30484369d76SAnthony Harivel qemu_notify_event();
30584369d76SAnthony Harivel }
30684369d76SAnthony Harivel
close_server_socket(void)30784369d76SAnthony Harivel static void close_server_socket(void)
30884369d76SAnthony Harivel {
30984369d76SAnthony Harivel assert(server_ioc);
31084369d76SAnthony Harivel
31184369d76SAnthony Harivel g_source_remove(server_watch);
31284369d76SAnthony Harivel server_watch = -1;
31384369d76SAnthony Harivel object_unref(OBJECT(server_ioc));
31484369d76SAnthony Harivel num_active_sockets--;
31584369d76SAnthony Harivel }
31684369d76SAnthony Harivel
31784369d76SAnthony Harivel #ifdef CONFIG_LIBCAP_NG
drop_privileges(void)31884369d76SAnthony Harivel static int drop_privileges(void)
31984369d76SAnthony Harivel {
32084369d76SAnthony Harivel /* clear all capabilities */
32184369d76SAnthony Harivel capng_clear(CAPNG_SELECT_BOTH);
32284369d76SAnthony Harivel
32384369d76SAnthony Harivel if (capng_update(CAPNG_ADD, CAPNG_EFFECTIVE | CAPNG_PERMITTED,
32484369d76SAnthony Harivel CAP_SYS_RAWIO) < 0) {
32584369d76SAnthony Harivel return -1;
32684369d76SAnthony Harivel }
32784369d76SAnthony Harivel
32884369d76SAnthony Harivel return 0;
32984369d76SAnthony Harivel }
33084369d76SAnthony Harivel #endif
33184369d76SAnthony Harivel
main(int argc,char ** argv)33284369d76SAnthony Harivel int main(int argc, char **argv)
33384369d76SAnthony Harivel {
33484369d76SAnthony Harivel const char *sopt = "hVk:f:dT:u:g:vq";
33584369d76SAnthony Harivel struct option lopt[] = {
33684369d76SAnthony Harivel { "help", no_argument, NULL, 'h' },
33784369d76SAnthony Harivel { "version", no_argument, NULL, 'V' },
33884369d76SAnthony Harivel { "socket", required_argument, NULL, 'k' },
33984369d76SAnthony Harivel { "pidfile", required_argument, NULL, 'f' },
34084369d76SAnthony Harivel { "daemon", no_argument, NULL, 'd' },
34184369d76SAnthony Harivel { "trace", required_argument, NULL, 'T' },
34284369d76SAnthony Harivel { "verbose", no_argument, NULL, 'v' },
34384369d76SAnthony Harivel { NULL, 0, NULL, 0 }
34484369d76SAnthony Harivel };
34584369d76SAnthony Harivel int opt_ind = 0;
34684369d76SAnthony Harivel int ch;
34784369d76SAnthony Harivel Error *local_err = NULL;
34884369d76SAnthony Harivel bool daemonize = false;
34984369d76SAnthony Harivel bool pidfile_specified = false;
35084369d76SAnthony Harivel bool socket_path_specified = false;
35184369d76SAnthony Harivel unsigned socket_activation;
35284369d76SAnthony Harivel
35384369d76SAnthony Harivel struct sigaction sa_sigterm;
35484369d76SAnthony Harivel memset(&sa_sigterm, 0, sizeof(sa_sigterm));
35584369d76SAnthony Harivel sa_sigterm.sa_handler = termsig_handler;
35684369d76SAnthony Harivel sigaction(SIGTERM, &sa_sigterm, NULL);
35784369d76SAnthony Harivel sigaction(SIGINT, &sa_sigterm, NULL);
35884369d76SAnthony Harivel sigaction(SIGHUP, &sa_sigterm, NULL);
35984369d76SAnthony Harivel
36084369d76SAnthony Harivel signal(SIGPIPE, SIG_IGN);
36184369d76SAnthony Harivel
36284369d76SAnthony Harivel error_init(argv[0]);
36384369d76SAnthony Harivel module_call_init(MODULE_INIT_TRACE);
36484369d76SAnthony Harivel module_call_init(MODULE_INIT_QOM);
36584369d76SAnthony Harivel qemu_add_opts(&qemu_trace_opts);
36684369d76SAnthony Harivel qemu_init_exec_dir(argv[0]);
36784369d76SAnthony Harivel
36884369d76SAnthony Harivel compute_default_paths();
36984369d76SAnthony Harivel
37084369d76SAnthony Harivel /*
37184369d76SAnthony Harivel * Sanity check
37284369d76SAnthony Harivel * 1. cpu must be Intel cpu
37384369d76SAnthony Harivel * 2. RAPL must be enabled
37484369d76SAnthony Harivel */
37584369d76SAnthony Harivel if (!is_intel_processor()) {
37684369d76SAnthony Harivel error_report("error: CPU is not INTEL cpu");
37784369d76SAnthony Harivel exit(EXIT_FAILURE);
37884369d76SAnthony Harivel }
37984369d76SAnthony Harivel
38084369d76SAnthony Harivel if (!is_rapl_enabled()) {
38184369d76SAnthony Harivel error_report("error: RAPL driver not enable");
38284369d76SAnthony Harivel exit(EXIT_FAILURE);
38384369d76SAnthony Harivel }
38484369d76SAnthony Harivel
38584369d76SAnthony Harivel while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
38684369d76SAnthony Harivel switch (ch) {
38784369d76SAnthony Harivel case 'k':
38884369d76SAnthony Harivel g_free(socket_path);
38984369d76SAnthony Harivel socket_path = g_strdup(optarg);
39084369d76SAnthony Harivel socket_path_specified = true;
39184369d76SAnthony Harivel if (socket_path[0] != '/') {
39284369d76SAnthony Harivel error_report("socket path must be absolute");
39384369d76SAnthony Harivel exit(EXIT_FAILURE);
39484369d76SAnthony Harivel }
39584369d76SAnthony Harivel break;
39684369d76SAnthony Harivel case 'f':
39784369d76SAnthony Harivel g_free(pidfile);
39884369d76SAnthony Harivel pidfile = g_strdup(optarg);
39984369d76SAnthony Harivel pidfile_specified = true;
40084369d76SAnthony Harivel break;
40184369d76SAnthony Harivel #ifdef CONFIG_LIBCAP_NG
40284369d76SAnthony Harivel case 'u': {
40384369d76SAnthony Harivel unsigned long res;
40484369d76SAnthony Harivel struct passwd *userinfo = getpwnam(optarg);
40584369d76SAnthony Harivel if (userinfo) {
40684369d76SAnthony Harivel uid = userinfo->pw_uid;
40784369d76SAnthony Harivel } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 &&
40884369d76SAnthony Harivel (uid_t)res == res) {
40984369d76SAnthony Harivel uid = res;
41084369d76SAnthony Harivel } else {
41184369d76SAnthony Harivel error_report("invalid user '%s'", optarg);
41284369d76SAnthony Harivel exit(EXIT_FAILURE);
41384369d76SAnthony Harivel }
41484369d76SAnthony Harivel break;
41584369d76SAnthony Harivel }
41684369d76SAnthony Harivel case 'g': {
41784369d76SAnthony Harivel unsigned long res;
41884369d76SAnthony Harivel struct group *groupinfo = getgrnam(optarg);
41984369d76SAnthony Harivel if (groupinfo) {
42084369d76SAnthony Harivel gid = groupinfo->gr_gid;
42184369d76SAnthony Harivel } else if (qemu_strtoul(optarg, NULL, 10, &res) == 0 &&
42284369d76SAnthony Harivel (gid_t)res == res) {
42384369d76SAnthony Harivel gid = res;
42484369d76SAnthony Harivel } else {
42584369d76SAnthony Harivel error_report("invalid group '%s'", optarg);
42684369d76SAnthony Harivel exit(EXIT_FAILURE);
42784369d76SAnthony Harivel }
42884369d76SAnthony Harivel break;
42984369d76SAnthony Harivel }
43084369d76SAnthony Harivel #else
43184369d76SAnthony Harivel case 'u':
43284369d76SAnthony Harivel case 'g':
43384369d76SAnthony Harivel error_report("-%c not supported by this %s", ch, argv[0]);
43484369d76SAnthony Harivel exit(1);
43584369d76SAnthony Harivel #endif
43684369d76SAnthony Harivel case 'd':
43784369d76SAnthony Harivel daemonize = true;
43884369d76SAnthony Harivel break;
439*2a99c2baSPaolo Bonzini case 'v':
440*2a99c2baSPaolo Bonzini verbose = true;
441*2a99c2baSPaolo Bonzini break;
44284369d76SAnthony Harivel case 'T':
44384369d76SAnthony Harivel trace_opt_parse(optarg);
44484369d76SAnthony Harivel break;
44584369d76SAnthony Harivel case 'V':
44684369d76SAnthony Harivel version(argv[0]);
44784369d76SAnthony Harivel exit(EXIT_SUCCESS);
44884369d76SAnthony Harivel break;
44984369d76SAnthony Harivel case 'h':
45084369d76SAnthony Harivel usage(argv[0]);
45184369d76SAnthony Harivel exit(EXIT_SUCCESS);
45284369d76SAnthony Harivel break;
45384369d76SAnthony Harivel case '?':
45484369d76SAnthony Harivel error_report("Try `%s --help' for more information.", argv[0]);
45584369d76SAnthony Harivel exit(EXIT_FAILURE);
45684369d76SAnthony Harivel }
45784369d76SAnthony Harivel }
45884369d76SAnthony Harivel
45984369d76SAnthony Harivel if (!trace_init_backends()) {
46084369d76SAnthony Harivel exit(EXIT_FAILURE);
46184369d76SAnthony Harivel }
46284369d76SAnthony Harivel trace_init_file();
46384369d76SAnthony Harivel qemu_set_log(LOG_TRACE, &error_fatal);
46484369d76SAnthony Harivel
46584369d76SAnthony Harivel socket_activation = check_socket_activation();
46684369d76SAnthony Harivel if (socket_activation == 0) {
46784369d76SAnthony Harivel SocketAddress saddr;
46884369d76SAnthony Harivel saddr = (SocketAddress){
46984369d76SAnthony Harivel .type = SOCKET_ADDRESS_TYPE_UNIX,
47084369d76SAnthony Harivel .u.q_unix.path = socket_path,
47184369d76SAnthony Harivel };
47284369d76SAnthony Harivel server_ioc = qio_channel_socket_new();
47384369d76SAnthony Harivel if (qio_channel_socket_listen_sync(server_ioc, &saddr,
47484369d76SAnthony Harivel 1, &local_err) < 0) {
47584369d76SAnthony Harivel object_unref(OBJECT(server_ioc));
47684369d76SAnthony Harivel error_report_err(local_err);
47784369d76SAnthony Harivel return 1;
47884369d76SAnthony Harivel }
47984369d76SAnthony Harivel } else {
48084369d76SAnthony Harivel /* Using socket activation - check user didn't use -p etc. */
48184369d76SAnthony Harivel if (socket_path_specified) {
48284369d76SAnthony Harivel error_report("Unix socket can't be set when"
48384369d76SAnthony Harivel "using socket activation");
48484369d76SAnthony Harivel exit(EXIT_FAILURE);
48584369d76SAnthony Harivel }
48684369d76SAnthony Harivel
48784369d76SAnthony Harivel /* Can only listen on a single socket. */
48884369d76SAnthony Harivel if (socket_activation > 1) {
48984369d76SAnthony Harivel error_report("%s does not support socket activation"
49084369d76SAnthony Harivel "with LISTEN_FDS > 1",
49184369d76SAnthony Harivel argv[0]);
49284369d76SAnthony Harivel exit(EXIT_FAILURE);
49384369d76SAnthony Harivel }
49484369d76SAnthony Harivel server_ioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD,
49584369d76SAnthony Harivel &local_err);
49684369d76SAnthony Harivel if (server_ioc == NULL) {
49784369d76SAnthony Harivel error_reportf_err(local_err,
49884369d76SAnthony Harivel "Failed to use socket activation: ");
49984369d76SAnthony Harivel exit(EXIT_FAILURE);
50084369d76SAnthony Harivel }
50184369d76SAnthony Harivel }
50284369d76SAnthony Harivel
50384369d76SAnthony Harivel qemu_init_main_loop(&error_fatal);
50484369d76SAnthony Harivel
50584369d76SAnthony Harivel server_watch = qio_channel_add_watch(QIO_CHANNEL(server_ioc),
50684369d76SAnthony Harivel G_IO_IN,
50784369d76SAnthony Harivel accept_client,
50884369d76SAnthony Harivel NULL, NULL);
50984369d76SAnthony Harivel
51084369d76SAnthony Harivel if (daemonize) {
51184369d76SAnthony Harivel if (daemon(0, 0) < 0) {
51284369d76SAnthony Harivel error_report("Failed to daemonize: %s", strerror(errno));
51384369d76SAnthony Harivel exit(EXIT_FAILURE);
51484369d76SAnthony Harivel }
51584369d76SAnthony Harivel }
51684369d76SAnthony Harivel
51784369d76SAnthony Harivel if (daemonize || pidfile_specified) {
51884369d76SAnthony Harivel qemu_write_pidfile(pidfile, &error_fatal);
51984369d76SAnthony Harivel }
52084369d76SAnthony Harivel
52184369d76SAnthony Harivel #ifdef CONFIG_LIBCAP_NG
52284369d76SAnthony Harivel if (drop_privileges() < 0) {
52384369d76SAnthony Harivel error_report("Failed to drop privileges: %s", strerror(errno));
52484369d76SAnthony Harivel exit(EXIT_FAILURE);
52584369d76SAnthony Harivel }
52684369d76SAnthony Harivel #endif
52784369d76SAnthony Harivel
52884369d76SAnthony Harivel info_report("Listening on %s", socket_path);
52984369d76SAnthony Harivel
53084369d76SAnthony Harivel state = RUNNING;
53184369d76SAnthony Harivel do {
53284369d76SAnthony Harivel main_loop_wait(false);
53384369d76SAnthony Harivel if (state == TERMINATE) {
53484369d76SAnthony Harivel state = TERMINATING;
53584369d76SAnthony Harivel close_server_socket();
53684369d76SAnthony Harivel }
53784369d76SAnthony Harivel } while (num_active_sockets > 0);
53884369d76SAnthony Harivel
53984369d76SAnthony Harivel exit(EXIT_SUCCESS);
54084369d76SAnthony Harivel }
541