/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
*/
#include <errno.h>
#include <stdio.h>
#include <stdio_ext.h>
#include <string.h>
#include <pthread.h>
#include <sys/mtio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <signal.h>
#include <wait.h>
/* The magic numbers from /etc/magic */
#define GZIP_MAGIC "\037\213"
#define BZIP_MAGIC "BZh"
#define COMP_MAGIC "\037\235"
#define BZCAT "/usr/bin/bzcat"
#define GZCAT "/usr/bin/gzcat"
#define ZCAT "/usr/bin/zcat"
#define MAX_HDR_MAGIC 3 /* Max. no. of compressed header magic bytes */
struct thr_args {
int in; /* input fd */
FILE *out;
/* output pipe (compressor reads the other end of pipe) */
char *buf; /* buffer used for i/o */
size_t rsz; /* data size already put on the buffer */
int (*readf)(int, char *);
};
static struct worker {
pid_t pid;
pthread_t tid;
FILE *outf;
struct worker *next;
} *workers;
/*
* Given a file descriptor that sources a possibly compressed file, returns
* one that will source the uncompressed one. Supported compression types
* are bzip2, gzip and compress.
*
* if the input file is seekable:
* read the magic number from the file, rewind it.
*
* if compressed:
* fork and exec approp. decompressor as needed w/ stdin as
* the input. The stdout would be a pipe, the read end
* fd of which is returned to the caller.
*
* else: # not compressed
* return input fd
*
* else: # not seekable
* read the magic number from the input file.
*
* if compressed:
* fork and exec approp. decompressor as needed w/ stdin as
* the input. The stdout would be a pipe, the read end
* fd of which will be returned to the caller. Write header
* that was read from input file to stdin of decompressor.
* Spawn worker thread that copies remainder of input file to
* stdin of decompressor. Return stdout of decompressor to caller.
*
* else: # not compressed
* create pipe
* write header that was read from input file to pipe.
* spawn worker thread that copies remainder of input file to pipe.
* return other end of pipe to caller.
*
* Limitations:
*
* 1/ The routine provides no method of waiting for its children.
* This means zombie processes may be left if the caller doesn't exit
* shortly after finishing the reading and that callers that wait for
* children may find unexpected children exiting.
*
* 2/ There is no specific error handling. If it fails for any reason,
* it just returns -1.
*/
/*
* Check if we can seek fd w/ affecting it.
*/
static int
is_seekable(int fd)
{
return (lseek(fd, 0, SEEK_CUR) != -1);
}
static int
is_tape(int fd)
{
struct stat st;
struct mtget mt;
(void) fstat(fd, &st);
return (S_ISCHR(st.st_mode) && ioctl(fd, MTIOCGET, &mt) != -1);
}
/*
* Read potential header into magic from input fd;
* return decompressor needed, NULL if none.
*/
static const char *
check_magic_header(int (*readf)(int, char *), char *magic,
int *rsz, int inputfd)
{
if ((*rsz = (*readf)(inputfd, magic)) <= 0)
return (NULL);
if (memcmp(magic, GZIP_MAGIC, 2) == 0) {
return (GZCAT);
} else if (memcmp(magic, BZIP_MAGIC, 3) == 0) {
return (BZCAT);
} else if (memcmp(magic, COMP_MAGIC, 2) == 0) {
return (ZCAT);
} else {
return (NULL);
}
}
/*
* Pass contents of inputfd through command;
* return fd from which output may be read.
*/
static int
filter_stream(int inputfd, const char *command)
{
int fd[2];
pid_t pid;
struct worker *wk;
if (pipe(fd) < 0)
return (-1);
if ((pid = fork()) == -1) {
perror("fork");
return (-1);
}
if (pid == 0) {
(void) dup2(inputfd, 0); /* refer to inputfd as 0 */
(void) dup2(fd[0], 1); /* refer out fd[0] as stdout */
(void) close(fd[1]); /* this is other end used by parent */
closefrom(3);
(void) execlp(command, command, NULL);
return (-1);
}
(void) close(fd[0]); /* client's output */
if ((wk = calloc(1, sizeof (struct worker))) == NULL) {
perror("malloc failed");
return (-1);
}
wk->pid = pid;
wk->next = workers;
workers = wk;
return (fd[1]);
}
/*
* Worker thread for splice routine.
*/
static void *
splice_thr(void * arg)
{
struct thr_args *args = (struct thr_args *)arg;
int nread;
sigset_t set;
/*
* write the first buffer contents which was used to check
* the header magic.
*/
if (fwrite(args->buf, 1, args->rsz, args->out) != args->rsz) {
perror("splice: write");
return (NULL);
}
/*
* The other end of pipe may be closed in the middle of input
* stream. We should quit properly when it happened.
*/
(void) sigemptyset(&set);
(void) sigaddset(&set, SIGPIPE);
(void) sigprocmask(SIG_BLOCK, &set, NULL);
while ((nread = (*args->readf)(args->in, args->buf)) != 0) {
if (nread < 0) {
perror("splice: read");
break;
}
if (fwrite(args->buf, 1, nread, args->out) != nread) {
if (ferror(args->out) && errno != EPIPE)
perror("splice: write");
break;
}
}
/* Avoid closing source fd to avoid it's reuse. */
(void) fclose(args->out);
free(args->buf);
free(args);
return (NULL);
}
/*
* Copy from input fd to output fd in separate thread.
*/
static int
splice(FILE *out, int (*readf)(int, char *), int in, char *buf, size_t rsz)
{
pthread_t thr;
struct thr_args *args;
struct worker *wk;
if ((args = malloc(sizeof (struct thr_args))) == NULL) {
perror("malloc failed");
return (-1);
}
args->in = in;
args->out = out;
args->buf = buf;
args->rsz = rsz;
args->readf = readf;
if (pthread_create(&thr, NULL, splice_thr, args) != 0) {
perror("pthread_create");
return (-1);
}
if ((wk = calloc(1, sizeof (struct worker))) == NULL) {
perror("malloc failed");
return (-1);
}
wk->outf = out;
wk->tid = thr;
wk->next = workers;
workers = wk;
return (0);
}
/*
* Returns fd from which contents of fd may be read,
* decompressing if needed. Works on non-seekable file
* descriptors. Returns -1 upon error.
*/
int
decompress_from_fd(int fd, int (*readf)(int, char *), size_t blksz)
{
char *buffer;
const char *decompressor;
int rsz;
FILE *out;
if ((buffer = malloc(blksz)) == NULL)
return (-1);
/* check if we need to decompress */
decompressor = check_magic_header(readf, buffer, &rsz, fd);
if (is_seekable(fd) && !is_tape(fd)) {
free(buffer);
/* rewind to start of file */
if (lseek(fd, 0, SEEK_SET) != 0)
return (-1);
if (decompressor != NULL) {
/*
* file is compressed; filter it through
* appropriate decompressor.
*/
return (filter_stream(fd, decompressor));
}
return (fd);
} else { /* not seekable */
int fds[2];
if (pipe(fds) == -1) {
free(buffer);
return (-1);
}
if ((out = fdopen(fds[0], "wE")) == NULL) {
free(buffer);
return (-1);
}
if (splice(out, readf, fd, buffer, rsz) == 0) {
if (decompressor != NULL) {
return (filter_stream(fds[1], decompressor));
} else {
return (fds[1]);
}
} else {
free(buffer);
return (-1);
}
}
/*NOTREACHED*/
}
void
decompress_from_fd_cleanup(void)
{
struct worker *wk, *nwk;
for (wk = workers; wk != NULL; wk = nwk) {
nwk = wk->next;
if (wk->pid != 0)
(void) kill(wk->pid, SIGTERM);
(void) waitpid(wk->pid, NULL, 0);
if (wk->tid != 0) {
(void) pthread_cancel(wk->tid);
(void) pthread_join(wk->tid, NULL);
}
if (wk->outf != NULL) {
/*
* Other end of pipe may have been closed. We
* shouldn't write anything which induces SIGPIPE
*/
__fpurge(wk->outf);
(void) fclose(wk->outf);
}
free(wk);
}
}