Prove the following generalization of the Theorem of Bonnet-Myers: Let $M^n$ be a complete Riemannian manifold. Suppose that there exist constants $a > 0$ and $c \ge 0$ such that for all pairs of points in $M^n$ and for all inimizing geodesics $\gamma(s)$, parametrized by arc length $s$, joining these points, we have $$ \text{Ric}(\gamma'(s)) \ge a + \frac{df}{ds}, \quad \text{along }\gamma, $$ where $f$ is a function of $s$, satisfying $|f(s)| \le c$ along $\gamma$. Then $\mathbb M^n$ is compact. Calculate an estimate for the diameter of $M^n$, and observe that if $f \equiv 0$ and $c=0$, we obtain the Theorem of Bonnet-Myers.
This is Exercise 9.3 of Riemannian Geometry by do Carmo.
I am certain that the argument for this problem is similar, although more sophisticated, to do Carmo's proof of the Theorem of Bonnet-Myers. My approach so far is as follows: \begin{align} \frac 12 \sum_{j=1}^{n-1} E_j''(0) &= \int_0^1 \sin^2 (\pi s)((n-1)\pi^2-(n-1)\ell^2 \text{Ric}_{\gamma(s)}(e_n(t))) \, ds \\ &\le \int_0^1 \sin^2(\pi s)((n-1)\pi^2-(n-1)\ell^2 \left(a+\frac{df}{ds} \right) \, ds \\ &=(n-1)\left[ \frac{\pi^2-\ell^2 a}2 -\ell^2 \int_0^1 \sin^2 (\pi s) \frac{df}{ds} \, ds \right]. \end{align} Assuming I'm on the right track (let me know if I'm not), how can I apply $|f(s)| \le c$? What can I do to express $\frac{df}{ds}$ in terms of $f$? Because that would allow me to use the inequality and go from here.
You're on the right track. The key is to note that you don't have $\frac{df}{ds}$ on it's own, but that you are actually integrating this derivative against $ds$. In particular, we can apply integration by parts to find that $$\int_{0}^{1}\sin^2(\pi s)\frac{df}{ds}~ds = -\pi\int_{0}^{1}\sin(2\pi s)f(s)~ds.$$ So finishing off your estimates we find that \begin{align} \frac{1}{2}\sum_{j=1}^{n-1}E_j''(0) & \leq (n-1)\left[\frac{\pi^2-\ell^2a}{2} - \ell^2\int_{0}^{1}\sin^2(\pi s)\frac{df}{ds}~ds\right] \\ & = (n-1)\left[\frac{\pi^2-\ell^2a}{2} + \pi\ell^2\int_{0}^{1}\sin(2\pi s)f(s)~ds\right] \\ & \leq (n-1)\left[\frac{\pi^2-\ell^2a}{2} + c\pi\ell^2\int_{0}^{1}|\sin(2\pi s)|~ds\right] \\ & = (n-1)\left[\frac{\pi^2-\ell^2a}{2} + 2c\ell^2\right]. \end{align} Now you should be able to find a suitable diameter bound which will give you the exact same contradiction that do Carmo gets in his proof of the Bonnet-Myers theorem. Finally, use the Hopf-Rinow theorem to conclude that the manifold is compact.