-
-/** Describes a command of para_server. */
-struct command {
- /** The name of the command. */
- const char *name;
- /** The handler function. */
- int (*handler)(int fd, int argc, const char **argv);
-};
-
-static struct command afs_cmds[] = {
-{
- .name = "add",
- .handler = com_add,
-},
-{
- .name = "addlyr",
- .handler = com_addlyr,
-},
-{
- .name = "addimg",
- .handler = com_addimg,
-},
-{
- .name = "addmood",
- .handler = com_addmood,
-},
-{
- .name = "addpl",
- .handler = com_addpl,
-},
-{
- .name = "catlyr",
- .handler = com_catlyr,
-},
-{
- .name = "catimg",
- .handler = com_catimg,
-},
-{
- .name = "mvimg",
- .handler = com_mvimg,
-},
-{
- .name = "mvlyr",
- .handler = com_mvlyr,
-},
-{
- .name = "mvmood",
- .handler = com_mvmood,
-},
-{
- .name = "mvpl",
- .handler = com_mvpl,
-},
-{
- .name = "catmood",
- .handler = com_catmood,
-},
-{
- .name = "catpl",
- .handler = com_catpl,
-},
-{
- .name = "rmatt",
- .handler = com_rmatt,
-},
-{
- .name = "init",
- .handler = com_init,
-},
-{
- .name = "lsatt",
- .handler = com_lsatt,
-},
-{
- .name = "ls",
- .handler = com_afs_ls,
-},
-{
- .name = "lslyr",
- .handler = com_lslyr,
-},
-{
- .name = "lsimg",
- .handler = com_lsimg,
-},
-{
- .name = "lsmood",
- .handler = com_lsmood,
-},
-{
- .name = "lspl",
- .handler = com_lspl,
-},
-{
- .name = "setatt",
- .handler = com_setatt,
-},
-{
- .name = "addatt",
- .handler = com_addatt,
-},
-{
- .name = "rm",
- .handler = com_afs_rm,
-},
-{
- .name = "rmlyr",
- .handler = com_rmlyr,
-},
-{
- .name = "rmimg",
- .handler = com_rmimg,
-},
-{
- .name = "rmmood",
- .handler = com_rmmood,
-},
-{
- .name = "rmpl",
- .handler = com_rmpl,
-},
-{
- .name = "touch",
- .handler = com_touch,
-},
-{
- .name = NULL,
-}
-};
-
-#if 0
-static void call_callback(void)
-{
- struct osl_object query, result = {.data = NULL};
- int ret, ret2;
-
- shm_callback_data->result_shmid = -1; /* no result */
- ret = shm_attach(shm_callback_data->query_shmid, ATTACH_RW,
- &query.data);
- if (ret < 0)
- goto out;
- query.size = shm_callback_data->query_size;
- shm_callback_data->callback_ret = shm_callback_data->handler(&query,
- &result);
- if (result.data && result.size) {
- void *sma;
- ret = shm_new(result.size);
- if (ret < 0)
- goto detach_query;
- shm_callback_data->result_shmid = ret;
- shm_callback_data->result_size = result.size;
- ret = shm_attach(shm_callback_data->result_shmid, ATTACH_RW, &sma);
- if (ret < 0)
- goto destroy_result;
- memcpy(sma, result.data, result.size);
- ret = shm_detach(sma);
- if (ret < 0) {
- PARA_ERROR_LOG("detach result failed\n");
- goto destroy_result;
- }
- }
- ret = 1;
- goto detach_query;
-destroy_result:
- if (shm_destroy(shm_callback_data->result_shmid) < 0)
- PARA_ERROR_LOG("destroy result failed\n");
- shm_callback_data->result_shmid = -1;
-detach_query:
- free(result.data);
- ret2 = shm_detach(query.data);
- if (ret2 < 0) {
- PARA_ERROR_LOG("detach query failed\n");
- if (ret >= 0)
- ret = ret2;
- }
-out:
- if (ret < 0)
- PARA_ERROR_LOG("sma error %d\n", ret);
- shm_callback_data->sma_ret = ret;
- shm_callback_data->handler = NULL;
- mutex_unlock(result_mutex); /* wake up child */
-}
-
-static int got_sigchld;
-static void server_loop(int child_pid)
-{
-// int status;
-
- PARA_DEBUG_LOG("server pid: %d, child pid: %d\n",
- getpid(), child_pid);
- for (;;) {
- mutex_lock(callback_mutex);
- if (shm_callback_data->handler)
- call_callback();
- mutex_unlock(callback_mutex);
- usleep(100);
- if (!got_sigchld)
- continue;
- mutex_destroy(result_mutex);
- mutex_destroy(callback_mutex);
- mutex_destroy(child_mutex);
- afs_shutdown(OSL_MARK_CLEAN);
- exit(EXIT_SUCCESS);
- }
-}
-
-int main(int argc, const char **argv)
-{
- int i, ret = -E_AFS_SYNTAX;
-
- signal(SIGUSR1, dummy);
- signal(SIGCHLD, sigchld_handler);
- if (argc < 2)
- goto out;
- ret = setup();
-// ret = afs_init();
- if (ret < 0) {
- PARA_EMERG_LOG("afs_init returned %d\n", ret);
- exit(EXIT_FAILURE);
- }
- ret = fork();
- if (ret < 0) {
- ret = -E_FORK;
- goto out;
- }
- if (ret)
- server_loop(ret);
- for (i = 0; cmd[i].name; i++) {
- if (strcmp(cmd[i].name, argv[1]))
- continue;
- ret = cmd[i].handler(1, argc - 1 , argv + 1);
- goto out;
-
- }
- PARA_ERROR_LOG("unknown command: %s\n", argv[1]);
- ret = -1;
-out:
- if (ret < 0)
- PARA_ERROR_LOG("error %d\n", ret);
- else
- PARA_DEBUG_LOG("%s", "success\n");
- afs_shutdown(0);
- return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
-}
-#endif