
# Copyright (C) Igor Sysoev
# Copyright (C) NGINX, Inc.


# Linux 3.17 with glibc 2.25, FreeBSD 12, Solaris 11.3.

njs_feature="getrandom()"
njs_feature_name=NJS_HAVE_GETRANDOM
njs_feature_run=yes
njs_feature_incs=
njs_feature_libs=
njs_feature_test="#include <unistd.h>
                  #include <sys/random.h>

                  int main(void) {
                      char  buf[4];

                      if (getrandom(buf, 4, 0) < 0) {
                          return 1;
                      }

                      return 0;
                  }"
. auto/feature


if [ $njs_found = no ]; then

    # Linux 3.17 SYS_getrandom.

    njs_feature="SYS_getrandom in Linux"
    njs_feature_name=NJS_HAVE_LINUX_SYS_GETRANDOM
    njs_feature_test="#include <unistd.h>
                      #include <sys/syscall.h>
                      #include <linux/random.h>

                      int main(void) {
                          char  buf[4];

                          if (syscall(SYS_getrandom, buf, 4, 0) < 0) {
                              return 1;
                          }

                          return 0;
                      }"
    . auto/feature
fi


if [ $njs_found = no ]; then

    # macOS 10.10.

    njs_feature="CCRandomGenerateBytes() in CommonCrypto/CommonRandom.h"
    njs_feature_name=NJS_HAVE_CCRANDOMGENERATEBYTES
    njs_feature_test="#include <CommonCrypto/CommonCryptoError.h>
                      #include <CommonCrypto/CommonRandom.h>

                      int main(void) {
                          char  buf[4];

                          if (CCRandomGenerateBytes(buf, 4) != kCCSuccess) {
                              return 1;
                          }

                          return 0;
                      }"
    . auto/feature
fi


if [ $njs_found = no ]; then

    # OpenBSD 5.6 lacks <sys/random.h>.

    njs_feature="getentropy()"
    njs_feature_name=NJS_HAVE_GETENTROPY
    njs_feature_test="#include <unistd.h>

                      int main(void) {
                          char  buf[4];

                          if (getentropy(buf, 4) == -1) {
                              return 1;
                          }

                          return 0;
                      }"
    . auto/feature
fi


if [ $njs_found = no ]; then

    # Solaris based systems.

    njs_feature="getentropy() in sys/random.h"
    njs_feature_name=NJS_HAVE_GETENTROPY_SYS_RANDOM
    njs_feature_test="#include <unistd.h>
                      #include <sys/random.h>

                      int main(void) {
                          char  buf[4];

                          if (getentropy(buf, 4) == -1) {
                              return 1;
                          }

                          return 0;
                      }"
    . auto/feature
fi
