void ensure_writable_dir_group(const char *dir, mode_t mode, const char *user, const char *group) { struct passwd *pw = getpwnam(user); if (!pw) perror_msg_and_die("Can't find user '%s'", user); struct group *gr = getgrnam(group); if (!gr) perror_msg_and_die("Can't find group '%s'", group); ensure_writable_dir_uid_gid(dir, mode, pw->pw_uid, gr->gr_gid); }
static void do_loadtable(int fd, unsigned char *inbuf, int tailsz, int fontsize) { struct unimapinit advice; struct unimapdesc ud; struct unipair *up; int ct = 0, maxct; int glyph; u_short unicode; maxct = tailsz; /* more than enough */ up = (struct unipair *) xmalloc(maxct * sizeof(struct unipair)); for (glyph = 0; glyph < fontsize; glyph++) { while (tailsz >= 2) { unicode = (((u_short) inbuf[1]) << 8) + inbuf[0]; tailsz -= 2; inbuf += 2; if (unicode == PSF_SEPARATOR) break; up[ct].unicode = unicode; up[ct].fontpos = glyph; ct++; } } /* Note: after PIO_UNIMAPCLR and before PIO_UNIMAP this printf did not work on many kernels */ advice.advised_hashsize = 0; advice.advised_hashstep = 0; advice.advised_hashlevel = 0; if (ioctl(fd, PIO_UNIMAPCLR, &advice)) { #ifdef ENOIOCTLCMD if (errno == ENOIOCTLCMD) { error_msg("It seems this kernel is older than 1.1.92"); error_msg_and_die("No Unicode mapping table loaded."); } else #endif perror_msg_and_die("PIO_UNIMAPCLR"); } ud.entry_ct = ct; ud.entries = up; if (ioctl(fd, PIO_UNIMAP, &ud)) { #if 0 if (errno == ENOMEM) { /* change advice parameters */ } #endif perror_msg_and_die("PIO_UNIMAP"); } }
int tftpd_getdata(int peer, int block, char *data, int size, int fNeedReset) { struct tftphdr *pkt; struct timeval tv; fd_set rfds; int len=-1; int timeout_counter = 4; pkt=(struct tftphdr *)data; do { tv.tv_sec = TFTP_TIMEOUT; tv.tv_usec = 0; FD_ZERO (&rfds); FD_SET (peer, &rfds); switch (select (FD_SETSIZE, &rfds, NULL, NULL, &tv)) { case 1: /* data ready */ len = recv (peer, data, size, 0); if (len < 0) perror_msg_and_die ("failed to read (data)"); pkt->th_opcode = ntohs (pkt->th_opcode); pkt->th_block = ntohs (pkt->th_block); if (pkt->th_opcode == ERROR) { bb_error_msg (pkt->th_data); myExit(fNeedReset, peer); } if ((pkt->th_opcode == DATA) && (pkt->th_block != block)) { //synchronize (peer); } break; case 0: /* timeout */ timeout_counter--; if (timeout_counter == 0) { bb_error_msg ("last timeout"); myExit(fNeedReset, peer); } break; default: /* error */ perror_msg_and_die ("select failed"); break; } }while (!(pkt->th_opcode == DATA) && (pkt->th_block == block)); return len; }
static int parse_devtable(FILE * devtable) { struct stat sb; if (lstat(rootdir, &sb)) { perror_msg_and_die("%s", rootdir); } if (chdir(rootdir)) perror_msg_and_die("%s", rootdir); if (devtable) parse_device_table(devtable); return 0; }
static int go(char *dname, FILE * devtable) { struct stat sb; if (lstat(dname, &sb)) { perror_msg_and_die("%s", dname); } if (chdir(dname)) perror_msg_and_die("%s", dname); if (devtable) parse_device_table(devtable); return 0; }
static void set_window_size(int rows, int cols, int fd, const char *device_name) { struct winsize win; if (get_win_size(fd, &win)) { if (errno != EINVAL) perror_msg_and_die("%s", device_name); memset(&win, 0, sizeof(win)); } if (rows >= 0) win.ws_row = rows; if (cols >= 0) win.ws_col = cols; # ifdef TIOCSSIZE #if !((MACRO_KERNEL_VERSION == 2) && (MACRO_KERNEL_PATCHLEVEL == 0) && defined(__sparc__)) /* Alexander Dupuy <*****@*****.**> wrote: The following code deals with a bug in the SunOS 4.x (and 3.x?) kernel. This comment from sys/ttold.h describes Sun's twisted logic - a better test would have been (ts_lines > 64k || ts_cols > 64k || ts_cols == 0). At any rate, the problem is gone in Solaris 2.x. */ if (win.ws_row == 0 || win.ws_col == 0) { struct ttysize ttysz; ttysz.ts_lines = win.ws_row; ttysz.ts_cols = win.ws_col; win.ws_row = 1; win.ws_col = 1; if (ioctl(fd, TIOCSWINSZ, (char *) &win)) perror_msg_and_die("%s", device_name); if (ioctl(fd, TIOCSSIZE, (char *) &ttysz)) perror_msg_and_die("%s", device_name); return; } # endif # endif if (ioctl(fd, TIOCSWINSZ, (char *) &win)) perror_msg_and_die("%s", device_name); }
off_t fstat_st_size_or_die(int fd) { struct stat statbuf; if (fstat(fd, &statbuf)) perror_msg_and_die("Can't stat"); return statbuf.st_size; }
extern void *xrealloc(void *ptr, size_t size) { ptr = realloc(ptr, size); if (ptr == NULL && size != 0) perror_msg_and_die("realloc"); return ptr; }
off_t stat_st_size_or_die(const char *filename) { struct stat statbuf; if (stat(filename, &statbuf)) perror_msg_and_die("Can't stat '%s'", filename); return statbuf.st_size; }
int xdup(int from) { int fd = dup(from); if (fd < 0) perror_msg_and_die("Can't duplicate file descriptor"); return fd; }
extern void *xcalloc(size_t nmemb, size_t size) { void *ptr = calloc(nmemb, size); if (ptr == NULL && nmemb != 0 && size != 0) perror_msg_and_die("calloc"); return ptr; }
extern int mkfifo_main(int argc, char **argv) { char *thisarg; mode_t mode = 0666; argc--; argv++; /* Parse any options */ while (argc > 1) { if (**argv != '-') show_usage(); thisarg = *argv; thisarg++; switch (*thisarg) { case 'm': argc--; argv++; parse_mode(*argv, &mode); break; default: show_usage(); } argc--; argv++; } if (argc < 1 || *argv[0] == '-') show_usage(); if (mkfifo(*argv, mode) < 0) perror_msg_and_die("mkfifo"); return EXIT_SUCCESS; }
FILE *xfopen(const char *path, const char *mode) { FILE *fp; if ((fp = fopen(path, mode)) == NULL) perror_msg_and_die("%s", path); return fp; }
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"); }
FILE *xfdopen(int fd, const char *mode) { FILE *const r = fdopen(fd, mode); if (NULL == r) perror_msg_and_die("Can't open file descriptor %d as FILE", fd); return r; }
extern void *xmalloc(size_t size) { void *ptr = malloc(size); if (ptr == NULL && size != 0) perror_msg_and_die("malloc"); return ptr; }
extern int klogd_main(int argc, char **argv) { /* no options, no getopt */ int opt; int doFork = TRUE; /* do normal option parsing */ while ((opt = getopt(argc, argv, "n")) > 0) { switch (opt) { case 'n': doFork = FALSE; break; default: show_usage(); } } if (doFork) { #if !defined(__UCLIBC__) || defined(__UCLIBC_HAS_MMU__) if (daemon(0, 1) < 0) perror_msg_and_die("daemon"); #else error_msg_and_die("daemon not supported"); #endif } doKlogd(); return EXIT_SUCCESS; }
// Die if we can't open a file and return a fd int xopen3(const char *pathname, int flags, int mode) { int ret; ret = open(pathname, flags, mode); if (ret < 0) perror_msg_and_die("Can't open '%s'", pathname); return ret; }
static void display_window_size(int fancy, int fd, const char *device_name) { struct winsize win; if (get_win_size(fd, &win)) { if (errno != EINVAL) perror_msg_and_die("%s", device_name); if (!fancy) perror_msg_and_die("%s: no size information for this device", device_name); } else { wrapf(fancy ? "rows %d; columns %d;" : "%d %d\n", win.ws_row, win.ws_col); if (!fancy) current_col = 0; } }
void ensure_writable_dir(const char *dir, mode_t mode, const char *user) { struct passwd *pw = getpwnam(user); if (!pw) perror_msg_and_die("Can't find user '%s'", user); ensure_writable_dir_uid_gid(dir, mode, pw->pw_uid, pw->pw_gid); }
/* Helpers */ static guint add_watch_or_die(GIOChannel *channel, unsigned condition, GIOFunc func) { errno = 0; guint r = g_io_add_watch(channel, (GIOCondition)condition, func, NULL); if (!r) perror_msg_and_die("g_io_add_watch failed"); return r; }
static int go(char *dname, FILE * devtable) { struct stat sb; if (lstat(dname, &sb)) { perror_msg_and_die("%s", dname); } if (chdir(dname)) perror_msg_and_die("%s", dname); recursive_add_directory("."); if (devtable) parse_device_table(devtable); create_target_filesystem(); cleanup(); return 0; }
void ensure_writable_dir(const char *dir, mode_t mode, const char *user) { struct stat sb; if (mkdir(dir, mode) != 0 && errno != EEXIST) perror_msg_and_die("Can't create '%s'", dir); if (stat(dir, &sb) != 0 || !S_ISDIR(sb.st_mode)) error_msg_and_die("'%s' is not a directory", dir); struct passwd *pw = getpwnam(user); if (!pw) perror_msg_and_die("Can't find user '%s'", user); if ((sb.st_uid != pw->pw_uid || sb.st_gid != pw->pw_gid) && lchown(dir, pw->pw_uid, pw->pw_gid) != 0) perror_msg_and_die("Can't set owner %u:%u on '%s'", (unsigned int)pw->pw_uid, (unsigned int)pw->pw_gid, dir); if ((sb.st_mode & 07777) != mode && chmod(dir, mode) != 0) perror_msg_and_die("Can't set mode %o on '%s'", mode, dir); }
static void full_write(int fd, const void *buf, int len) { int ret; while (len > 0) { ret = write(fd, buf, len); if (ret < 0) perror_msg_and_die("write"); if (ret == 0) perror_msg_and_die("write returned zero"); len -= ret; buf += ret; out_ofs += ret; } }
extern int touch_main(int argc, char **argv) { int fd; int create = TRUE; /* Parse options */ while (--argc > 0 && **(++argv) == '-') { while (*(++(*argv))) { switch (**argv) { case 'c': create = FALSE; break; default: show_usage(); } } } if (argc < 1) { show_usage(); } while (argc > 0) { fd = open(*argv, create ? O_RDWR | O_CREAT : O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); if (fd < 0) { if (! create && errno == ENOENT) { argc--; argv++; continue; } else { perror_msg_and_die("%s", *argv); } } close(fd); if (utime(*argv, NULL)) { perror_msg_and_die("%s", *argv); } argc--; argv++; } return EXIT_SUCCESS; }
/* 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; }
extern int pwd_main(int argc, char **argv) { char buf[BUFSIZ + 1]; if (getcwd(buf, sizeof(buf)) == NULL) perror_msg_and_die("getcwd"); puts(buf); return EXIT_SUCCESS; }
/* * Send a ack packet */ void tftpd_ack (int peer, int block) { struct tftphdr pkt; pkt.th_opcode = htons (ACK); pkt.th_block = htons (block); if (send (peer, &pkt, sizeof(pkt), 0)!=sizeof(pkt)) perror_msg_and_die("tftpd_ack send"); }
/* append a new user to the passwd file */ static int addgroup(const char *filename, char *group, gid_t gid) { FILE *etc_group; #ifdef TLG_FEATURE_SHADOWPASSWDS FILE *etc_gshadow; char *gshadow = GSHADOW_FILE; #endif /* TLG_FEATURE_SHADOWPASSWDS */ struct group gr; /* group:passwd:gid:userlist */ const char *entryfmt = "%s:%s:%d:%s\n"; /* make sure gid and group haven't already been allocated */ gr.gr_gid = gid; gr.gr_name = group; if (group_study(filename, &gr)) return 1; /* add entry to group */ etc_group = fopen(filename, "a"); if (!etc_group) { perror_msg_and_die("%s", filename); } fprintf(etc_group, entryfmt, group, default_passwd, gr.gr_gid, ""); fclose(etc_group); #ifdef TLG_FEATURE_SHADOWPASSWDS /* add entry to gshadow if necessary */ if (access(gshadow, W_OK) == 0) { etc_gshadow = fopen(gshadow, "a"); if (!etc_gshadow) { perror_msg_and_die("%s", gshadow); } fprintf(etc_gshadow, "%s:!::\n", group); fclose(etc_gshadow); } #endif /* TLG_FEATURE_SHADOWPASSWDS */ /* return 1; */ return 0; }
extern int freeramdisk_main(int argc, char **argv) { int f; if (argc != 2 || *argv[1] == '-') { show_usage(); } if ((f = open(argv[1], O_RDWR)) == -1) { perror_msg_and_die("cannot open %s", argv[1]); } if (ioctl(f, BLKFLSBUF) < 0) { perror_msg_and_die("failed ioctl on %s", argv[1]); } /* Don't bother closing. Exit does * that, so we can save a few bytes */ /* close(f); */ return EXIT_SUCCESS; }