Merge branch 'dm/ni-maxhost-may-be-missing'
authorJunio C Hamano <gitster@pobox.com>
Tue, 19 Mar 2013 19:18:21 +0000 (12:18 -0700)
committerJunio C Hamano <gitster@pobox.com>
Tue, 19 Mar 2013 19:18:21 +0000 (12:18 -0700)
On systems without NI_MAXHOST in their system header files,
connect.c (hence most of the transport) did not compile.

* dm/ni-maxhost-may-be-missing:
git-compat-util.h: Provide missing netdb.h definitions

1  2 
daemon.c
git-compat-util.h
diff --combined daemon.c
index 4602b46a5c39e1d501143ab4e95b55aff5c8f23b,34d95c1674930aa6ea429f7ee9173c239aae6bd5..df8c0ab0588e70ad6e6f56195535030055782d85
+++ b/daemon.c
@@@ -9,10 -9,6 +9,6 @@@
  #define HOST_NAME_MAX 256
  #endif
  
- #ifndef NI_MAXSERV
- #define NI_MAXSERV 32
- #endif
  #ifdef NO_INITGROUPS
  #define initgroups(x, y) (0) /* nothing */
  #endif
@@@ -30,7 -26,6 +26,7 @@@ static const char daemon_usage[] 
  "           [--interpolated-path=<path>]\n"
  "           [--reuseaddr] [--pid-file=<file>]\n"
  "           [--(enable|disable|allow-override|forbid-override)=<service>]\n"
 +"           [--access-hook=<path>]\n"
  "           [--inetd | [--listen=<host_or_ipaddr>] [--port=<n>]\n"
  "                      [--detach] [--user=<user> [--group=<group>]]\n"
  "           [<directory>...]";
@@@ -257,71 -252,6 +253,71 @@@ static int daemon_error(const char *dir
        return -1;
  }
  
 +static char *access_hook;
 +
 +static int run_access_hook(struct daemon_service *service, const char *dir, const char *path)
 +{
 +      struct child_process child;
 +      struct strbuf buf = STRBUF_INIT;
 +      const char *argv[8];
 +      const char **arg = argv;
 +      char *eol;
 +      int seen_errors = 0;
 +
 +#define STRARG(x) ((x) ? (x) : "")
 +      *arg++ = access_hook;
 +      *arg++ = service->name;
 +      *arg++ = path;
 +      *arg++ = STRARG(hostname);
 +      *arg++ = STRARG(canon_hostname);
 +      *arg++ = STRARG(ip_address);
 +      *arg++ = STRARG(tcp_port);
 +      *arg = NULL;
 +#undef STRARG
 +
 +      memset(&child, 0, sizeof(child));
 +      child.use_shell = 1;
 +      child.argv = argv;
 +      child.no_stdin = 1;
 +      child.no_stderr = 1;
 +      child.out = -1;
 +      if (start_command(&child)) {
 +              logerror("daemon access hook '%s' failed to start",
 +                       access_hook);
 +              goto error_return;
 +      }
 +      if (strbuf_read(&buf, child.out, 0) < 0) {
 +              logerror("failed to read from pipe to daemon access hook '%s'",
 +                       access_hook);
 +              strbuf_reset(&buf);
 +              seen_errors = 1;
 +      }
 +      if (close(child.out) < 0) {
 +              logerror("failed to close pipe to daemon access hook '%s'",
 +                       access_hook);
 +              seen_errors = 1;
 +      }
 +      if (finish_command(&child))
 +              seen_errors = 1;
 +
 +      if (!seen_errors) {
 +              strbuf_release(&buf);
 +              return 0;
 +      }
 +
 +error_return:
 +      strbuf_ltrim(&buf);
 +      if (!buf.len)
 +              strbuf_addstr(&buf, "service rejected");
 +      eol = strchr(buf.buf, '\n');
 +      if (eol)
 +              *eol = '\0';
 +      errno = EACCES;
 +      daemon_error(dir, buf.buf);
 +      strbuf_release(&buf);
 +      return -1;
 +}
 +
  static int run_service(char *dir, struct daemon_service *service)
  {
        const char *path;
                return daemon_error(dir, "service not enabled");
        }
  
 +      /*
 +       * Optionally, a hook can choose to deny access to the
 +       * repository depending on the phase of the moon.
 +       */
 +      if (access_hook && run_access_hook(service, dir, path))
 +              return -1;
 +
        /*
         * We'll ignore SIGTERM from now on, we have a
         * good client.
@@@ -1215,10 -1138,6 +1211,10 @@@ int main(int argc, char **argv
                        export_all_trees = 1;
                        continue;
                }
 +              if (!prefixcmp(arg, "--access-hook=")) {
 +                      access_hook = arg + 14;
 +                      continue;
 +              }
                if (!prefixcmp(arg, "--timeout=")) {
                        timeout = atoi(arg+10);
                        continue;
diff --combined git-compat-util.h
index b636e0dd0c2ea8284ed6f34e32c448eae433a20a,503282c863965bf54f6013b34a4a8d55e34c4932..e715285e7c2e0a3d0b959505921b344dda55089d
@@@ -74,8 -74,7 +74,8 @@@
  # define _XOPEN_SOURCE 500
  # endif
  #elif !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__USLC__) && \
 -      !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__)
 +      !defined(_M_UNIX) && !defined(__sgi) && !defined(__DragonFly__) && \
 +      !defined(__TANDEM) && !defined(__QNX__)
  #define _XOPEN_SOURCE 600 /* glibc2 and AIX 5.3L need 500, OpenBSD needs 600 for S_ISLNK() */
  #define _XOPEN_SOURCE_EXTENDED 1 /* AIX 5.3L needs this */
  #endif
  #include <stdlib.h>
  #include <stdarg.h>
  #include <string.h>
 +#ifdef HAVE_STRINGS_H
 +#include <strings.h> /* for strcasecmp() */
 +#endif
  #include <errno.h>
  #include <limits.h>
 +#ifdef NEEDS_SYS_PARAM_H
  #include <sys/param.h>
 +#endif
  #include <sys/types.h>
  #include <dirent.h>
  #include <sys/time.h>
  #include <time.h>
  #include <signal.h>
 +#ifndef USE_WILDMATCH
  #include <fnmatch.h>
 +#endif
  #include <assert.h>
  #include <regex.h>
  #include <utime.h>
  #else
  #include <stdint.h>
  #endif
 +#ifdef NO_INTPTR_T
 +/*
 + * On I16LP32, ILP32 and LP64 "long" is the save bet, however
 + * on LLP86, IL33LLP64 and P64 it needs to be "long long",
 + * while on IP16 and IP16L32 it is "int" (resp. "short")
 + * Size needs to match (or exceed) 'sizeof(void *)'.
 + * We can't take "long long" here as not everybody has it.
 + */
 +typedef long intptr_t;
 +typedef unsigned long uintptr_t;
 +#endif
  #if defined(__CYGWIN__)
  #undef _XOPEN_SOURCE
  #include <grp.h>
  #define probe_utf8_pathname_composition(a,b)
  #endif
  
 +#ifdef MKDIR_WO_TRAILING_SLASH
 +#define mkdir(a,b) compat_mkdir_wo_trailing_slash((a),(b))
 +extern int compat_mkdir_wo_trailing_slash(const char*, mode_t);
 +#endif
 +
 +#ifdef NO_STRUCT_ITIMERVAL
 +struct itimerval {
 +      struct timeval it_interval;
 +      struct timeval it_value;
 +}
 +#endif
 +
 +#ifdef NO_SETITIMER
 +#define setitimer(which,value,ovalue)
 +#endif
 +
  #ifndef NO_LIBGEN_H
  #include <libgen.h>
  #else
@@@ -213,6 -178,17 +213,17 @@@ extern char *gitbasename(char *)
  #include <openssl/err.h>
  #endif
  
+ /* On most systems <netdb.h> would have given us this, but
+  * not on some systems (e.g. z/OS).
+  */
+ #ifndef NI_MAXHOST
+ #define NI_MAXHOST 1025
+ #endif
+ #ifndef NI_MAXSERV
+ #define NI_MAXSERV 32
+ #endif
  /* On most systems <limits.h> would have given us this, but
   * not on some systems (e.g. GNU/Hurd).
   */
  
  #include "compat/bswap.h"
  
 +#ifdef USE_WILDMATCH
 +#include "wildmatch.h"
 +#define FNM_PATHNAME WM_PATHNAME
 +#define FNM_CASEFOLD WM_CASEFOLD
 +#define FNM_NOMATCH  WM_NOMATCH
 +static inline int fnmatch(const char *pattern, const char *string, int flags)
 +{
 +      return wildmatch(pattern, string, flags, NULL);
 +}
 +#endif
 +
  /* General helper functions */
  extern void vreportf(const char *prefix, const char *err, va_list params);
  extern void vwritef(int fd, const char *prefix, const char *err, va_list params);
@@@ -303,17 -268,6 +314,17 @@@ extern NORETURN void die_errno(const ch
  extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
  extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
  
 +/*
 + * Let callers be aware of the constant return value; this can help
 + * gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
 + * because some compilers may not support variadic macros. Since we're only
 + * trying to help gcc, anyway, it's OK; other compilers will fall back to
 + * using the function as usual.
 + */
 +#if defined(__GNUC__) && ! defined(__clang__)
 +#define error(...) (error(__VA_ARGS__), -1)
 +#endif
 +
  extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
  extern void set_error_routine(void (*routine)(const char *err, va_list params));
  
@@@ -421,6 -375,11 +432,6 @@@ extern uintmax_t gitstrtoumax(const cha
  extern intmax_t gitstrtoimax(const char *, char **, int);
  #endif
  
 -#ifdef NO_STRTOK_R
 -#define strtok_r gitstrtok_r
 -extern char *gitstrtok_r(char *s, const char *delim, char **save_ptr);
 -#endif
 -
  #ifdef NO_HSTRERROR
  #define hstrerror githstrerror
  extern const char *githstrerror(int herror);
@@@ -432,10 -391,6 +443,10 @@@ void *gitmemmem(const void *haystack, s
                  const void *needle, size_t needlelen);
  #endif
  
 +#ifdef NO_GETPAGESIZE
 +#define getpagesize() sysconf(_SC_PAGESIZE)
 +#endif
 +
  #ifdef FREAD_READS_DIRECTORIES
  #ifdef fopen
  #undef fopen
@@@ -531,39 -486,27 +542,39 @@@ extern const char tolower_trans_tbl[256
  #undef isdigit
  #undef isalpha
  #undef isalnum
 +#undef isprint
  #undef islower
  #undef isupper
  #undef tolower
  #undef toupper
 -extern unsigned char sane_ctype[256];
 +#undef iscntrl
 +#undef ispunct
 +#undef isxdigit
 +
 +extern const unsigned char sane_ctype[256];
  #define GIT_SPACE 0x01
  #define GIT_DIGIT 0x02
  #define GIT_ALPHA 0x04
  #define GIT_GLOB_SPECIAL 0x08
  #define GIT_REGEX_SPECIAL 0x10
  #define GIT_PATHSPEC_MAGIC 0x20
 +#define GIT_CNTRL 0x40
 +#define GIT_PUNCT 0x80
  #define sane_istest(x,mask) ((sane_ctype[(unsigned char)(x)] & (mask)) != 0)
  #define isascii(x) (((x) & ~0x7f) == 0)
  #define isspace(x) sane_istest(x,GIT_SPACE)
  #define isdigit(x) sane_istest(x,GIT_DIGIT)
  #define isalpha(x) sane_istest(x,GIT_ALPHA)
  #define isalnum(x) sane_istest(x,GIT_ALPHA | GIT_DIGIT)
 +#define isprint(x) ((x) >= 0x20 && (x) <= 0x7e)
  #define islower(x) sane_iscase(x, 1)
  #define isupper(x) sane_iscase(x, 0)
  #define is_glob_special(x) sane_istest(x,GIT_GLOB_SPECIAL)
  #define is_regex_special(x) sane_istest(x,GIT_GLOB_SPECIAL | GIT_REGEX_SPECIAL)
 +#define iscntrl(x) (sane_istest(x,GIT_CNTRL))
 +#define ispunct(x) sane_istest(x, GIT_PUNCT | GIT_REGEX_SPECIAL | \
 +              GIT_GLOB_SPECIAL | GIT_PATHSPEC_MAGIC)
 +#define isxdigit(x) (hexval_table[x] != -1)
  #define tolower(x) sane_case((unsigned char)(x), 0x20)
  #define toupper(x) sane_case((unsigned char)(x), 0)
  #define is_pathspec_magic(x) sane_istest(x,GIT_PATHSPEC_MAGIC)
@@@ -672,12 -615,8 +683,12 @@@ int rmdir_or_warn(const char *path)
   */
  int remove_or_warn(unsigned int mode, const char *path);
  
 -/* Call access(2), but warn for any error besides ENOENT. */
 +/*
 + * Call access(2), but warn for any error except "missing file"
 + * (ENOENT or ENOTDIR).
 + */
  int access_or_warn(const char *path, int mode);
 +int access_or_die(const char *path, int mode);
  
  /* Warn on an inaccessible file that ought to be accessible */
  void warn_on_inaccessible(const char *path);