@@ -445,6 +445,7 @@ type function_usage = {
445445 mutable uses_attach : bool ;
446446 mutable uses_detach : bool ;
447447 mutable uses_map_operations : bool ;
448+ mutable uses_daemon : bool ;
448449 mutable used_maps : string list ;
449450 mutable used_dispatch_functions : int list ;
450451}
@@ -454,6 +455,7 @@ let create_function_usage () = {
454455 uses_attach = false ;
455456 uses_detach = false ;
456457 uses_map_operations = false ;
458+ uses_daemon = false ;
457459 used_maps = [] ;
458460 used_dispatch_functions = [] ;
459461}
@@ -725,6 +727,7 @@ let track_function_usage ctx instr =
725727 | "load" -> ctx.function_usage.uses_load < - true
726728 | "attach" -> ctx.function_usage.uses_attach < - true
727729 | "detach" -> ctx.function_usage.uses_detach < - true
730+ | "daemon" -> ctx.function_usage.uses_daemon < - true
728731 | "dispatch" ->
729732 let num_buffers = List. length args in
730733 if not (List. mem num_buffers ctx.function_usage.used_dispatch_functions) then
@@ -2915,6 +2918,7 @@ let generate_complete_userspace_program_from_ir ?(config_declarations = []) ?(ty
29152918 uses_attach = acc_usage.uses_attach || func_usage.uses_attach;
29162919 uses_detach = acc_usage.uses_detach || func_usage.uses_detach;
29172920 uses_map_operations = acc_usage.uses_map_operations || func_usage.uses_map_operations;
2921+ uses_daemon = acc_usage.uses_daemon || func_usage.uses_daemon;
29182922 used_maps = List. fold_left (fun acc map_name ->
29192923 if List. mem map_name acc then acc else map_name :: acc
29202924 ) acc_usage.used_maps func_usage.used_maps;
@@ -3537,12 +3541,56 @@ static int add_attachment(int prog_fd, const char *target, uint32_t flags,
35373541
35383542 let bpf_obj_decl = "" in (* Skeleton now handles the BPF object *)
35393543
3540- let functions_list = List.filter (fun s -> s <> "") [attachment_storage; load_function; attach_function; detach_function] in
3544+ (* Generate daemon function if used *)
3545+ let daemon_function = if all_usage.uses_daemon then
3546+ sprintf {|void daemon_builtin(void) {
3547+ // Standard Unix daemon process
3548+ if (daemon(0, 0) != 0) {
3549+ perror("daemon");
3550+ exit(1);
3551+ }
3552+
3553+ // Setup daemon infrastructure
3554+ signal(SIGTERM, handle_signal);
3555+ signal(SIGINT, handle_signal);
3556+ signal(SIGHUP, SIG_IGN);
3557+
3558+ // Create PID file
3559+ FILE *pidfile = fopen("/var/run/%s.pid", "w");
3560+ if (pidfile) {
3561+ fprintf(pidfile, "%%d\n", getpid());
3562+ fclose(pidfile);
3563+ }
3564+
3565+ // Daemon main loop - never returns
3566+ while (keep_running) {
3567+ sleep(1);
3568+ }
3569+
3570+ // Cleanup and exit
3571+ unlink("/var/run/%s.pid");
3572+ exit(0);
3573+ }|} base_name base_name
3574+ else "" in
3575+
3576+ let functions_list = List.filter (fun s -> s <> "") [attachment_storage; load_function; attach_function; detach_function; daemon_function] in
35413577 if functions_list = [] && bpf_obj_decl = "" then ""
35423578 else
35433579 sprintf "\n/* BPF Helper Functions (generated only when used) */\n%s\n\n%s"
35443580 bpf_obj_decl (String.concat "\n\n" functions_list) in
35453581
3582+ (* Generate daemon signal handling variables if used *)
3583+ let daemon_globals = if all_usage.uses_daemon then
3584+ sprintf {|
3585+ // Daemon signal handling
3586+ static volatile sig_atomic_t keep_running = 1;
3587+
3588+ static void handle_signal(int sig) {
3589+ keep_running = 0;
3590+ }
3591+ |}
3592+ else "" in
3593+
35463594 (* Generate struct_ops attach functions *)
35473595 let struct_ops_attach_functions = generate_struct_ops_attach_functions ir_multi_prog in
35483596
@@ -3552,6 +3600,7 @@ static int add_attachment(int prog_fd, const char *target, uint32_t flags,
35523600
35533601%s
35543602
3603+ %s
35553604%s
35563605
35573606%s
@@ -3575,7 +3624,7 @@ static int add_attachment(int prog_fd, const char *target, uint32_t flags,
35753624%s
35763625
35773626%s
3578- |} includes string_typedefs type_alias_definitions string_helpers enum_definitions structs_with_pinned skeleton_code all_fd_declarations map_operation_functions ringbuf_handlers ringbuf_dispatch_functions auto_bpf_init_code getopt_parsing_code bpf_helper_functions struct_ops_attach_functions functions
3627+ |} includes string_typedefs type_alias_definitions string_helpers daemon_globals enum_definitions structs_with_pinned skeleton_code all_fd_declarations map_operation_functions ringbuf_handlers ringbuf_dispatch_functions auto_bpf_init_code getopt_parsing_code bpf_helper_functions struct_ops_attach_functions functions
35793628
35803629(* * Generate userspace C code from IR multi-program *)
35813630let generate_userspace_code_from_ir ?(config_declarations = []) ?(type_aliases = []) ?(tail_call_analysis = {Tail_call_analyzer.dependencies = []; prog_array_size = 0; index_mapping = Hashtbl.create 16; errors = []}) ?(kfunc_dependencies = {kfunc_definitions = []; private_functions = []; program_dependencies = []; module_name = ""}) ?(resolved_imports = []) ?symbol_table ?btf_path (ir_multi_prog : ir_multi_program) ?(output_dir = ".") source_filename =
0 commit comments