static int hw_start_acquisition(int device_index, gpointer session_data) { struct sr_datafeed_packet *packet; struct sr_datafeed_header *header; struct databag *mydata; /* TODO: 'mydata' is never g_free()'d? */ if (!(mydata = g_try_malloc(sizeof(struct databag)))) { sr_err("demo: %s: mydata malloc failed", __func__); return SR_ERR_MALLOC; } mydata->sample_generator = default_pattern; mydata->session_data = session_data; mydata->device_index = device_index; mydata->samples_counter = 0; if (pipe(mydata->pipe_fds)) { /* TODO: Better error message. */ sr_err("demo: %s: pipe() failed", __func__); return SR_ERR; } channels[0] = g_io_channel_unix_new(mydata->pipe_fds[0]); channels[1] = g_io_channel_unix_new(mydata->pipe_fds[1]); /* Set channel encoding to binary (default is UTF-8). */ g_io_channel_set_encoding(channels[0], NULL, NULL); g_io_channel_set_encoding(channels[1], NULL, NULL); /* Make channels to unbuffered. */ g_io_channel_set_buffered(channels[0], FALSE); g_io_channel_set_buffered(channels[1], FALSE); sr_source_add(mydata->pipe_fds[0], G_IO_IN | G_IO_ERR, 40, receive_data, session_data); /* Run the demo thread. */ g_thread_init(NULL); /* This must to be done between g_thread_init() & g_thread_create(). */ mydata->timer = g_timer_new(); thread_running = 1; my_thread = g_thread_create((GThreadFunc)thread_func, mydata, TRUE, NULL); if (!my_thread) { sr_err("demo: %s: g_thread_create failed", __func__); return SR_ERR; /* TODO */ } if (!(packet = g_try_malloc(sizeof(struct sr_datafeed_packet)))) { sr_err("demo: %s: packet malloc failed", __func__); return SR_ERR_MALLOC; } if (!(header = g_try_malloc(sizeof(struct sr_datafeed_header)))) { sr_err("demo: %s: header malloc failed", __func__); return SR_ERR_MALLOC; } packet->type = SR_DF_HEADER; packet->payload = header; packet->timeoffset = 0; packet->duration = 0; header->feed_version = 1; gettimeofday(&header->starttime, NULL); header->samplerate = cur_samplerate; header->num_logic_probes = NUM_PROBES; header->num_analog_probes = 0; sr_session_bus(session_data, packet); g_free(header); g_free(packet); return SR_OK; }
static gboolean sock_get_address_info_async_cb(GIOChannel *source, GIOCondition condition, gpointer data) { SockLookupData *lookup_data = (SockLookupData *)data; GList *addr_list = NULL; SockAddrData *addr_data; gsize bytes_read; gint ai_member[4]; struct sockaddr *addr; gchar *canonical_name = NULL; gchar len = 0; GError *err = NULL; g_io_channel_set_encoding(source, NULL, &err); if (err) { g_warning("can unset encoding: %s\n", err->message); g_error_free(err); return FALSE; } g_io_channel_set_buffered(source, FALSE); if (g_io_channel_read_chars(source, &len, sizeof(len), &bytes_read, &err) == G_IO_STATUS_NORMAL) { if (err != NULL) { g_warning("g_io_channel_read_chars: %s\n", err->message); g_error_free(err); return FALSE; } if (bytes_read == sizeof(len) && len > 0) { gchar *cur = NULL; gint todo = len; canonical_name = g_malloc0(len + 1); cur = canonical_name; while (todo > 0) { if (g_io_channel_read_chars(source, cur, todo, &bytes_read, &err) != G_IO_STATUS_NORMAL) { if (err) { g_warning("canonical name not read %s\n", err->message); g_free(canonical_name); canonical_name = NULL; g_error_free(err); err = NULL; break; } } else { cur += bytes_read; todo -= bytes_read; } if (bytes_read == 0) { g_warning("canonical name not read\n"); g_free(canonical_name); canonical_name = NULL; break; } } } } for (;;) { if (g_io_channel_read_chars(source, (gchar *)ai_member, sizeof(ai_member), &bytes_read, &err) != G_IO_STATUS_NORMAL) { if (err != NULL) { g_warning("g_io_channel_read_chars: addr len %s\n", err->message); g_error_free(err); err = NULL; break; } } if (bytes_read == 0 || bytes_read != sizeof(ai_member)) break; if (ai_member[0] == AF_UNSPEC) { g_warning("DNS lookup failed\n"); log_error(LOG_PROTOCOL, _("%s:%d: unknown host.\n"), lookup_data->hostname, lookup_data->port); break; } addr = g_malloc(ai_member[3]); if (g_io_channel_read_chars(source, (gchar *)addr, ai_member[3], &bytes_read, &err) != G_IO_STATUS_NORMAL) { if (err != NULL) { g_warning("g_io_channel_read_chars: addr data read %s\n", err->message); g_error_free(err); err = NULL; g_free(addr); break; } } if (bytes_read != ai_member[3]) { g_warning("sock_get_address_info_async_cb: " "incomplete address data\n"); g_free(addr); break; } addr_data = g_new0(SockAddrData, 1); addr_data->family = ai_member[0]; addr_data->socktype = ai_member[1]; addr_data->protocol = ai_member[2]; addr_data->addr_len = ai_member[3]; addr_data->addr = addr; addr_list = g_list_append(addr_list, addr_data); } g_io_channel_shutdown(source, TRUE, &err); if (err) g_error_free(err); g_io_channel_unref(source); #ifdef G_OS_WIN32 /* FIXME: We would need to cancel the thread. */ #else kill(lookup_data->child_pid, SIGKILL); waitpid(lookup_data->child_pid, NULL, 0); #endif lookup_data->canonical_name = canonical_name; lookup_data->func(addr_list, lookup_data->data); g_free(lookup_data->canonical_name); g_free(lookup_data->hostname); g_free(lookup_data); return FALSE; }
GObex *g_obex_new(GIOChannel *io, GObexTransportType transport_type, gssize io_rx_mtu, gssize io_tx_mtu) { GObex *obex; GIOCondition cond; if (gobex_debug == 0) { const char *env = g_getenv("GOBEX_DEBUG"); if (env) { gobex_debug = g_parse_debug_string(env, keys, 7); g_setenv("G_MESSAGES_DEBUG", "gobex", FALSE); } else gobex_debug = G_OBEX_DEBUG_NONE; } g_obex_debug(G_OBEX_DEBUG_COMMAND, ""); if (io == NULL) return NULL; if (io_rx_mtu >= 0 && io_rx_mtu < G_OBEX_MINIMUM_MTU) return NULL; if (io_tx_mtu >= 0 && io_tx_mtu < G_OBEX_MINIMUM_MTU) return NULL; obex = g_new0(GObex, 1); obex->io = g_io_channel_ref(io); obex->ref_count = 1; obex->conn_id = CONNID_INVALID; obex->rx_last_op = G_OBEX_OP_NONE; obex->io_rx_mtu = io_rx_mtu; obex->io_tx_mtu = io_tx_mtu; if (io_rx_mtu > G_OBEX_MAXIMUM_MTU) obex->rx_mtu = G_OBEX_MAXIMUM_MTU; else if (io_rx_mtu < G_OBEX_MINIMUM_MTU) obex->rx_mtu = G_OBEX_DEFAULT_MTU; else obex->rx_mtu = io_rx_mtu; obex->tx_mtu = G_OBEX_MINIMUM_MTU; obex->tx_queue = g_queue_new(); obex->rx_buf = g_malloc(obex->rx_mtu); obex->tx_buf = g_malloc(obex->tx_mtu); switch (transport_type) { case G_OBEX_TRANSPORT_STREAM: obex->read = read_stream; obex->write = write_stream; break; case G_OBEX_TRANSPORT_PACKET: obex->use_srm = TRUE; obex->read = read_packet; obex->write = write_packet; break; default: g_obex_unref(obex); return NULL; } g_io_channel_set_encoding(io, NULL, NULL); g_io_channel_set_buffered(io, FALSE); cond = G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL; obex->io_source = g_io_add_watch(io, cond, incoming_data, obex); return obex; }
void process_run (const gchar *command, const gchar **envp, const gchar *path, guint64 max_time, GIOFunc io_callback, ProcessFinishCallback finish_callback, GCancellable *cancellable, gpointer user_data) { ProcessData *process_data; //struct termios term; struct winsize win = { .ws_col = 80, .ws_row = 24, .ws_xpixel = 480, .ws_ypixel = 192, }; process_data = g_slice_new0 (ProcessData); process_data->localwatchdog = FALSE; process_data->command = g_strsplit (command, " ", 0); process_data->path = path; process_data->max_time = max_time; process_data->io_callback = io_callback; process_data->finish_callback = finish_callback; process_data->user_data = user_data; process_data->io = NULL; process_data->cancellable = cancellable; if (fflush (stdout) != 0) g_warning ("Failed to flush stdout: %s\n", g_strerror (errno)); if (fflush (stderr) != 0) g_warning ("Failed to flush stderr: %s\n", g_strerror (errno)); process_data->pid = forkpty (&process_data->fd, NULL, NULL, &win); if (process_data->pid < 0) { /* Failed to fork */ g_set_error (&process_data->error, RESTRAINT_PROCESS_ERROR, RESTRAINT_PROCESS_FORK_ERROR, "Failed to fork: %s", g_strerror (errno)); g_idle_add (process_pid_finish, process_data); return; } else if (process_data->pid == 0) { /* Child process. */ // Flush any input that hasn't been read if (fflush (stdin) != 0) g_warning ("Failed to flush stdin: %s\n", g_strerror (errno)); setbuf (stdout, NULL); setbuf (stderr, NULL); if (process_data->path && (chdir (process_data->path) == -1)) { /* command_path was supplied and we failed to chdir to it. */ g_warning ("Failed to chdir() to %s: %s\n", process_data->path, g_strerror (errno)); exit (1); } if (envp) environ = (gchar **) envp; // Print the command being executed. gchar *pcommand = g_strjoinv (" ", (gchar **) process_data->command); g_print ("%s\n", pcommand); g_free (pcommand); /* Spawn the command */ if (execvp (*process_data->command, (gchar **) process_data->command) == -1) { g_warning ("Failed to exec() %s, %s error:%s\n", *process_data->command, process_data->path, g_strerror (errno)); exit (1); } } /* Parent process. */ // If we get the cancel signal kill any running process if (process_data->cancellable) { process_data->cancel_handler = g_cancellable_connect (process_data->cancellable, G_CALLBACK(process_cancelled_cb), process_data, NULL); } // close file descriptors on exec. Should prevent leaking fd's to child processes. if (fcntl (process_data->fd, F_SETFD, FD_CLOEXEC) < 0) { g_warning("Failed to set close on exec"); } // Localwatchdog handler if (process_data->max_time != 0) { process_data->timeout_handler_id = g_timeout_add_seconds_full (G_PRIORITY_DEFAULT, process_data->max_time, process_timeout_callback, process_data, NULL); } // IO handler if (io_callback != NULL) { GIOChannel *io = g_io_channel_unix_new (process_data->fd); g_io_channel_set_flags (io, G_IO_FLAG_NONBLOCK, NULL); // Set Encoding to NULL to keep g_io_channel from trying to decode it. g_io_channel_set_encoding (io, NULL, NULL); // Disable Buffering g_io_channel_set_buffered (io, FALSE); process_data->io = io; process_data->io_handler_id = g_io_add_watch_full (io, G_PRIORITY_DEFAULT, G_IO_IN | G_IO_HUP | G_IO_NVAL, process_io_cb, process_data, process_io_finish); } // Monitor pid for return code process_data->pid_handler_id = g_child_watch_add_full (G_PRIORITY_DEFAULT, process_data->pid, process_pid_callback, process_data, NULL); } void process_pid_callback (GPid pid, gint status, gpointer user_data) { ProcessData *process_data = (ProcessData *) user_data; process_data->pid_result = status; process_data->pid = 0; if (process_data->fd != -1 ) { close (process_data->fd); process_data->fd = -1; } if (process_data->finish_handler_id == 0) { process_data->finish_handler_id = g_idle_add (process_pid_finish, process_data); } }