2016-05-16 20:02:51 -04:00
|
|
|
// +build linux
|
|
|
|
|
2020-02-09 11:42:08 -05:00
|
|
|
package overlayutils // import "github.com/docker/docker/daemon/graphdriver/overlayutils"
|
2016-05-16 20:02:51 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"encoding/base32"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2020-02-09 11:42:08 -05:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-23 10:22:32 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2016-05-16 20:02:51 -04:00
|
|
|
)
|
|
|
|
|
2020-02-09 11:42:08 -05:00
|
|
|
// GenerateID creates a new random string identifier with the given length
|
|
|
|
func GenerateID(l int, logger *logrus.Entry) string {
|
2016-05-16 20:02:51 -04:00
|
|
|
const (
|
|
|
|
// ensures we backoff for less than 450ms total. Use the following to
|
|
|
|
// select new value, in units of 10ms:
|
|
|
|
// n*(n+1)/2 = d -> n^2 + n - 2d -> n = (sqrt(8d + 1) - 1)/2
|
|
|
|
maxretries = 9
|
|
|
|
backoff = time.Millisecond * 10
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
totalBackoff time.Duration
|
|
|
|
count int
|
|
|
|
retries int
|
|
|
|
size = (l*5 + 7) / 8
|
|
|
|
u = make([]byte, size)
|
|
|
|
)
|
|
|
|
// TODO: Include time component, counter component, random component
|
|
|
|
|
|
|
|
for {
|
|
|
|
// This should never block but the read may fail. Because of this,
|
|
|
|
// we just try to read the random number generator until we get
|
|
|
|
// something. This is a very rare condition but may happen.
|
|
|
|
b := time.Duration(retries) * backoff
|
|
|
|
time.Sleep(b)
|
|
|
|
totalBackoff += b
|
|
|
|
|
|
|
|
n, err := io.ReadFull(rand.Reader, u[count:])
|
|
|
|
if err != nil {
|
|
|
|
if retryOnError(err) && retries < maxretries {
|
|
|
|
count += n
|
|
|
|
retries++
|
2018-10-11 15:50:45 -04:00
|
|
|
logger.Errorf("error generating version 4 uuid, retrying: %v", err)
|
2016-05-16 20:02:51 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Any other errors represent a system problem. What did someone
|
|
|
|
// do to /dev/urandom?
|
|
|
|
panic(fmt.Errorf("error reading random number generator, retried for %v: %v", totalBackoff.String(), err))
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
s := base32.StdEncoding.EncodeToString(u)
|
|
|
|
|
|
|
|
return s[:l]
|
|
|
|
}
|
|
|
|
|
|
|
|
// retryOnError tries to detect whether or not retrying would be fruitful.
|
|
|
|
func retryOnError(err error) bool {
|
|
|
|
switch err := err.(type) {
|
|
|
|
case *os.PathError:
|
|
|
|
return retryOnError(err.Err) // unpack the target error
|
|
|
|
case syscall.Errno:
|
2017-05-23 10:22:32 -04:00
|
|
|
if err == unix.EPERM {
|
2016-05-16 20:02:51 -04:00
|
|
|
// EPERM represents an entropy pool exhaustion, a condition under
|
|
|
|
// which we backoff and retry.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|