void zmq::swap_t::fetch (zmq_msg_t *msg_) { // There must be at least one message available. zmq_assert (read_pos != write_pos); // Retrieve the message size. size_t msg_size; copy_from_file (&msg_size, sizeof msg_size); // Initialize the message. zmq_msg_init_size (msg_, msg_size); // Retrieve the message flags. copy_from_file (&msg_->flags, sizeof msg_->flags); // Retrieve the message payload. copy_from_file (zmq_msg_data (msg_), msg_size); }
int main(int argc, char** argv) { AppArgs::Init(); if (!( AppArgs::AddOption("about", '?', AAT_NO_VALUE, false) && AppArgs::AddOption("help", 'h', AAT_NO_VALUE, false) && AppArgs::AddOption("list", 'l', AAT_NO_VALUE, false) && AppArgs::AddOption("remove", 'r', AAT_NO_VALUE, false) && AppArgs::AddOption("edit", 'e', AAT_NO_VALUE, false) && AppArgs::AddOption("types", 't', AAT_NO_VALUE, false) && AppArgs::AddOption("reload", 'd', AAT_NO_VALUE, false) && AppArgs::AddOption("user", 'u', AAT_MANDATORY_VALUE, false) && AppArgs::AddOption("config", 'f', AAT_MANDATORY_VALUE, false) && AppArgs::AddOption("version", 'V', AAT_NO_VALUE, false))) { fprintf(stderr, "error while initializing application"); return 1; } AppArgs::Parse(argc, argv); if (AppArgs::ExistsOption("help")) { fprintf(stderr, "%s\n", INCRONTAB_HELP); return 0; } if (AppArgs::ExistsOption("about")) { fprintf(stderr, "%s\n", INCRONTAB_DESCRIPTION); return 0; } if (AppArgs::ExistsOption("version")) { fprintf(stderr, "%s\n", INCRONTAB_VERSION); return 0; } bool oper = AppArgs::ExistsOption("list") || AppArgs::ExistsOption("remove") || AppArgs::ExistsOption("edit") || AppArgs::ExistsOption("types") || AppArgs::ExistsOption("reload"); size_t vals = AppArgs::GetValueCount(); if (!oper && vals == 0) { fprintf(stderr, "invalid arguments - specify operation or source file\n"); return 1; } if (oper && vals > 0) { fprintf(stderr, "invalid arguments - operation and source file cannot be combined\n"); return 1; } uid_t uid = getuid(); std::string user; bool chuser = AppArgs::GetOption("user", user); if (uid != 0 && chuser) { fprintf(stderr, "cannot override user to '%s': insufficient privileges\n", user.c_str()); return 1; } struct passwd* ppwd = NULL; if (chuser) { if ((ppwd = getpwnam(user.c_str())) != NULL) { if ( setenv("LOGNAME", ppwd->pw_name, 1) != 0 || setenv("USER", ppwd->pw_name, 1) != 0 || setenv("USERNAME", ppwd->pw_name, 1) != 0 || setenv("HOME", ppwd->pw_dir, 1) != 0 || setenv("SHELL", ppwd->pw_shell, 1) != 0) { perror("cannot set environment variables"); return 1; } } else { fprintf(stderr, "user '%s' not found\n", user.c_str()); return 1; } } else { ppwd = getpwuid(uid); if (ppwd == NULL) { fprintf(stderr, "cannot determine current user\n"); return 1; } user = ppwd->pw_name; } try { IncronCfg::Init(); std::string cfg(INCRON_CONFIG); if (AppArgs::GetOption("config", cfg)) { if (uid != 0) { fprintf(stderr, "insufficient privileges to use custom configuration (will use default)\n"); } else if (euidaccess(cfg.c_str(), R_OK) != 0) { perror("cannot read configuration file (will use default)"); } } IncronCfg::Load(cfg); if (!IncronTab::CheckUser(user)) { fprintf(stderr, "user '%s' is not allowed to use incron\n", user.c_str()); return 1; } if (!oper) { std::string file; if (!AppArgs::GetValue(0, file) || !copy_from_file(file, user)) { return 1; } } else { if (AppArgs::ExistsOption("list")) { if (!list_table(user)) return 1; } else if (AppArgs::ExistsOption("remove")) { if (!remove_table(user)) return 1; } else if (AppArgs::ExistsOption("edit")) { if (!edit_table(user)) return 1; } else if (AppArgs::ExistsOption("types")) { list_types(); } else if (AppArgs::ExistsOption("reload")) { if (!reload_table(user)) return 1; } else { fprintf(stderr, "invalid usage\n"); return 1; } } return 0; } catch (InotifyException e) { fprintf(stderr, "*** unhandled exception occurred ***\n"); fprintf(stderr, "%s\n", e.GetMessage().c_str()); fprintf(stderr, "error: (%i) %s\n", e.GetErrorNumber(), strerror(e.GetErrorNumber())); return 1; } }
int main(int argc, char** argv) { struct arguments arguments; arguments.user = NULL; arguments.oper = OPER_NONE; arguments.file = NULL; argp_parse (&argp, argc, argv, 0, 0, &arguments); if (arguments.file != NULL && arguments.oper != OPER_NONE) { fprintf(stderr, "invalid arguments - specify source file or operation\n"); return 1; } if (arguments.file == NULL && arguments.oper == OPER_NONE) { fprintf(stderr, "invalid arguments - specify source file or operation\n"); return 1; } uid_t uid = getuid(); if (uid != 0 && arguments.user != NULL) { fprintf(stderr, "cannot access table for user %s: permission denied\n", arguments.user); return 1; } struct passwd pwd; if (arguments.user == NULL) { struct passwd* ppwd = getpwuid(uid); if (ppwd == NULL) { fprintf(stderr, "cannot determine current user\n"); return 1; } memcpy(&pwd, ppwd, sizeof(pwd)); arguments.user = pwd.pw_name; } else if (getpwnam(arguments.user) == NULL) { fprintf(stderr, "user %s not found\n", arguments.user); return 1; } if (!InCronTab::CheckUser(arguments.user)) { fprintf(stderr, "user %s is not allowed to use incron\n", arguments.user); return 1; } switch (arguments.oper) { case OPER_NONE: fprintf(stderr, "copying table from file: %s\n", arguments.file); if (!copy_from_file(arguments.file, arguments.user)) return 1; break; case OPER_LIST: if (!list_table(arguments.user)) return 1; break; case OPER_REMOVE: fprintf(stderr, "removing table for user %s\n", arguments.user); if (!remove_table(arguments.user)) return 1; break; case OPER_EDIT: if (!edit_table(arguments.user)) return 1; break; default: fprintf(stderr, "invalid usage\n"); return 1; } return 0; }