Exemple #1
0
static
int attach_binary_item(struct abrt_xmlrpc *ax, const char *bug_id,
                const char *item_name, struct problem_item *item)
{
    if (!(item->flags & CD_FLAG_BIN))
        return 0;

    char *filename = item->content;
    int fd = open(filename, O_RDONLY);
    if (fd < 0)
    {
        perror_msg("Can't open '%s'", filename);
        return 0;
    }
    errno = 0;
    struct stat st;
    if (fstat(fd, &st) != 0 || !S_ISREG(st.st_mode))
    {
        perror_msg("'%s': not a regular file", filename);
        close(fd);
        return 0;
    }
    VERB3 log("attaching '%s' as binary", item_name);
    int r = rhbz_attach_fd(ax, bug_id, item_name, fd, RHBZ_NOMAIL_NOTIFY | RHBZ_BINARY_ATTACHMENT);
    close(fd);
    return (r == 0);
}
void
select_playtgt (dspdev_t * dsp)
{
/*
 * Handling of the -o command line option (playback target selection).
 *
 * Empty or "?" shows the available playback sources.
 */
  int i, src;
  oss_mixer_enuminfo ei;

  if (ioctl (dsp->fd, SNDCTL_DSP_GET_PLAYTGT_NAMES, &ei) == -1)
    {
      perror_msg ("SNDCTL_DSP_GET_PLAYTGT_NAMES");
      exit (E_SETUP_ERROR);
    }

  if (ioctl (dsp->fd, SNDCTL_DSP_GET_PLAYTGT, &src) == -1)
    {
      perror_msg ("SNDCTL_DSP_GET_PLAYTGT");
      exit (E_SETUP_ERROR);
    }

  if ((dsp->playtgt[0] == '\0') || (strcmp (dsp->playtgt, "?") == 0))
    {
      print_msg (STARTM,
                 "\nPossible playback targets for the selected device:\n\n");

      for (i = 0; i < ei.nvalues; i++)
	{
	  print_msg (CONTM, "\t%s", ei.strings + ei.strindex[i]);
	  if (i == src)
	    print_msg (CONTM, " (currently selected)");
	  print_msg (CONTM, "\n");
	}
      print_msg (ENDM, "\n");
      exit (0);
    }

  for (i = 0; i < ei.nvalues; i++)
    {
      char *s = ei.strings + ei.strindex[i];
      if (strcmp (s, dsp->playtgt) == 0)
	{
	  src = i;
	  if (ioctl (dsp->fd, SNDCTL_DSP_SET_PLAYTGT, &src) == -1)
	    {
	      perror_msg ("SNDCTL_DSP_SET_PLAYTGT");
	      exit (E_SETUP_ERROR);
	    }

	  return;
	}
    }

  print_msg (ERRORM,
	     "Unknown playback target name '%s' - use -o? to get the list\n",
	     dsp->playtgt);
  exit (E_USAGE);
}
/**
 * Produce an IN_CREATE notification for a new file and start wathing on it.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata  A pointer to user data (#handle_context).
 * @param[in] path   File name of a new file.
 * @param[in] inode  Inode number of a new file.
 **/
static void
handle_added (void *udata, const char *path, ino_t inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

    struct inotify_event *ie = NULL;
    int ie_len = 0;

    ie = create_inotify_event (ctx->w->fd, IN_CREATE, 0, path, &ie_len);
    if (ie != NULL) {
        bulk_write (ctx->be, ie, ie_len);
        free (ie);
    } else {
        perror_msg ("Failed to create an IN_CREATE event for %s", path);
    }

    char *npath = path_concat (ctx->w->filename, path);
    if (npath != NULL) {
        watch *neww = worker_start_watching (ctx->wrk, npath, path, ctx->w->flags, WATCH_DEPENDENCY);
        if (neww == NULL) {
            perror_msg ("Failed to start watching on a new dependency %s", npath);
        } else {
            neww->parent = ctx->w;
        }
        free (npath);
    } else {
        perror_msg ("Failed to allocate a path to start watching a dependency");
    }
}
Exemple #4
0
int
decompress_file(const char *path_in, const char *path_out, mode_t mode_out)
{
    int fdi = open(path_in, O_RDONLY | O_CLOEXEC);
    if (fdi < 0)
    {
        perror_msg("Could not open file: %s", path_in);
        return -1;
    }

    int fdo = open(path_out, O_WRONLY | O_CLOEXEC | O_EXCL | O_CREAT, mode_out);
    if (fdo < 0)
    {
        close(fdi);
        perror_msg("Could not create file: %s", path_out);
        return -1;
    }

    int ret = decompress_fd(fdi, fdo);
    close(fdi);
    close(fdo);

    if (ret != 0)
        unlink(path_out);

    return ret;
}
void
select_recsrc (dspdev_t * dsp)
{
/*
 * Handling of the -i command line option (recording source selection).
 *
 * Empty or "?" shows the available recording sources.
 */
  int i, src;
  oss_mixer_enuminfo ei;

  if (ioctl (dsp->fd, SNDCTL_DSP_GET_RECSRC_NAMES, &ei) == -1)
    {
      perror_msg ("SNDCTL_DSP_GET_RECSRC_NAMES");
      exit (E_SETUP_ERROR);
    }

  if (ioctl (dsp->fd, SNDCTL_DSP_GET_RECSRC, &src) == -1)
    {
      perror_msg ("SNDCTL_DSP_GET_RECSRC");
      exit (E_SETUP_ERROR);
    }

  if (dsp->recsrc[0] == '\0' || strcmp (dsp->recsrc, "?") == 0)
    {
      print_msg (STARTM,
                 "\nPossible recording sources for the selected device:\n\n");

      for (i = 0; i < ei.nvalues; i++)
	{
	  print_msg (CONTM, "\t%s", ei.strings + ei.strindex[i]);
	  if (i == src)
	    print_msg (CONTM, " (currently selected)");
	  print_msg (CONTM, "\n");
	}
      print_msg (ENDM, "\n");
      exit (0);
    }

  for (i = 0; i < ei.nvalues; i++)
    {
      char *s = ei.strings + ei.strindex[i];
      if (strcmp (s, dsp->recsrc) == 0)
	{
	  src = i;
	  if (ioctl (dsp->fd, SNDCTL_DSP_SET_RECSRC, &src) == -1)
	    {
	      perror_msg ("SNDCTL_DSP_SET_RECSRC");
	      exit (E_SETUP_ERROR);
	    }
	  return;
	}
    }

  print_msg (ERRORM,
	     "Unknown recording source name '%s' - use -i? to get the list\n",
	     dsp->recsrc);
  exit (E_USAGE);
}
Exemple #6
0
/* Return values:
 * -1: error (in this case, errno is 0 if error message is already logged)
 *  0: failed to lock (someone else has it locked)
 *  1: success
 */
int create_symlink_lockfile(const char* lock_file, const char* pid)
{
    while (symlink(pid, lock_file) != 0)
    {
        if (errno != EEXIST)
        {
            if (errno != ENOENT && errno != ENOTDIR && errno != EACCES)
            {
                perror_msg("Can't create lock file '%s'", lock_file);
                errno = 0;
            }
            return -1;
        }

        char pid_buf[sizeof(pid_t)*3 + 4];
        ssize_t r = readlink(lock_file, pid_buf, sizeof(pid_buf) - 1);
        if (r < 0)
        {
            if (errno == ENOENT)
            {
                /* Looks like lock_file was deleted */
                usleep(SYMLINK_RETRY_USLEEP); /* avoid CPU eating loop */
                continue;
            }
            perror_msg("Can't read lock file '%s'", lock_file);
            errno = 0;
            return -1;
        }
        pid_buf[r] = '\0';

        if (strcmp(pid_buf, pid) == 0)
        {
            log("Lock file '%s' is already locked by us", lock_file);
            errno = EALREADY;
            return 0;
        }
        if (isdigit_str(pid_buf))
        {
            char pid_str[sizeof("/proc/") + sizeof(pid_buf)];
            snprintf(pid_str, sizeof(pid_str), "/proc/%s", pid_buf);
            if (access(pid_str, F_OK) == 0)
            {
                log("Lock file '%s' is locked by process %s", lock_file, pid_buf);
                return 0;
            }
            log("Lock file '%s' was locked by process %s, but it crashed?", lock_file, pid_buf);
        }
        /* The file may be deleted by now by other process. Ignore ENOENT */
        if (unlink(lock_file) != 0 && errno != ENOENT)
        {
            perror_msg("Can't remove stale lock file '%s'", lock_file);
            errno = 0;
            return -1;
        }
    }

    log_info("Locked '%s'", lock_file);
    return 1;
}
Exemple #7
0
/* Callback called by glib main loop when a client connects to ABRT's socket. */
static gboolean server_socket_cb(GIOChannel *source, GIOCondition condition, gpointer ptr_unused)
{
    kill_idle_timeout();
    load_abrt_conf();

    int socket = accept(g_io_channel_unix_get_fd(source), NULL, NULL);
    if (socket == -1)
    {
        perror_msg("accept");
        goto server_socket_finitio;
    }

    log_notice("New client connected");
    fflush(NULL); /* paranoia */

    int pipefd[2];
    xpipe(pipefd);

    pid_t pid = fork();
    if (pid < 0)
    {
        perror_msg("fork");
        close(socket);
        close(pipefd[0]);
        close(pipefd[1]);
        goto server_socket_finitio;
    }
    if (pid == 0) /* child */
    {
        xdup2(socket, STDIN_FILENO);
        xdup2(socket, STDOUT_FILENO);
        close(socket);

        close(pipefd[0]);
        xmove_fd(pipefd[1], STDERR_FILENO);

        char *argv[3];  /* abrt-server [-s] NULL */
        char **pp = argv;
        *pp++ = (char*)"abrt-server";
        if (logmode & LOGMODE_JOURNAL)
            *pp++ = (char*)"-s";
        *pp = NULL;

        execvp(argv[0], argv);
        perror_msg_and_die("Can't execute '%s'", argv[0]);
    }

    /* parent */
    close(socket);
    close(pipefd[1]);
    add_abrt_server_proc(pid, pipefd[0]);

server_socket_finitio:
    start_idle_timeout();
    return TRUE;
}
Exemple #8
0
/* The function expects that FILENAME_COUNT dump dir element is created by
 * abrtd after all post-create events are successfully done. Thus if
 * FILENAME_COUNT element doesn't exist abrtd can consider the dump directory
 * as unprocessed.
 *
 * Relying on content of dump directory has one problem. If a hook provides
 * FILENAME_COUNT abrtd will consider the dump directory as processed.
 */
static void mark_unprocessed_dump_dirs_not_reportable(const char *path)
{
    log_notice("Searching for unprocessed dump directories");

    DIR *dp = opendir(path);
    if (!dp)
    {
        perror_msg("Can't open directory '%s'", path);
        return;
    }

    struct dirent *dent;
    while ((dent = readdir(dp)) != NULL)
    {
        if (dot_or_dotdot(dent->d_name))
            continue; /* skip "." and ".." */

        char *full_name = concat_path_file(path, dent->d_name);

        struct stat stat_buf;
        if (stat(full_name, &stat_buf) != 0)
        {
            perror_msg("Can't access path '%s'", full_name);
            goto next_dd;
        }

        if (S_ISDIR(stat_buf.st_mode) == 0)
            /* This is expected. The dump location contains some aux files */
            goto next_dd;

        struct dump_dir *dd = dd_opendir(full_name, /*flags*/0);
        if (dd)
        {
            if (!problem_dump_dir_is_complete(dd) && !dd_exist(dd, FILENAME_NOT_REPORTABLE))
            {
                log_warning("Marking '%s' not reportable (no '"FILENAME_COUNT"' item)", full_name);

                dd_save_text(dd, FILENAME_NOT_REPORTABLE, _("The problem data are "
                            "incomplete. This usually happens when a problem "
                            "is detected while computer is shutting down or "
                            "user is logging out. In order to provide "
                            "valuable problem reports, ABRT will not allow "
                            "you to submit this problem. If you have time and "
                            "want to help the developers in their effort to "
                            "sort out this problem, please contact them directly."));

            }
            dd_close(dd);
        }

  next_dd:
        free(full_name);
    }
    closedir(dp);
}
Exemple #9
0
int uname_main(int argc, char **argv)
{
	struct utsname name;
	char processor[256];

#if defined(__sparc__) && defined(__linux__)
	char *fake_sparc = getenv("FAKE_SPARC");
#endif

	toprint = 0;

	/* Parse any options */
	//fprintf(stderr, "argc=%d, argv=%s\n", argc, *argv);
	while (--argc > 0 && **(++argv) == '-') {
		while (*(++(*argv))) {
			switch (**argv) {
			case 's':
				toprint |= PRINT_SYSNAME;
				break;
			case 'n':
				toprint |= PRINT_NODENAME;
				break;
			case 'r':
				toprint |= PRINT_RELEASE;
				break;
			case 'v':
				toprint |= PRINT_VERSION;
				break;
			case 'm':
				toprint |= PRINT_MACHINE;
				break;
			case 'p':
				toprint |= PRINT_PROCESSOR;
				break;
			case 'a':
				toprint = (PRINT_SYSNAME | PRINT_NODENAME | PRINT_RELEASE |
						   PRINT_PROCESSOR | PRINT_VERSION |
						   PRINT_MACHINE);
				break;
			default:
				show_usage();
			}
		}
	}

	if (toprint == 0)
		toprint = PRINT_SYSNAME;

	if (uname(&name) == -1)
		perror_msg("cannot get system name");

#if defined (HAVE_SYSINFO) && defined (SI_ARCHITECTURE)
	if (sysinfo(SI_ARCHITECTURE, processor, sizeof(processor)) == -1)
		perror_msg("cannot get processor type");
}
Exemple #10
0
static int
decompress_using_fork_execvp(const char** cmd, int fdi, int fdo)
{
    pid_t child = fork();
    if (child < 0)
    {
        VERB1 perror_msg("fork() for decompression");
        return -1;
    }

    if (child == 0)
    {
        close(STDIN_FILENO);
        if (dup2(fdi, STDIN_FILENO) < 0)
        {
            VERB1 perror_msg("Decompression failed: dup2(fdi, STDIN_FILENO)");
            exit(EXIT_FAILURE);
        }

        close(STDOUT_FILENO);
        if (dup2(fdo, STDOUT_FILENO) < 0)
        {
            VERB1 perror_msg("Decompression failed: dup2(fdo, STDOUT_FILENO)");
            exit(EXIT_FAILURE);
        }

        execvp(cmd[0], (char **)cmd);

        VERB1 perror_msg("Decompression failed: execlp('%s')", cmd[0]);
        exit(EXIT_FAILURE);
    }

    int status = 0;
    int r = safe_waitpid(child, &status, 0);
    if (r < 0)
    {
        VERB1 perror_msg("Decompression failed: waitpid($1) failed");
        return -2;
    }

    if (!WIFEXITED(status))
    {
        log_info("Decompression process returned abnormally");
        return -3;
    }

    if (WEXITSTATUS(status) != 0)
    {
        log_info("Decompression process exited with %d", WEXITSTATUS(r));
        return -4;
    }

    return 0;
}
/**
 * Produce an IN_DELETE/IN_CREATE notifications pair for an overwritten file.
 * Reopen a watch for the overwritten file.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata  A pointer to user data (#handle_context).
 * @param[in] path   File name of the overwritten file.
 * @param[in] inode  Inode number of the overwritten file.
 **/
static void
handle_overwritten (void *udata, const char *path, ino_t inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

   int i;
    for (i = 0; i < ctx->wrk->sets.length; i++) {
        watch *wi = ctx->wrk->sets.watches[i];
        if (wi && (strcmp (wi->filename, path) == 0)
            && wi->parent == ctx->w) {
            if (watch_reopen (wi) == -1) {
                /* I dont know, what to do */
                /* Not a very beautiful way to remove a single dependency */
                dep_list *dl = dl_create (wi->filename, wi->inode);
                worker_remove_many (ctx->wrk, ctx->w, dl, 0);
                dl_shallow_free (dl);
            } else {
                uint32_t cookie = inode & 0x00000000FFFFFFFF;
                int event_len = 0;
                struct inotify_event *ev;

                ev = create_inotify_event (ctx->w->fd, IN_DELETE, cookie,
                                           path,
                                           &event_len);
                if (ev != NULL) {
                    bulk_write (ctx->be, ev, event_len);
                    free (ev);
                }  else {
                    perror_msg ("Failed to create an IN_DELETE event (*) for %s",
                                path);
                }

                ev = create_inotify_event (ctx->w->fd, IN_CREATE, cookie,
                                           path,
                                           &event_len);
                if (ev != NULL) {
                    bulk_write (ctx->be, ev, event_len);
                    free (ev);
                } else {
                    perror_msg ("Failed to create an IN_CREATE event (*) for %s",
                                path);
                }
            }
            break;
        }
    }
}
/**
 * Produce an IN_MOVED_FROM/IN_MOVED_TO notifications pair for a replaced file.
 * Also stops wathing on the replaced file.
 *
 * This function is used as a callback and is invoked from the dep-list
 * routines.
 *
 * @param[in] udata       A pointer to user data (#handle_context).
 * @param[in] from_path   File name of the source file.
 * @param[in] from_inode  Inode number of the source file.
 * @param[in] to_path     File name of the replaced file.
 * @param[in] to_inode    Inode number of the replaced file.
**/
static void
handle_replaced (void       *udata,
                 const char *from_path,
                 ino_t       from_inode,
                 const char *to_path,
                 ino_t       to_inode)
{
    assert (udata != NULL);

    handle_context *ctx = (handle_context *) udata;
    assert (ctx->wrk != NULL);
    assert (ctx->w != NULL);
    assert (ctx->be != NULL);

    uint32_t cookie = from_inode & 0x00000000FFFFFFFF;
    int event_len = 0;
    struct inotify_event *ev;

    ev = create_inotify_event (ctx->w->fd, IN_MOVED_FROM, cookie,
                               from_path,
                               &event_len);
    if (ev != NULL) {
        bulk_write (ctx->be, ev, event_len);
        free (ev);
    }  else {
        perror_msg ("Failed to create an IN_MOVED_FROM event (*) for %s",
                    from_path);
    }

    ev = create_inotify_event (ctx->w->fd, IN_MOVED_TO, cookie,
                               to_path,
                               &event_len);
    if (ev != NULL) {
        bulk_write (ctx->be, ev, event_len);
        free (ev);
    } else {
        perror_msg ("Failed to create an IN_MOVED_TO event (*) for %s",
                    to_path);
    }

    int i;
    for (i = 1; i < ctx->wrk->sets.length; i++) {
        watch *iw = ctx->wrk->sets.watches[i];
        if (iw && iw->parent == ctx->w && strcmp (to_path, iw->filename) == 0) {
            dep_list *dl = dl_create (iw->filename, iw->inode);
            worker_remove_many (ctx->wrk, ctx->w, dl, 0);
            dl_shallow_free (dl);
            break;
        }
    }
}
Exemple #13
0
static int create_pidfile(void)
{
    /* Note:
     * No O_EXCL: we would happily overwrite stale pidfile from previous boot.
     * No O_TRUNC: we must first try to lock the file, and if lock fails,
     * there is another live abrtd. O_TRUNCing the file in this case
     * would be wrong - it'll erase the pid to empty string!
     */
    int fd = open(VAR_RUN_PIDFILE, O_RDWR|O_CREAT, 0644);
    if (fd >= 0)
    {
        if (lockf(fd, F_TLOCK, 0) < 0)
        {
            perror_msg("Can't lock file '%s'", VAR_RUN_PIDFILE);
            /* should help with problems like rhbz#859724 */
            char pid_str[sizeof(long)*3 + 4];
            int r = full_read(fd, pid_str, sizeof(pid_str));
            close(fd);

            /* File can contain garbage. Be careful interpreting it as PID */
            if (r > 0)
            {
                pid_str[r] = '\0';
                errno = 0;
                long locking_pid = strtol(pid_str, NULL, 10);
                if (!errno && locking_pid > 0 && locking_pid <= INT_MAX)
                {
                    char *cmdline = get_cmdline(locking_pid);
                    if (cmdline)
                    {
                        error_msg("Process %lu '%s' is holding the lock", locking_pid, cmdline);
                        free(cmdline);
                    }
                }
            }

            return -1;
        }
        close_on_exec_on(fd);
        /* write our pid to it */
        char buf[sizeof(long)*3 + 2];
        int len = sprintf(buf, "%lu\n", (long)getpid());
        IGNORE_RESULT(write(fd, buf, len));
        IGNORE_RESULT(ftruncate(fd, len));
        /* we leak opened+locked fd intentionally */
        return 0;
    }

    perror_msg("Can't open '%s'", VAR_RUN_PIDFILE);
    return -1;
}
Exemple #14
0
int
gz_close(int gunzip_pid)
{
	int status;
	int ret;

	if (gz_use_vfork) {
		/* The gunzip process remains running in the background if we
		 * used the vfork()/exec() technique - so we have to kill it
		 * forcibly.  There might be a better way to do this, but that
		 * affect a lot of other parts of opkg, and this works fine.
		 */
		if (kill(gunzip_pid, SIGTERM) == -1) {
			perror_msg("gz_close(): unable to kill gunzip pid.");
			return -1;
		}
	}


	if (waitpid(gunzip_pid, &status, 0) == -1) {
		perror_msg("waitpid");
		return -1;
	}

	if (gz_use_vfork) {
		/* Bail out here if we used the vfork()/exec() technique. */
		return 0;
	}

	if (WIFSIGNALED(status)) {
		error_msg("Unzip process killed by signal %d.\n",
			WTERMSIG(status));
		return -1;
	}

	if (!WIFEXITED(status)) {
		/* shouldn't happen */
		error_msg("Your system is broken: got status %d from waitpid.\n",
				status);
		return -1;
	}

	if ((ret = WEXITSTATUS(status))) {
		error_msg("Unzip process failed with return code %d.\n",
				ret);
		return -1;
	}

	return 0;
}
Exemple #15
0
// Update attribute of given file.
static int update_attr(struct dirtree *root)
{
  unsigned long fval = 0;
  char *fpath = NULL;
  int fd;

  if (!dirtree_notdotdot(root)) return 0;

  /*
   * if file is a link and recursive is set or file is not regular+link+dir
   * (like fifo or dev file) then escape the file.
   */
  if ((S_ISLNK(root->st.st_mode) && chattr.recursive)
    || (!S_ISREG(root->st.st_mode) && !S_ISLNK(root->st.st_mode)
      && !S_ISDIR(root->st.st_mode)))
    return 0;

  fpath = dirtree_path(root, NULL);
  if (-1 == (fd=open(fpath, O_RDONLY | O_NONBLOCK))) {
    free(fpath);
    return DIRTREE_ABORT;
  }
  // Get current attr of file.
  if (ext2_getflag(fd, &(root->st), &fval) < 0) {
    perror_msg("read flags of '%s'", fpath);
    free(fpath);
    xclose(fd);
    return DIRTREE_ABORT;
  }
  if (chattr.set) { // for '=' operator.
    if (ext2_setflag(fd, &(root->st), chattr.set) < 0)
      perror_msg("setting flags '%s'", fpath);
  } else { // for '-' / '+' operator.
    fval &= ~(chattr.rm);
    fval |= chattr.add;
    if (!S_ISDIR(root->st.st_mode)) fval &= ~FS_DIRSYNC_FL;
    if (ext2_setflag(fd, &(root->st), fval) < 0)
      perror_msg("setting flags '%s'", fpath);
  }
  if (chattr.vflag) { // set file version
    if (ioctl(fd, FS_IOC_SETVERSION, (void*)&chattr.version) < 0)
      perror_msg("while setting version on '%s'", fpath);
  }
  free(fpath);
  xclose(fd);

  if (S_ISDIR(root->st.st_mode) && chattr.recursive) return DIRTREE_RECURSE;
  return 0;
}
Exemple #16
0
static void print_file_attr(char *path)
{
  unsigned long flag = 0, version = 0;
  int fd;
  struct stat sb;

  if (!stat(path, &sb) && !S_ISREG(sb.st_mode) && !S_ISDIR(sb.st_mode)) {
    errno = EOPNOTSUPP;
    goto LABEL1;
  }
  if (-1 == (fd=open(path, O_RDONLY | O_NONBLOCK))) goto LABEL1;

  if (toys.optflags & FLAG_v) { 
    if (ioctl(fd, FS_IOC_GETVERSION, (void*)&version) < 0) goto LABEL2;
    xprintf("%5lu ", version);
  }

  if (ext2_getflag(fd, &sb, &flag) < 0) perror_msg("reading flags '%s'", path);
  else {
    struct ext2_attr *ptr = e2attrs;

    if (toys.optflags & FLAG_l) {
      int name_found = 0;

      xprintf("%-50s ", path);
      for (; ptr->name; ptr++) {
        if (flag & ptr->flag) {
          if (name_found) xprintf(", "); //for formatting.
          xprintf("%s", ptr->name);
          name_found = 1;
        }
      }
      if (!name_found) xprintf("---");
      xputc('\n');
    } else {
      int index = 0;

      for (; ptr->name; ptr++)
        toybuf[index++] = (flag & ptr->flag) ? ptr->opt : '-';
      toybuf[index] = '\0';
      xprintf("%s %s\n", toybuf, path);
    }
  }
  xclose(fd);
  return;
LABEL2: xclose(fd);
LABEL1: perror_msg("reading '%s'", path);
}
static void
find_devname (char * devname, const char * num)
{
/*
 * OSS 4.0 the audio device numbering may be different from the
 * legacy /dev/dsp# numbering reported by /dev/sndstat. Try to find the
 * device name (devnode) that matches the given device number.
 *
 * Prior versions of ossplay simply used the the /dev/dsp# number.
 */
  int dev;
  int mixer_fd;
  oss_audioinfo ai;
  const char * devmixer;

  if ((devmixer = getenv("OSS_MIXERDEV")) == NULL)
     devmixer = "/dev/mixer";

  if (sscanf (num, "%d", &dev) != 1)
    {
      print_msg (ERRORM, "Invalid audio device number '%s'\n", num);
      exit (E_SETUP_ERROR);
    }

  if ((mixer_fd = open (devmixer, O_RDWR, 0)) == -1)
    {
      perror_msg (devmixer);
      print_msg (WARNM, "Warning: Defaulting to /dev/dsp%s\n", num);
      snprintf (devname, OSS_DEVNODE_SIZE, "/dev/dsp%s", num);
      return;
    }

  ai.dev = dev;

  if (ioctl (mixer_fd, SNDCTL_AUDIOINFO, &ai) == -1)
    {
      perror_msg ("SNDCTL_AUDIOINFO");
      print_msg (WARNM, "Warning: Defaulting to /dev/dsp%s\n", num);
      snprintf (devname, OSS_DEVNODE_SIZE, "/dev/dsp%s", num);
      close (mixer_fd);
      return;
    }

  strncpy (devname, ai.devnode, OSS_DEVNODE_SIZE);

  close (mixer_fd);
  return;
}
static void do_loadfont(int fd, char *inbuf, int unit, int fontsize)
{
	char buf[16384];
	int i;

	memset(buf, 0, sizeof(buf));

	if (unit < 1 || unit > 32)
		error_msg_and_die("Bad character size %d", unit);

	for (i = 0; i < fontsize; i++)
		memcpy(buf + (32 * i), inbuf + (unit * i), unit);

#if defined( PIO_FONTX ) && !defined( __sparc__ )
	{
		struct consolefontdesc cfd;

		cfd.charcount = fontsize;
		cfd.charheight = unit;
		cfd.chardata = buf;

		if (ioctl(fd, PIO_FONTX, &cfd) == 0)
			return;				/* success */
		perror_msg("PIO_FONTX ioctl error (trying PIO_FONT)");
	}
#endif
	if (ioctl(fd, PIO_FONT, buf))
		perror_msg_and_die("PIO_FONT ioctl error");
}
char *
xgetcwd (char *cwd)
{
  char *ret;
  unsigned path_max;

  path_max = (unsigned) PATH_MAX;
  path_max += 2;                /* The getcwd docs say to do this. */

  if(cwd==0)
	cwd = xmalloc (path_max);

  while ((ret = getcwd (cwd, path_max)) == NULL && errno == ERANGE) {
      path_max += PATH_INCR;
      cwd = xrealloc (cwd, path_max);
  }

  if (ret == NULL) {
      free (cwd);
      perror_msg("getcwd()");
      return NULL;
  }

  return cwd;
}
Exemple #20
0
static int
tarExtractSymLink(TarInfo *header, int extractFlag, int tostdoutFlag)
{
	if (extractFlag==FALSE || tostdoutFlag==TRUE)
		return( TRUE);

#ifdef	S_ISLNK
	if (symlink(header->linkname, header->name) < 0) {
		perror_msg("%s: Cannot create symlink to '%s'", header->name,
				header->linkname); 
		return( FALSE);
	}
	/* Try to change ownership of the symlink.
	 * If libs doesn't support that, don't bother.
	 * Changing the pointed-to-file is the Wrong Thing(tm).
	 */
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
	lchown(header->name, header->uid, header->gid);
#endif

	/* Do not change permissions or date on symlink,
	 * since it changes the pointed to file instead.  duh. */
#else
	error_msg("%s: Cannot create symlink to '%s': %s", 
			header->name, header->linkname, 
			"symlinks not supported"); 
#endif
	return( TRUE);
}
Exemple #21
0
bool dir_is_in_dump_location(const char *dir_name)
{
    unsigned len = strlen(g_settings_dump_location);

    /* The path must start with "g_settings_dump_location" */
    if (strncmp(dir_name, g_settings_dump_location, len) != 0)
    {
        log_debug("Bad parent directory: '%s' not in '%s'", g_settings_dump_location, dir_name);
        return false;
    }

    /* and must be a sub-directory of the g_settings_dump_location dir */
    const char *base_name = dir_name + len;
    while (*base_name && *base_name == '/')
        ++base_name;

    if (*(base_name - 1) != '/' || !str_is_correct_filename(base_name))
    {
        log_debug("Invalid dump directory name: '%s'", base_name);
        return false;
    }

    /* and we are sure it is a directory */
    struct stat sb;
    if (lstat(dir_name, &sb) < 0)
    {
        VERB2 perror_msg("stat('%s')", dir_name);
        return errno== ENOENT;
    }

    return S_ISDIR(sb.st_mode);
}
void
open_device (dspdev_t * dsp)
{
  const char * devdsp;

  if (dsp->fd >= 0)
     close_device (dsp);

  dsp->format = 0; dsp->channels = 0; dsp->speed = 0;

  if ((devdsp = getenv("OSS_AUDIODEV")) == NULL)
     devdsp = "/dev/dsp";

  if (raw_mode)
    dsp->flags |= O_EXCL;	/* Disable redirection to the virtual mixer */

  if (dsp->dname[0] == '\0') strcpy (dsp->dname, devdsp);

  if ((dsp->fd = open (dsp->dname, dsp->flags, 0)) == -1)
    {
      perror_msg (dsp->dname);
      describe_error ();
      exit (E_SETUP_ERROR);
    }

  if (raw_mode)
    {
      /*
       * Disable sample rate/format conversions.
       */
      int tmp = 0;
      ioctl (dsp->fd, SNDCTL_DSP_COOKEDMODE, &tmp);
    }
}
Exemple #23
0
void do_id(char *username)
{
  int flags, i, ngroups, cmd_groups = toys.which->name[0] == 'g';
  struct passwd *pw;
  struct group *grp;
  uid_t uid = getuid(), euid = geteuid();
  gid_t gid = getgid(), egid = getegid(), *groups;

  if (cmd_groups)
      toys.optflags |= FLAG_G | FLAG_n;

  flags = toys.optflags;

  // check if a username is given
  if (username) {
    pw = xgetpwnam(username);
    uid = euid = pw->pw_uid;
    gid = egid = pw->pw_gid;
    if (cmd_groups) printf("%s : ", pw->pw_name);
  }

  i = flags & FLAG_r;
  pw = xgetpwuid(i ? uid : euid);
  if (flags & FLAG_u) s_or_u(pw->pw_name, pw->pw_uid, 1);

  grp = xgetgrgid(i ? gid : egid);
  if (flags & FLAG_g) s_or_u(grp->gr_name, grp->gr_gid, 1);

  if (!(flags & FLAG_G)) {
    showid("uid=", pw->pw_uid, pw->pw_name);
    showid(" gid=", grp->gr_gid, grp->gr_name);

    if (!i) {
      if (uid != euid) {
        pw = xgetpwuid(euid);
        showid(" euid=", pw->pw_uid, pw->pw_name);
      }
      if (gid != egid) {
        grp = xgetgrgid(egid);
        showid(" egid=", grp->gr_gid, grp->gr_name);
      }
    }

    showid(" groups=", grp->gr_gid, grp->gr_name);
  }

  groups = (gid_t *)toybuf;
  i = sizeof(toybuf)/sizeof(gid_t);
  ngroups = username ? getgrouplist(username, gid, groups, &i)
    : getgroups(i, groups);
  if (ngroups<0) perror_exit(0);

  for (i = 0; i<ngroups; i++) {
    if (i) xputc(' ');
    if (!(grp = getgrgid(groups[i]))) perror_msg(0);
    else if (flags & FLAG_G) s_or_u(grp->gr_name, grp->gr_gid, 0);
    else if (grp->gr_gid != egid) showid("", grp->gr_gid, grp->gr_name);
  }
  xputc('\n');
}
static int
rm_Action(const char *fileName, struct stat *statbuf, void* junk)
{
	int status = TRUE;

	if (S_ISDIR(statbuf->st_mode)) {
		if (rmdir(fileName) < 0) {
			perror_msg("rmdir(%s)", fileName);
			status = FALSE;
		}
	} else if (unlink(fileName) < 0) {
		perror_msg("unlink(%s)", fileName);
		status = FALSE;
	}
	return status;
}
Exemple #25
0
static void notify_next_post_create_process(struct abrt_server_proc *finished)
{
    if (finished != NULL)
        s_dir_queue = g_list_remove(s_dir_queue, finished);

    while (s_dir_queue != NULL)
    {
        struct abrt_server_proc *n = (struct abrt_server_proc *)s_dir_queue->data;
        if (n->type == AS_POST_CREATE)
            break;

        if (kill(n->pid, SIGUSR1) >= 0)
        {
            n->type = AS_POST_CREATE;
            break;
        }

        /* This could happen only if the notified process disappeared - crashed?
         */
        perror_msg("Failed to send SIGUSR1 to %d", n->pid);
        log_warning("Directory '%s' will not be processed", n->dirname);

        /* Remove the problematic process from the post-crate directory queue
         * and go to try to notify another process.
         */
        s_dir_queue = g_list_delete_link(s_dir_queue, s_dir_queue);
    }
}
/* create (sym)links for each applet */
static void install_links(const char *busybox, int use_symbolic_links)
{
	__link_f Link = link;

#ifdef ACTION_TEC_DIFFSERV
	char command[1024];
#else
    char command[256];
#endif //ACTION_TEC_DIFFSERV

	int i;
	int rc;

	if (use_symbolic_links) 
		Link = symlink;

	for (i = 0; applets[i].name != NULL; i++) {
		sprintf ( command, "%s/%s", 
				install_dir[applets[i].location], applets[i].name);
		rc = Link(busybox, command);

		if (rc) {
			perror_msg("%s", command);
		}
	}
}
/* 
 * Where in the filesystem is this busybox?
 * [return]
 *		malloc'd string w/ full pathname of busybox's location
 *		NULL on failure
 */
static char *busybox_fullpath()
{
	pid_t pid;
#ifdef ACTION_TEC_DIFFSERV
	char path[1024];
#else
	char proc[256];
#endif //ACTION_TEC_DIFFSERV
	int len;

	pid = getpid();
	sprintf(proc, "/proc/%d/exe", pid);
#ifdef ACTION_TEC_DIFFSERV
	len = readlink(proc, path, 1024); 
#else
    len = readlink(proc, path, 256);
#endif// ACTION_TEC_DIFFSERV
	if (len != -1) {
		path[len] = 0;
	} else {
		perror_msg("%s", proc);
		return NULL;
	}
	return strdup(path);
}
Exemple #28
0
// Warn if we can't open a file and return a fd.
int open3_or_warn(const char *pathname, int flags, int mode)
{
    int ret;
    ret = open(pathname, flags, mode);
    if (ret < 0)
        perror_msg("Can't open '%s'", pathname);
    return ret;
}
Exemple #29
0
static int writeTarFile(const char* tarName, int verboseFlag, char **argv,
		char** excludeList)
{
	int tarFd=-1;
	int errorFlag=FALSE;
	ssize_t size;
	struct TarBallInfo tbInfo;
	tbInfo.verboseFlag = verboseFlag;
	tbInfo.hlInfoHead = NULL;

	/* Make sure there is at least one file to tar up.  */
	if (*argv == NULL)
		error_msg_and_die("Cowardly refusing to create an empty archive");

	/* Open the tar file for writing.  */
	if (!strcmp(tarName, "-"))
		tbInfo.tarFd = fileno(stdout);
	else
		tbInfo.tarFd = open (tarName, O_WRONLY | O_CREAT | O_TRUNC, 0644);
	if (tbInfo.tarFd < 0) {
		perror_msg( "Error opening '%s'", tarName);
		freeHardLinkInfo(&tbInfo.hlInfoHead);
		return ( FALSE);
	}
	tbInfo.excludeList=excludeList;
	/* Store the stat info for the tarball's file, so
	 * can avoid including the tarball into itself....  */
	if (fstat(tbInfo.tarFd, &tbInfo.statBuf) < 0)
		error_msg_and_die(io_error, tarName, strerror(errno)); 

	/* Read the directory/files and iterate over them one at a time */
	while (*argv != NULL) {
		if (recursive_action(*argv++, TRUE, FALSE, FALSE,
					writeFileToTarball, writeFileToTarball, 
					(void*) &tbInfo) == FALSE) {
			errorFlag = TRUE;
		}
	}
	/* Write two empty blocks to the end of the archive */
	for (size=0; size<(2*TAR_BLOCK_SIZE); size++) {
		write(tbInfo.tarFd, "\0", 1);
	}

	/* To be pedantically correct, we would check if the tarball
	 * is smaller than 20 tar blocks, and pad it if it was smaller,
	 * but that isn't necessary for GNU tar interoperability, and
	 * so is considered a waste of space */

	/* Hang up the tools, close up shop, head home */
	close(tarFd);
	if (errorFlag == TRUE) {
		error_msg("Error exit delayed from previous errors");
		freeHardLinkInfo(&tbInfo.hlInfoHead);
		return(FALSE);
	}
	freeHardLinkInfo(&tbInfo.hlInfoHead);
	return( TRUE);
}
Exemple #30
0
FILE *wfopen(const char *path, const char *mode)
{
	FILE *fp;
	if ((fp = fopen(path, mode)) == NULL) {
		perror_msg("%s", path);
		errno = 0;
	}
	return fp;
}