AristoChen / usb-proxy

A USB proxy based on raw-gadget and libusb
Apache License 2.0
152 stars 29 forks source link

Unable to exit normally #18

Closed wzw88486969 closed 8 months ago

wzw88486969 commented 9 months ago
int main(int argc, char **argv)
{
    const char *device = "dummy_udc.0";
    const char *driver = "dummy_udc";
    int vendor_id = -1;
    int product_id = -1;

    struct sigaction action;
    memset(&action, 0, sizeof(struct sigaction));
    action.sa_handler = handle_signal;
    sigaction(SIGTERM, &action, NULL);
    sigaction(SIGINT, &action, NULL);

    //........................................................
    printf("Device is: %s\n", device);
    printf("Driver is: %s\n", driver);
    printf("vendor_id is: %d\n", vendor_id);
    printf("product_id is: %d\n", product_id);

    if (injection_enabled) {
        printf("Injection enabled\n");
        if (injection_file.empty()) {
            printf("Injection file not specified\n");
            return 1;
        }
        struct stat buffer;
        if (stat(injection_file.c_str(), &buffer) != 0) {
            printf("Injection file %s not found\n", injection_file.c_str());
            return 1;
        }

        Json::Reader jsonReader;
        std::ifstream ifs(injection_file.c_str());
        if (jsonReader.parse(ifs, injection_config))
            printf("Parsed injection file: %s\n", injection_file.c_str());
        else {
            printf("Error parsing injection file: %s\n", injection_file.c_str());
            return 1;
        }
        ifs.close();
    }

    //.......................................................
    while (connect_device(vendor_id, product_id)) {
        sleep(1);
    }
    printf("Device opened successfully\n");

    setup_host_usb_desc();
    printf("Setup USB config successfully\n");

    int fd = usb_raw_open();
    usb_raw_init(fd, USB_SPEED_HIGH, driver, device);
    usb_raw_run(fd);

    ep0_loop(fd);

    close(fd);
    //---------here-1------------------------
    int bNumConfigurations = device_device_desc.bNumConfigurations;
    for (int i = 0; i < bNumConfigurations; i++) {
        int bNumInterfaces = device_config_desc[i]->bNumInterfaces;
        for (int j = 0; j < bNumInterfaces; j++) {
            int num_altsetting = device_config_desc[i]->interface[j].num_altsetting;
            for (int k = 0; k < num_altsetting; k++) {
                delete[] host_device_desc.configs[i].interfaces[j].altsettings[k].endpoints;
            }
            delete[] host_device_desc.configs[i].interfaces[j].altsettings;
        }
        delete[] host_device_desc.configs[i].interfaces;
    }
    //---------here-2------------------------
    delete[] host_device_desc.configs;
    delete[] device_config_desc;

    if (context && callback_handle != -1) {
        libusb_hotplug_deregister_callback(context, callback_handle);
    }
    if (hotplug_monitor_thread &&
        pthread_join(hotplug_monitor_thread, NULL)) {
        fprintf(stderr, "Error join hotplug_monitor_thread\n");
    }

    return 0;
}

Hello author: When sending a signal to the program through kill SIGINT, the program cannot exit normally and cannot reach the position of here-2 at all

xairy commented 9 months ago

I believe #15 fixes this.

wzw88486969 commented 8 months ago

@xairy thanks

AristoChen commented 8 months ago

Close as #15 is merged already, please reopen if still have this issue.